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
'''simple docstring''' from copy import deepcopy class __A : def __init__(self : Dict , __a : list[int] | None = None , __a : int | None = None ): if arr is None and size is not None: UpperCAmelCase_ = size UpperCAmelCase_ = [0] * size elif arr is not None: self.init(__a ) else: raise ValueError("Either arr or size must be specified" ) def _lowercase (self : int , __a : list[int] ): UpperCAmelCase_ = len(__a ) UpperCAmelCase_ = deepcopy(__a ) for i in range(1 , self.size ): UpperCAmelCase_ = self.next_(__a ) if j < self.size: self.tree[j] += self.tree[i] def _lowercase (self : Optional[int] ): UpperCAmelCase_ = self.tree[:] for i in range(self.size - 1 , 0 , -1 ): UpperCAmelCase_ = self.next_(__a ) if j < self.size: arr[j] -= arr[i] return arr @staticmethod def _lowercase (__a : int ): return index + (index & (-index)) @staticmethod def _lowercase (__a : int ): return index - (index & (-index)) def _lowercase (self : Tuple , __a : int , __a : int ): if index == 0: self.tree[0] += value return while index < self.size: self.tree[index] += value UpperCAmelCase_ = self.next_(__a ) def _lowercase (self : Optional[Any] , __a : int , __a : int ): self.add(__a , value - self.get(__a ) ) def _lowercase (self : Dict , __a : int ): if right == 0: return 0 UpperCAmelCase_ = self.tree[0] right -= 1 # make right inclusive while right > 0: result += self.tree[right] UpperCAmelCase_ = self.prev(__a ) return result def _lowercase (self : int , __a : int , __a : int ): return self.prefix(__a ) - self.prefix(__a ) def _lowercase (self : str , __a : int ): return self.query(__a , index + 1 ) def _lowercase (self : List[str] , __a : int ): value -= self.tree[0] if value < 0: return -1 UpperCAmelCase_ = 1 # Largest power of 2 <= size while j * 2 < self.size: j *= 2 UpperCAmelCase_ = 0 while j > 0: if i + j < self.size and self.tree[i + j] <= value: value -= self.tree[i + j] i += j j //= 2 return i if __name__ == "__main__": import doctest doctest.testmod()
1
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
0
'''simple docstring''' import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __lowerCAmelCase : '''simple docstring''' def __init__(self : Union[str, Any] ): '''simple docstring''' lowercase__ = '''''' lowercase__ = '''''' lowercase__ = [] lowercase__ = 0 lowercase__ = 256 lowercase__ = 0 lowercase__ = 0 lowercase__ = 0 lowercase__ = 0 def UpperCamelCase__ (self : Union[str, Any] , UpperCamelCase : int ): '''simple docstring''' lowercase__ = cva.imread(UpperCamelCase , 0 ) lowercase__ = copy.deepcopy(self.img ) lowercase__ ,lowercase__ ,lowercase__ = plt.hist(self.img.ravel() , 256 , [0, 256] , label='''x''' ) lowercase__ = np.sum(UpperCamelCase ) for i in range(len(UpperCamelCase ) ): lowercase__ = x[i] / self.k self.sk += prk lowercase__ = (self.L - 1) * self.sk if self.rem != 0: lowercase__ = int(last % last ) lowercase__ = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(UpperCamelCase ) lowercase__ = int(np.ma.count(self.img ) / self.img[1].size ) lowercase__ = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): lowercase__ = self.img[j][i] if num != self.last_list[num]: lowercase__ = self.last_list[num] cva.imwrite('''output_data/output.jpg''' , self.img ) def UpperCamelCase__ (self : int ): '''simple docstring''' plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCamelCase__ (self : Tuple ): '''simple docstring''' cva.imshow('''Output-Image''' , self.img ) cva.imshow('''Input-Image''' , self.original_image ) cva.waitKey(5000 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCamelCase : Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') lowerCamelCase : int = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
2
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
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" requires_backends(cls , ['''flax'''] ) class A ( metaclass=__snake_case ): __magic_name__ = ['''flax'''] def __init__( self , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls , *SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" requires_backends(cls , ['''flax'''] )
3
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
0
'''simple docstring''' __snake_case ={ """A""": ["""B""", """C""", """E"""], """B""": ["""A""", """D""", """E"""], """C""": ["""A""", """F""", """G"""], """D""": ["""B"""], """E""": ["""A""", """B""", """D"""], """F""": ["""C"""], """G""": ["""C"""], } def a_ ( lowerCamelCase : dict , lowerCamelCase : List[str] , lowerCamelCase : Optional[int] ): lowerCAmelCase = set() # keep track of all the paths to be checked lowerCAmelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue lowerCAmelCase = queue.pop(0 ) # get the last node from the path lowerCAmelCase = path[-1] if node not in explored: lowerCAmelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: lowerCAmelCase = list(lowerCamelCase ) new_path.append(lowerCamelCase ) queue.append(lowerCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(lowerCamelCase ) # in case there's no path between the 2 nodes return [] def a_ ( lowerCamelCase : dict , lowerCamelCase : List[Any] , lowerCamelCase : List[Any] ): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 lowerCAmelCase = [start] lowerCAmelCase = set(lowerCamelCase ) # Keep tab on distances from `start` node. lowerCAmelCase = {start: 0, target: -1} while queue: lowerCAmelCase = queue.pop(0 ) if node == target: lowerCAmelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(lowerCamelCase ) queue.append(lowerCamelCase ) lowerCAmelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, """G""", """D""")) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, """G""", """D""")) # returns 4
4
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
0
from __future__ import annotations from typing import Any class lowerCamelCase__ ( lowerCAmelCase): pass class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> None: _lowercase =data _lowercase =None def __iter__(self ) -> List[Any]: _lowercase =self _lowercase =[] while node: if node in visited: raise ContainsLoopError visited.append(UpperCAmelCase ) yield node.data _lowercase =node.next_node @property def __A (self ) -> bool: try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCAmelCase__ = Node(1) UpperCAmelCase__ = Node(2) UpperCAmelCase__ = Node(3) UpperCAmelCase__ = Node(4) print(root_node.has_loop) # False UpperCAmelCase__ = root_node.next_node print(root_node.has_loop) # True UpperCAmelCase__ = Node(5) UpperCAmelCase__ = Node(6) UpperCAmelCase__ = Node(5) UpperCAmelCase__ = Node(6) print(root_node.has_loop) # False UpperCAmelCase__ = Node(1) print(root_node.has_loop) # False
5
# 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
0
from __future__ import annotations import collections import pprint from pathlib import Path def __lowerCAmelCase ( a__ ) -> str: return "".join(sorted(a__ ) ) def __lowerCAmelCase ( a__ ) -> list[str]: return word_by_signature[signature(a__ )] A : str = Path(__file__).parent.joinpath('words.txt').read_text(encoding='utf-8') A : List[str] = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Dict = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open('anagrams.txt', 'w') as file: file.write('all_anagrams = \n ') file.write(pprint.pformat(all_anagrams))
6
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
0
from math import factorial, pi def _snake_case( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : int = 30 ) -> float: '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ): raise ValueError('maclaurin_sin() requires either an int or float for theta' ) if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or accuracy <= 0: raise ValueError('maclaurin_sin() requires a positive int for accuracy' ) A__ = float(SCREAMING_SNAKE_CASE__ ) A__ = theta // (2 * pi) theta -= 2 * div * pi return sum( (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1 ) for r in range(SCREAMING_SNAKE_CASE__ ) ) def _snake_case( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : int = 30 ) -> float: '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ): raise ValueError('maclaurin_cos() requires either an int or float for theta' ) if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or accuracy <= 0: raise ValueError('maclaurin_cos() requires a positive int for accuracy' ) A__ = float(SCREAMING_SNAKE_CASE__ ) A__ = theta // (2 * pi) theta -= 2 * div * pi return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r ) for r in range(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": import doctest doctest.testmod() print(maclaurin_sin(10)) print(maclaurin_sin(-10)) print(maclaurin_sin(10, 15)) print(maclaurin_sin(-10, 15)) print(maclaurin_cos(5)) print(maclaurin_cos(-5)) print(maclaurin_cos(10, 15)) print(maclaurin_cos(-10, 15))
7
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
0
import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint lowerCAmelCase_ = { '''169M''': 12, '''430M''': 24, '''1B5''': 24, '''3B''': 32, '''7B''': 32, '''14B''': 40, } lowerCAmelCase_ = { '''169M''': 7_68, '''430M''': 10_24, '''1B5''': 20_48, '''3B''': 25_60, '''7B''': 40_96, '''14B''': 51_20, } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = list(state_dict.keys() ) for name in state_dict_keys: snake_case_ = state_dict.pop(SCREAMING_SNAKE_CASE__ ) # emb -> embedding if name.startswith('''emb.''' ): snake_case_ = name.replace('''emb.''' , '''embeddings.''' ) # ln_0 -> pre_ln (only present at block 0) if name.startswith('''blocks.0.ln0''' ): snake_case_ = name.replace('''blocks.0.ln0''' , '''blocks.0.pre_ln''' ) # att -> attention snake_case_ = re.sub(R'''blocks\.(\d+)\.att''' , R'''blocks.\1.attention''' , SCREAMING_SNAKE_CASE__ ) # ffn -> feed_forward snake_case_ = re.sub(R'''blocks\.(\d+)\.ffn''' , R'''blocks.\1.feed_forward''' , SCREAMING_SNAKE_CASE__ ) # time_mix_k -> time_mix_key and reshape if name.endswith('''.time_mix_k''' ): snake_case_ = name.replace('''.time_mix_k''' , '''.time_mix_key''' ) # time_mix_v -> time_mix_value and reshape if name.endswith('''.time_mix_v''' ): snake_case_ = name.replace('''.time_mix_v''' , '''.time_mix_value''' ) # time_mix_r -> time_mix_key and reshape if name.endswith('''.time_mix_r''' ): snake_case_ = name.replace('''.time_mix_r''' , '''.time_mix_receptance''' ) if name != "head.weight": snake_case_ = '''rwkv.''' + name snake_case_ = weight return state_dict def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=None ): # 1. If possible, build the tokenizer. if tokenizer_file is None: print('''No `--tokenizer_file` provided, we will use the default tokenizer.''' ) snake_case_ = 50277 snake_case_ = AutoTokenizer.from_pretrained('''EleutherAI/gpt-neox-20b''' ) else: snake_case_ = PreTrainedTokenizerFast(tokenizer_file=SCREAMING_SNAKE_CASE__ ) snake_case_ = len(SCREAMING_SNAKE_CASE__ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE__ ) # 2. Build the config snake_case_ = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: snake_case_ = candidate break if size is None: raise ValueError('''Could not infer the size, please provide it with the `--size` argument.''' ) if size not in possible_sizes: raise ValueError(F'''`size` should be one of {possible_sizes}, got {size}.''' ) snake_case_ = RwkvConfig( vocab_size=SCREAMING_SNAKE_CASE__ , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(SCREAMING_SNAKE_CASE__ ) # 3. Download model file then convert state_dict snake_case_ = hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.load(SCREAMING_SNAKE_CASE__ , map_location='''cpu''' ) snake_case_ = convert_state_dict(SCREAMING_SNAKE_CASE__ ) # 4. Split in shards and save snake_case_, snake_case_ = shard_checkpoint(SCREAMING_SNAKE_CASE__ ) for shard_file, shard in shards.items(): torch.save(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) if index is not None: snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Save the index as well with open(SCREAMING_SNAKE_CASE__ , '''w''' , encoding='''utf-8''' ) as f: snake_case_ = json.dumps(SCREAMING_SNAKE_CASE__ , indent=2 , sort_keys=SCREAMING_SNAKE_CASE__ ) + '''\n''' f.write(SCREAMING_SNAKE_CASE__ ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( '''Cleaning up shards. This may error with an OOM error, it this is the case don\'t worry you still have converted the model.''' ) snake_case_ = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: snake_case_ = torch.load(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError('''Please provide a `model_name` to push the model to the Hub.''' ) snake_case_ = AutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__ ) model.push_to_hub(SCREAMING_SNAKE_CASE__ , max_shard_size='''2GB''' ) tokenizer.push_to_hub(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--repo_id''', default=None, type=str, required=True, help='''Repo ID from which to pull the checkpoint.''' ) parser.add_argument( '''--checkpoint_file''', default=None, type=str, required=True, help='''Name of the checkpoint file in the repo.''' ) parser.add_argument( '''--output_dir''', default=None, type=str, required=True, help='''Where to save the converted model.''' ) parser.add_argument( '''--tokenizer_file''', default=None, type=str, help='''Path to the tokenizer file to use (if not provided, only the model is converted).''', ) parser.add_argument( '''--size''', default=None, type=str, help='''Size of the model. Will be inferred from the `checkpoint_file` if not passed.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Push to the Hub the converted model.''', ) parser.add_argument( '''--model_name''', default=None, type=str, help='''Name of the pushed model on the Hub, including the username / organization.''', ) lowerCAmelCase_ = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
8
# 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
0
import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def _UpperCamelCase ( lowercase__ , lowercase__=False ): try: __SCREAMING_SNAKE_CASE : str = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __SCREAMING_SNAKE_CASE : Any = default else: # KEY is set, convert it to True or False. try: __SCREAMING_SNAKE_CASE : List[Any] = strtobool(lowercase__ ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F'''If set, {key} must be yes or no.''' ) return _value __lowerCAmelCase : Dict =parse_flag_from_env('RUN_SLOW', default=False) def _UpperCamelCase ( lowercase__ ): return unittest.skip('''Test was skipped''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(_run_slow_tests , '''test is slow''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(not torch.cuda.is_available() , '''test requires only a CPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(torch.cuda.is_available() , '''test requires a GPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_xpu_available() , '''test requires a XPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_mps_available() , '''test requires a `mps` backend support in `torch`''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless( is_transformers_available() and is_datasets_available() , '''test requires the Hugging Face suite''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_bnb_available() , '''test requires the bitsandbytes library''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_tpu_available() , '''test requires TPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(torch.cuda.device_count() == 1 , '''test requires a GPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(torch.xpu.device_count() == 1 , '''test requires a XPU''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(torch.cuda.device_count() > 1 , '''test requires multiple GPUs''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(torch.xpu.device_count() > 1 , '''test requires multiple XPUs''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_safetensors_available() , '''test requires safetensors''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_deepspeed_available() , '''test requires DeepSpeed''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_torch_version('''>=''' , '''1.12.0''' ) , '''test requires torch version >= 1.12.0''' )(lowercase__ ) def _UpperCamelCase ( lowercase__=None , lowercase__=None ): if test_case is None: return partial(lowercase__ , version=lowercase__ ) return unittest.skipUnless(is_torch_version('''>=''' , lowercase__ ) , F'''test requires torch version >= {version}''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_tensorboard_available() , '''test requires Tensorboard''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_wandb_available() , '''test requires wandb''' )(lowercase__ ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless(is_comet_ml_available() , '''test requires comet_ml''' )(lowercase__ ) __lowerCAmelCase : Optional[Any] =( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def _UpperCamelCase ( lowercase__ ): return unittest.skipUnless( _atleast_one_tracker_available , '''test requires at least one tracker to be available and for `comet_ml` to not be installed''' , )(lowercase__ ) class _lowercase ( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = True @classmethod def __magic_name__( cls :Optional[Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE : Optional[Any] = tempfile.mkdtemp() @classmethod def __magic_name__( cls :List[Any] ) -> List[str]: if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def __magic_name__( self :List[Any] ) -> List[str]: if self.clear_on_setup: for path in Path(self.tmpdir ).glob('''**/*''' ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(lowerCAmelCase__ ) class _lowercase ( unittest.TestCase ): '''simple docstring''' def __magic_name__( self :List[str] ) -> Any: super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class _lowercase ( unittest.TestCase ): '''simple docstring''' def __magic_name__( self :str , lowerCAmelCase__ :Union[mock.Mock, List[mock.Mock]] ) -> Tuple: __SCREAMING_SNAKE_CASE : List[str] = mocks if isinstance(lowerCAmelCase__ , (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : int = AcceleratorState() __SCREAMING_SNAKE_CASE : Optional[int] = tensor[None].clone().to(state.device ) __SCREAMING_SNAKE_CASE : List[str] = gather(lowercase__ ).cpu() __SCREAMING_SNAKE_CASE : Union[str, Any] = tensor[0].cpu() for i in range(tensors.shape[0] ): if not torch.equal(tensors[i] , lowercase__ ): return False return True class _lowercase : '''simple docstring''' def __init__( self :Union[str, Any] , lowerCAmelCase__ :int , lowerCAmelCase__ :int , lowerCAmelCase__ :str ) -> List[str]: __SCREAMING_SNAKE_CASE : List[str] = returncode __SCREAMING_SNAKE_CASE : Optional[int] = stdout __SCREAMING_SNAKE_CASE : Dict = stderr async def _UpperCamelCase ( lowercase__ , lowercase__ ): while True: __SCREAMING_SNAKE_CASE : Tuple = await stream.readline() if line: callback(lowercase__ ) else: break async def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=None , lowercase__=None , lowercase__=False , lowercase__=False ): if echo: print('''\nRunning: ''' , ''' '''.join(lowercase__ ) ) __SCREAMING_SNAKE_CASE : Union[str, Any] = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=lowercase__ , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=lowercase__ , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __SCREAMING_SNAKE_CASE : Tuple = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = [] def tee(lowercase__ , lowercase__ , lowercase__ , lowercase__="" ): __SCREAMING_SNAKE_CASE : Tuple = line.decode('''utf-8''' ).rstrip() sink.append(lowercase__ ) if not quiet: print(lowercase__ , lowercase__ , file=lowercase__ ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout , lambda lowercase__ : tee(lowercase__ , lowercase__ , sys.stdout , label='''stdout:''' ) ) ), asyncio.create_task(_read_stream(p.stderr , lambda lowercase__ : tee(lowercase__ , lowercase__ , sys.stderr , label='''stderr:''' ) ) ), ] , timeout=lowercase__ , ) return _RunOutput(await p.wait() , lowercase__ , lowercase__ ) def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=None , lowercase__=180 , lowercase__=False , lowercase__=True ): __SCREAMING_SNAKE_CASE : Union[str, Any] = asyncio.get_event_loop() __SCREAMING_SNAKE_CASE : Dict = loop.run_until_complete( _stream_subprocess(lowercase__ , env=lowercase__ , stdin=lowercase__ , timeout=lowercase__ , quiet=lowercase__ , echo=lowercase__ ) ) __SCREAMING_SNAKE_CASE : Optional[int] = ''' '''.join(lowercase__ ) if result.returncode > 0: __SCREAMING_SNAKE_CASE : int = '''\n'''.join(result.stderr ) raise RuntimeError( F'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' F'''The combined stderr from workers follows:\n{stderr}''' ) return result class _lowercase ( A__ ): '''simple docstring''' pass def _UpperCamelCase ( lowercase__ , lowercase__=False ): try: __SCREAMING_SNAKE_CASE : Union[str, Any] = subprocess.check_output(lowercase__ , stderr=subprocess.STDOUT ) if return_stdout: if hasattr(lowercase__ , '''decode''' ): __SCREAMING_SNAKE_CASE : List[str] = output.decode('''utf-8''' ) return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F'''Command `{' '.join(lowercase__ )}` failed with the following error:\n\n{e.output.decode()}''' ) from e
9
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
0
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @slow def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Optional[Any]: '''simple docstring''' lowerCamelCase__: Optional[int] =FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small") lowerCamelCase__: Optional[int] =AutoTokenizer.from_pretrained("google/mt5-small") lowerCamelCase__: List[Any] =tokenizer("Hello there" , return_tensors="np").input_ids lowerCamelCase__: Dict =tokenizer("Hi I am" , return_tensors="np").input_ids lowerCamelCase__: Tuple =shift_tokens_right(UpperCAmelCase_ , model.config.pad_token_id , model.config.decoder_start_token_id) lowerCamelCase__: Optional[Any] =model(UpperCAmelCase_ , decoder_input_ids=UpperCAmelCase_).logits lowerCamelCase__: Optional[Any] =optax.softmax_cross_entropy(UpperCAmelCase_ , onehot(UpperCAmelCase_ , logits.shape[-1])).mean() lowerCamelCase__: Dict =-(labels.shape[-1] * loss.item()) lowerCamelCase__: List[str] =-84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1E-4)
10
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
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'facebook/xlm-roberta-xl': 'https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json', 'facebook/xlm-roberta-xxl': 'https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json', # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = "xlm-roberta-xl" def __init__( self , __lowerCamelCase=2_5_0_8_8_0 , __lowerCamelCase=2_5_6_0 , __lowerCamelCase=3_6 , __lowerCamelCase=3_2 , __lowerCamelCase=1_0_2_4_0 , __lowerCamelCase="gelu" , __lowerCamelCase=0.1 , __lowerCamelCase=0.1 , __lowerCamelCase=5_1_4 , __lowerCamelCase=1 , __lowerCamelCase=0.0_2 , __lowerCamelCase=1e-05 , __lowerCamelCase=1 , __lowerCamelCase=0 , __lowerCamelCase=2 , __lowerCamelCase="absolute" , __lowerCamelCase=True , __lowerCamelCase=None , **__lowerCamelCase , ) -> List[Any]: super().__init__(pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase) _A : Any = vocab_size _A : Dict = hidden_size _A : str = num_hidden_layers _A : Optional[int] = num_attention_heads _A : Any = hidden_act _A : Any = intermediate_size _A : Union[str, Any] = hidden_dropout_prob _A : List[str] = attention_probs_dropout_prob _A : Optional[int] = max_position_embeddings _A : Dict = type_vocab_size _A : Optional[Any] = initializer_range _A : int = layer_norm_eps _A : List[Any] = position_embedding_type _A : Tuple = use_cache _A : Union[str, Any] = classifier_dropout class lowerCAmelCase__ ( a): '''simple docstring''' @property def _lowerCamelCase ( self) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: "batch", 1: "choice", 2: "sequence"} else: _A : Any = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ])
11
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
0
from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL UpperCAmelCase_ = logging.get_logger(__name__) def lowerCamelCase__ ( A__ : List[Any] ): '''simple docstring''' if isinstance(A__ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(A__ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(A__ ): return [[videos]] raise ValueError(f'Could not make batched video from {videos}' ) class lowerCamelCase__( __lowerCamelCase): UpperCAmelCase__ : Optional[Any] = ['pixel_values'] def __init__( self: Any , UpperCamelCase_: bool = True , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase_: bool = True , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: bool = True , UpperCamelCase_: Union[int, float] = 1 / 2_55 , UpperCamelCase_: bool = True , UpperCamelCase_: bool = True , UpperCamelCase_: Optional[Union[float, List[float]]] = None , UpperCamelCase_: Optional[Union[float, List[float]]] = None , **UpperCamelCase_: Dict , ): super().__init__(**UpperCamelCase_ ) __lowerCamelCase = size if size is not None else {"""shortest_edge""": 2_56} __lowerCamelCase = get_size_dict(UpperCamelCase_ , default_to_square=UpperCamelCase_ ) __lowerCamelCase = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} __lowerCamelCase = get_size_dict(UpperCamelCase_ , param_name="""crop_size""" ) __lowerCamelCase = do_resize __lowerCamelCase = size __lowerCamelCase = do_center_crop __lowerCamelCase = crop_size __lowerCamelCase = resample __lowerCamelCase = do_rescale __lowerCamelCase = rescale_factor __lowerCamelCase = offset __lowerCamelCase = do_normalize __lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN __lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowerCAmelCase__ ( self: Optional[int] , UpperCamelCase_: np.ndarray , UpperCamelCase_: Dict[str, int] , UpperCamelCase_: PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase_: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase_: str , ): __lowerCamelCase = get_size_dict(UpperCamelCase_ , default_to_square=UpperCamelCase_ ) if "shortest_edge" in size: __lowerCamelCase = get_resize_output_image_size(UpperCamelCase_ , size["""shortest_edge"""] , default_to_square=UpperCamelCase_ ) elif "height" in size and "width" in size: __lowerCamelCase = (size["""height"""], size["""width"""]) else: raise ValueError(F'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}' ) return resize(UpperCamelCase_ , size=UpperCamelCase_ , resample=UpperCamelCase_ , data_format=UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Tuple , UpperCamelCase_: np.ndarray , UpperCamelCase_: Dict[str, int] , UpperCamelCase_: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase_: List[Any] , ): __lowerCamelCase = get_size_dict(UpperCamelCase_ ) if "height" not in size or "width" not in size: raise ValueError(F'Size must have \'height\' and \'width\' as keys. Got {size.keys()}' ) return center_crop(UpperCamelCase_ , size=(size["""height"""], size["""width"""]) , data_format=UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Optional[Any] , UpperCamelCase_: np.ndarray , UpperCamelCase_: Union[int, float] , UpperCamelCase_: bool = True , UpperCamelCase_: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase_: Union[str, Any] , ): __lowerCamelCase = image.astype(np.floataa ) if offset: __lowerCamelCase = image - (scale / 2) return rescale(UpperCamelCase_ , scale=UpperCamelCase_ , data_format=UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Dict , UpperCamelCase_: np.ndarray , UpperCamelCase_: Union[float, List[float]] , UpperCamelCase_: Union[float, List[float]] , UpperCamelCase_: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase_: Optional[int] , ): return normalize(UpperCamelCase_ , mean=UpperCamelCase_ , std=UpperCamelCase_ , data_format=UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: List[Any] , UpperCamelCase_: ImageInput , UpperCamelCase_: bool = None , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: PILImageResampling = None , UpperCamelCase_: bool = None , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: bool = None , UpperCamelCase_: float = None , UpperCamelCase_: bool = None , UpperCamelCase_: bool = None , UpperCamelCase_: Optional[Union[float, List[float]]] = None , UpperCamelCase_: Optional[Union[float, List[float]]] = None , UpperCamelCase_: Optional[ChannelDimension] = ChannelDimension.FIRST , ): if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) if offset and not do_rescale: raise ValueError("""For offset, do_rescale must also be set to True.""" ) # All transformations expect numpy arrays. __lowerCamelCase = to_numpy_array(UpperCamelCase_ ) if do_resize: __lowerCamelCase = self.resize(image=UpperCamelCase_ , size=UpperCamelCase_ , resample=UpperCamelCase_ ) if do_center_crop: __lowerCamelCase = self.center_crop(UpperCamelCase_ , size=UpperCamelCase_ ) if do_rescale: __lowerCamelCase = self.rescale(image=UpperCamelCase_ , scale=UpperCamelCase_ , offset=UpperCamelCase_ ) if do_normalize: __lowerCamelCase = self.normalize(image=UpperCamelCase_ , mean=UpperCamelCase_ , std=UpperCamelCase_ ) __lowerCamelCase = to_channel_dimension_format(UpperCamelCase_ , UpperCamelCase_ ) return image def lowerCAmelCase__ ( self: Union[str, Any] , UpperCamelCase_: ImageInput , UpperCamelCase_: bool = None , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: PILImageResampling = None , UpperCamelCase_: bool = None , UpperCamelCase_: Dict[str, int] = None , UpperCamelCase_: bool = None , UpperCamelCase_: float = None , UpperCamelCase_: bool = None , UpperCamelCase_: bool = None , UpperCamelCase_: Optional[Union[float, List[float]]] = None , UpperCamelCase_: Optional[Union[float, List[float]]] = None , UpperCamelCase_: Optional[Union[str, TensorType]] = None , UpperCamelCase_: ChannelDimension = ChannelDimension.FIRST , **UpperCamelCase_: str , ): __lowerCamelCase = do_resize if do_resize is not None else self.do_resize __lowerCamelCase = resample if resample is not None else self.resample __lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop __lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale __lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor __lowerCamelCase = offset if offset is not None else self.offset __lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize __lowerCamelCase = image_mean if image_mean is not None else self.image_mean __lowerCamelCase = image_std if image_std is not None else self.image_std __lowerCamelCase = size if size is not None else self.size __lowerCamelCase = get_size_dict(UpperCamelCase_ , default_to_square=UpperCamelCase_ ) __lowerCamelCase = crop_size if crop_size is not None else self.crop_size __lowerCamelCase = get_size_dict(UpperCamelCase_ , param_name="""crop_size""" ) 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.""" ) __lowerCamelCase = make_batched(UpperCamelCase_ ) __lowerCamelCase = [ [ self._preprocess_image( image=UpperCamelCase_ , do_resize=UpperCamelCase_ , size=UpperCamelCase_ , resample=UpperCamelCase_ , do_center_crop=UpperCamelCase_ , crop_size=UpperCamelCase_ , do_rescale=UpperCamelCase_ , rescale_factor=UpperCamelCase_ , offset=UpperCamelCase_ , do_normalize=UpperCamelCase_ , image_mean=UpperCamelCase_ , image_std=UpperCamelCase_ , data_format=UpperCamelCase_ , ) for img in video ] for video in videos ] __lowerCamelCase = {"""pixel_values""": videos} return BatchFeature(data=UpperCamelCase_ , tensor_type=UpperCamelCase_ )
12
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
0
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : int = StableDiffusionPanoramaPipeline _UpperCAmelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCAmelCase : Optional[int] = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCAmelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCAmelCase : List[str] = TEXT_TO_IMAGE_IMAGE_PARAMS def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: int = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DDIMScheduler() torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) SCREAMING_SNAKE_CASE_: str = CLIPTextModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") SCREAMING_SNAKE_CASE_: int = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int]=0): SCREAMING_SNAKE_CASE_: Optional[int] = torch.manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = { "prompt": "a photo of the dolomites", "generator": generator, # Setting height and width to None to prevent OOMs on CPU. "height": None, "width": None, "num_inference_steps": 1, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Tuple = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Any = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Tuple): super().test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : List[str]): super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.25E-3) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Union[str, Any] = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = "french fries" SCREAMING_SNAKE_CASE_: List[str] = sd_pipe(**lowerCAmelCase__ , negative_prompt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = output.images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: int = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: List[str] = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Any = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = sd_pipe(**lowerCAmelCase__ , view_batch_size=2) SCREAMING_SNAKE_CASE_: List[Any] = output.images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Any = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Any = EulerAncestralDiscreteScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="scaled_linear") SCREAMING_SNAKE_CASE_: Tuple = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: List[Any] = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Optional[int] = PNDMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="scaled_linear" , skip_prk_steps=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Optional[int] = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[Any]=0): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = { "prompt": "a photo of the dolomites", "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[Any] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Any = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: int = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: List[str] = self.get_inputs() SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) SCREAMING_SNAKE_CASE_: List[str] = np.array( [ 0.3696_8392, 0.2702_5372, 0.3244_6766, 0.2837_9387, 0.3636_3274, 0.3073_3347, 0.2710_0027, 0.2705_4125, 0.2553_6096, ]) assert np.abs(expected_slice - image_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Dict = StableDiffusionPanoramaPipeline.from_pretrained( "stabilityai/stable-diffusion-2-base" , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = LMSDiscreteScheduler.from_config(pipe.scheduler.config) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: Optional[int] = self.get_inputs() SCREAMING_SNAKE_CASE_: List[str] = pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: Optional[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) SCREAMING_SNAKE_CASE_: int = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ]) assert np.abs(expected_slice - image_slice).max() < 1E-3 def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Tuple = 0 def callback_fn(lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : torch.FloatTensor) -> None: SCREAMING_SNAKE_CASE_: str = True nonlocal number_of_steps number_of_steps += 1 if step == 1: SCREAMING_SNAKE_CASE_: int = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) SCREAMING_SNAKE_CASE_: str = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_: Any = np.array( [ 0.1868_1869, 0.3390_7816, 0.536_1276, 0.1443_2865, -0.0285_6611, -0.7394_1123, 0.2339_7987, 0.4732_2682, -0.3782_3164, ]) assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2 elif step == 2: SCREAMING_SNAKE_CASE_: str = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) SCREAMING_SNAKE_CASE_: Dict = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_: List[str] = np.array( [ 0.1853_9645, 0.3398_7248, 0.537_8559, 0.1443_7142, -0.0245_5261, -0.733_8317, 0.2399_0755, 0.4735_6272, -0.378_6505, ]) assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2 SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Dict = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: List[str] = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: List[Any] = self.get_inputs() pipe(**lowerCAmelCase__ , callback=lowerCAmelCase__ , callback_steps=1) assert callback_fn.has_been_called assert number_of_steps == 3 def _SCREAMING_SNAKE_CASE ( self : Dict): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE_: Optional[Any] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Optional[Any] = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: Dict = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing(1) pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_inputs() SCREAMING_SNAKE_CASE_: int = pipe(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
13
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
0
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = ['''image_processor''', '''tokenizer'''] UpperCAmelCase__ = '''LayoutLMv3ImageProcessor''' UpperCAmelCase__ = ('''LayoutLMv3Tokenizer''', '''LayoutLMv3TokenizerFast''') def __init__( self : Optional[int] , UpperCAmelCase__ : int=None , UpperCAmelCase__ : List[str]=None , **UpperCAmelCase__ : str) ->Tuple: '''simple docstring''' A__ = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , 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 : Any , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , UpperCAmelCase__ : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , UpperCAmelCase__ : Union[List[List[int]], List[List[List[int]]]] = None , UpperCAmelCase__ : Optional[Union[List[int], List[List[int]]]] = None , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : Union[bool, str, PaddingStrategy] = False , UpperCAmelCase__ : Union[bool, str, TruncationStrategy] = None , UpperCAmelCase__ : Optional[int] = None , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : Optional[int] = None , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : Optional[Union[str, TensorType]] = None , **UpperCAmelCase__ : List[str] , ) ->BatchEncoding: '''simple docstring''' if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.''') if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''') # first, apply the image processor A__ = self.image_processor(images=UpperCAmelCase__ , return_tensors=UpperCAmelCase__) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(UpperCAmelCase__ , UpperCAmelCase__): A__ = [text] # add batch dimension (as the image processor always adds a batch dimension) A__ = features['''words'''] A__ = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=UpperCAmelCase__ , stride=UpperCAmelCase__ , pad_to_multiple_of=UpperCAmelCase__ , return_token_type_ids=UpperCAmelCase__ , return_attention_mask=UpperCAmelCase__ , return_overflowing_tokens=UpperCAmelCase__ , return_special_tokens_mask=UpperCAmelCase__ , return_offsets_mapping=UpperCAmelCase__ , return_length=UpperCAmelCase__ , verbose=UpperCAmelCase__ , return_tensors=UpperCAmelCase__ , **UpperCAmelCase__ , ) # add pixel values A__ = features.pop('''pixel_values''') if return_overflowing_tokens is True: A__ = self.get_overflowing_images(UpperCAmelCase__ , encoded_inputs['''overflow_to_sample_mapping''']) A__ = images return encoded_inputs def SCREAMING_SNAKE_CASE ( self : int , UpperCAmelCase__ : Dict , UpperCAmelCase__ : List[str]) ->List[Any]: '''simple docstring''' A__ = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx]) if len(UpperCAmelCase__) != len(UpperCAmelCase__): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(UpperCAmelCase__)} and {len(UpperCAmelCase__)}""") return images_with_overflow def SCREAMING_SNAKE_CASE ( self : int , *UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Dict) ->Dict: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCAmelCase__ , **UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Dict , *UpperCAmelCase__ : Optional[Any] , **UpperCAmelCase__ : Optional[Any]) ->Union[str, Any]: '''simple docstring''' return self.tokenizer.decode(*UpperCAmelCase__ , **UpperCAmelCase__) @property def SCREAMING_SNAKE_CASE ( self : Tuple) ->Optional[int]: '''simple docstring''' return ["input_ids", "bbox", "attention_mask", "pixel_values"] @property def SCREAMING_SNAKE_CASE ( self : int) ->Union[str, Any]: '''simple docstring''' 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 SCREAMING_SNAKE_CASE ( self : Tuple) ->Union[str, Any]: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , UpperCAmelCase__ , ) return self.image_processor
14
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
0
def UpperCAmelCase ( a_ = 1_0_0_0 ) -> int: """simple docstring""" __A = 2**power __A = 0 while n: __A , __A = r + n % 1_0, n // 1_0 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
15
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
0
"""simple docstring""" import os def __UpperCAmelCase ( ) -> int: with open(os.path.dirname(__lowerCamelCase ) + '''/grid.txt''' ) as f: lowercase__ : Optional[int] = [] # noqa: E741 for _ in range(20 ): l.append([int(__lowerCamelCase ) for x in f.readline().split()] ) lowercase__ : Optional[int] = 0 # right for i in range(20 ): for j in range(17 ): lowercase__ : int = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: lowercase__ : Tuple = temp # down for i in range(17 ): for j in range(20 ): lowercase__ : str = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: lowercase__ : List[str] = temp # diagonal 1 for i in range(17 ): for j in range(17 ): lowercase__ : Any = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: lowercase__ : int = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): lowercase__ : Tuple = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: lowercase__ : List[str] = temp return maximum if __name__ == "__main__": print(solution())
16
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
0
"""simple docstring""" import os import unittest from transformers import FunnelTokenizer, FunnelTokenizerFast from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _lowerCAmelCase ( lowercase ,unittest.TestCase ): """simple docstring""" __UpperCAmelCase : int = FunnelTokenizer __UpperCAmelCase : Optional[int] = FunnelTokenizerFast __UpperCAmelCase : str = True __UpperCAmelCase : str = True def _lowercase ( self : str ): super().setUp() __lowercase = [ "<unk>", "<cls>", "<sep>", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] __lowercase = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def _lowercase ( self : List[Any], **UpperCAmelCase__ : int ): return FunnelTokenizer.from_pretrained(self.tmpdirname, **UpperCAmelCase__ ) def _lowercase ( self : Optional[Any], **UpperCAmelCase__ : Union[str, Any] ): return FunnelTokenizerFast.from_pretrained(self.tmpdirname, **UpperCAmelCase__ ) def _lowercase ( self : Any, UpperCAmelCase__ : Dict ): __lowercase = "UNwant\u00E9d,running" __lowercase = "unwanted, running" return input_text, output_text def _lowercase ( self : str ): __lowercase = self.tokenizer_class(self.vocab_file ) __lowercase = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(UpperCAmelCase__, ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase__ ), [7, 4, 5, 1_0, 8, 9] ) def _lowercase ( self : Optional[int] ): __lowercase = self.get_tokenizers(do_lower_case=UpperCAmelCase__ ) for tokenizer in tokenizers: __lowercase = tokenizer("UNwant\u00E9d,running" ) __lowercase = len(inputs["input_ids"] ) - 1 self.assertListEqual(inputs["token_type_ids"], [2] + [0] * sentence_len ) __lowercase = tokenizer("UNwant\u00E9d,running", "UNwant\u00E9d,running" ) self.assertListEqual(inputs["token_type_ids"], [2] + [0] * sentence_len + [1] * sentence_len )
17
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
0
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def _snake_case ( lowerCAmelCase : Dict , lowerCAmelCase : List[str] ): """simple docstring""" assert isinstance(lowerCAmelCase , lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def _snake_case ( lowerCAmelCase : Dict , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE_ : int = JsonDatasetReader(lowerCAmelCase , cache_dir=lowerCAmelCase , keep_in_memory=lowerCAmelCase ).read() _check_json_dataset(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def _snake_case ( lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Optional[int] , lowerCAmelCase : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE_ : List[Any] = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE_ : int = ( Features({feature: Value(lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE_ : Dict = JsonDatasetReader(lowerCAmelCase , features=lowerCAmelCase , cache_dir=lowerCAmelCase ).read() _check_json_dataset(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_3": "float64", "col_1": "string", "col_2": "int64"}, ] , ) def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Any = {"col_3": "float64", "col_1": "string", "col_2": "int64"} SCREAMING_SNAKE_CASE_ : Dict = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE_ : Optional[Any] = ( Features({feature: Value(lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE_ : List[Any] = JsonDatasetReader(lowerCAmelCase , features=lowerCAmelCase , cache_dir=lowerCAmelCase ).read() assert isinstance(lowerCAmelCase , lowerCAmelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def _snake_case ( lowerCAmelCase : List[Any] , lowerCAmelCase : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = {"col_2": "int64", "col_3": "float64", "col_1": "string"} SCREAMING_SNAKE_CASE_ : List[str] = features.copy() SCREAMING_SNAKE_CASE_ : List[str] = ( Features({feature: Value(lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE_ : Optional[int] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Union[str, Any] = JsonDatasetReader(lowerCAmelCase , features=lowerCAmelCase , cache_dir=lowerCAmelCase ).read() assert isinstance(lowerCAmelCase , lowerCAmelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def _snake_case ( lowerCAmelCase : Tuple , lowerCAmelCase : Optional[int] , lowerCAmelCase : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE_ : Optional[int] = JsonDatasetReader(lowerCAmelCase , cache_dir=lowerCAmelCase , split=lowerCAmelCase ).read() _check_json_dataset(lowerCAmelCase , lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def _snake_case ( lowerCAmelCase : List[Any] , lowerCAmelCase : Optional[int] , lowerCAmelCase : str ): """simple docstring""" if issubclass(lowerCAmelCase , lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : Tuple = jsonl_path elif issubclass(lowerCAmelCase , lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : List[str] = [jsonl_path] SCREAMING_SNAKE_CASE_ : List[Any] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE_ : Union[str, Any] = JsonDatasetReader(lowerCAmelCase , cache_dir=lowerCAmelCase ).read() _check_json_dataset(lowerCAmelCase , lowerCAmelCase ) def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : Union[str, Any]=("train",) ): """simple docstring""" assert isinstance(lowerCAmelCase , lowerCAmelCase ) for split in splits: SCREAMING_SNAKE_CASE_ : Optional[int] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def _snake_case ( lowerCAmelCase : Any , lowerCAmelCase : int , lowerCAmelCase : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE_ : List[str] = JsonDatasetReader({"train": jsonl_path} , cache_dir=lowerCAmelCase , keep_in_memory=lowerCAmelCase ).read() _check_json_datasetdict(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def _snake_case ( lowerCAmelCase : Dict , lowerCAmelCase : Optional[int] , lowerCAmelCase : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : Optional[Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE_ : Optional[int] = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE_ : Tuple = ( Features({feature: Value(lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE_ : Dict = JsonDatasetReader({"train": jsonl_path} , features=lowerCAmelCase , cache_dir=lowerCAmelCase ).read() _check_json_datasetdict(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : Any , lowerCAmelCase : Union[str, Any] ): """simple docstring""" if split: SCREAMING_SNAKE_CASE_ : Any = {split: jsonl_path} else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = "train" SCREAMING_SNAKE_CASE_ : List[Any] = {"train": jsonl_path, "test": jsonl_path} SCREAMING_SNAKE_CASE_ : List[str] = tmp_path / "cache" SCREAMING_SNAKE_CASE_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE_ : List[str] = JsonDatasetReader(lowerCAmelCase , cache_dir=lowerCAmelCase ).read() _check_json_datasetdict(lowerCAmelCase , lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def _snake_case ( lowerCAmelCase : str ): """simple docstring""" return json.load(lowerCAmelCase ) def _snake_case ( lowerCAmelCase : Optional[int] ): """simple docstring""" return [json.loads(lowerCAmelCase ) for line in buffer] class a__ : @pytest.mark.parametrize("lines, load_json_function",[(True, load_json_lines), (False, load_json)] ) def __UpperCamelCase ( self : Optional[Any],_A : Optional[Any],_A : str,_A : Union[str, Any] ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_A,_A,lines=_A ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE_ : Dict = load_json_function(_A ) assert isinstance(_A,_A ) assert isinstance(exported_content[0],_A ) assert len(_A ) == 10 @pytest.mark.parametrize( "orient, container, keys, len_at",[ ("records", list, {"tokens", "labels", "answers", "id"}, None), ("split", dict, {"columns", "data"}, "data"), ("index", dict, set("0123456789" ), None), ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"), ("values", list, None, None), ("table", dict, {"schema", "data"}, "data"), ],) def __UpperCamelCase ( self : str,_A : Any,_A : List[str],_A : Union[str, Any],_A : Union[str, Any],_A : List[Any] ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_A,_A,lines=_A,orient=_A ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE_ : str = load_json(_A ) assert isinstance(_A,_A ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_A,"keys" ) and not hasattr(exported_content[0],"keys" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_A ) == 10 @pytest.mark.parametrize("lines, load_json_function",[(True, load_json_lines), (False, load_json)] ) def __UpperCamelCase ( self : Tuple,_A : int,_A : Any,_A : Optional[int] ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_A,_A,lines=_A,num_proc=2 ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE_ : Optional[int] = load_json_function(_A ) assert isinstance(_A,_A ) assert isinstance(exported_content[0],_A ) assert len(_A ) == 10 @pytest.mark.parametrize( "orient, container, keys, len_at",[ ("records", list, {"tokens", "labels", "answers", "id"}, None), ("split", dict, {"columns", "data"}, "data"), ("index", dict, set("0123456789" ), None), ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"), ("values", list, None, None), ("table", dict, {"schema", "data"}, "data"), ],) def __UpperCamelCase ( self : Union[str, Any],_A : Dict,_A : Optional[int],_A : Optional[int],_A : Dict,_A : int ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_A,_A,lines=_A,orient=_A,num_proc=2 ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE_ : Any = load_json(_A ) assert isinstance(_A,_A ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_A,"keys" ) and not hasattr(exported_content[0],"keys" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_A ) == 10 def __UpperCamelCase ( self : str,_A : List[str] ): """simple docstring""" with pytest.raises(_A ): with io.BytesIO() as buffer: JsonDatasetWriter(_A,_A,num_proc=0 ) @pytest.mark.parametrize("compression, extension",[("gzip", "gz"), ("bz2", "bz2"), ("xz", "xz")] ) def __UpperCamelCase ( self : Optional[int],_A : str,_A : Union[str, Any],_A : Optional[Any],_A : List[Any],_A : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = tmp_path_factory.mktemp("data" ) / F'test.json.{extension}' SCREAMING_SNAKE_CASE_ : Any = str(shared_datadir / F'test_file.json.{extension}' ) JsonDatasetWriter(_A,_A,compression=_A ).write() with fsspec.open(_A,"rb",compression="infer" ) as f: SCREAMING_SNAKE_CASE_ : Union[str, Any] = f.read() with fsspec.open(_A,"rb",compression="infer" ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() assert exported_content == original_content
18
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
0
def lowerCamelCase_ ( lowerCamelCase__ ): return 1_0 - x * x def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ ): # Bolzano theory in order to find if there is a root between a and b if equation(lowerCamelCase__ ) * equation(lowerCamelCase__ ) >= 0: raise ValueError("Wrong space!" ) lowerCamelCase_ = a while (b - a) >= 0.01: # Find middle point lowerCamelCase_ = (a + b) / 2 # Check if middle point is root if equation(lowerCamelCase__ ) == 0.0: break # Decide the side to repeat the steps if equation(lowerCamelCase__ ) * equation(lowerCamelCase__ ) < 0: lowerCamelCase_ = c else: lowerCamelCase_ = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
19
__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
0
def _snake_case( SCREAMING_SNAKE_CASE__ ) -> bool: if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: raise ValueError("""Input list must be a non empty list""" ) if len(SCREAMING_SNAKE_CASE__ ) == 1: return True lowercase : Any = series[1] - series[0] for index in range(len(SCREAMING_SNAKE_CASE__ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def _snake_case( SCREAMING_SNAKE_CASE__ ) -> float: if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(SCREAMING_SNAKE_CASE__ ) == 0: raise ValueError("""Input list must be a non empty list""" ) lowercase : List[str] = 0 for val in series: answer += val return answer / len(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": import doctest doctest.testmod()
20
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
0
def UpperCamelCase_( lowerCamelCase_ ) -> list[int]: if length <= 0 or not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(lowerCamelCase_ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
21
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
0
'''simple docstring''' import os import re import shutil import sys import tempfile import unittest import black __SCREAMING_SNAKE_CASE :Optional[int] = 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. __SCREAMING_SNAKE_CASE :Any = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class A_ ( unittest.TestCase ): def lowercase ( self : List[Any] ): _UpperCAmelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , "models/bert/" ) ) _UpperCAmelCase = self.transformer_dir shutil.copy( os.path.join(snake_case_ , "src/transformers/models/bert/modeling_bert.py" ) , os.path.join(self.transformer_dir , "models/bert/modeling_bert.py" ) , ) def lowercase ( self : Optional[Any] ): _UpperCAmelCase = "src/transformers" shutil.rmtree(self.transformer_dir ) def lowercase ( self : Dict , snake_case_ : int , snake_case_ : Any , snake_case_ : Any , snake_case_ : Union[str, Any]=None ): _UpperCAmelCase = comment + f'\nclass {class_name}(nn.Module):\n' + class_code if overwrite_result is not None: _UpperCAmelCase = comment + f'\nclass {class_name}(nn.Module):\n' + overwrite_result _UpperCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=1_1_9 ) _UpperCAmelCase = black.format_str(snake_case_ , mode=snake_case_ ) _UpperCAmelCase = os.path.join(self.transformer_dir , "new_code.py" ) with open(snake_case_ , "w" , newline="\n" ) as f: f.write(snake_case_ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case_ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case_ ) with open(snake_case_ , "r" ) as f: self.assertTrue(f.read() , snake_case_ ) def lowercase ( self : List[Any] ): _UpperCAmelCase = check_copies.find_code_in_transformers("models.bert.modeling_bert.BertLMPredictionHead" ) self.assertEqual(snake_case_ , snake_case_ ) def lowercase ( self : Any ): # 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" , snake_case_ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , re.sub("Bert" , "TestModel" , snake_case_ ) , ) # Copy consistency with a really long name _UpperCAmelCase = "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" , snake_case_ , snake_case_ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , snake_case_ , overwrite_result=re.sub("Bert" , "TestModel" , snake_case_ ) , ) def lowercase ( self : List[Any] ): _UpperCAmelCase = check_copies.LOCALIZED_READMES["README_zh-hans.md"] _UpperCAmelCase = ( "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." ) _UpperCAmelCase = ( "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" ) _UpperCAmelCase = ( "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" ) _UpperCAmelCase , _UpperCAmelCase = check_copies.convert_to_localized_md( snake_case_ , snake_case_ , localized_readme["format_model_list"] ) self.assertFalse(snake_case_ ) self.assertEqual(snake_case_ , snake_case_ ) _UpperCAmelCase , _UpperCAmelCase = check_copies.convert_to_localized_md( snake_case_ , snake_case_ , localized_readme["format_model_list"] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(snake_case_ ) _UpperCAmelCase = ( "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." ) _UpperCAmelCase = ( "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" ) _UpperCAmelCase = ( "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" ) _UpperCAmelCase , _UpperCAmelCase = check_copies.convert_to_localized_md( snake_case_ , snake_case_ , localized_readme["format_model_list"] ) # Check if the model link is synchronized. self.assertEqual(snake_case_ , snake_case_ )
22
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
0
'''simple docstring''' 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, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging UpperCamelCase__: Optional[Any] = logging.get_logger(__name__) if is_vision_available(): import PIL class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" lowerCamelCase__ = ["""pixel_values"""] def __init__( self : List[Any] , __snake_case : bool = True , __snake_case : Dict[str, int] = None , __snake_case : PILImageResampling = PILImageResampling.BICUBIC , __snake_case : bool = True , __snake_case : Dict[str, int] = None , __snake_case : bool = True , __snake_case : Union[int, float] = 1 / 255 , __snake_case : bool = True , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : bool = True , **__snake_case : List[str] , ) -> None: super().__init__(**__snake_case ) UpperCAmelCase : Any = size if size is not None else {'''shortest_edge''': 224} UpperCAmelCase : int = get_size_dict(__snake_case , default_to_square=__snake_case ) UpperCAmelCase : List[Any] = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} UpperCAmelCase : List[str] = get_size_dict(__snake_case , default_to_square=__snake_case , param_name='''crop_size''' ) UpperCAmelCase : Any = do_resize UpperCAmelCase : List[Any] = size UpperCAmelCase : str = resample UpperCAmelCase : Tuple = do_center_crop UpperCAmelCase : Any = crop_size UpperCAmelCase : str = do_rescale UpperCAmelCase : int = rescale_factor UpperCAmelCase : Union[str, Any] = do_normalize UpperCAmelCase : Optional[Any] = image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCAmelCase : Dict = image_std if image_std is not None else OPENAI_CLIP_STD UpperCAmelCase : List[Any] = do_convert_rgb def A ( self : Union[str, Any] , __snake_case : np.ndarray , __snake_case : Dict[str, int] , __snake_case : PILImageResampling = PILImageResampling.BICUBIC , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : List[Any] , ) -> np.ndarray: UpperCAmelCase : List[str] = get_size_dict(__snake_case , default_to_square=__snake_case ) if "shortest_edge" not in size: raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) UpperCAmelCase : Union[str, Any] = get_resize_output_image_size(__snake_case , size=size['''shortest_edge'''] , default_to_square=__snake_case ) return resize(__snake_case , size=__snake_case , resample=__snake_case , data_format=__snake_case , **__snake_case ) def A ( self : Tuple , __snake_case : np.ndarray , __snake_case : Dict[str, int] , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Union[str, Any] , ) -> np.ndarray: UpperCAmelCase : Tuple = get_size_dict(__snake_case ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(__snake_case , size=(size['''height'''], size['''width''']) , data_format=__snake_case , **__snake_case ) def A ( self : Any , __snake_case : np.ndarray , __snake_case : Union[int, float] , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Optional[Any] , ) -> List[str]: return rescale(__snake_case , scale=__snake_case , data_format=__snake_case , **__snake_case ) def A ( self : List[Any] , __snake_case : np.ndarray , __snake_case : Union[float, List[float]] , __snake_case : Union[float, List[float]] , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Dict , ) -> np.ndarray: return normalize(__snake_case , mean=__snake_case , std=__snake_case , data_format=__snake_case , **__snake_case ) def A ( self : List[str] , __snake_case : ImageInput , __snake_case : bool = None , __snake_case : Dict[str, int] = None , __snake_case : PILImageResampling = None , __snake_case : bool = None , __snake_case : int = None , __snake_case : bool = None , __snake_case : float = None , __snake_case : bool = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : bool = None , __snake_case : Optional[Union[str, TensorType]] = None , __snake_case : Optional[ChannelDimension] = ChannelDimension.FIRST , **__snake_case : Optional[int] , ) -> PIL.Image.Image: UpperCAmelCase : Any = do_resize if do_resize is not None else self.do_resize UpperCAmelCase : str = size if size is not None else self.size UpperCAmelCase : Dict = get_size_dict(__snake_case , param_name='''size''' , default_to_square=__snake_case ) UpperCAmelCase : List[str] = resample if resample is not None else self.resample UpperCAmelCase : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase : Any = get_size_dict(__snake_case , param_name='''crop_size''' , default_to_square=__snake_case ) UpperCAmelCase : List[Any] = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase : Tuple = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase : Any = image_mean if image_mean is not None else self.image_mean UpperCAmelCase : Union[str, Any] = image_std if image_std is not None else self.image_std UpperCAmelCase : Any = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCAmelCase : List[str] = make_list_of_images(__snake_case ) if not valid_images(__snake_case ): 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.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCAmelCase : str = [convert_to_rgb(__snake_case ) for image in images] # All transformations expect numpy arrays. UpperCAmelCase : Union[str, Any] = [to_numpy_array(__snake_case ) for image in images] if do_resize: UpperCAmelCase : int = [self.resize(image=__snake_case , size=__snake_case , resample=__snake_case ) for image in images] if do_center_crop: UpperCAmelCase : Optional[Any] = [self.center_crop(image=__snake_case , size=__snake_case ) for image in images] if do_rescale: UpperCAmelCase : Optional[int] = [self.rescale(image=__snake_case , scale=__snake_case ) for image in images] if do_normalize: UpperCAmelCase : int = [self.normalize(image=__snake_case , mean=__snake_case , std=__snake_case ) for image in images] UpperCAmelCase : List[Any] = [to_channel_dimension_format(__snake_case , __snake_case ) for image in images] UpperCAmelCase : Tuple = {'''pixel_values''': images} return BatchFeature(data=__snake_case , tensor_type=__snake_case )
23
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
0
from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case_ = logging.get_logger(__name__) snake_case_ = { 'facebook/vit-mae-base': 'https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): A_ : Tuple = 'vit_mae' def __init__(self : Dict , a__ : Union[str, Any]=768 , a__ : Optional[Any]=12 , a__ : Optional[int]=12 , a__ : Any=3072 , a__ : Any="gelu" , a__ : Optional[Any]=0.0 , a__ : Dict=0.0 , a__ : int=0.0_2 , a__ : Optional[Any]=1E-12 , a__ : Optional[Any]=224 , a__ : List[str]=16 , a__ : str=3 , a__ : List[str]=True , a__ : Dict=16 , a__ : List[str]=512 , a__ : List[str]=8 , a__ : Any=2048 , a__ : Dict=0.7_5 , a__ : int=False , **a__ : List[str] , ): """simple docstring""" super().__init__(**a__ ) __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = initializer_range __snake_case = layer_norm_eps __snake_case = image_size __snake_case = patch_size __snake_case = num_channels __snake_case = qkv_bias __snake_case = decoder_num_attention_heads __snake_case = decoder_hidden_size __snake_case = decoder_num_hidden_layers __snake_case = decoder_intermediate_size __snake_case = mask_ratio __snake_case = norm_pix_loss
24
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
0
"""simple docstring""" def lowercase_ ( _snake_case = 50 ): SCREAMING_SNAKE_CASE__ : Tuple = [1] * (length + 1) for row_length in range(3 ,length + 1 ): for block_length in range(3 ,row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f"""{solution() = }""")
25
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
0
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor _snake_case = logging.get_logger(__name__) class lowercase ( UpperCamelCase__ ): def __init__( self , *_a , **_a ) -> None: warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , _a , ) super().__init__(*_a , **_a )
26
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
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __lowercase : List[Any] = logging.get_logger(__name__) class __UpperCamelCase ( lowerCAmelCase_ ): A_ = ["pixel_values"] def __init__( self , __a = True , __a = None , __a = PILImageResampling.BICUBIC , __a = True , __a = 1 / 255 , __a = True , __a = None , __a = None , __a = True , **__a , ): '''simple docstring''' super().__init__(**__a ) __a : int = size if size is not None else {'height': 384, 'width': 384} __a : Any = get_size_dict(__a , default_to_square=__a ) __a : Union[str, Any] = do_resize __a : Tuple = size __a : Optional[int] = resample __a : str = do_rescale __a : List[Any] = rescale_factor __a : Tuple = do_normalize __a : Tuple = image_mean if image_mean is not None else OPENAI_CLIP_MEAN __a : List[str] = image_std if image_std is not None else OPENAI_CLIP_STD __a : str = do_convert_rgb def __UpperCAmelCase ( self , __a , __a , __a = PILImageResampling.BICUBIC , __a = None , **__a , ): '''simple docstring''' __a : str = get_size_dict(__a , default_to_square=__a ) 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 : Optional[Any] = (size['height'], size['width']) return resize(__a , size=__a , resample=__a , data_format=__a , **__a ) def __UpperCAmelCase ( self , __a , __a , __a = None , **__a , ): '''simple docstring''' return rescale(__a , scale=__a , data_format=__a , **__a ) def __UpperCAmelCase ( self , __a , __a , __a , __a = None , **__a , ): '''simple docstring''' return normalize(__a , mean=__a , std=__a , data_format=__a , **__a ) def __UpperCAmelCase ( self , __a , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = ChannelDimension.FIRST , **__a , ): '''simple docstring''' __a : str = do_resize if do_resize is not None else self.do_resize __a : Tuple = resample if resample is not None else self.resample __a : Dict = do_rescale if do_rescale is not None else self.do_rescale __a : Optional[int] = rescale_factor if rescale_factor is not None else self.rescale_factor __a : str = do_normalize if do_normalize is not None else self.do_normalize __a : Optional[int] = image_mean if image_mean is not None else self.image_mean __a : Any = image_std if image_std is not None else self.image_std __a : str = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __a : Any = size if size is not None else self.size __a : Union[str, Any] = get_size_dict(__a , default_to_square=__a ) __a : Union[str, Any] = make_list_of_images(__a ) if not valid_images(__a ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # PIL RGBA images are converted to RGB if do_convert_rgb: __a : List[Any] = [convert_to_rgb(__a ) for image in images] # All transformations expect numpy arrays. __a : int = [to_numpy_array(__a ) for image in images] if do_resize: __a : Any = [self.resize(image=__a , size=__a , resample=__a ) for image in images] if do_rescale: __a : Union[str, Any] = [self.rescale(image=__a , scale=__a ) for image in images] if do_normalize: __a : List[str] = [self.normalize(image=__a , mean=__a , std=__a ) for image in images] __a : Tuple = [to_channel_dimension_format(__a , __a ) for image in images] __a : int = BatchFeature(data={'pixel_values': images} , tensor_type=__a ) return encoded_outputs
27
# 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
0
'''simple docstring''' # coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : Union[str, Any] = "3" print("Python version:", sys.version) print("OS platform:", platform.platform()) print("OS architecture:", platform.machine()) try: import torch print("Torch version:", torch.__version__) print("Cuda available:", torch.cuda.is_available()) print("Cuda version:", torch.version.cuda) print("CuDNN version:", torch.backends.cudnn.version()) print("Number of GPUs available:", torch.cuda.device_count()) except ImportError: print("Torch version:", None) try: import transformers print("transformers version:", transformers.__version__) except ImportError: print("transformers version:", None)
28
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
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import copy import importlib.metadata import json import os from dataclasses import dataclass from typing import Any, Dict, Union from packaging import version from ..utils import is_torch_available, logging if is_torch_available(): import torch __UpperCAmelCase = logging.get_logger(__name__) @dataclass class lowerCamelCase : '''simple docstring''' def __init__( self , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=6.0 , _UpperCamelCase=None , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=None , _UpperCamelCase="fp4" , _UpperCamelCase=False , **_UpperCamelCase , ) -> Optional[int]: UpperCAmelCase_ : Union[str, Any] = load_in_abit UpperCAmelCase_ : Any = load_in_abit UpperCAmelCase_ : List[Any] = llm_inta_threshold UpperCAmelCase_ : Tuple = llm_inta_skip_modules UpperCAmelCase_ : Tuple = llm_inta_enable_fpaa_cpu_offload UpperCAmelCase_ : Optional[Any] = llm_inta_has_fpaa_weight UpperCAmelCase_ : Union[str, Any] = bnb_abit_quant_type UpperCAmelCase_ : Dict = bnb_abit_use_double_quant if bnb_abit_compute_dtype is None: UpperCAmelCase_ : int = torch.floataa elif isinstance(_UpperCamelCase , _UpperCamelCase ): UpperCAmelCase_ : str = getattr(_UpperCamelCase , _UpperCamelCase ) elif isinstance(_UpperCamelCase , torch.dtype ): UpperCAmelCase_ : Optional[Any] = bnb_abit_compute_dtype else: raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype' ) self.post_init() def __UpperCAmelCase ( self ) -> int: if not isinstance(self.llm_inta_threshold , _UpperCamelCase ): raise ValueError('llm_int8_threshold must be a float' ) if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules , _UpperCamelCase ): raise ValueError('llm_int8_skip_modules must be a list of strings' ) if not isinstance(self.llm_inta_enable_fpaa_cpu_offload , _UpperCamelCase ): raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean' ) if not isinstance(self.llm_inta_has_fpaa_weight , _UpperCamelCase ): raise ValueError('llm_int8_has_fp16_weight must be a boolean' ) if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype , torch.dtype ): raise ValueError('bnb_4bit_compute_dtype must be torch.dtype' ) if not isinstance(self.bnb_abit_quant_type , _UpperCamelCase ): raise ValueError('bnb_4bit_quant_type must be a string' ) if not isinstance(self.bnb_abit_use_double_quant , _UpperCamelCase ): raise ValueError('bnb_4bit_use_double_quant must be a boolean' ) if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes' ) ) >= version.parse( '0.39.0' ): raise ValueError( '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version' ) def __UpperCAmelCase ( self ) -> str: return self.load_in_abit or self.load_in_abit def __UpperCAmelCase ( self ) -> List[str]: if self.load_in_abit: return "llm_int8" elif self.load_in_abit and self.bnb_abit_quant_type == "fp4": return "fp4" elif self.load_in_abit and self.bnb_abit_quant_type == "nf4": return "nf4" else: return None @classmethod def __UpperCAmelCase ( cls , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) -> Tuple: UpperCAmelCase_ : str = cls(**_UpperCamelCase ) UpperCAmelCase_ : Dict = [] for key, value in kwargs.items(): if hasattr(_UpperCamelCase , _UpperCamelCase ): setattr(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) to_remove.append(_UpperCamelCase ) for key in to_remove: kwargs.pop(_UpperCamelCase , _UpperCamelCase ) if return_unused_kwargs: return config, kwargs else: return config def __UpperCAmelCase ( self , _UpperCamelCase ) -> int: with open(_UpperCamelCase , 'w' , encoding='utf-8' ) as writer: UpperCAmelCase_ : Union[str, Any] = self.to_dict() UpperCAmelCase_ : Optional[Any] = json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + '\n' writer.write(_UpperCamelCase ) def __UpperCAmelCase ( self ) -> Dict[str, Any]: UpperCAmelCase_ : Optional[Any] = copy.deepcopy(self.__dict__ ) UpperCAmelCase_ : Any = str(output['bnb_4bit_compute_dtype'] ).split('.' )[1] return output def __repr__( self ) -> Optional[Any]: return f"{self.__class__.__name__} {self.to_json_string()}" def __UpperCAmelCase ( self , _UpperCamelCase = True ) -> str: if use_diff is True: UpperCAmelCase_ : Tuple = self.to_diff_dict() else: UpperCAmelCase_ : Dict = self.to_dict() return json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + "\n" def __UpperCAmelCase ( self ) -> Dict[str, Any]: UpperCAmelCase_ : str = self.to_dict() # get the default config dict UpperCAmelCase_ : Optional[Any] = BitsAndBytesConfig().to_dict() UpperCAmelCase_ : Optional[int] = {} # only serialize values that differ from the default config for key, value in config_dict.items(): if value != default_config_dict[key]: UpperCAmelCase_ : List[str] = value return serializable_config_dict
29
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
0
from typing import Dict import numpy as np import torch from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map def a ( snake_case__: Dict[str, torch.Tensor] ): '''simple docstring''' lowercase_ = [] lowercase_ = [] lowercase_ = [] for rt in rc.restypes: lowercase_ = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]] restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] ) lowercase_ = {name: i for i, name in enumerate(snake_case__ )} restype_atomaa_to_atomaa_list.append( [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] ) restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] ) # Add dummy mapping for restype 'UNK' restype_atomaa_to_atomaa_list.append([0] * 14 ) restype_atomaa_to_atomaa_list.append([0] * 37 ) restype_atomaa_mask_list.append([0.0] * 14 ) lowercase_ = torch.tensor( snake_case__ , dtype=torch.intaa , device=protein['''aatype'''].device , ) lowercase_ = torch.tensor( snake_case__ , dtype=torch.intaa , device=protein['''aatype'''].device , ) lowercase_ = torch.tensor( snake_case__ , dtype=torch.floataa , device=protein['''aatype'''].device , ) lowercase_ = protein['''aatype'''].to(torch.long ) # create the mapping for (residx, atom14) --> atom37, i.e. an array # with shape (num_res, 14) containing the atom37 indices for this protein lowercase_ = restype_atomaa_to_atomaa[protein_aatype] lowercase_ = restype_atomaa_mask[protein_aatype] lowercase_ = residx_atomaa_mask lowercase_ = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back lowercase_ = restype_atomaa_to_atomaa[protein_aatype] lowercase_ = residx_atomaa_to_atomaa.long() # create the corresponding mask lowercase_ = torch.zeros([21, 37] , dtype=torch.floataa , device=protein['''aatype'''].device ) for restype, restype_letter in enumerate(rc.restypes ): lowercase_ = rc.restype_atoa[restype_letter] lowercase_ = rc.residue_atoms[restype_name] for atom_name in atom_names: lowercase_ = rc.atom_order[atom_name] lowercase_ = 1 lowercase_ = restype_atomaa_mask[protein_aatype] lowercase_ = residx_atomaa_mask return protein def a ( snake_case__: Dict[str, torch.Tensor] ): '''simple docstring''' lowercase_ = tree_map(lambda snake_case__ : torch.tensor(snake_case__ , device=batch['''aatype'''].device ) , snake_case__ , np.ndarray ) lowercase_ = tensor_tree_map(lambda snake_case__ : np.array(snake_case__ ) , make_atomaa_masks(snake_case__ ) ) return out
30
# 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
0
'''simple docstring''' def UpperCamelCase_ ( ) -> int: """simple docstring""" return 1 def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else five_pence(x - 5 ) + two_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else ten_pence(x - 10 ) + five_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else twenty_pence(x - 20 ) + ten_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else fifty_pence(x - 50 ) + twenty_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else one_pound(x - 100 ) + fifty_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else two_pound(x - 200 ) + one_pound(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int = 200 ) -> int: """simple docstring""" return two_pound(_UpperCAmelCase ) if __name__ == "__main__": print(solution(int(input().strip())))
31
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
0
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : UNetaDModel snake_case__ : ScoreSdeVeScheduler def __init__( self : str , SCREAMING_SNAKE_CASE__ : UNetaDModel , SCREAMING_SNAKE_CASE__ : ScoreSdeVeScheduler ) -> Optional[int]: super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) @torch.no_grad() def __call__( self : str , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 2_0_0_0 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , **SCREAMING_SNAKE_CASE__ : str , ) -> Union[ImagePipelineOutput, Tuple]: a_ : Tuple = self.unet.config.sample_size a_ : Optional[Any] = (batch_size, 3, img_size, img_size) a_ : Optional[Any] = self.unet a_ : List[str] = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ) * self.scheduler.init_noise_sigma a_ : Tuple = sample.to(self.device ) self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ ) self.scheduler.set_sigmas(SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): a_ : Dict = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): a_ : Tuple = self.unet(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).sample a_ : Optional[Any] = self.scheduler.step_correct(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ).prev_sample # prediction step a_ : Optional[int] = model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).sample a_ : Tuple = self.scheduler.step_pred(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ) a_ , a_ : Union[str, Any] = output.prev_sample, output.prev_sample_mean a_ : Dict = sample_mean.clamp(0 , 1 ) a_ : Optional[Any] = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": a_ : List[Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE__ )
32
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
0
"""simple docstring""" from __future__ import annotations import requests __A : Optional[Any] = set( '''approved_at_utc approved_by author_flair_background_color author_flair_css_class author_flair_richtext author_flair_template_id author_fullname author_premium can_mod_post category clicked content_categories created_utc downs edited gilded gildings hidden hide_score is_created_from_ads_ui is_meta is_original_content is_reddit_media_domain is_video link_flair_css_class link_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title name permalink pwls quarantine saved score secure_media secure_media_embed selftext subreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type total_awards_received ups upvote_ratio url user_reports'''.split() ) def lowercase ( __snake_case : str , __snake_case : int = 1 , __snake_case : str = "new" , __snake_case : list | None = None ): lowercase_ : Tuple = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(__snake_case ) - valid_terms ) ): lowercase_ : Union[str, Any] = F'''Invalid search term: {invalid_search_terms}''' raise ValueError(__snake_case ) lowercase_ : Optional[Any] = requests.get( F'''https://reddit.com/r/{subreddit}/{age}.json?limit={limit}''' , headers={'''User-agent''': '''A random string'''} , ) if response.status_code == 4_2_9: raise requests.HTTPError lowercase_ : Optional[int] = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(__snake_case )} lowercase_ : str = {} for id_ in range(__snake_case ): lowercase_ : Dict = { item: data['''data''']['''children'''][id_]['''data'''][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('''learnpython''', wanted_data=['''title''', '''url''', '''selftext''']))
33
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
0
'''simple docstring''' import enum import os from hashlib import shaaaa from typing import Optional from .. import config from .logging import get_logger A =get_logger(__name__) class _a ( enum.Enum ): __a : Union[str, Any] = """all_checks""" __a : Tuple = """basic_checks""" __a : Tuple = """no_checks""" class _a ( __a ): pass class _a ( __a ): pass class _a ( __a ): pass class _a ( __a ): pass def snake_case_ (_a : Optional[dict] , _a : dict , _a : Any=None ): if expected_checksums is None: logger.info('''Unable to verify checksums.''' ) return if len(set(_a ) - set(_a ) ) > 0: raise ExpectedMoreDownloadedFiles(str(set(_a ) - set(_a ) ) ) if len(set(_a ) - set(_a ) ) > 0: raise UnexpectedDownloadedFile(str(set(_a ) - set(_a ) ) ) UpperCAmelCase = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]] UpperCAmelCase = ''' for ''' + verification_name if verification_name is not None else '''''' if len(_a ) > 0: raise NonMatchingChecksumError( F"Checksums didn't match{for_verification_name}:\n" F"{bad_urls}\n" '''Set `verification_mode=\'no_checks\'` to skip checksums verification and ignore this error''' ) logger.info('''All the checksums matched successfully''' + for_verification_name ) class _a ( __a ): pass class _a ( __a ): pass class _a ( __a ): pass class _a ( __a ): pass def snake_case_ (_a : Optional[dict] , _a : dict ): if expected_splits is None: logger.info('''Unable to verify splits sizes.''' ) return if len(set(_a ) - set(_a ) ) > 0: raise ExpectedMoreSplits(str(set(_a ) - set(_a ) ) ) if len(set(_a ) - set(_a ) ) > 0: raise UnexpectedSplits(str(set(_a ) - set(_a ) ) ) UpperCAmelCase = [ {'''expected''': expected_splits[name], '''recorded''': recorded_splits[name]} for name in expected_splits if expected_splits[name].num_examples != recorded_splits[name].num_examples ] if len(_a ) > 0: raise NonMatchingSplitsSizesError(str(_a ) ) logger.info('''All the splits matched successfully.''' ) def snake_case_ (_a : str , _a : bool = True ): if record_checksum: UpperCAmelCase = shaaaa() with open(_a , '''rb''' ) as f: for chunk in iter(lambda: f.read(1 << 2_0 ) , b'''''' ): m.update(_a ) UpperCAmelCase = m.hexdigest() else: UpperCAmelCase = None return {"num_bytes": os.path.getsize(_a ), "checksum": checksum} def snake_case_ (_a : Optional[int] ): if dataset_size and config.IN_MEMORY_MAX_SIZE: return dataset_size < config.IN_MEMORY_MAX_SIZE else: return False
34
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
0
'''simple docstring''' from dataclasses import dataclass, field from typing import Optional @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Model name or path of model to be trained."} ) lowercase = field( default="./" , metadata={"help": "Save dir where model repo is cloned and models updates are saved to."} ) lowercase = field( default="codeparrot/codeparrot-clean-train" , metadata={"help": "Name or path of training dataset."} ) lowercase = field( default="codeparrot/codeparrot-clean-valid" , metadata={"help": "Name or path of validation dataset."} ) lowercase = field(default=2 , metadata={"help": "Batch size for training."} ) lowercase = field(default=2 , metadata={"help": "Batch size for evaluation."} ) lowercase = field(default=0.1 , metadata={"help": "Value of weight decay."} ) lowercase = field( default=1_00_00 , metadata={"help": "Size of buffer used to shuffle streaming dataset."} ) lowercase = field(default=2e-4 , metadata={"help": "Learning rate fo training."} ) lowercase = field(default="cosine" , metadata={"help": "Learning rate."} ) lowercase = field( default=7_50 , metadata={"help": "Number of warmup steps in the learning rate schedule."} ) lowercase = field( default=16 , metadata={"help": "Number of gradient accumulation steps."} ) lowercase = field( default=_a , metadata={"help": "Use gradient checkpointing to reduce memory footprint."} ) lowercase = field(default=5_00_00 , metadata={"help": "Maximum number of training steps."} ) lowercase = field( default=-1 , metadata={"help": "Maximum number of evaluation steps. If -1 the full dataset is evaluated."} ) lowercase = field(default=10_24 , metadata={"help": "Sequence lengths used for training."} ) lowercase = field(default=1 , metadata={"help": "Training seed."} ) lowercase = field( default=10_24 , metadata={"help": "Interval to save checkpoints. Measured as number of forward passes not training steps."} , ) lowercase = field( default=_a , metadata={"help": "States path if the training should continue from a checkpoint folder."} ) lowercase = field(default=_a , metadata={"help": "If True the data is pretokenized."} ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Model name or path of model to be evaluated."} ) lowercase = field( default="codeparrot/codeparrot-clean-valid" , metadata={"help": "Name or path of validation dataset."} ) lowercase = field(default=2 , metadata={"help": "Batch size used for evaluation."} ) lowercase = field( default=-1 , metadata={"help": "Maximum number of evaluation steps. If -1 the full dataset is evaluated."} ) lowercase = field(default=10_24 , metadata={"help": "Length of sequences to be evaluated."} ) lowercase = field(default=1 , metadata={"help": "Random seed used for evaluation."} ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Model name or path of model to be evaluated."} ) lowercase = field(default=_a , metadata={"help": "Number of workers used for code evaluation."} ) lowercase = field( default=_a , metadata={"help": "The number of human-eval tasks to run. If not included all tasks are evaluated."} , ) lowercase = field( default=_a , metadata={"help": "Sample from the language model's output distribution."} ) lowercase = field(default=0.2 , metadata={"help": "Sampling temperature used for generation."} ) lowercase = field(default=2_56 , metadata={"help": "Maximum number of newly generated tokens."} ) lowercase = field(default=0 , metadata={"help": "Top-k parameter used for generation."} ) lowercase = field(default=0.95 , metadata={"help": "Top-p parameter used for nucleus sampling."} ) lowercase = field(default=10 , metadata={"help": "Number of generations to run in parallel."} ) lowercase = field( default=2_00 , metadata={"help": "Number of completions to generate for each sample."} ) lowercase = field(default=1 , metadata={"help": "Random seed used for evaluation."} ) lowercase = field( default="eval_results.json" , metadata={"help": "Random seed used for evaluation."} ) lowercase = field( default="0" , metadata={"help": "Allow `code_eval` to execute Python code on machine"} ) lowercase = field( default=-1 , metadata={ "help": ( "Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive" " number corresponds to which GPU device id to run on." ) } , ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default=_a , metadata={ "help": "The number of CPU cores to use for parallel preprocessing. Default uses the maximum available." } , ) lowercase = field( default="transformersbook/codeparrot" , metadata={"help": "Folder or name of dataset to process."} ) lowercase = field( default="codeparrot-clean" , metadata={"help": "Folder to save processed processed dataset."} ) lowercase = field( default=10_00_00 , metadata={"help": "Number of files to save per JSON output file."} ) lowercase = field(default="content" , metadata={"help": "Column containing text data to process."} ) lowercase = field( default=10_00 , metadata={"help": "Maximum line length in file, otherwise file is filtered."} ) lowercase = field( default=1_00 , metadata={"help": "Maximum mean line length in file, otherwise file is filtered."} ) lowercase = field( default=0.25 , metadata={"help": "Maximum fraction of non-alphanumeric characters, otherwise file is filtered."} ) lowercase = field( default=1.5 , metadata={"help": "Minimum character token ratio for the file, otherwise file is filtered."} ) lowercase = field( default=0.7 , metadata={"help": "Probability for filtering config, test and uncommon files."} ) lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Name or path to the tokenizer."} , ) lowercase = field( default=_a , metadata={"help": "If True, near-duplicate samples are removed."} ) lowercase = field( default=0.85 , metadata={"help": "Jaccard threshold for near-duplicate samples."} ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="gpt2" , metadata={"help": "Base tokenizer to build new tokenizer from."} ) lowercase = field( default="transformersbook/codeparrot-train" , metadata={"help": "Dataset to train tokenizer on."} ) lowercase = field(default="content" , metadata={"help": "Column containing text data to process."} ) lowercase = field(default=20_00_00 , metadata={"help": "Number of examples to train tokenizer on."} ) lowercase = field( default=3_27_68 , metadata={"help": "Number of examples to train the tokenizer on."} ) lowercase = field(default="codeparrot" , metadata={"help": "Name of new tokenizer."} ) lowercase = field(default=_a , metadata={"help": "Push saved tokenizer to the hub."} ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Name or path to the tokenizer."} ) lowercase = field( default="codeparrot/codeparrot-clean-train" , metadata={"help": "Name or path to the dataset to pretokenize."} ) lowercase = field( default="tokenized-codeparrot-train" , metadata={"help": "Repo name of the pretokenized data."} ) lowercase = field(default=_a , metadata={"help": "Number of workers used for code evaluation."} ) @dataclass class UpperCAmelCase_ : """simple docstring""" lowercase = field( default="gpt2-large" , metadata={"help": "Configuration to use for model initialization."} ) lowercase = field( default="codeparrot/codeparrot" , metadata={"help": "Tokenizer attached to model."} ) lowercase = field(default="codeparrot" , metadata={"help": "Name of the created model."} ) lowercase = field(default=_a , metadata={"help": "Push saved tokenizer to the hub."} )
35
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
0
from ...processing_utils import ProcessorMixin class UpperCAmelCase_ ( a): lowerCamelCase__ = 'SpeechT5FeatureExtractor' lowerCamelCase__ = 'SpeechT5Tokenizer' def __init__( self, __a, __a): '''simple docstring''' super().__init__(__a, __a) def __call__( self, *__a, **__a): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = kwargs.pop("audio", __a) _lowerCAmelCase : Dict = kwargs.pop("text", __a) _lowerCAmelCase : Dict = kwargs.pop("text_target", __a) _lowerCAmelCase : Union[str, Any] = kwargs.pop("audio_target", __a) _lowerCAmelCase : Any = kwargs.pop("sampling_rate", __a) if audio is not None and text is not None: raise ValueError( "Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?") if audio_target is not None and text_target is not None: raise ValueError( "Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?") if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( "You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.") if audio is not None: _lowerCAmelCase : Tuple = self.feature_extractor(__a, *__a, sampling_rate=__a, **__a) elif text is not None: _lowerCAmelCase : List[Any] = self.tokenizer(__a, **__a) else: _lowerCAmelCase : Dict = None if audio_target is not None: _lowerCAmelCase : Union[str, Any] = self.feature_extractor(audio_target=__a, *__a, sampling_rate=__a, **__a) _lowerCAmelCase : Optional[int] = targets["input_values"] elif text_target is not None: _lowerCAmelCase : List[Any] = self.tokenizer(__a, **__a) _lowerCAmelCase : Union[str, Any] = targets["input_ids"] else: _lowerCAmelCase : Union[str, Any] = None if inputs is None: return targets if targets is not None: _lowerCAmelCase : Any = labels _lowerCAmelCase : List[Any] = targets.get("attention_mask") if decoder_attention_mask is not None: _lowerCAmelCase : Tuple = decoder_attention_mask return inputs def snake_case__ ( self, *__a, **__a): '''simple docstring''' _lowerCAmelCase : List[str] = kwargs.pop("input_values", __a) _lowerCAmelCase : int = kwargs.pop("input_ids", __a) _lowerCAmelCase : List[Any] = kwargs.pop("labels", __a) if input_values is not None and input_ids is not None: raise ValueError("Cannot process both `input_values` and `input_ids` inputs.") if input_values is None and input_ids is None and labels is None: raise ValueError( "You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.") if input_values is not None: _lowerCAmelCase : List[str] = self.feature_extractor.pad(__a, *__a, **__a) elif input_ids is not None: _lowerCAmelCase : Optional[Any] = self.tokenizer.pad(__a, **__a) else: _lowerCAmelCase : List[Any] = None if labels is not None: if "input_ids" in labels or (isinstance(__a, __a) and "input_ids" in labels[0]): _lowerCAmelCase : str = self.tokenizer.pad(__a, **__a) _lowerCAmelCase : str = targets["input_ids"] else: _lowerCAmelCase : Union[str, Any] = self.feature_extractor.feature_size _lowerCAmelCase : str = self.feature_extractor.num_mel_bins _lowerCAmelCase : str = self.feature_extractor.pad(__a, *__a, **__a) _lowerCAmelCase : List[Any] = feature_size_hack _lowerCAmelCase : str = targets["input_values"] else: _lowerCAmelCase : Optional[Any] = None if inputs is None: return targets if targets is not None: _lowerCAmelCase : str = labels _lowerCAmelCase : List[str] = targets.get("attention_mask") if decoder_attention_mask is not None: _lowerCAmelCase : Any = decoder_attention_mask return inputs def snake_case__ ( self, *__a, **__a): '''simple docstring''' return self.tokenizer.batch_decode(*__a, **__a) def snake_case__ ( self, *__a, **__a): '''simple docstring''' return self.tokenizer.decode(*__a, **__a)
36
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
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer from .base import PipelineTool class lowerCAmelCase_( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Dict = '''facebook/bart-large-mnli''' __lowercase : Optional[int] = ( '''This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which ''' '''should be the text to classify, and `labels`, which should be the list of labels to use for classification. ''' '''It returns the most likely label in the list of provided `labels` for the input text.''' ) __lowercase : str = '''text_classifier''' __lowercase : Union[str, Any] = AutoTokenizer __lowercase : int = AutoModelForSequenceClassification __lowercase : Dict = ['''text''', ['''text''']] __lowercase : int = ['''text'''] def UpperCAmelCase_ ( self ) -> List[Any]: super().setup() lowerCAmelCase__ : Optional[int] = self.model.config lowerCAmelCase__ : str = -1 for idx, label in config.idalabel.items(): if label.lower().startswith("""entail""" ): lowerCAmelCase__ : List[Any] = int(__UpperCAmelCase ) if self.entailment_id == -1: raise ValueError("""Could not determine the entailment ID from the model config, please pass it at init.""" ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ) -> List[Any]: lowerCAmelCase__ : Optional[int] = labels return self.pre_processor( [text] * len(__UpperCAmelCase ) ,[F"""This example is {label}""" for label in labels] ,return_tensors="""pt""" ,padding="""max_length""" ,) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> str: lowerCAmelCase__ : List[str] = outputs.logits lowerCAmelCase__ : Union[str, Any] = torch.argmax(logits[:, 2] ).item() return self._labels[label_id]
37
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
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase_ : Dict = logging.get_logger(__name__) UpperCAmelCase_ : Any = { '''kssteven/ibert-roberta-base''': '''https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json''', '''kssteven/ibert-roberta-large''': '''https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json''', '''kssteven/ibert-roberta-large-mnli''': ( '''https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json''' ), } class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : Dict = """ibert""" def __init__( self : Dict , __lowerCamelCase : Dict=30_522 , __lowerCamelCase : Optional[Any]=768 , __lowerCamelCase : List[Any]=12 , __lowerCamelCase : Tuple=12 , __lowerCamelCase : int=3_072 , __lowerCamelCase : Union[str, Any]="gelu" , __lowerCamelCase : List[str]=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : str=512 , __lowerCamelCase : List[str]=2 , __lowerCamelCase : int=0.02 , __lowerCamelCase : int=1E-12 , __lowerCamelCase : Optional[Any]=1 , __lowerCamelCase : List[Any]=0 , __lowerCamelCase : Tuple=2 , __lowerCamelCase : int="absolute" , __lowerCamelCase : int=False , __lowerCamelCase : Union[str, Any]="none" , **__lowerCamelCase : Union[str, Any] , ): super().__init__(pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) UpperCamelCase :List[Any] = vocab_size UpperCamelCase :Optional[int] = hidden_size UpperCamelCase :List[Any] = num_hidden_layers UpperCamelCase :Optional[int] = num_attention_heads UpperCamelCase :List[str] = hidden_act UpperCamelCase :Tuple = intermediate_size UpperCamelCase :Any = hidden_dropout_prob UpperCamelCase :Union[str, Any] = attention_probs_dropout_prob UpperCamelCase :Any = max_position_embeddings UpperCamelCase :int = type_vocab_size UpperCamelCase :List[str] = initializer_range UpperCamelCase :str = layer_norm_eps UpperCamelCase :int = position_embedding_type UpperCamelCase :Optional[Any] = quant_mode UpperCamelCase :int = force_dequant class _SCREAMING_SNAKE_CASE ( _a ): @property def _A ( self : int ): if self.task == "multiple-choice": UpperCamelCase :Union[str, Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCamelCase :int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
38
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
0
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) _a = logging.getLogger() def __A ( )-> Dict: """simple docstring""" _UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('-f' ) _UpperCAmelCase = parser.parse_args() return args.f def __A ( __lowerCAmelCase )-> Dict: """simple docstring""" _UpperCAmelCase = {} _UpperCAmelCase = os.path.join(__lowerCAmelCase , 'all_results.json' ) if os.path.exists(__lowerCAmelCase ): with open(__lowerCAmelCase , 'r' ) as f: _UpperCAmelCase = json.load(__lowerCAmelCase ) else: raise ValueError(F"""can't find {path}""" ) return results def __A ( )-> str: """simple docstring""" _UpperCAmelCase = torch.cuda.is_available() and torch_device == 'cuda' return is_using_cuda and is_apex_available() _a = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __lowerCamelCase ( snake_case__): """simple docstring""" @classmethod def UpperCamelCase ( cls ): """simple docstring""" _UpperCAmelCase = tempfile.mkdtemp() _UpperCAmelCase = os.path.join(cls.tmpdir , 'default_config.yml' ) write_basic_config(save_location=cls.configPath ) _UpperCAmelCase = ['accelerate', 'launch', '--config_file', cls.configPath] @classmethod def UpperCamelCase ( cls ): """simple docstring""" shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --seed=42 --checkpointing_steps epoch --with_tracking """.split() if is_cuda_and_apex_available(): testargs.append('--fp16' ) run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'glue_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking """.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertLess(result['perplexity'] , 100 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'clm_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertLess(result['perplexity'] , 42 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'mlm_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = 7 if get_gpu_count() > 1 else 2 _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) self.assertLess(result['train_loss'] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'ner_no_trainer' ) ) ) @unittest.skip(reason='Fix me @muellerzr' ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['eval_f1'] , 28 ) self.assertGreaterEqual(result['eval_exact'] , 28 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'qa_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_accuracy'] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'swag_no_trainer' ) ) ) @slow @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_rouge1'] , 10 ) self.assertGreaterEqual(result['eval_rouge2'] , 2 ) self.assertGreaterEqual(result['eval_rougeL'] , 7 ) self.assertGreaterEqual(result['eval_rougeLsum'] , 7 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'summarization_no_trainer' ) ) ) @slow @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_bleu'] , 30 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'translation_no_trainer' ) ) ) @slow def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = logging.StreamHandler(sys.stdout ) logger.addHandler(UpperCAmelCase ) _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch """.split() run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) self.assertGreaterEqual(result['eval_overall_accuracy'] , 0.10 ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = F""" {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 """.split() if is_cuda_and_apex_available(): testargs.append('--fp16' ) run_command(self._launch_args + testargs ) _UpperCAmelCase = get_results(UpperCAmelCase ) # The base model scores a 25% self.assertGreaterEqual(result['eval_accuracy'] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'step_1' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase , 'image_classification_no_trainer' ) ) )
39
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
0
"""simple docstring""" from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder __lowercase = datasets.utils.logging.get_logger(__name__) class _A ( folder_based_builder.FolderBasedBuilderConfig ): """simple docstring""" UpperCAmelCase : bool = None UpperCAmelCase : bool = None class _A ( folder_based_builder.FolderBasedBuilder ): """simple docstring""" UpperCAmelCase : Any = datasets.Audio() UpperCAmelCase : str = """audio""" UpperCAmelCase : Optional[int] = AudioFolderConfig UpperCAmelCase : List[str] # definition at the bottom of the script UpperCAmelCase : str = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) __lowercase = [ """.aiff""", """.au""", """.avr""", """.caf""", """.flac""", """.htk""", """.svx""", """.mat4""", """.mat5""", """.mpc2k""", """.ogg""", """.paf""", """.pvf""", """.raw""", """.rf64""", """.sd2""", """.sds""", """.ircam""", """.voc""", """.w64""", """.wav""", """.nist""", """.wavex""", """.wve""", """.xi""", """.mp3""", """.opus""", ] __lowercase = AUDIO_EXTENSIONS
40
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
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor _A : Dict =logging.get_logger(__name__) class _lowercase ( _lowercase ): def __init__( self: Optional[int] , *UpperCamelCase__: Dict , **UpperCamelCase__: int ): warnings.warn( """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use OwlViTImageProcessor instead.""" , UpperCamelCase__ , ) super().__init__(*UpperCamelCase__ , **UpperCamelCase__ )
41
__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
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( __A ) -> Union[str, Any]: _snake_case = [] _snake_case = [] _snake_case = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator _snake_case = len(__A ) if (len(__A ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(__A ) , 'Postfix'.center(__A ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(__A ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(__A ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(__A ) == 0: stack.append(__A ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(__A ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(__A ) # push x to stack print( x.center(8 ) , (''.join(__A )).ljust(__A ) , (''.join(__A )).ljust(__A ) , sep=' | ' , ) # Output in tabular format while len(__A ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(__A )).ljust(__A ) , (''.join(__A )).ljust(__A ) , sep=' | ' , ) # Output in tabular format return "".join(__A ) # return Postfix as str def SCREAMING_SNAKE_CASE__ ( __A ) -> Dict: _snake_case = list(infix[::-1] ) # reverse the infix equation for i in range(len(__A ) ): if infix[i] == "(": _snake_case = ')' # change "(" to ")" elif infix[i] == ")": _snake_case = '(' # change ")" to "(" return (infix_2_postfix(''.join(__A ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": lowercase : Union[str, Any] = input("\nEnter an Infix Equation = ") # Input an Infix equation lowercase : Union[str, Any] = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
42
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
0
def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Any = 1 for i in range(1 , num + 1 ): fact *= i return fact def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Optional[Any] = 0 while number > 0: __UpperCamelCase :Optional[Any] = number % 10 sum_of_digits += last_digit __UpperCamelCase :str = number // 10 # Removing the last_digit from the given number return sum_of_digits def lowerCamelCase ( SCREAMING_SNAKE_CASE = 100 ): '''simple docstring''' __UpperCamelCase :Dict = factorial(SCREAMING_SNAKE_CASE ) __UpperCamelCase :Dict = split_and_add(SCREAMING_SNAKE_CASE ) return result if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
43
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
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _a : List[str] = logging.get_logger(__name__) _a : Dict = { 'vinvino02/glpn-kitti': 'https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json', # See all GLPN models at https://huggingface.co/models?filter=glpn } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : str = "glpn" def __init__( self , a__=3 , a__=4 , a__=[2, 2, 2, 2] , a__=[8, 4, 2, 1] , a__=[32, 64, 160, 256] , a__=[7, 3, 3, 3] , a__=[4, 2, 2, 2] , a__=[1, 2, 5, 8] , a__=[4, 4, 4, 4] , a__="gelu" , a__=0.0 , a__=0.0 , a__=0.0_2 , a__=0.1 , a__=1e-6 , a__=64 , a__=10 , a__=-1 , **a__ , ): super().__init__(**a__ ) _lowerCAmelCase : int = num_channels _lowerCAmelCase : str = num_encoder_blocks _lowerCAmelCase : str = depths _lowerCAmelCase : str = sr_ratios _lowerCAmelCase : Optional[Any] = hidden_sizes _lowerCAmelCase : Any = patch_sizes _lowerCAmelCase : Any = strides _lowerCAmelCase : str = mlp_ratios _lowerCAmelCase : Optional[int] = num_attention_heads _lowerCAmelCase : Any = hidden_act _lowerCAmelCase : Optional[int] = hidden_dropout_prob _lowerCAmelCase : Any = attention_probs_dropout_prob _lowerCAmelCase : str = initializer_range _lowerCAmelCase : Any = drop_path_rate _lowerCAmelCase : Dict = layer_norm_eps _lowerCAmelCase : int = decoder_hidden_size _lowerCAmelCase : Optional[Any] = max_depth _lowerCAmelCase : Any = head_in_index
44
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
0
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
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
0
"""simple docstring""" def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' while b: lowerCAmelCase , lowerCAmelCase = b, a % b return a def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return a if b == 0 else euclidean_gcd_recursive(SCREAMING_SNAKE_CASE , a % b ) def UpperCAmelCase__ ( ): '''simple docstring''' print(F'euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}' ) print(F'euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}' ) print(F'euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}' ) print(F'euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}' ) print(F'euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}' ) print(F'euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}' ) print(F'euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}' ) print(F'euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}' ) print(F'euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}' ) print(F'euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}' ) if __name__ == "__main__": main()
46
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
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase : Optional[int] = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : List[str] = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : str = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowerCamelCase : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
47
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
0
from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax SCREAMING_SNAKE_CASE__ : Union[str, Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase__ ) class UpperCamelCase__ (lowerCAmelCase__ ): '''simple docstring''' def __init__( self , **UpperCamelCase__ ) -> Optional[Any]: super().__init__(**UpperCamelCase__ ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self , UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: return super().__call__(UpperCamelCase__ , **UpperCamelCase__ ) def _lowercase ( self , **UpperCamelCase__ ) -> List[Any]: lowerCamelCase : Optional[int] = {} if "candidate_labels" in kwargs: lowerCamelCase : str = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: lowerCamelCase : str = kwargs["hypothesis_template"] return preprocess_params, {}, {} def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="This is a photo of {}." ) -> List[Any]: lowerCamelCase : Optional[Any] = load_image(UpperCamelCase__ ) lowerCamelCase : List[Any] = self.image_processor(images=[image] , return_tensors=self.framework ) lowerCamelCase : Dict = candidate_labels lowerCamelCase : Dict = [hypothesis_template.format(UpperCamelCase__ ) for x in candidate_labels] lowerCamelCase : Dict = self.tokenizer(UpperCamelCase__ , return_tensors=self.framework , padding=UpperCamelCase__ ) lowerCamelCase : Union[str, Any] = [text_inputs] return inputs def _lowercase ( self , UpperCamelCase__ ) -> Union[str, Any]: lowerCamelCase : List[str] = model_inputs.pop("candidate_labels" ) lowerCamelCase : Dict = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , UpperCamelCase__ ): lowerCamelCase : Dict = text_inputs[0] else: # Batching case. lowerCamelCase : int = text_inputs[0][0] lowerCamelCase : List[Any] = self.model(**UpperCamelCase__ , **UpperCamelCase__ ) lowerCamelCase : Tuple = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_image, } return model_outputs def _lowercase ( self , UpperCamelCase__ ) -> str: lowerCamelCase : Union[str, Any] = model_outputs.pop("candidate_labels" ) lowerCamelCase : Tuple = model_outputs["logits"][0] if self.framework == "pt": lowerCamelCase : Any = logits.softmax(dim=-1 ).squeeze(-1 ) lowerCamelCase : Optional[Any] = probs.tolist() if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): lowerCamelCase : List[Any] = [scores] elif self.framework == "tf": lowerCamelCase : str = stable_softmax(UpperCamelCase__ , axis=-1 ) lowerCamelCase : str = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) lowerCamelCase : Any = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(UpperCamelCase__ , UpperCamelCase__ ) , key=lambda UpperCamelCase__ : -x[0] ) ] return result
48
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
0
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __snake_case :Optional[int] = [ '''openmmlab/upernet-convnext-tiny''', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __snake_case :List[str] = '''UperNetConfig''' class _A ( nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Union[int, Tuple[int, int]] , __SCREAMING_SNAKE_CASE : Union[int, Tuple[int, int], str] = 0 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Union[int, Tuple[int, int]] = 1 , ): '''simple docstring''' super().__init__() __a = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) __a = nn.BatchNormad(__SCREAMING_SNAKE_CASE) __a = nn.ReLU() def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : torch.Tensor): '''simple docstring''' __a = self.conv(__SCREAMING_SNAKE_CASE) __a = self.batch_norm(__SCREAMING_SNAKE_CASE) __a = self.activation(__SCREAMING_SNAKE_CASE) return output class _A ( nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int): '''simple docstring''' super().__init__() __a = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1), ] for i, layer in enumerate(self.layers): self.add_module(str(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : torch.Tensor): '''simple docstring''' __a = input for layer in self.layers: __a = layer(__SCREAMING_SNAKE_CASE) return hidden_state class _A ( nn.Module ): def __init__( self : int , __SCREAMING_SNAKE_CASE : Tuple[int, ...] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : bool): '''simple docstring''' super().__init__() __a = pool_scales __a = align_corners __a = in_channels __a = channels __a = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE): __a = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE) self.blocks.append(__SCREAMING_SNAKE_CASE) self.add_module(str(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : torch.Tensor): '''simple docstring''' __a = [] for ppm in self.blocks: __a = ppm(__SCREAMING_SNAKE_CASE) __a = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners) ppm_outs.append(__SCREAMING_SNAKE_CASE) return ppm_outs class _A ( nn.Module ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' super().__init__() __a = config __a = config.pool_scales # e.g. (1, 2, 3, 6) __a = in_channels __a = config.hidden_size __a = False __a = nn.Convad(self.channels , config.num_labels , kernel_size=1) # PSP Module __a = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) __a = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module __a = nn.ModuleList() __a = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer __a = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1) __a = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1) self.lateral_convs.append(__SCREAMING_SNAKE_CASE) self.fpn_convs.append(__SCREAMING_SNAKE_CASE) __a = UperNetConvModule( len(self.in_channels) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowerCamelCase ( self : Tuple): '''simple docstring''' self.apply(self._init_weights) def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any]): '''simple docstring''' if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = inputs[-1] __a = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE)) __a = torch.cat(__SCREAMING_SNAKE_CASE , dim=1) __a = self.bottleneck(__SCREAMING_SNAKE_CASE) return output def _lowerCamelCase ( self : List[str] , __SCREAMING_SNAKE_CASE : torch.Tensor): '''simple docstring''' __a = [lateral_conv(encoder_hidden_states[i]) for i, lateral_conv in enumerate(self.lateral_convs)] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE)) # build top-down path __a = len(__SCREAMING_SNAKE_CASE) for i in range(used_backbone_levels - 1 , 0 , -1): __a = laterals[i - 1].shape[2:] __a = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners) # build outputs __a = [self.fpn_convs[i](laterals[i]) for i in range(used_backbone_levels - 1)] # append psp feature fpn_outs.append(laterals[-1]) for i in range(used_backbone_levels - 1 , 0 , -1): __a = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners) __a = torch.cat(__SCREAMING_SNAKE_CASE , dim=1) __a = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE) __a = self.classifier(__SCREAMING_SNAKE_CASE) return output class _A ( nn.Module ): def __init__( self : str , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : int = 2 , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : Union[int, Tuple[int, int]] = 1): '''simple docstring''' super().__init__() __a = config __a = config.auxiliary_in_channels __a = config.auxiliary_channels __a = config.auxiliary_num_convs __a = config.auxiliary_concat_input __a = in_index __a = (kernel_size // 2) * dilation __a = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE)) for i in range(self.num_convs - 1): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE)) if self.num_convs == 0: __a = nn.Identity() else: __a = nn.Sequential(*__SCREAMING_SNAKE_CASE) if self.concat_input: __a = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2) __a = nn.Convad(self.channels , config.num_labels , kernel_size=1) def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' self.apply(self._init_weights) def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : torch.Tensor): '''simple docstring''' __a = encoder_hidden_states[self.in_index] __a = self.convs(__SCREAMING_SNAKE_CASE) if self.concat_input: __a = self.conv_cat(torch.cat([hidden_states, output] , dim=1)) __a = self.classifier(__SCREAMING_SNAKE_CASE) return output class _A ( __UpperCAmelCase ): UpperCamelCase__ : Optional[Any] = UperNetConfig UpperCamelCase__ : List[str] = '''pixel_values''' UpperCamelCase__ : int = True def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : List[Any]=False): '''simple docstring''' if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): __a = value __snake_case :Any = r''' Parameters: This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. config ([`UperNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' __snake_case :Tuple = r''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See `attentions` under returned tensors for more detail. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''' ,__UpperCAmelCase ,) class _A ( __UpperCAmelCase ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : str): '''simple docstring''' super().__init__(__SCREAMING_SNAKE_CASE) __a = AutoBackbone.from_config(config.backbone_config) # Semantic segmentation head(s) __a = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels) __a = UperNetFCNHead(__SCREAMING_SNAKE_CASE) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''')) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC) def _lowerCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : Optional[torch.Tensor] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[torch.Tensor] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , ): '''simple docstring''' __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 __a = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE) __a = outputs.feature_maps __a = self.decode_head(__SCREAMING_SNAKE_CASE) __a = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE) __a = None if self.auxiliary_head is not None: __a = self.auxiliary_head(__SCREAMING_SNAKE_CASE) __a = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE) __a = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''') else: # compute weighted loss __a = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index) __a = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: __a = (logits,) + outputs[1:] else: __a = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
49
# 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
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: _UpperCAmelCase : str = None _UpperCAmelCase : List[Any] = logging.get_logger(__name__) _UpperCAmelCase : List[str] = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} _UpperCAmelCase : Optional[int] = { """vocab_file""": { """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/spiece.model""", """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/spiece.model""", """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model""", """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model""", """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/spiece.model""", """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/spiece.model""", """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model""", """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model""", }, """tokenizer_file""": { """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json""", """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json""", """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json""", """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json""", """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json""", """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json""", """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json""", """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json""", }, } _UpperCAmelCase : Optional[Any] = { """albert-base-v1""": 5_12, """albert-large-v1""": 5_12, """albert-xlarge-v1""": 5_12, """albert-xxlarge-v1""": 5_12, """albert-base-v2""": 5_12, """albert-large-v2""": 5_12, """albert-xlarge-v2""": 5_12, """albert-xxlarge-v2""": 5_12, } _UpperCAmelCase : Optional[int] = """▁""" class lowerCAmelCase ( __UpperCamelCase ): UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = AlbertTokenizer def __init__( self : Optional[Any] , UpperCAmelCase : Tuple=None , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : List[Any]=False , UpperCAmelCase : Tuple="[CLS]" , UpperCAmelCase : str="[SEP]" , UpperCAmelCase : Any="<unk>" , UpperCAmelCase : Any="[SEP]" , UpperCAmelCase : List[Any]="<pad>" , UpperCAmelCase : List[str]="[CLS]" , UpperCAmelCase : Optional[int]="[MASK]" , **UpperCAmelCase : int , ) -> Dict: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCamelCase__ : Any = ( AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase , normalized=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token ) super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , remove_space=UpperCAmelCase , keep_accents=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , **UpperCAmelCase , ) lowerCamelCase__ : Union[str, Any] = do_lower_case lowerCamelCase__ : List[Any] = remove_space lowerCamelCase__ : Optional[Any] = keep_accents lowerCamelCase__ : Tuple = vocab_file lowerCamelCase__ : Tuple = False if not self.vocab_file else True def A_ ( self : List[str] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: lowerCamelCase__ : Optional[Any] = [self.sep_token_id] lowerCamelCase__ : List[Any] = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def A_ ( self : List[Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: lowerCamelCase__ : List[str] = [self.sep_token_id] lowerCamelCase__ : int = [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 ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return lowerCamelCase__ : List[str] = 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,)
50
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
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer snake_case_ : Optional[int] = logging.get_logger(__name__) snake_case_ : str = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} snake_case_ : int = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] snake_case_ : Dict = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } snake_case_ : int = {f"funnel-transformer/{name}": 512 for name in _model_names} snake_case_ : Tuple = {f"funnel-transformer/{name}": {"do_lower_case": True} for name in _model_names} class __snake_case ( a ): UpperCAmelCase__ : Optional[Any] = VOCAB_FILES_NAMES UpperCAmelCase__ : Dict = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : Dict = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ : Optional[int] = FunnelTokenizer UpperCAmelCase__ : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : int = 2 def __init__( self : List[str] , _snake_case : str=None , _snake_case : Tuple=None , _snake_case : int=True , _snake_case : Optional[Any]="<unk>" , _snake_case : List[Any]="<sep>" , _snake_case : Dict="<pad>" , _snake_case : List[Any]="<cls>" , _snake_case : str="<mask>" , _snake_case : Optional[Any]="<s>" , _snake_case : List[str]="</s>" , _snake_case : int=True , _snake_case : Any=True , _snake_case : int=None , _snake_case : List[Any]="##" , **_snake_case : Tuple , ): """simple docstring""" super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , bos_token=_snake_case , eos_token=_snake_case , clean_text=_snake_case , tokenize_chinese_chars=_snake_case , strip_accents=_snake_case , wordpieces_prefix=_snake_case , **_snake_case , ) UpperCAmelCase_ = json.loads(self.backend_tokenizer.normalizer.__getstate__()) if ( normalizer_state.get('''lowercase''' , _snake_case) != do_lower_case or normalizer_state.get('''strip_accents''' , _snake_case) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _snake_case) != tokenize_chinese_chars ): UpperCAmelCase_ = getattr(_snake_case , normalizer_state.pop('''type''')) UpperCAmelCase_ = do_lower_case UpperCAmelCase_ = strip_accents UpperCAmelCase_ = tokenize_chinese_chars UpperCAmelCase_ = normalizer_class(**_snake_case) UpperCAmelCase_ = do_lower_case def lowerCamelCase ( self : Tuple , _snake_case : str , _snake_case : Optional[Any]=None): """simple docstring""" UpperCAmelCase_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def lowerCamelCase ( self : List[Any] , _snake_case : List[int] , _snake_case : Optional[List[int]] = None): """simple docstring""" UpperCAmelCase_ = [self.sep_token_id] UpperCAmelCase_ = [self.cls_token_id] if token_ids_a is None: return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0] return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def lowerCamelCase ( self : Tuple , _snake_case : str , _snake_case : Optional[str] = None): """simple docstring""" UpperCAmelCase_ = self._tokenizer.model.save(_snake_case , name=_snake_case) return tuple(_snake_case)
51
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
0
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 __lowerCamelCase : Optional[int] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right __lowerCamelCase : Optional[Any] = 25_0004 __lowerCamelCase : Optional[Any] = 25_0020 @require_sentencepiece @require_tokenizers class A__ ( __snake_case , unittest.TestCase ): _UpperCAmelCase :Optional[int] = MBartTokenizer _UpperCAmelCase :int = MBartTokenizerFast _UpperCAmelCase :Dict = True _UpperCAmelCase :Any = True def __UpperCamelCase( self ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase : List[Any] = MBartTokenizer(A_ , keep_accents=A_ ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = MBartTokenizer(A_ , keep_accents=A_ ) UpperCamelCase : str = tokenizer.tokenize("This is a test" ) self.assertListEqual(A_ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A_ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) UpperCamelCase : Optional[Any] = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A_ , [ 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", "é", ".", ] , ) UpperCamelCase : Any = tokenizer.convert_tokens_to_ids(A_ ) self.assertListEqual( A_ , [ 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 ^ ] , ) UpperCamelCase : str = tokenizer.convert_ids_to_tokens(A_ ) self.assertListEqual( A_ , [ 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 __UpperCamelCase( self ): '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return UpperCamelCase : Union[str, Any] = (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})""" ): UpperCamelCase : Any = self.rust_tokenizer_class.from_pretrained(A_ , **A_ ) UpperCamelCase : Any = self.tokenizer_class.from_pretrained(A_ , **A_ ) UpperCamelCase : Union[str, Any] = tempfile.mkdtemp() UpperCamelCase : Any = tokenizer_r.save_pretrained(A_ ) UpperCamelCase : str = tokenizer_p.save_pretrained(A_ ) # 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 ) ) UpperCamelCase : Dict = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(A_ , A_ ) # Checks everything loads correctly in the same way UpperCamelCase : Dict = tokenizer_r.from_pretrained(A_ ) UpperCamelCase : List[str] = tokenizer_p.from_pretrained(A_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A_ , A_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(A_ ) # Save tokenizer rust, legacy_format=True UpperCamelCase : Any = tempfile.mkdtemp() UpperCamelCase : List[str] = tokenizer_r.save_pretrained(A_ , legacy_format=A_ ) UpperCamelCase : Dict = tokenizer_p.save_pretrained(A_ ) # Checks it save with the same files self.assertSequenceEqual(A_ , A_ ) # Checks everything loads correctly in the same way UpperCamelCase : Any = tokenizer_r.from_pretrained(A_ ) UpperCamelCase : Optional[Any] = tokenizer_p.from_pretrained(A_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A_ , A_ ) ) shutil.rmtree(A_ ) # Save tokenizer rust, legacy_format=False UpperCamelCase : str = tempfile.mkdtemp() UpperCamelCase : int = tokenizer_r.save_pretrained(A_ , legacy_format=A_ ) UpperCamelCase : List[Any] = tokenizer_p.save_pretrained(A_ ) # 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 UpperCamelCase : Union[str, Any] = tokenizer_r.from_pretrained(A_ ) UpperCamelCase : Dict = tokenizer_p.from_pretrained(A_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A_ , A_ ) ) shutil.rmtree(A_ ) @require_torch @require_sentencepiece @require_tokenizers class A__ ( unittest.TestCase ): _UpperCAmelCase :Optional[Any] = 'facebook/mbart-large-en-ro' _UpperCAmelCase :Optional[Any] = [ ' 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.', ] _UpperCAmelCase :int = [ 'Ş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.', ] _UpperCAmelCase :Dict = [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 __UpperCamelCase( cls ): '''simple docstring''' UpperCamelCase : MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="en_XX" , tgt_lang="ro_RO" ) UpperCamelCase : Tuple = 1 return cls def __UpperCamelCase( self ): '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ar_AR"] , 25_0001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["en_EN"] , 25_0004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ro_RO"] , 25_0020 ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : List[Any] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A_ ) def __UpperCamelCase( self ): '''simple docstring''' self.assertIn(A_ , self.tokenizer.all_special_ids ) UpperCamelCase : int = [RO_CODE, 884, 9019, 96, 9, 916, 8_6792, 36, 1_8743, 1_5596, 5, 2] UpperCamelCase : Optional[Any] = self.tokenizer.decode(A_ , skip_special_tokens=A_ ) UpperCamelCase : str = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A_ ) self.assertEqual(A_ , A_ ) self.assertNotIn(self.tokenizer.eos_token , A_ ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Optional[int] = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , A_ ) UpperCamelCase : Union[str, Any] = 10 UpperCamelCase : List[Any] = self.tokenizer(A_ , max_length=A_ , truncation=A_ ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , A_ ) self.assertEqual(len(A_ ) , A_ ) def __UpperCamelCase( self ): '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [25_0026, 25_0001] ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Tuple = tempfile.mkdtemp() UpperCamelCase : List[str] = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A_ ) UpperCamelCase : List[Any] = MBartTokenizer.from_pretrained(A_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A_ ) @require_torch def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=A_ , return_tensors="pt" ) UpperCamelCase : int = 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 __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Union[str, Any] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A_ , truncation=A_ , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) UpperCamelCase : List[str] = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) self.assertIsInstance(A_ , A_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) UpperCamelCase : Union[str, Any] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A_ ) 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 __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Tuple = self.tokenizer(self.src_text , padding=A_ , truncation=A_ , max_length=3 , return_tensors="pt" ) UpperCamelCase : str = self.tokenizer( text_target=self.tgt_text , padding=A_ , truncation=A_ , max_length=10 , return_tensors="pt" ) UpperCamelCase : Optional[int] = targets["input_ids"] UpperCamelCase : Any = shift_tokens_right(A_ , 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 __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Optional[Any] = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="en_XX" , tgt_lang="ar_AR" ) self.assertEqual( nested_simplify(A_ ) , { # A, test, EOS, en_XX "input_ids": [[62, 3034, 2, 25_0004]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 25_0001, } , )
52
# 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
0
'''simple docstring''' def lowercase__ ( __lowercase : int = 10**9 ) -> int: """simple docstring""" __UpperCamelCase = 1 __UpperCamelCase = 2 __UpperCamelCase = 0 __UpperCamelCase = 0 __UpperCamelCase = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __UpperCamelCase = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(f'{solution() = }')
53
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
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging a__ : Any = logging.get_logger(__name__) a__ : Union[str, Any] = { '''transfo-xl-wt103''': '''https://huggingface.co/transfo-xl-wt103/resolve/main/config.json''', } class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" snake_case__ : Optional[int] = "transfo-xl" snake_case__ : Optional[Any] = ["mems"] snake_case__ : List[Any] = { "n_token": "vocab_size", "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : List[Any] , UpperCAmelCase__ : Union[str, Any]=2_6_7_7_3_5 , UpperCAmelCase__ : str=[2_0_0_0_0, 4_0_0_0_0, 2_0_0_0_0_0] , UpperCAmelCase__ : int=1_0_2_4 , UpperCAmelCase__ : Optional[int]=1_0_2_4 , UpperCAmelCase__ : Union[str, Any]=1_6 , UpperCAmelCase__ : Union[str, Any]=6_4 , UpperCAmelCase__ : List[Any]=4_0_9_6 , UpperCAmelCase__ : Any=4 , UpperCAmelCase__ : Any=False , UpperCAmelCase__ : List[str]=1_8 , UpperCAmelCase__ : Dict=1_6_0_0 , UpperCAmelCase__ : Optional[Any]=1_0_0_0 , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : Dict=True , UpperCAmelCase__ : Union[str, Any]=0 , UpperCAmelCase__ : str=-1 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Optional[int]=0.1 , UpperCAmelCase__ : Any=0.0 , UpperCAmelCase__ : Any=True , UpperCAmelCase__ : List[Any]="normal" , UpperCAmelCase__ : Any=0.01 , UpperCAmelCase__ : str=0.01 , UpperCAmelCase__ : Any=0.02 , UpperCAmelCase__ : Tuple=1E-5 , UpperCAmelCase__ : Optional[int]=0 , **UpperCAmelCase__ : Optional[Any] , ) -> List[Any]: __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = [] self.cutoffs.extend(UpperCAmelCase__ ) if proj_share_all_but_first: __SCREAMING_SNAKE_CASE = [False] + [True] * len(self.cutoffs ) else: __SCREAMING_SNAKE_CASE = [False] + [False] * len(self.cutoffs ) __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = d_embed __SCREAMING_SNAKE_CASE = d_head __SCREAMING_SNAKE_CASE = d_inner __SCREAMING_SNAKE_CASE = div_val __SCREAMING_SNAKE_CASE = pre_lnorm __SCREAMING_SNAKE_CASE = n_layer __SCREAMING_SNAKE_CASE = n_head __SCREAMING_SNAKE_CASE = mem_len __SCREAMING_SNAKE_CASE = same_length __SCREAMING_SNAKE_CASE = attn_type __SCREAMING_SNAKE_CASE = clamp_len __SCREAMING_SNAKE_CASE = sample_softmax __SCREAMING_SNAKE_CASE = adaptive __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = dropatt __SCREAMING_SNAKE_CASE = untie_r __SCREAMING_SNAKE_CASE = init __SCREAMING_SNAKE_CASE = init_range __SCREAMING_SNAKE_CASE = proj_init_std __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = layer_norm_epsilon super().__init__(eos_token_id=UpperCAmelCase__ , **UpperCAmelCase__ ) @property def UpperCAmelCase_ ( self : Union[str, Any] ) -> str: # Message copied from Transformer-XL documentation logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def UpperCAmelCase_ ( self : str , UpperCAmelCase__ : str ) -> Optional[int]: # Message copied from Transformer-XL documentation raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
54
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
0
'''simple docstring''' import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class snake_case ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase=13 , UpperCamelCase=7 , UpperCamelCase=True , UpperCamelCase=True , UpperCamelCase=True , UpperCamelCase=True , UpperCamelCase=99 , UpperCamelCase=32 , UpperCamelCase=5 , UpperCamelCase=4 , UpperCamelCase=37 , UpperCamelCase="gelu" , UpperCamelCase=0.1 , UpperCamelCase=0.1 , UpperCamelCase=512 , UpperCamelCase=16 , UpperCamelCase=2 , UpperCamelCase=0.02 , UpperCamelCase=4 , ): """simple docstring""" lowerCamelCase_ = parent lowerCamelCase_ = batch_size lowerCamelCase_ = seq_length lowerCamelCase_ = is_training lowerCamelCase_ = use_attention_mask lowerCamelCase_ = use_token_type_ids lowerCamelCase_ = use_labels lowerCamelCase_ = vocab_size lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_act lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = max_position_embeddings lowerCamelCase_ = type_vocab_size lowerCamelCase_ = type_sequence_label_size lowerCamelCase_ = initializer_range lowerCamelCase_ = num_choices def snake_case ( self ): """simple docstring""" lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ = None if self.use_attention_mask: lowerCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ = None if self.use_token_type_ids: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase_ = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCamelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = config_and_inputs lowerCamelCase_ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = config_and_inputs lowerCamelCase_ = True lowerCamelCase_ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class snake_case ( lowercase , unittest.TestCase ): """simple docstring""" _lowerCamelCase = True _lowerCamelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = FlaxRobertaModelTester(self ) @slow def snake_case ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase_ = model_class_name.from_pretrained("roberta-base" , from_pt=UpperCamelCase ) lowerCamelCase_ = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCamelCase )
55
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
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : str = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
56
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
0
"""simple docstring""" # This is the module that test_patching.py uses to test patch_submodule() import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests A : int = open # noqa: we just need to have a builtin inside this module to test it properly
57
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
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class a_ ( unittest.TestCase ): '''simple docstring''' def __init__( self , A , A=7 , A=3 , A=10 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , A=None , ) -> List[Any]: _SCREAMING_SNAKE_CASE = size if size is not None else {"""shortest_edge""": 18} _SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = num_frames _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = min_resolution _SCREAMING_SNAKE_CASE = max_resolution _SCREAMING_SNAKE_CASE = do_resize _SCREAMING_SNAKE_CASE = size _SCREAMING_SNAKE_CASE = do_normalize _SCREAMING_SNAKE_CASE = image_mean _SCREAMING_SNAKE_CASE = image_std _SCREAMING_SNAKE_CASE = crop_size def snake_case_( self ) -> str: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class a_ ( snake_case_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase = VivitImageProcessor if is_vision_available() else None def snake_case_( self ) -> List[str]: _SCREAMING_SNAKE_CASE = VivitImageProcessingTester(self ) @property def snake_case_( self ) -> Any: return self.image_processor_tester.prepare_image_processor_dict() def snake_case_( self ) -> int: _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , """image_mean""" ) ) self.assertTrue(hasattr(A , """image_std""" ) ) self.assertTrue(hasattr(A , """do_normalize""" ) ) self.assertTrue(hasattr(A , """do_resize""" ) ) self.assertTrue(hasattr(A , """do_center_crop""" ) ) self.assertTrue(hasattr(A , """size""" ) ) def snake_case_( self ) -> int: _SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) _SCREAMING_SNAKE_CASE = 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 snake_case_( self ) -> Optional[int]: # Initialize image_processing _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos _SCREAMING_SNAKE_CASE = prepare_video_inputs(self.image_processor_tester , equal_resolution=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processing(A , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def snake_case_( self ) -> List[str]: # Initialize image_processing _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processing(A , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def snake_case_( self ) -> str: # Initialize image_processing _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processing(A , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
58
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
0
import csv import tweepy # Twitter API credentials __lowerCamelCase = """""" __lowerCamelCase = """""" __lowerCamelCase = """""" __lowerCamelCase = """""" def UpperCamelCase ( __lowerCamelCase : str ): # authorize twitter, initialize tweepy snake_case : Optional[Any] = tweepy.OAuthHandler(__lowerCamelCase , __lowerCamelCase ) auth.set_access_token(__lowerCamelCase , __lowerCamelCase ) snake_case : int = tweepy.API(__lowerCamelCase ) # initialize a list to hold all the tweepy Tweets snake_case : Union[str, Any] = [] # make initial request for most recent tweets (200 is the maximum allowed count) snake_case : Dict = api.user_timeline(screen_name=__lowerCamelCase , count=200 ) # save most recent tweets alltweets.extend(__lowerCamelCase ) # save the id of the oldest tweet less one snake_case : Tuple = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(__lowerCamelCase ) > 0: print(f"""getting tweets before {oldest}""" ) # all subsequent requests use the max_id param to prevent duplicates snake_case : List[Any] = api.user_timeline( screen_name=__lowerCamelCase , count=200 , max_id=__lowerCamelCase ) # save most recent tweets alltweets.extend(__lowerCamelCase ) # update the id of the oldest tweet less one snake_case : List[Any] = alltweets[-1].id - 1 print(f"""...{len(__lowerCamelCase )} tweets downloaded so far""" ) # transform the tweepy tweets into a 2D array that will populate the csv snake_case : Optional[Any] = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f"""new_{screen_name}_tweets.csv""" , "w" ) as f: snake_case : str = csv.writer(__lowerCamelCase ) writer.writerow(["id", "created_at", "text"] ) writer.writerows(__lowerCamelCase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("""FirePing32""")
59
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
0
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class snake_case_( unittest.TestCase ): def lowerCamelCase__ ( self : List[Any] ): lowerCAmelCase : Optional[int] = { '''task_specific_params''': { '''summarization''': {'''length_penalty''': 1.0, '''max_length''': 1_2_8, '''min_length''': 1_2, '''num_beams''': 4}, '''summarization_cnn''': {'''length_penalty''': 2.0, '''max_length''': 1_4_2, '''min_length''': 5_6, '''num_beams''': 4}, '''summarization_xsum''': {'''length_penalty''': 1.0, '''max_length''': 6_2, '''min_length''': 1_1, '''num_beams''': 6}, } } lowerCAmelCase : int = { '''task_specific_params.summarization.length_penalty''': 1.0, '''task_specific_params.summarization.max_length''': 1_2_8, '''task_specific_params.summarization.min_length''': 1_2, '''task_specific_params.summarization.num_beams''': 4, '''task_specific_params.summarization_cnn.length_penalty''': 2.0, '''task_specific_params.summarization_cnn.max_length''': 1_4_2, '''task_specific_params.summarization_cnn.min_length''': 5_6, '''task_specific_params.summarization_cnn.num_beams''': 4, '''task_specific_params.summarization_xsum.length_penalty''': 1.0, '''task_specific_params.summarization_xsum.max_length''': 6_2, '''task_specific_params.summarization_xsum.min_length''': 1_1, '''task_specific_params.summarization_xsum.num_beams''': 6, } self.assertEqual(flatten_dict(UpperCamelCase_ ) , UpperCamelCase_ ) def lowerCamelCase__ ( self : Dict ): lowerCAmelCase : Dict = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ ) , x.transpose() ) ) lowerCAmelCase : List[Any] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def lowerCamelCase__ ( self : Optional[int] ): lowerCAmelCase : Optional[int] = np.random.randn(3 , 4 ) lowerCAmelCase : Any = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ ) , transpose(UpperCamelCase_ ).numpy() ) ) lowerCAmelCase : Union[str, Any] = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : Tuple = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ , axes=(1, 2, 0) ) , transpose(UpperCamelCase_ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def lowerCamelCase__ ( self : Tuple ): lowerCAmelCase : Any = np.random.randn(3 , 4 ) lowerCAmelCase : str = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ ) , transpose(UpperCamelCase_ ).numpy() ) ) lowerCAmelCase : Optional[Any] = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : List[Any] = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ , axes=(1, 2, 0) ) , transpose(UpperCamelCase_ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def lowerCamelCase__ ( self : List[str] ): lowerCAmelCase : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase : Union[str, Any] = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ ) , np.asarray(transpose(UpperCamelCase_ ) ) ) ) lowerCAmelCase : Tuple = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : Dict = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(transpose(UpperCamelCase_ , axes=(1, 2, 0) ) , np.asarray(transpose(UpperCamelCase_ , axes=(1, 2, 0) ) ) ) ) def lowerCamelCase__ ( self : str ): lowerCAmelCase : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (4, 3) ) , np.reshape(UpperCamelCase_ , (4, 3) ) ) ) lowerCAmelCase : Union[str, Any] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (1_2, 5) ) , np.reshape(UpperCamelCase_ , (1_2, 5) ) ) ) @require_torch def lowerCamelCase__ ( self : Any ): lowerCAmelCase : Optional[Any] = np.random.randn(3 , 4 ) lowerCAmelCase : Optional[Any] = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (4, 3) ) , reshape(UpperCamelCase_ , (4, 3) ).numpy() ) ) lowerCAmelCase : List[Any] = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : Union[str, Any] = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (1_2, 5) ) , reshape(UpperCamelCase_ , (1_2, 5) ).numpy() ) ) @require_tf def lowerCamelCase__ ( self : Optional[int] ): lowerCAmelCase : List[Any] = np.random.randn(3 , 4 ) lowerCAmelCase : str = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (4, 3) ) , reshape(UpperCamelCase_ , (4, 3) ).numpy() ) ) lowerCAmelCase : int = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : Tuple = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (1_2, 5) ) , reshape(UpperCamelCase_ , (1_2, 5) ).numpy() ) ) @require_flax def lowerCamelCase__ ( self : Optional[Any] ): lowerCAmelCase : Dict = np.random.randn(3 , 4 ) lowerCAmelCase : Union[str, Any] = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (4, 3) ) , np.asarray(reshape(UpperCamelCase_ , (4, 3) ) ) ) ) lowerCAmelCase : List[str] = np.random.randn(3 , 4 , 5 ) lowerCAmelCase : int = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(reshape(UpperCamelCase_ , (1_2, 5) ) , np.asarray(reshape(UpperCamelCase_ , (1_2, 5) ) ) ) ) def lowerCamelCase__ ( self : List[str] ): lowerCAmelCase : List[Any] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ ) , np.squeeze(UpperCamelCase_ ) ) ) lowerCAmelCase : Any = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ , axis=2 ) , np.squeeze(UpperCamelCase_ , axis=2 ) ) ) @require_torch def lowerCamelCase__ ( self : int ): lowerCAmelCase : Any = np.random.randn(1 , 3 , 4 ) lowerCAmelCase : Union[str, Any] = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ ) , squeeze(UpperCamelCase_ ).numpy() ) ) lowerCAmelCase : Dict = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase : Tuple = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ , axis=2 ) , squeeze(UpperCamelCase_ , axis=2 ).numpy() ) ) @require_tf def lowerCamelCase__ ( self : Optional[Any] ): lowerCAmelCase : str = np.random.randn(1 , 3 , 4 ) lowerCAmelCase : List[str] = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ ) , squeeze(UpperCamelCase_ ).numpy() ) ) lowerCAmelCase : Optional[Any] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase : Optional[int] = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ , axis=2 ) , squeeze(UpperCamelCase_ , axis=2 ).numpy() ) ) @require_flax def lowerCamelCase__ ( self : Any ): lowerCAmelCase : int = np.random.randn(1 , 3 , 4 ) lowerCAmelCase : str = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ ) , np.asarray(squeeze(UpperCamelCase_ ) ) ) ) lowerCAmelCase : List[str] = np.random.randn(1 , 4 , 1 , 5 ) lowerCAmelCase : str = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(squeeze(UpperCamelCase_ , axis=2 ) , np.asarray(squeeze(UpperCamelCase_ , axis=2 ) ) ) ) def lowerCamelCase__ ( self : Tuple ): lowerCAmelCase : List[Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(UpperCamelCase_ , axis=1 ) , np.expand_dims(UpperCamelCase_ , axis=1 ) ) ) @require_torch def lowerCamelCase__ ( self : Dict ): lowerCAmelCase : Any = np.random.randn(3 , 4 ) lowerCAmelCase : Any = torch.tensor(UpperCamelCase_ ) self.assertTrue(np.allclose(expand_dims(UpperCamelCase_ , axis=1 ) , expand_dims(UpperCamelCase_ , axis=1 ).numpy() ) ) @require_tf def lowerCamelCase__ ( self : Union[str, Any] ): lowerCAmelCase : Tuple = np.random.randn(3 , 4 ) lowerCAmelCase : Optional[int] = tf.constant(UpperCamelCase_ ) self.assertTrue(np.allclose(expand_dims(UpperCamelCase_ , axis=1 ) , expand_dims(UpperCamelCase_ , axis=1 ).numpy() ) ) @require_flax def lowerCamelCase__ ( self : Optional[int] ): lowerCAmelCase : Dict = np.random.randn(3 , 4 ) lowerCAmelCase : Tuple = jnp.array(UpperCamelCase_ ) self.assertTrue(np.allclose(expand_dims(UpperCamelCase_ , axis=1 ) , np.asarray(expand_dims(UpperCamelCase_ , axis=1 ) ) ) )
60
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
0
"""simple docstring""" import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class A_ : '''simple docstring''' def __init__( self , lowercase_ , lowercase_=2 , lowercase_=8 , lowercase_=True , lowercase_=True , lowercase_=True , lowercase_=True , lowercase_=99 , lowercase_=16 , lowercase_=5 , lowercase_=2 , lowercase_=36 , lowercase_="gelu" , lowercase_=0.0 , lowercase_=0.0 , lowercase_=512 , lowercase_=16 , lowercase_=2 , lowercase_=0.02 , lowercase_=3 , lowercase_=4 , lowercase_=None , ): """simple docstring""" UpperCAmelCase_ : int = parent UpperCAmelCase_ : Optional[Any] = batch_size UpperCAmelCase_ : Any = seq_length UpperCAmelCase_ : Optional[int] = is_training UpperCAmelCase_ : int = use_input_mask UpperCAmelCase_ : Optional[Any] = use_token_type_ids UpperCAmelCase_ : Dict = use_labels UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : Union[str, Any] = hidden_size UpperCAmelCase_ : List[Any] = num_hidden_layers UpperCAmelCase_ : Optional[int] = num_attention_heads UpperCAmelCase_ : Dict = intermediate_size UpperCAmelCase_ : List[Any] = hidden_act UpperCAmelCase_ : int = hidden_dropout_prob UpperCAmelCase_ : List[str] = attention_probs_dropout_prob UpperCAmelCase_ : Any = max_position_embeddings UpperCAmelCase_ : int = type_vocab_size UpperCAmelCase_ : int = type_sequence_label_size UpperCAmelCase_ : Tuple = initializer_range UpperCAmelCase_ : Tuple = num_labels UpperCAmelCase_ : List[str] = num_choices UpperCAmelCase_ : Any = scope def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase_ : Tuple = None if self.use_input_mask: UpperCAmelCase_ : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_ : Any = None if self.use_token_type_ids: UpperCAmelCase_ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase_ : Optional[Any] = None UpperCAmelCase_ : Optional[int] = None UpperCAmelCase_ : Optional[Any] = None if self.use_labels: UpperCAmelCase_ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase_ : str = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase_ : int = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ ( self ): """simple docstring""" return MraConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase_ , initializer_range=self.initializer_range , ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = self.get_config() UpperCAmelCase_ : List[Any] = 300 return config def UpperCamelCase__ ( self ): """simple docstring""" ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ) : Union[str, Any] = self.prepare_config_and_inputs() UpperCAmelCase_ : str = True UpperCAmelCase_ : Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase_ : Dict = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = MraModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : Tuple = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ ) UpperCAmelCase_ : Optional[int] = model(lowercase_ , token_type_ids=lowercase_ ) UpperCAmelCase_ : Tuple = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , ): """simple docstring""" UpperCAmelCase_ : Any = True UpperCAmelCase_ : Union[str, Any] = MraModel(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : List[str] = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , ) UpperCAmelCase_ : Union[str, Any] = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , encoder_hidden_states=lowercase_ , ) UpperCAmelCase_ : Dict = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = MraForMaskedLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : List[str] = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Dict = MraForQuestionAnswering(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : int = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , start_positions=lowercase_ , end_positions=lowercase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Dict = self.num_labels UpperCAmelCase_ : Dict = MraForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : List[Any] = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self.num_labels UpperCAmelCase_ : int = MraForTokenClassification(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : List[Any] = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Dict = self.num_choices UpperCAmelCase_ : List[Any] = MraForMultipleChoice(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : Optional[int] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase_ : Union[str, Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase_ : Optional[int] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase_ : int = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = self.prepare_config_and_inputs() ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ) : Optional[Any] = config_and_inputs UpperCAmelCase_ : List[str] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ : Optional[int] = False SCREAMING_SNAKE_CASE__ : Optional[int] = False SCREAMING_SNAKE_CASE__ : List[str] = False SCREAMING_SNAKE_CASE__ : Tuple = False SCREAMING_SNAKE_CASE__ : Dict = () def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = MraModelTester(self ) UpperCAmelCase_ : Tuple = ConfigTester(self , config_class=lowercase_ , hidden_size=37 ) def UpperCamelCase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase_ : Tuple = type self.model_tester.create_and_check_model(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase_ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Optional[int] = MraModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @unittest.skip(reason="MRA does not output attentions" ) def UpperCamelCase__ ( self ): """simple docstring""" return @require_torch class A_ (unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = MraModel.from_pretrained("uw-madison/mra-base-512-4" ) UpperCAmelCase_ : List[str] = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(lowercase_ )[0] UpperCAmelCase_ : str = torch.Size((1, 256, 768) ) self.assertEqual(output.shape , lowercase_ ) UpperCAmelCase_ : Optional[Any] = torch.tensor( [[[-0.01_40, 0.08_30, -0.03_81], [0.15_46, 0.14_02, 0.02_20], [0.11_62, 0.08_51, 0.01_65]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1E-4 ) ) @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = MraForMaskedLM.from_pretrained("uw-madison/mra-base-512-4" ) UpperCAmelCase_ : Optional[int] = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase_ : Dict = model(lowercase_ )[0] UpperCAmelCase_ : Dict = 5_0265 UpperCAmelCase_ : str = torch.Size((1, 256, vocab_size) ) self.assertEqual(output.shape , lowercase_ ) UpperCAmelCase_ : Optional[int] = torch.tensor( [[[9.25_95, -3.60_38, 11.88_19], [9.38_69, -3.26_93, 11.09_56], [11.85_24, -3.49_38, 13.12_10]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1E-4 ) ) @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = MraForMaskedLM.from_pretrained("uw-madison/mra-base-4096-8-d3" ) UpperCAmelCase_ : List[Any] = torch.arange(4096 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase_ : str = model(lowercase_ )[0] UpperCAmelCase_ : List[str] = 5_0265 UpperCAmelCase_ : Tuple = torch.Size((1, 4096, vocab_size) ) self.assertEqual(output.shape , lowercase_ ) UpperCAmelCase_ : Optional[int] = torch.tensor( [[[5.47_89, -2.35_64, 7.50_64], [7.90_67, -1.33_69, 9.96_68], [9.07_12, -1.81_06, 7.03_80]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1E-4 ) )
61
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
0
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase__ ( A_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : int = KandinskyImgaImgPipeline UpperCAmelCase__ : List[Any] = ["prompt", "image_embeds", "negative_image_embeds", "image"] UpperCAmelCase__ : str = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] UpperCAmelCase__ : Optional[int] = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Optional[Any] = False @property def _a ( self ) -> Optional[Any]: return 32 @property def _a ( self ) -> Dict: return 32 @property def _a ( self ) -> List[str]: return self.time_input_dim @property def _a ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def _a ( self ) -> List[str]: return 100 @property def _a ( self ) -> str: __UpperCamelCase =XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def _a ( self ) -> int: torch.manual_seed(0 ) __UpperCamelCase =MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) __UpperCamelCase =MultilingualCLIP(A_ ) __UpperCamelCase =text_encoder.eval() return text_encoder @property def _a ( self ) -> int: torch.manual_seed(0 ) __UpperCamelCase ={ 'in_channels': 4, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } __UpperCamelCase =UNetaDConditionModel(**A_ ) return model @property def _a ( self ) -> Any: return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _a ( self ) -> int: torch.manual_seed(0 ) __UpperCamelCase =VQModel(**self.dummy_movq_kwargs ) return model def _a ( self ) -> Any: __UpperCamelCase =self.dummy_text_encoder __UpperCamelCase =self.dummy_tokenizer __UpperCamelCase =self.dummy_unet __UpperCamelCase =self.dummy_movq __UpperCamelCase ={ 'num_train_timesteps': 1000, 'beta_schedule': 'linear', 'beta_start': 0.0_0085, 'beta_end': 0.012, 'clip_sample': False, 'set_alpha_to_one': False, 'steps_offset': 0, 'prediction_type': 'epsilon', 'thresholding': False, } __UpperCamelCase =DDIMScheduler(**A_ ) __UpperCamelCase ={ 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def _a ( self , A_ , A_=0 ) -> int: __UpperCamelCase =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(A_ ) ).to(A_ ) __UpperCamelCase =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(A_ ) # create init_image __UpperCamelCase =floats_tensor((1, 3, 64, 64) , rng=random.Random(A_ ) ).to(A_ ) __UpperCamelCase =image.cpu().permute(0 , 2 , 3 , 1 )[0] __UpperCamelCase =Image.fromarray(np.uinta(A_ ) ).convert('RGB' ).resize((256, 256) ) if str(A_ ).startswith('mps' ): __UpperCamelCase =torch.manual_seed(A_ ) else: __UpperCamelCase =torch.Generator(device=A_ ).manual_seed(A_ ) __UpperCamelCase ={ 'prompt': 'horse', 'image': init_image, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 10, 'guidance_scale': 7.0, 'strength': 0.2, 'output_type': 'np', } return inputs def _a ( self ) -> Dict: __UpperCamelCase ='cpu' __UpperCamelCase =self.get_dummy_components() __UpperCamelCase =self.pipeline_class(**A_ ) __UpperCamelCase =pipe.to(A_ ) pipe.set_progress_bar_config(disable=A_ ) __UpperCamelCase =pipe(**self.get_dummy_inputs(A_ ) ) __UpperCamelCase =output.images __UpperCamelCase =pipe( **self.get_dummy_inputs(A_ ) , return_dict=A_ , )[0] __UpperCamelCase =image[0, -3:, -3:, -1] __UpperCamelCase =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __UpperCamelCase =np.array( [0.6147_4943, 0.607_3539, 0.4330_8544, 0.592_8269, 0.4749_3595, 0.4675_5973, 0.461_3838, 0.4536_8797, 0.5011_9233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def _a ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _a ( self ) -> List[str]: __UpperCamelCase =load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_img2img_frog.npy' ) __UpperCamelCase =load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) __UpperCamelCase ='A red cartoon frog, 4k' __UpperCamelCase =KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' , torch_dtype=torch.floataa ) pipe_prior.to(A_ ) __UpperCamelCase =KandinskyImgaImgPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1' , torch_dtype=torch.floataa ) __UpperCamelCase =pipeline.to(A_ ) pipeline.set_progress_bar_config(disable=A_ ) __UpperCamelCase =torch.Generator(device='cpu' ).manual_seed(0 ) __UpperCamelCase , __UpperCamelCase =pipe_prior( A_ , generator=A_ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() __UpperCamelCase =pipeline( A_ , image=A_ , image_embeds=A_ , negative_image_embeds=A_ , generator=A_ , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='np' , ) __UpperCamelCase =output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(A_ , A_ )
62
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
0
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def _lowerCamelCase ( lowercase : List[str] ) -> Optional[Any]: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0X4E_00 and cp <= 0X9F_FF) or (cp >= 0X34_00 and cp <= 0X4D_BF) # or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF) # or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F) # or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F) # or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF) # or (cp >= 0XF9_00 and cp <= 0XFA_FF) or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F) # ): # return True return False def _lowerCamelCase ( lowercase : str ) -> Tuple: # word like '180' or '身高' or '神' for char in word: _a = ord(lowercase ) if not _is_chinese_char(lowercase ): return 0 return 1 def _lowerCamelCase ( lowercase : List[str] ) -> List[Any]: _a = set() for token in tokens: _a = len(lowercase ) > 1 and is_chinese(lowercase ) if chinese_word: word_set.add(lowercase ) _a = list(lowercase ) return word_list def _lowerCamelCase ( lowercase : List[str] , lowercase : set() ) -> Dict: if not chinese_word_set: return bert_tokens _a = max([len(lowercase ) for w in chinese_word_set] ) _a = bert_tokens _a , _a = 0, len(lowercase ) while start < end: _a = True if is_chinese(bert_word[start] ): _a = min(end - start , lowercase ) for i in range(lowercase , 1 , -1 ): _a = "".join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): _a = "##" + bert_word[j] _a = start + i _a = False break if single_word: start += 1 return bert_word def _lowerCamelCase ( lowercase : List[str] , lowercase : LTP , lowercase : BertTokenizer ) -> int: _a = [] for i in range(0 , len(lowercase ) , 100 ): _a = ltp_tokenizer.pipeline(lines[i : i + 100] , tasks=["cws"] ).cws _a = [get_chinese_word(lowercase ) for r in res] ltp_res.extend(lowercase ) assert len(lowercase ) == len(lowercase ) _a = [] for i in range(0 , len(lowercase ) , 100 ): _a = bert_tokenizer(lines[i : i + 100] , add_special_tokens=lowercase , truncation=lowercase , max_length=512 ) bert_res.extend(res["input_ids"] ) assert len(lowercase ) == len(lowercase ) _a = [] for input_ids, chinese_word in zip(lowercase , lowercase ): _a = [] for id in input_ids: _a = bert_tokenizer._convert_id_to_token(lowercase ) input_tokens.append(lowercase ) _a = add_sub_symbol(lowercase , lowercase ) _a = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(lowercase ): if token[:2] == "##": _a = token[2:] # save chinese tokens' pos if len(lowercase ) == 1 and _is_chinese_char(ord(lowercase ) ): ref_id.append(lowercase ) ref_ids.append(lowercase ) assert len(lowercase ) == len(lowercase ) return ref_ids def _lowerCamelCase ( lowercase : str ) -> Tuple: # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm) # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp) with open(args.file_name , "r" , encoding="utf-8" ) as f: _a = f.readlines() _a = [line.strip() for line in data if len(lowercase ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' _a = LTP(args.ltp ) # faster in GPU device _a = BertTokenizer.from_pretrained(args.bert ) _a = prepare_ref(lowercase , lowercase , lowercase ) with open(args.save_path , "w" , encoding="utf-8" ) as f: _a = [json.dumps(lowercase ) + "\n" for ref in ref_ids] f.writelines(lowercase ) if __name__ == "__main__": lowerCAmelCase_ : Tuple = argparse.ArgumentParser(description='prepare_chinese_ref') parser.add_argument( '--file_name', required=False, type=str, default='./resources/chinese-demo.txt', help='file need process, same as training data in lm', ) parser.add_argument( '--ltp', required=False, type=str, default='./resources/ltp', help='resources for LTP tokenizer, usually a path', ) parser.add_argument( '--bert', required=False, type=str, default='./resources/robert', help='resources for Bert tokenizer', ) parser.add_argument( '--save_path', required=False, type=str, default='./resources/ref.txt', help='path to save res', ) lowerCAmelCase_ : Tuple = parser.parse_args() main(args)
63
__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
0
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: A_ = None A_ = logging.get_logger(__name__) A_ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A_ = { '''vocab_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/spiece.model''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/spiece.model''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/spiece.model''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/spiece.model''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/tokenizer.json''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/tokenizer.json''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/tokenizer.json''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/tokenizer.json''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/tokenizer.json''', }, } # TODO(PVP) - this should be removed in Transformers v5 A_ = { '''t5-small''': 5_12, '''t5-base''': 5_12, '''t5-large''': 5_12, '''t5-3b''': 5_12, '''t5-11b''': 5_12, } class lowercase( __a ): '''simple docstring''' lowercase__ = VOCAB_FILES_NAMES lowercase__ = PRETRAINED_VOCAB_FILES_MAP lowercase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ = ["input_ids", "attention_mask"] lowercase__ = TaTokenizer lowercase__ = [] def __init__( self: List[Any], a_: Dict=None, a_: str=None, a_: Optional[Any]="</s>", a_: Optional[Any]="<unk>", a_: Any="<pad>", a_: Optional[int]=100, a_: Optional[Any]=None, **a_: Dict, ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: _snake_case : int = [f"<extra_id_{i}>" for i in range(a_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens _snake_case : Optional[Any] = len(set(filter(lambda a_ : bool("""extra_id_""" in str(a_ ) ), a_ ) ) ) if extra_tokens != extra_ids: raise ValueError( f"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids""" """ tokens""" ) super().__init__( a_, tokenizer_file=a_, eos_token=a_, unk_token=a_, pad_token=a_, extra_ids=a_, additional_special_tokens=a_, **a_, ) _snake_case : str = vocab_file _snake_case : Dict = False if not self.vocab_file else True _snake_case : Dict = extra_ids @staticmethod def UpperCamelCase_ ( a_: Union[str, Any], a_: List[Any], a_: Optional[int] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: _snake_case : Union[str, Any] = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( """This tokenizer was incorrectly instantiated with a model max length of""" f" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this" """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with""" """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on""" f" {pretrained_model_name_or_path} automatically truncating your input to" f" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences" f" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with" """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please""" """ instantiate this tokenizer with `model_max_length` set to your preferred value.""", a_, ) return max_model_length def UpperCamelCase_ ( self: Dict, a_: str, a_: Optional[str] = None ): '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(a_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _snake_case : Optional[Any] = os.path.join( a_, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a_ ): copyfile(self.vocab_file, a_ ) logger.info(f"Copy vocab file to {out_vocab_file}" ) return (out_vocab_file,) def UpperCamelCase_ ( self: int, a_: List[int], a_: Optional[List[int]] = None ): '''simple docstring''' _snake_case : str = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: _snake_case : str = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def UpperCamelCase_ ( self: Tuple, a_: List[int], a_: Optional[List[int]] = None ): '''simple docstring''' _snake_case : List[Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def UpperCamelCase_ ( self: int ): '''simple docstring''' return list( set(filter(lambda a_ : bool(re.search(r"""<extra_id_\d+>""", a_ ) ) is not None, self.additional_special_tokens ) ) ) def UpperCamelCase_ ( self: Union[str, Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(a_ ) for token in self.get_sentinel_tokens()]
64
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
0
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 : @property def lowercase_ (self : Tuple ) -> str: """simple docstring""" return self.get_dummy_input() @property def lowercase_ (self : str ) -> Tuple: """simple docstring""" if self.block_type == "down": return (4, 3_2, 1_6, 1_6) elif self.block_type == "mid": return (4, 3_2, 3_2, 3_2) elif self.block_type == "up": return (4, 3_2, 6_4, 6_4) raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""" ) def lowercase_ (self : Dict , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : List[Any]=False , __UpperCAmelCase : str=False , __UpperCAmelCase : Optional[Any]=False , ) -> Any: """simple docstring""" UpperCAmelCase__ = 4 UpperCAmelCase__ = 3_2 UpperCAmelCase__ = (3_2, 3_2) UpperCAmelCase__ = torch.manual_seed(0 ) UpperCAmelCase__ = torch.device(__UpperCAmelCase ) UpperCAmelCase__ = (batch_size, num_channels) + sizes UpperCAmelCase__ = randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=__UpperCAmelCase ) UpperCAmelCase__ = {"hidden_states": hidden_states} if include_temb: UpperCAmelCase__ = 1_2_8 UpperCAmelCase__ = randn_tensor((batch_size, temb_channels) , generator=__UpperCAmelCase , device=__UpperCAmelCase ) if include_res_hidden_states_tuple: UpperCAmelCase__ = torch.manual_seed(1 ) UpperCAmelCase__ = (randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=__UpperCAmelCase ),) if include_encoder_hidden_states: UpperCAmelCase__ = floats_tensor((batch_size, 3_2, 3_2) ).to(__UpperCAmelCase ) if include_skip_sample: UpperCAmelCase__ = randn_tensor(((batch_size, 3) + sizes) , generator=__UpperCAmelCase , device=__UpperCAmelCase ) return dummy_input def lowercase_ (self : Optional[int] ) -> int: """simple docstring""" UpperCAmelCase__ = { "in_channels": 3_2, "out_channels": 3_2, "temb_channels": 1_2_8, } if self.block_type == "up": UpperCAmelCase__ = 3_2 if self.block_type == "mid": init_dict.pop("out_channels" ) UpperCAmelCase__ = self.dummy_input return init_dict, inputs_dict def lowercase_ (self : List[str] , __UpperCAmelCase : Any ) -> str: """simple docstring""" UpperCAmelCase__ , UpperCAmelCase__ = self.prepare_init_args_and_inputs_for_common() UpperCAmelCase__ = self.block_class(**__UpperCAmelCase ) unet_block.to(__UpperCAmelCase ) unet_block.eval() with torch.no_grad(): UpperCAmelCase__ = unet_block(**__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase__ = output[0] self.assertEqual(output.shape , self.output_shape ) UpperCAmelCase__ = output[0, -1, -3:, -3:] UpperCAmelCase__ = 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 lowercase_ (self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ , UpperCAmelCase__ = self.prepare_init_args_and_inputs_for_common() UpperCAmelCase__ = self.block_class(**__UpperCAmelCase ) model.to(__UpperCAmelCase ) model.train() UpperCAmelCase__ = model(**__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase__ = output[0] UpperCAmelCase__ = torch.device(__UpperCAmelCase ) UpperCAmelCase__ = randn_tensor(output.shape , device=__UpperCAmelCase ) UpperCAmelCase__ = torch.nn.functional.mse_loss(__UpperCAmelCase , __UpperCAmelCase ) loss.backward()
65
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
0
"""simple docstring""" from math import factorial def A_ ( _lowercase, _lowercase ): '''simple docstring''' if n < k or k < 0: raise ValueError("""Please enter positive integers for n and k where n >= k""" ) return factorial(_lowercase ) // (factorial(_lowercase ) * factorial(n - k )) if __name__ == "__main__": print( "The number of five-card hands possible from a standard", F"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( "If a class of 40 students must be arranged into groups of", F"""4 for group projects, there are {combinations(40, 4)} ways""", "to arrange them.\n", ) print( "If 10 teams are competing in a Formula One race, there", F"""are {combinations(10, 3)} ways that first, second and""", "third place can be awarded.", )
66
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
0
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class a__ : def __init__( self : List[str] , a : Any , a : Union[str, Any]=12 , a : List[Any]=7 , a : str=True , a : str=True , a : Dict=True , a : Union[str, Any]=99 , a : Optional[Any]=32 , a : int=32 , a : int=2 , a : Optional[int]=4 , a : Dict=37 , a : Optional[Any]=0.1 , a : Dict=0.1 , a : Optional[int]=5_12 , a : List[Any]=0.02 , a : Union[str, Any]=0 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_input_mask __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = projection_dim __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = dropout __lowerCamelCase = attention_dropout __lowerCamelCase = max_position_embeddings __lowerCamelCase = initializer_range __lowerCamelCase = scope __lowerCamelCase = bos_token_id def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_input_mask: __lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: __lowerCamelCase = input_mask.numpy() __lowerCamelCase , __lowerCamelCase = input_mask.shape __lowerCamelCase = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(a ): __lowerCamelCase = 1 __lowerCamelCase = 0 __lowerCamelCase = self.get_config() return config, input_ids, tf.convert_to_tensor(a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : int , a : Tuple , a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = TFBlipTextModel(config=a ) __lowerCamelCase = model(a , attention_mask=a , training=a ) __lowerCamelCase = model(a , training=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class a__ ( UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[Any] =(TFBlipTextModel,) if is_tf_available() else () lowerCamelCase : Any =False lowerCamelCase : List[str] =False lowerCamelCase : int =False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = BlipTextModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" pass @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = TFBlipTextModel.from_pretrained(a ) self.assertIsNotNone(a ) def SCREAMING_SNAKE_CASE__ ( self : int , a : int=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=a )
67
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
0
from collections import deque from math import floor from random import random from time import time class a__ : """simple docstring""" def __init__( self ) -> Dict: '''simple docstring''' A__ = {} def UpperCamelCase ( self , lowercase , lowercase , lowercase=1 ) -> Tuple: '''simple docstring''' if self.graph.get(lowercase ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: A__ = [[w, v]] if not self.graph.get(lowercase ): A__ = [] def UpperCamelCase ( self ) -> Optional[Any]: '''simple docstring''' return list(self.graph ) def UpperCamelCase ( self , lowercase , lowercase ) -> int: '''simple docstring''' if self.graph.get(lowercase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowercase ) def UpperCamelCase ( self , lowercase=-2 , lowercase=-1 ) -> Any: '''simple docstring''' if s == d: return [] A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return visited def UpperCamelCase ( self , lowercase=-1 ) -> Optional[Any]: '''simple docstring''' if c == -1: A__ = floor(random() * 10000 ) + 10 for i in range(lowercase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): A__ = floor(random() * c ) + 1 if n != i: self.add_pair(lowercase , lowercase , 1 ) def UpperCamelCase ( self , lowercase=-2 ) -> Any: '''simple docstring''' A__ = deque() A__ = [] if s == -2: A__ = list(self.graph )[0] d.append(lowercase ) visited.append(lowercase ) 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 UpperCamelCase ( self , lowercase ) -> Tuple: '''simple docstring''' A__ = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def UpperCamelCase ( self , lowercase ) -> int: '''simple docstring''' return len(self.graph[u] ) def UpperCamelCase ( self , lowercase=-2 ) -> str: '''simple docstring''' A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return sorted_nodes def UpperCamelCase ( self ) -> List[Any]: '''simple docstring''' A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) - 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = False indirect_parents.append(lowercase ) A__ = s A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return list(lowercase ) def UpperCamelCase ( self ) -> List[str]: '''simple docstring''' A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) - 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = False indirect_parents.append(lowercase ) A__ = s A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return False def UpperCamelCase ( self , lowercase=-2 , lowercase=-1 ) -> Any: '''simple docstring''' A__ = time() self.dfs(lowercase , lowercase ) A__ = time() return end - begin def UpperCamelCase ( self , lowercase=-2 ) -> int: '''simple docstring''' A__ = time() self.bfs(lowercase ) A__ = time() return end - begin class a__ : """simple docstring""" def __init__( self ) -> int: '''simple docstring''' A__ = {} def UpperCamelCase ( self , lowercase , lowercase , lowercase=1 ) -> Union[str, Any]: '''simple docstring''' if self.graph.get(lowercase ): # 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(lowercase ): # 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 UpperCamelCase ( self , lowercase , lowercase ) -> Union[str, Any]: '''simple docstring''' if self.graph.get(lowercase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowercase ) # the other way round if self.graph.get(lowercase ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(lowercase ) def UpperCamelCase ( self , lowercase=-2 , lowercase=-1 ) -> List[str]: '''simple docstring''' if s == d: return [] A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return visited def UpperCamelCase ( self , lowercase=-1 ) -> str: '''simple docstring''' if c == -1: A__ = floor(random() * 10000 ) + 10 for i in range(lowercase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): A__ = floor(random() * c ) + 1 if n != i: self.add_pair(lowercase , lowercase , 1 ) def UpperCamelCase ( self , lowercase=-2 ) -> Dict: '''simple docstring''' A__ = deque() A__ = [] if s == -2: A__ = list(self.graph )[0] d.append(lowercase ) visited.append(lowercase ) 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 UpperCamelCase ( self , lowercase ) -> Tuple: '''simple docstring''' return len(self.graph[u] ) def UpperCamelCase ( self ) -> Dict: '''simple docstring''' A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) - 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = False indirect_parents.append(lowercase ) A__ = s A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return list(lowercase ) def UpperCamelCase ( self ) -> int: '''simple docstring''' A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(lowercase ) visited.append(lowercase ) 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(lowercase ) - 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(lowercase ) != 0: A__ = stack[len(lowercase ) - 1] else: A__ = False indirect_parents.append(lowercase ) A__ = s A__ = ss # check if se have reached the starting point if len(lowercase ) == 0: return False def UpperCamelCase ( self ) -> List[str]: '''simple docstring''' return list(self.graph ) def UpperCamelCase ( self , lowercase=-2 , lowercase=-1 ) -> Optional[Any]: '''simple docstring''' A__ = time() self.dfs(lowercase , lowercase ) A__ = time() return end - begin def UpperCamelCase ( self , lowercase=-2 ) -> List[Any]: '''simple docstring''' A__ = time() self.bfs(lowercase ) A__ = time() return end - begin
68
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
0
"""simple docstring""" def UpperCAmelCase ( UpperCAmelCase ) -> int: snake_case_ = [[0 for _ in range(UpperCAmelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): snake_case_ = 1 for n in range(m + 1 ): for k in range(1 , UpperCAmelCase ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: __UpperCamelCase = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: __UpperCamelCase = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
69
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
0
'''simple docstring''' import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class UpperCAmelCase : def __init__( self : Optional[int] , __snake_case : int , __snake_case : Optional[Any]=2 , __snake_case : int=True , __snake_case : str=False , __snake_case : List[str]=10 , __snake_case : Union[str, Any]=3 , __snake_case : List[Any]=32 * 4 , __snake_case : str=32 * 6 , __snake_case : int=4 , __snake_case : str=32 , ) -> str: _lowerCAmelCase = parent _lowerCAmelCase = batch_size _lowerCAmelCase = is_training _lowerCAmelCase = use_auxiliary_loss _lowerCAmelCase = num_queries _lowerCAmelCase = num_channels _lowerCAmelCase = min_size _lowerCAmelCase = max_size _lowerCAmelCase = num_labels _lowerCAmelCase = mask_feature_size def lowercase__ ( self : Optional[int] ) -> Optional[int]: _lowerCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( __snake_case ) _lowerCAmelCase = torch.ones([self.batch_size, self.min_size, self.max_size] , device=__snake_case ) _lowerCAmelCase = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=__snake_case ) > 0.5 ).float() _lowerCAmelCase = (torch.rand((self.batch_size, self.num_labels) , device=__snake_case ) > 0.5).long() _lowerCAmelCase = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowercase__ ( self : Any ) -> Union[str, Any]: return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=1_28 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def lowercase__ ( self : Tuple ) -> Tuple: _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = self.prepare_config_and_inputs() _lowerCAmelCase = {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask} return config, inputs_dict def lowercase__ ( self : List[Any] , __snake_case : str , __snake_case : Optional[int] ) -> List[Any]: _lowerCAmelCase = output.encoder_hidden_states _lowerCAmelCase = output.pixel_decoder_hidden_states _lowerCAmelCase = output.transformer_decoder_hidden_states self.parent.assertTrue(len(__snake_case ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__snake_case ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__snake_case ) , config.decoder_config.decoder_layers ) def lowercase__ ( self : str , __snake_case : Any , __snake_case : Union[str, Any] , __snake_case : Optional[int] , __snake_case : Dict=False ) -> Dict: with torch.no_grad(): _lowerCAmelCase = MaskFormerModel(config=__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = model(pixel_values=__snake_case , pixel_mask=__snake_case ) _lowerCAmelCase = model(__snake_case , output_hidden_states=__snake_case ) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(__snake_case , __snake_case ) def lowercase__ ( self : Union[str, Any] , __snake_case : int , __snake_case : int , __snake_case : Dict , __snake_case : Dict , __snake_case : str ) -> str: _lowerCAmelCase = MaskFormerForInstanceSegmentation(config=__snake_case ) model.to(__snake_case ) model.eval() def comm_check_on_output(__snake_case : List[str] ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): _lowerCAmelCase = model(pixel_values=__snake_case , pixel_mask=__snake_case ) _lowerCAmelCase = model(__snake_case ) comm_check_on_output(__snake_case ) _lowerCAmelCase = model( pixel_values=__snake_case , pixel_mask=__snake_case , mask_labels=__snake_case , class_labels=__snake_case ) comm_check_on_output(__snake_case ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class UpperCAmelCase ( snake_case_ , snake_case_ , unittest.TestCase ): _lowercase: Any = (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () _lowercase: int = ( {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) _lowercase: Optional[int] = False _lowercase: Union[str, Any] = False _lowercase: Dict = False _lowercase: Union[str, Any] = False def lowercase__ ( self : Tuple ) -> List[Any]: _lowerCAmelCase = MaskFormerModelTester(self ) _lowerCAmelCase = ConfigTester(self , config_class=__snake_case , has_text_modality=__snake_case ) def lowercase__ ( self : Dict ) -> Dict: self.config_tester.run_common_tests() def lowercase__ ( self : Optional[Any] ) -> str: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(__snake_case , **__snake_case , output_hidden_states=__snake_case ) def lowercase__ ( self : str ) -> Optional[int]: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__snake_case ) @unittest.skip(reason="""MaskFormer does not use inputs_embeds""" ) def lowercase__ ( self : List[Any] ) -> List[str]: pass @unittest.skip(reason="""MaskFormer does not have a get_input_embeddings method""" ) def lowercase__ ( self : Optional[Any] ) -> Optional[int]: pass @unittest.skip(reason="""MaskFormer is not a generative model""" ) def lowercase__ ( self : Union[str, Any] ) -> List[str]: pass @unittest.skip(reason="""MaskFormer does not use token embeddings""" ) def lowercase__ ( self : List[str] ) -> Any: pass @require_torch_multi_gpu @unittest.skip( reason="""MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def lowercase__ ( self : Optional[int] ) -> Optional[int]: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def lowercase__ ( self : Optional[Any] ) -> Dict: pass def lowercase__ ( self : Tuple ) -> Union[str, Any]: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = model_class(__snake_case ) _lowerCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase = [*signature.parameters.keys()] _lowerCAmelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __snake_case ) @slow def lowercase__ ( self : Optional[Any] ) -> str: for model_name in ["facebook/maskformer-swin-small-coco"]: _lowerCAmelCase = MaskFormerModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) def lowercase__ ( self : str ) -> int: _lowerCAmelCase = (self.model_tester.min_size,) * 2 _lowerCAmelCase = { """pixel_values""": torch.randn((2, 3, *size) , device=__snake_case ), """mask_labels""": torch.randn((2, 10, *size) , device=__snake_case ), """class_labels""": torch.zeros(2 , 10 , device=__snake_case ).long(), } _lowerCAmelCase = MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(__snake_case ) _lowerCAmelCase = model(**__snake_case ) self.assertTrue(outputs.loss is not None ) def lowercase__ ( self : Optional[Any] ) -> List[Any]: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(__snake_case , **__snake_case , output_hidden_states=__snake_case ) def lowercase__ ( self : str ) -> Optional[int]: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = model_class(__snake_case ).to(__snake_case ) _lowerCAmelCase = model(**__snake_case , output_attentions=__snake_case ) self.assertTrue(outputs.attentions is not None ) def lowercase__ ( self : Tuple ) -> str: if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss _lowerCAmelCase = self.all_model_classes[1] _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.train() _lowerCAmelCase = model(__snake_case , mask_labels=__snake_case , class_labels=__snake_case ).loss loss.backward() def lowercase__ ( self : Dict ) -> int: # only MaskFormerForInstanceSegmentation has the loss _lowerCAmelCase = self.all_model_classes[1] _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() _lowerCAmelCase = True _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.train() _lowerCAmelCase = model(__snake_case , mask_labels=__snake_case , class_labels=__snake_case ) _lowerCAmelCase = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() _lowerCAmelCase = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't _lowerCAmelCase = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() _lowerCAmelCase = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=__snake_case ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) A__ : int =1e-4 def UpperCamelCase__ ( ): """simple docstring""" _lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_vision @slow class UpperCAmelCase ( unittest.TestCase ): @cached_property def lowercase__ ( self : Dict ) -> Dict: return ( MaskFormerImageProcessor.from_pretrained("""facebook/maskformer-swin-small-coco""" ) if is_vision_available() else None ) def lowercase__ ( self : str ) -> Union[str, Any]: _lowerCAmelCase = MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco""" ).to(__snake_case ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(__snake_case , return_tensors="""pt""" ).to(__snake_case ) _lowerCAmelCase = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(__snake_case , (1, 3, 8_00, 10_88) ) with torch.no_grad(): _lowerCAmelCase = model(**__snake_case ) _lowerCAmelCase = torch.tensor( [[-0.04_82, 0.92_28, 0.49_51], [-0.25_47, 0.80_17, 0.85_27], [-0.00_69, 0.33_85, -0.00_89]] ).to(__snake_case ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , __snake_case , atol=__snake_case ) ) _lowerCAmelCase = torch.tensor( [[-0.84_22, -0.84_34, -0.97_18], [-1.01_44, -0.55_65, -0.41_95], [-1.00_38, -0.44_84, -0.19_61]] ).to(__snake_case ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , __snake_case , atol=__snake_case ) ) _lowerCAmelCase = torch.tensor( [[0.28_52, -0.01_59, 0.97_35], [0.62_54, 0.18_58, 0.85_29], [-0.06_80, -0.41_16, 1.84_13]] ).to(__snake_case ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , __snake_case , atol=__snake_case ) ) def lowercase__ ( self : Any ) -> Tuple: _lowerCAmelCase = ( MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco""" ) .to(__snake_case ) .eval() ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(__snake_case , return_tensors="""pt""" ).to(__snake_case ) _lowerCAmelCase = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(__snake_case , (1, 3, 8_00, 10_88) ) with torch.no_grad(): _lowerCAmelCase = model(**__snake_case ) # masks_queries_logits _lowerCAmelCase = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) _lowerCAmelCase = [ [-1.3_73_71_24, -1.7_72_49_37, -1.9_36_42_33], [-1.5_97_72_81, -1.9_86_79_39, -2.1_52_36_95], [-1.5_79_53_98, -1.9_26_98_32, -2.09_39_42], ] _lowerCAmelCase = torch.tensor(__snake_case ).to(__snake_case ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , __snake_case , atol=__snake_case ) ) # class_queries_logits _lowerCAmelCase = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) _lowerCAmelCase = torch.tensor( [ [1.6_5_1_2E0_0, -5.2_5_7_2E0_0, -3.3_5_1_9E0_0], [3.6_1_6_9E-0_2, -5.9_0_2_5E0_0, -2.9_3_1_3E0_0], [1.0_7_6_6E-0_4, -7.7_6_3_0E0_0, -5.1_2_6_3E0_0], ] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , __snake_case , atol=__snake_case ) ) def lowercase__ ( self : Tuple ) -> Optional[Any]: _lowerCAmelCase = ( MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff""" ) .to(__snake_case ) .eval() ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(__snake_case , return_tensors="""pt""" ).to(__snake_case ) _lowerCAmelCase = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(__snake_case , (1, 3, 8_00, 10_88) ) with torch.no_grad(): _lowerCAmelCase = model(**__snake_case ) # masks_queries_logits _lowerCAmelCase = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) _lowerCAmelCase = [[-0.90_46, -2.63_66, -4.60_62], [-3.41_79, -5.78_90, -8.80_57], [-4.91_79, -7.65_60, -10.77_11]] _lowerCAmelCase = torch.tensor(__snake_case ).to(__snake_case ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , __snake_case , atol=__snake_case ) ) # class_queries_logits _lowerCAmelCase = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) _lowerCAmelCase = torch.tensor( [[4.71_88, -3.25_85, -2.88_57], [6.68_71, -2.91_81, -1.24_87], [7.24_49, -2.27_64, -2.18_74]] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , __snake_case , atol=__snake_case ) ) def lowercase__ ( self : Optional[int] ) -> Union[str, Any]: _lowerCAmelCase = ( MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco""" ) .to(__snake_case ) .eval() ) _lowerCAmelCase = self.default_image_processor _lowerCAmelCase = image_processor( [np.zeros((3, 8_00, 13_33) ), np.zeros((3, 8_00, 13_33) )] , segmentation_maps=[np.zeros((3_84, 3_84) ).astype(np.floataa ), np.zeros((3_84, 3_84) ).astype(np.floataa )] , return_tensors="""pt""" , ) _lowerCAmelCase = inputs["""pixel_values"""].to(__snake_case ) _lowerCAmelCase = [el.to(__snake_case ) for el in inputs["""mask_labels"""]] _lowerCAmelCase = [el.to(__snake_case ) for el in inputs["""class_labels"""]] with torch.no_grad(): _lowerCAmelCase = model(**__snake_case ) self.assertTrue(outputs.loss is not None )
70
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
0
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A_ :Dict = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __A ( a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : str =XLMProphetNetTokenizer UpperCamelCase__ : int =False UpperCamelCase__ : Optional[int] =True def __lowercase ( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing __UpperCamelCase : List[Any] =XLMProphetNetTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[Any] ='[PAD]' __UpperCamelCase : Union[str, Any] =0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '[PAD]' ) self.assertEqual(vocab_keys[1] , '[CLS]' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(lowerCamelCase__ ) , 1012 ) def __lowercase ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1012 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int =XLMProphetNetTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) __UpperCamelCase : List[str] =tokenizer.tokenize('This is a test' ) self.assertListEqual(lowerCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) __UpperCamelCase : List[str] =tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) __UpperCamelCase : List[str] =tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] , ) __UpperCamelCase : int =tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '[UNK]', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '[UNK]', '.', ] , ) @cached_property def __lowercase ( self ): """simple docstring""" return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ='Hello World!' __UpperCamelCase : Union[str, Any] =[35389, 6672, 49, 2] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : int ={'input_ids': [[11073, 82783, 18, 26, 82783, 549, 51540, 248, 17209, 1301, 217, 20, 215186, 1325, 147, 17209, 1301, 217, 20, 56370, 53, 122020, 20, 16477, 27, 87355, 4548, 20, 4728, 78392, 17, 159969, 18, 26, 24491, 629, 15, 538, 22704, 5439, 15, 2788, 24491, 9885, 15, 43534, 605, 15, 814, 18403, 33200, 29, 15, 43534, 24458, 12410, 111, 24966, 83669, 9637, 144068, 26, 850, 22346, 27, 147, 24966, 83669, 83490, 26, 39113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 122020, 115785, 34, 816, 1339, 46887, 18, 147, 53905, 1951, 42238, 41170, 17732, 834, 436, 15, 27523, 98733, 217, 147, 5542, 4981, 930, 17347, 16, 2], [20091, 629, 94, 82786, 58, 490, 20, 1528, 84, 53905, 344, 80592, 110128, 18822, 5267, 1306, 62, 152537, 308, 7997, 401, 124427, 549, 35442, 225, 109, 15055, 25748, 147, 7119, 43712, 34, 767, 135366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 63784, 119466, 17, 147808, 88214, 18, 656, 81, 32, 3296, 10280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name='microsoft/xprophetnet-large-wiki100-cased' , revision='1acad1643ddd54a44df6a1b797ada8373685d90e' , )
71
# 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
0
"""simple docstring""" 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, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) def snake_case_ ( A_ : Dict ): '''simple docstring''' if isinstance(A_, (list, tuple) ) and isinstance(videos[0], (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(A_, (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(A_ ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class __snake_case ( _lowercase): snake_case__ : Dict = ["pixel_values"] def __init__( self : str , __lowerCAmelCase : bool = True , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , __lowerCAmelCase : bool = True , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : bool = True , __lowerCAmelCase : Union[int, float] = 1 / 2_5_5 , __lowerCAmelCase : bool = True , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , **__lowerCAmelCase : Tuple , ): """simple docstring""" super().__init__(**__lowerCAmelCase ) _lowerCamelCase : Optional[int] = size if size is not None else {'''shortest_edge''': 2_2_4} _lowerCamelCase : List[Any] = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4} _lowerCamelCase : str = get_size_dict(__lowerCAmelCase , param_name='''crop_size''' ) _lowerCamelCase : int = do_resize _lowerCamelCase : Optional[Any] = size _lowerCamelCase : Optional[int] = do_center_crop _lowerCamelCase : Optional[Any] = crop_size _lowerCamelCase : str = resample _lowerCamelCase : Any = do_rescale _lowerCamelCase : List[Any] = rescale_factor _lowerCamelCase : List[Any] = do_normalize _lowerCamelCase : Optional[int] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _lowerCamelCase : str = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Dict[str, int] , __lowerCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : List[Any] , ): """simple docstring""" _lowerCamelCase : Optional[Any] = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) if "shortest_edge" in size: _lowerCamelCase : Tuple = get_resize_output_image_size(__lowerCAmelCase , size['''shortest_edge'''] , default_to_square=__lowerCAmelCase ) elif "height" in size and "width" in size: _lowerCamelCase : str = (size['''height'''], size['''width''']) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Dict[str, int] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Dict , ): """simple docstring""" _lowerCamelCase : int = get_size_dict(__lowerCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(__lowerCAmelCase , size=(size['''height'''], size['''width''']) , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[int, float] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Dict , ): """simple docstring""" return rescale(__lowerCAmelCase , scale=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[str] , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Tuple , ): """simple docstring""" return normalize(__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Any , __lowerCAmelCase : ImageInput , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : PILImageResampling = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : float = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , ): """simple docstring""" if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. _lowerCamelCase : Any = to_numpy_array(__lowerCAmelCase ) if do_resize: _lowerCamelCase : Tuple = self.resize(image=__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase ) if do_center_crop: _lowerCamelCase : List[str] = self.center_crop(__lowerCAmelCase , size=__lowerCAmelCase ) if do_rescale: _lowerCamelCase : Optional[Any] = self.rescale(image=__lowerCAmelCase , scale=__lowerCAmelCase ) if do_normalize: _lowerCamelCase : int = self.normalize(image=__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase ) _lowerCamelCase : str = to_channel_dimension_format(__lowerCAmelCase , __lowerCAmelCase ) return image def SCREAMING_SNAKE_CASE ( self : Any , __lowerCAmelCase : ImageInput , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : PILImageResampling = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : float = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , __lowerCAmelCase : ChannelDimension = ChannelDimension.FIRST , **__lowerCAmelCase : Any , ): """simple docstring""" _lowerCamelCase : List[Any] = do_resize if do_resize is not None else self.do_resize _lowerCamelCase : str = resample if resample is not None else self.resample _lowerCamelCase : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCamelCase : Any = do_rescale if do_rescale is not None else self.do_rescale _lowerCamelCase : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCamelCase : List[Any] = do_normalize if do_normalize is not None else self.do_normalize _lowerCamelCase : Dict = image_mean if image_mean is not None else self.image_mean _lowerCamelCase : int = image_std if image_std is not None else self.image_std _lowerCamelCase : List[str] = size if size is not None else self.size _lowerCamelCase : str = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) _lowerCamelCase : List[str] = crop_size if crop_size is not None else self.crop_size _lowerCamelCase : str = get_size_dict(__lowerCAmelCase , param_name='''crop_size''' ) if not valid_images(__lowerCAmelCase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) _lowerCamelCase : Any = make_batched(__lowerCAmelCase ) _lowerCamelCase : Tuple = [ [ self._preprocess_image( image=__lowerCAmelCase , do_resize=__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase , do_center_crop=__lowerCAmelCase , crop_size=__lowerCAmelCase , do_rescale=__lowerCAmelCase , rescale_factor=__lowerCAmelCase , do_normalize=__lowerCAmelCase , image_mean=__lowerCAmelCase , image_std=__lowerCAmelCase , data_format=__lowerCAmelCase , ) for img in video ] for video in videos ] _lowerCamelCase : List[str] = {'''pixel_values''': videos} return BatchFeature(data=__lowerCAmelCase , tensor_type=__lowerCAmelCase )
72
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
0
import csv import tweepy # Twitter API credentials a ="""""" a ="""""" a ="""""" a ="""""" def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> None: # authorize twitter, initialize tweepy __lowerCamelCase : Tuple = tweepy.OAuthHandler(lowerCamelCase__ , lowerCamelCase__ ) auth.set_access_token(lowerCamelCase__ , lowerCamelCase__ ) __lowerCamelCase : Optional[int] = tweepy.API(lowerCamelCase__ ) # initialize a list to hold all the tweepy Tweets __lowerCamelCase : str = [] # make initial request for most recent tweets (200 is the maximum allowed count) __lowerCamelCase : Union[str, Any] = api.user_timeline(screen_name=lowerCamelCase__ , count=2_0_0 ) # save most recent tweets alltweets.extend(lowerCamelCase__ ) # save the id of the oldest tweet less one __lowerCamelCase : Any = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowerCamelCase__ ) > 0: print(F"getting tweets before {oldest}" ) # all subsequent requests use the max_id param to prevent duplicates __lowerCamelCase : str = api.user_timeline( screen_name=lowerCamelCase__ , count=2_0_0 , max_id=lowerCamelCase__ ) # save most recent tweets alltweets.extend(lowerCamelCase__ ) # update the id of the oldest tweet less one __lowerCamelCase : Optional[int] = alltweets[-1].id - 1 print(F"...{len(lowerCamelCase__ )} tweets downloaded so far" ) # transform the tweepy tweets into a 2D array that will populate the csv __lowerCamelCase : str = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(F"new_{screen_name}_tweets.csv" , 'w' ) as f: __lowerCamelCase : Any = csv.writer(lowerCamelCase__ ) writer.writerow(['id', 'created_at', 'text'] ) writer.writerows(lowerCamelCase__ ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("""FirePing32""")
73
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
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _lowercase = logging.get_logger(__name__) _lowercase = { '''microsoft/swinv2-tiny-patch4-window8-256''': ( '''https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json''' ), } class lowerCAmelCase_ ( _lowercase ): '''simple docstring''' _lowerCamelCase: Tuple = '''swinv2''' _lowerCamelCase: Optional[int] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Optional[Any] ,A_ : str=224 ,A_ : Union[str, Any]=4 ,A_ : Union[str, Any]=3 ,A_ : Union[str, Any]=96 ,A_ : Optional[int]=[2, 2, 6, 2] ,A_ : Any=[3, 6, 12, 24] ,A_ : Union[str, Any]=7 ,A_ : str=4.0 ,A_ : int=True ,A_ : str=0.0 ,A_ : List[str]=0.0 ,A_ : str=0.1 ,A_ : List[Any]="gelu" ,A_ : Tuple=False ,A_ : Dict=0.02 ,A_ : Union[str, Any]=1e-5 ,A_ : int=32 ,**A_ : int ,) -> Any: super().__init__(**A_ ) A = image_size A = patch_size A = num_channels A = embed_dim A = depths A = len(A_ ) A = num_heads A = window_size A = mlp_ratio A = qkv_bias A = hidden_dropout_prob A = attention_probs_dropout_prob A = drop_path_rate A = hidden_act A = use_absolute_embeddings A = layer_norm_eps A = initializer_range A = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A = int(embed_dim * 2 ** (len(A_ ) - 1) ) A = (0, 0, 0, 0)
74
# 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
0
'''simple docstring''' def a_ ( __snake_case : int = 6008_5147_5143 ) -> int: """simple docstring""" try: lowerCamelCase_ =int(__snake_case ) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''' ) if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''' ) lowerCamelCase_ =2 lowerCamelCase_ =0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 lowerCamelCase_ =i while n % i == 0: lowerCamelCase_ =n // i i += 1 return int(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
75
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
0
from __future__ import annotations def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : Tuple = get_failure_array(_a) # 2) Step through text searching for pattern SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : Union[str, Any] = 0, 0 # index into text, pattern while i < len(_a): if pattern[j] == text[i]: if j == (len(_a) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: SCREAMING_SNAKE_CASE : List[str] = failure[j - 1] continue i += 1 return False def lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : Any = [0] SCREAMING_SNAKE_CASE : Tuple = 0 SCREAMING_SNAKE_CASE : Optional[int] = 1 while j < len(_a): if pattern[i] == pattern[j]: i += 1 elif i > 0: SCREAMING_SNAKE_CASE : Union[str, Any] = failure[i - 1] continue j += 1 failure.append(_a) return failure if __name__ == "__main__": # Test 1) a_ = 'abc1abc12' a_ = 'alskfjaldsabc1abc1abc12k23adsfabcabc' a_ = 'alskfjaldsk23adsfabcabc' assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) a_ = 'ABABX' a_ = 'ABABZABABYABABX' assert kmp(pattern, text) # Test 3) a_ = 'AAAB' a_ = 'ABAAAAAB' assert kmp(pattern, text) # Test 4) a_ = 'abcdabcy' a_ = 'abcxabcdabxabcdabcdabcy' assert kmp(pattern, text) # Test 5) a_ = 'aabaabaaa' assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
76
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
0
"""simple docstring""" from maths.prime_factors import prime_factors def a_ ( _lowerCAmelCase : int ): '''simple docstring''' if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): lowercase__ : Union[str, Any] = f"""Input value of [number={number}] must be an integer""" raise TypeError(_lowerCAmelCase ) if number < 1: raise ValueError('Input must be a positive integer' ) return -1 if len(prime_factors(_lowerCAmelCase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
77
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
0
"""simple docstring""" def _lowerCAmelCase ( lowercase_ ): UpperCAmelCase = [0] * len(lowercase_ ) for i in range(1 , len(lowercase_ ) ): # use last results for better performance - dynamic programming UpperCAmelCase = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: UpperCAmelCase = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 UpperCAmelCase = j return prefix_result def _lowerCAmelCase ( lowercase_ ): return max(prefix_function(lowercase_ ) ) if __name__ == "__main__": import doctest doctest.testmod()
78
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
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCamelCase_ = { '''configuration_convnext''': ['''CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvNextConfig''', '''ConvNextOnnxConfig'''] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = ['''ConvNextFeatureExtractor'''] lowerCamelCase_ = ['''ConvNextImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvNextForImageClassification''', '''ConvNextModel''', '''ConvNextPreTrainedModel''', '''ConvNextBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''TFConvNextForImageClassification''', '''TFConvNextModel''', '''TFConvNextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
79
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
0
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel a__ : str = logging.getLogger(__name__) def _UpperCamelCase ( __A , __A ) -> Dict: '''simple docstring''' if os.path.exists(__A ): if os.path.exists(os.path.join(__A , "config.json" ) ) and os.path.isfile( os.path.join(__A , "config.json" ) ): os.remove(os.path.join(__A , "config.json" ) ) if os.path.exists(os.path.join(__A , "pytorch_model.bin" ) ) and os.path.isfile( os.path.join(__A , "pytorch_model.bin" ) ): os.remove(os.path.join(__A , "pytorch_model.bin" ) ) else: os.makedirs(__A ) model.save_pretrained(__A ) def _UpperCamelCase ( __A , __A=False ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = 2 if unlogit: UpperCamelCase__ = torch.pow(__A , __A ) UpperCamelCase__ = p * torch.log(__A ) UpperCamelCase__ = 0 return -plogp.sum(dim=-1 ) def _UpperCamelCase ( __A ) -> List[str]: '''simple docstring''' logger.info("lv, h >\t" + "\t".join(F'''{x + 1}''' for x in range(len(__A ) ) ) ) for row in range(len(__A ) ): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + "\t".join(F'''{x:.5f}''' for x in tensor[row].cpu().data ) ) else: logger.info(F'''layer {row + 1}:\t''' + "\t".join(F'''{x:d}''' for x in tensor[row].cpu().data ) ) def _UpperCamelCase ( __A , __A , __A , __A=True , __A=True , __A=None , __A=False ) -> str: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = model.config.num_hidden_layers, model.config.num_attention_heads UpperCamelCase__ = torch.zeros(__A , __A ).to(args.device ) UpperCamelCase__ = torch.zeros(__A , __A ).to(args.device ) if head_mask is None: UpperCamelCase__ = torch.ones(__A , __A ).to(args.device ) head_mask.requires_grad_(requires_grad=__A ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: UpperCamelCase__ = None UpperCamelCase__ = 0.0 UpperCamelCase__ = 0.0 for step, inputs in enumerate(tqdm(__A , desc="Iteration" , disable=args.local_rank not in [-1, 0] ) ): UpperCamelCase__ = tuple(t.to(args.device ) for t in inputs ) ((UpperCamelCase__) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) UpperCamelCase__ = model(__A , labels=__A , head_mask=__A ) # (loss), lm_logits, presents, (all hidden_states), (attentions) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A ): UpperCamelCase__ = entropy(attn.detach() , __A ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: UpperCamelCase__ = 2 UpperCamelCase__ = torch.pow(torch.pow(__A , __A ).sum(-1 ) , 1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1E-20 if not args.dont_normalize_global_importance: UpperCamelCase__ = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info("Attention entropies" ) print_ad_tensor(__A ) if compute_importance: logger.info("Head importance scores" ) print_ad_tensor(__A ) logger.info("Head ranked by importance scores" ) UpperCamelCase__ = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device ) UpperCamelCase__ = torch.arange( head_importance.numel() , device=args.device ) UpperCamelCase__ = head_ranks.view_as(__A ) print_ad_tensor(__A ) return attn_entropy, head_importance, total_loss def _UpperCamelCase ( __A , __A , __A ) -> List[Any]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = compute_heads_importance(__A , __A , __A , compute_entropy=__A ) UpperCamelCase__ = 1 / loss # instead of downsteam score use the LM loss logger.info("Pruning: original score: %f, threshold: %f" , __A , original_score * args.masking_threshold ) UpperCamelCase__ = torch.ones_like(__A ) UpperCamelCase__ = max(1 , int(new_head_mask.numel() * args.masking_amount ) ) UpperCamelCase__ = original_score while current_score >= original_score * args.masking_threshold: UpperCamelCase__ = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads UpperCamelCase__ = float("Inf" ) UpperCamelCase__ = head_importance.view(-1 ).sort()[1] if len(__A ) <= num_to_mask: print("BREAK BY num_to_mask" ) break # mask heads UpperCamelCase__ = current_heads_to_mask[:num_to_mask] logger.info("Heads to mask: %s" , str(current_heads_to_mask.tolist() ) ) UpperCamelCase__ = new_head_mask.view(-1 ) UpperCamelCase__ = 0.0 UpperCamelCase__ = new_head_mask.view_as(__A ) UpperCamelCase__ = new_head_mask.clone().detach() print_ad_tensor(__A ) # Compute metric and head importance again UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A ) UpperCamelCase__ = 1 / loss logger.info( "Masking: current score: %f, remaining heads %d (%.1f percents)" , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info("Final head mask" ) print_ad_tensor(__A ) np.save(os.path.join(args.output_dir , "head_mask.npy" ) , head_mask.detach().cpu().numpy() ) return head_mask def _UpperCamelCase ( __A , __A , __A , __A ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = datetime.now() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A ) UpperCamelCase__ = 1 / loss UpperCamelCase__ = datetime.now() - before_time UpperCamelCase__ = sum(p.numel() for p in model.parameters() ) UpperCamelCase__ = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A ) ) } for k, v in heads_to_prune.items(): if isinstance(__A , __A ): UpperCamelCase__ = [ v, ] assert sum(len(__A ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(__A ) UpperCamelCase__ = sum(p.numel() for p in model.parameters() ) UpperCamelCase__ = datetime.now() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) UpperCamelCase__ = 1 / loss UpperCamelCase__ = datetime.now() - before_time logger.info( "Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)" , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info("Pruning: score with masking: %f score with pruning: %f" , __A , __A ) logger.info("Pruning: speed ratio (original timing / new timing): %f percents" , original_time / new_time * 100 ) save_model(__A , args.output_dir ) def _UpperCamelCase ( ) -> str: '''simple docstring''' UpperCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--data_dir" , default=__A , type=__A , required=__A , help="The input data dir. Should contain the .tsv files (or other data files) for the task." , ) parser.add_argument( "--model_name_or_path" , default=__A , type=__A , required=__A , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--output_dir" , default=__A , type=__A , required=__A , help="The output directory where the model predictions and checkpoints will be written." , ) # Other parameters parser.add_argument( "--config_name" , default="" , type=__A , help="Pretrained config name or path if not the same as model_name_or_path" , ) parser.add_argument( "--tokenizer_name" , default="" , type=__A , help="Pretrained tokenizer name or path if not the same as model_name_or_path" , ) parser.add_argument( "--cache_dir" , default=__A , type=__A , help="Where do you want to store the pre-trained models downloaded from s3" , ) parser.add_argument( "--data_subset" , type=__A , default=-1 , help="If > 0: limit the data to a subset of data_subset instances." ) parser.add_argument( "--overwrite_output_dir" , action="store_true" , help="Whether to overwrite data in output directory" ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) parser.add_argument( "--dont_normalize_importance_by_layer" , action="store_true" , help="Don't normalize importance score by layers" ) parser.add_argument( "--dont_normalize_global_importance" , action="store_true" , help="Don't normalize all importance scores between 0 and 1" , ) parser.add_argument( "--try_masking" , action="store_true" , help="Whether to try to mask head until a threshold of accuracy." ) parser.add_argument( "--masking_threshold" , default=0.9 , type=__A , help="masking threshold in term of metrics (stop masking when metric < threshold * original metric value)." , ) parser.add_argument( "--masking_amount" , default=0.1 , type=__A , help="Amount to heads to masking at each masking step." ) parser.add_argument("--metric_name" , default="acc" , type=__A , help="Metric to use for head masking." ) parser.add_argument( "--max_seq_length" , default=128 , type=__A , help=( "The maximum total input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, sequences shorter padded." ) , ) parser.add_argument("--batch_size" , default=1 , type=__A , help="Batch size." ) parser.add_argument("--seed" , type=__A , default=42 ) parser.add_argument("--local_rank" , type=__A , default=-1 , help="local_rank for distributed training on gpus" ) parser.add_argument("--no_cuda" , action="store_true" , help="Whether not to use CUDA when available" ) parser.add_argument("--server_ip" , type=__A , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=__A , default="" , help="Can be used for distant debugging." ) UpperCamelCase__ = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("Waiting for debugger attach" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: UpperCamelCase__ = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu" ) UpperCamelCase__ = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) UpperCamelCase__ = torch.device("cuda" , args.local_rank ) UpperCamelCase__ = 1 torch.distributed.init_process_group(backend="nccl" ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info("device: {} n_gpu: {}, distributed: {}".format(args.device , args.n_gpu , bool(args.local_rank != -1 ) ) ) UpperCamelCase__ = GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: UpperCamelCase__ = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A ) elif args.n_gpu > 1: UpperCamelCase__ = nn.DataParallel(__A ) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A ) torch.save(__A , os.path.join(args.output_dir , "run_args.bin" ) ) logger.info("Training/evaluation parameters %s" , __A ) # Prepare dataset UpperCamelCase__ = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa ), ] ) UpperCamelCase__ = (torch.from_numpy(__A ),) UpperCamelCase__ = TensorDataset(*__A ) UpperCamelCase__ = RandomSampler(__A ) UpperCamelCase__ = DataLoader(__A , sampler=__A , batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: UpperCamelCase__ = mask_heads(__A , __A , __A ) prune_heads(__A , __A , __A , __A ) if __name__ == "__main__": main()
80
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
0
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowerCamelCase_ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase_ : List[str] = { """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""", } class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = "instructblip_vision_model" def __init__( self , __A=1408 , __A=6144 , __A=39 , __A=16 , __A=224 , __A=14 , __A="gelu" , __A=1E-6 , __A=0.0 , __A=1E-1_0 , __A=True , **__A , ) -> Tuple: super().__init__(**__A ) a =hidden_size a =intermediate_size a =num_hidden_layers a =num_attention_heads a =patch_size a =image_size a =initializer_range a =attention_dropout a =layer_norm_eps a =hidden_act a =qkv_bias @classmethod def SCREAMING_SNAKE_CASE ( cls , __A , **__A ) -> "PretrainedConfig": cls._set_token_in_kwargs(__A ) a , a =cls.get_config_dict(__A , **__A ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": a =config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__A , **__A ) class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = "instructblip_qformer" def __init__( self , __A=3_0522 , __A=768 , __A=12 , __A=12 , __A=3072 , __A="gelu" , __A=0.1 , __A=0.1 , __A=512 , __A=0.02 , __A=1E-1_2 , __A=0 , __A="absolute" , __A=2 , __A=1408 , **__A , ) -> Union[str, Any]: super().__init__(pad_token_id=__A , **__A ) a =vocab_size a =hidden_size a =num_hidden_layers a =num_attention_heads a =hidden_act a =intermediate_size a =hidden_dropout_prob a =attention_probs_dropout_prob a =max_position_embeddings a =initializer_range a =layer_norm_eps a =position_embedding_type a =cross_attention_frequency a =encoder_hidden_size @classmethod def SCREAMING_SNAKE_CASE ( cls , __A , **__A ) -> "PretrainedConfig": cls._set_token_in_kwargs(__A ) a , a =cls.get_config_dict(__A , **__A ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": a =config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__A , **__A ) class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = "instructblip" __lowerCAmelCase = True def __init__( self , __A=None , __A=None , __A=None , __A=32 , **__A ) -> Optional[int]: super().__init__(**__A ) if vision_config is None: a ={} logger.info('''vision_config is None. initializing the InstructBlipVisionConfig with default values.''' ) if qformer_config is None: a ={} logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.''' ) if text_config is None: a ={} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) a =InstructBlipVisionConfig(**__A ) a =InstructBlipQFormerConfig(**__A ) a =text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' a =CONFIG_MAPPING[text_model_type](**__A ) a =self.text_config.tie_word_embeddings a =self.text_config.is_encoder_decoder a =num_query_tokens a =self.vision_config.hidden_size a =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES a =1.0 a =0.02 @classmethod def SCREAMING_SNAKE_CASE ( cls , __A , __A , __A , **__A , ) -> Any: return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **__A , ) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a =copy.deepcopy(self.__dict__ ) a =self.vision_config.to_dict() a =self.qformer_config.to_dict() a =self.text_config.to_dict() a =self.__class__.model_type return output
81
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
0
from collections.abc import Sequence def _UpperCAmelCase ( snake_case , snake_case = False ): """simple docstring""" if not arr: return 0 _lowerCAmelCase = 0 if allow_empty_subarrays else float("""-inf""" ) _lowerCAmelCase = 0.0 for num in arr: _lowerCAmelCase = max(0 if allow_empty_subarrays else num , curr_sum + num ) _lowerCAmelCase = max(snake_case , snake_case ) return max_sum if __name__ == "__main__": from doctest import testmod testmod() A__ = [-2, 1, -3, 4, -1, 2, 1, -5, 4] print(f"{max_subarray_sum(nums) = }")
82
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
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case_ : Optional[Any] = { 'configuration_swinv2': ['SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Swinv2Config'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : int = [ 'SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST', 'Swinv2ForImageClassification', 'Swinv2ForMaskedImageModeling', 'Swinv2Model', 'Swinv2PreTrainedModel', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys snake_case_ : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
83
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
0
"""simple docstring""" import argparse import collections import os import re import tempfile import pandas as pd from datasets import Dataset from huggingface_hub import hf_hub_download, upload_folder from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/update_metadata.py __UpperCAmelCase = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. __UpperCAmelCase = direct_transformers_import(TRANSFORMERS_PATH) # Regexes that match TF/Flax/PT model names. __UpperCAmelCase = re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)') __UpperCAmelCase = re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)') # Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes. __UpperCAmelCase = re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)') # Fill this with tuples (pipeline_tag, model_mapping, auto_model) __UpperCAmelCase = [ ('pretraining', 'MODEL_FOR_PRETRAINING_MAPPING_NAMES', 'AutoModelForPreTraining'), ('feature-extraction', 'MODEL_MAPPING_NAMES', 'AutoModel'), ('audio-classification', 'MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForAudioClassification'), ('text-generation', 'MODEL_FOR_CAUSAL_LM_MAPPING_NAMES', 'AutoModelForCausalLM'), ('automatic-speech-recognition', 'MODEL_FOR_CTC_MAPPING_NAMES', 'AutoModelForCTC'), ('image-classification', 'MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForImageClassification'), ('image-segmentation', 'MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES', 'AutoModelForImageSegmentation'), ('fill-mask', 'MODEL_FOR_MASKED_LM_MAPPING_NAMES', 'AutoModelForMaskedLM'), ('object-detection', 'MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES', 'AutoModelForObjectDetection'), ( 'zero-shot-object-detection', 'MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES', 'AutoModelForZeroShotObjectDetection', ), ('question-answering', 'MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES', 'AutoModelForQuestionAnswering'), ('text2text-generation', 'MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES', 'AutoModelForSeq2SeqLM'), ('text-classification', 'MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForSequenceClassification'), ('automatic-speech-recognition', 'MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES', 'AutoModelForSpeechSeq2Seq'), ( 'table-question-answering', 'MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES', 'AutoModelForTableQuestionAnswering', ), ('token-classification', 'MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForTokenClassification'), ('multiple-choice', 'MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES', 'AutoModelForMultipleChoice'), ( 'next-sentence-prediction', 'MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES', 'AutoModelForNextSentencePrediction', ), ( 'audio-frame-classification', 'MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForAudioFrameClassification', ), ('audio-xvector', 'MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES', 'AutoModelForAudioXVector'), ( 'document-question-answering', 'MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES', 'AutoModelForDocumentQuestionAnswering', ), ( 'visual-question-answering', 'MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING_NAMES', 'AutoModelForVisualQuestionAnswering', ), ('image-to-text', 'MODEL_FOR_FOR_VISION_2_SEQ_MAPPING_NAMES', 'AutoModelForVision2Seq'), ( 'zero-shot-image-classification', 'MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForZeroShotImageClassification', ), ('depth-estimation', 'MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES', 'AutoModelForDepthEstimation'), ('video-classification', 'MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForVideoClassification'), ('mask-generation', 'MODEL_FOR_MASK_GENERATION_MAPPING_NAMES', 'AutoModelForMaskGeneration'), ] def _snake_case ( lowercase__ : Optional[int] ) -> List[str]: '''simple docstring''' lowerCAmelCase_ :List[Any] = re.finditer(""".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)""" , lowercase__ ) return [m.group(0 ) for m in matches] def _snake_case ( ) -> str: '''simple docstring''' lowerCAmelCase_ :List[str] = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES lowerCAmelCase_ :Optional[Any] = { config.replace("""Config""" , """""" ): model_type for model_type, config in config_maping_names.items() } # Dictionaries flagging if each model prefix has a backend in PT/TF/Flax. lowerCAmelCase_ :List[Any] = collections.defaultdict(lowercase__ ) lowerCAmelCase_ :Dict = collections.defaultdict(lowercase__ ) lowerCAmelCase_ :Union[str, Any] = collections.defaultdict(lowercase__ ) # Let's lookup through all transformers object (once) and find if models are supported by a given backend. for attr_name in dir(lowercase__ ): lowerCAmelCase_ :List[Any] = None if _re_tf_models.match(lowercase__ ) is not None: lowerCAmelCase_ :List[str] = tf_models lowerCAmelCase_ :Dict = _re_tf_models.match(lowercase__ ).groups()[0] elif _re_flax_models.match(lowercase__ ) is not None: lowerCAmelCase_ :Tuple = flax_models lowerCAmelCase_ :List[str] = _re_flax_models.match(lowercase__ ).groups()[0] elif _re_pt_models.match(lowercase__ ) is not None: lowerCAmelCase_ :List[Any] = pt_models lowerCAmelCase_ :str = _re_pt_models.match(lowercase__ ).groups()[0] if lookup_dict is not None: while len(lowercase__ ) > 0: if attr_name in model_prefix_to_model_type: lowerCAmelCase_ :List[Any] = True break # Try again after removing the last word in the name lowerCAmelCase_ :Optional[int] = """""".join(camel_case_split(lowercase__ )[:-1] ) lowerCAmelCase_ :Optional[Any] = set(list(pt_models.keys() ) + list(tf_models.keys() ) + list(flax_models.keys() ) ) lowerCAmelCase_ :Dict = list(lowercase__ ) all_models.sort() lowerCAmelCase_ :str = {"""model_type""": all_models} lowerCAmelCase_ :Optional[int] = [pt_models[t] for t in all_models] lowerCAmelCase_ :Dict = [tf_models[t] for t in all_models] lowerCAmelCase_ :Optional[Any] = [flax_models[t] for t in all_models] # Now let's use the auto-mapping names to make sure lowerCAmelCase_ :Any = {} for t in all_models: if t in transformers_module.models.auto.processing_auto.PROCESSOR_MAPPING_NAMES: lowerCAmelCase_ :Any = """AutoProcessor""" elif t in transformers_module.models.auto.tokenization_auto.TOKENIZER_MAPPING_NAMES: lowerCAmelCase_ :List[str] = """AutoTokenizer""" elif t in transformers_module.models.auto.feature_extraction_auto.FEATURE_EXTRACTOR_MAPPING_NAMES: lowerCAmelCase_ :List[Any] = """AutoFeatureExtractor""" else: # Default to AutoTokenizer if a model has nothing, for backward compatibility. lowerCAmelCase_ :Union[str, Any] = """AutoTokenizer""" lowerCAmelCase_ :Union[str, Any] = [processors[t] for t in all_models] return pd.DataFrame(lowercase__ ) def _snake_case ( lowercase__ : Any ) -> str: '''simple docstring''' lowerCAmelCase_ :str = [ transformers_module.models.auto.modeling_auto, transformers_module.models.auto.modeling_tf_auto, transformers_module.models.auto.modeling_flax_auto, ] for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS: lowerCAmelCase_ :Dict = [model_mapping, f"""TF_{model_mapping}""", f"""FLAX_{model_mapping}"""] lowerCAmelCase_ :Dict = [auto_class, f"""TF_{auto_class}""", f"""Flax_{auto_class}"""] # Loop through all three frameworks for module, cls, mapping in zip(lowercase__ , lowercase__ , lowercase__ ): # The type of pipeline may not exist in this framework if not hasattr(lowercase__ , lowercase__ ): continue # First extract all model_names lowerCAmelCase_ :Optional[Any] = [] for name in getattr(lowercase__ , lowercase__ ).values(): if isinstance(lowercase__ , lowercase__ ): model_names.append(lowercase__ ) else: model_names.extend(list(lowercase__ ) ) # Add pipeline tag and auto model class for those models table.update({model_name: (pipeline_tag, cls) for model_name in model_names} ) return table def _snake_case ( lowercase__ : List[Any] , lowercase__ : Union[str, Any] ) -> Any: '''simple docstring''' lowerCAmelCase_ :Union[str, Any] = get_frameworks_table() lowerCAmelCase_ :Optional[int] = Dataset.from_pandas(lowercase__ ) lowerCAmelCase_ :Any = hf_hub_download( """huggingface/transformers-metadata""" , """pipeline_tags.json""" , repo_type="""dataset""" , token=lowercase__ ) lowerCAmelCase_ :List[str] = Dataset.from_json(lowercase__ ) lowerCAmelCase_ :int = { tags_dataset[i]["""model_class"""]: (tags_dataset[i]["""pipeline_tag"""], tags_dataset[i]["""auto_class"""]) for i in range(len(lowercase__ ) ) } lowerCAmelCase_ :Dict = update_pipeline_and_auto_class_table(lowercase__ ) # Sort the model classes to avoid some nondeterministic updates to create false update commits. lowerCAmelCase_ :str = sorted(table.keys() ) lowerCAmelCase_ :List[str] = pd.DataFrame( { """model_class""": model_classes, """pipeline_tag""": [table[m][0] for m in model_classes], """auto_class""": [table[m][1] for m in model_classes], } ) lowerCAmelCase_ :List[str] = Dataset.from_pandas(lowercase__ ) with tempfile.TemporaryDirectory() as tmp_dir: frameworks_dataset.to_json(os.path.join(lowercase__ , """frameworks.json""" ) ) tags_dataset.to_json(os.path.join(lowercase__ , """pipeline_tags.json""" ) ) if commit_sha is not None: lowerCAmelCase_ :List[Any] = ( f"""Update with commit {commit_sha}\n\nSee: """ f"""https://github.com/huggingface/transformers/commit/{commit_sha}""" ) else: lowerCAmelCase_ :int = """Update""" upload_folder( repo_id="""huggingface/transformers-metadata""" , folder_path=lowercase__ , repo_type="""dataset""" , token=lowercase__ , commit_message=lowercase__ , ) def _snake_case ( ) -> str: '''simple docstring''' lowerCAmelCase_ :Tuple = {tag: cls for tag, _, cls in PIPELINE_TAGS_AND_AUTO_MODELS} lowerCAmelCase_ :Optional[int] = transformers_module.pipelines.SUPPORTED_TASKS lowerCAmelCase_ :int = [] for key in pipeline_tasks: if key not in in_table: lowerCAmelCase_ :Any = pipeline_tasks[key]["""pt"""] if isinstance(lowercase__ , (list, tuple) ): lowerCAmelCase_ :int = model[0] lowerCAmelCase_ :List[Any] = model.__name__ if model not in in_table.values(): missing.append(lowercase__ ) if len(lowercase__ ) > 0: lowerCAmelCase_ :Optional[int] = """, """.join(lowercase__ ) raise ValueError( """The following pipeline tags are not present in the `PIPELINE_TAGS_AND_AUTO_MODELS` constant inside """ f"""`utils/update_metadata.py`: {msg}. Please add them!""" ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('--token', type=str, help='The token to use to push to the transformers-metadata dataset.') parser.add_argument('--commit_sha', type=str, help='The sha of the commit going with this update.') parser.add_argument('--check-only', action='store_true', help='Activate to just check all pipelines are present.') __UpperCAmelCase = parser.parse_args() if args.check_only: check_pipeline_tags() else: update_metadata(args.token, args.commit_sha)
84
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
0
'''simple docstring''' import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.getLogger(__name__) require_version("pytorch_lightning>=1.0.4") _SCREAMING_SNAKE_CASE : List[str] = { "base": AutoModel, "sequence-classification": AutoModelForSequenceClassification, "question-answering": AutoModelForQuestionAnswering, "pretraining": AutoModelForPreTraining, "token-classification": AutoModelForTokenClassification, "language-modeling": AutoModelWithLMHead, "summarization": AutoModelForSeqaSeqLM, "translation": AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization _SCREAMING_SNAKE_CASE : Union[str, Any] = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } _SCREAMING_SNAKE_CASE : Any = sorted(arg_to_scheduler.keys()) _SCREAMING_SNAKE_CASE : List[str] = "{" + ", ".join(arg_to_scheduler_choices) + "}" class _snake_case ( pl.LightningModule ): def __init__( self , a__ , a__=None , a__="base" , a__=None , a__=None , a__=None , **a__ , ) -> Optional[Any]: '''simple docstring''' super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(a__ ) snake_case_ = 0 snake_case_ = Path(self.hparams.output_dir ) snake_case_ = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: snake_case_ = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({"num_labels": num_labels} if num_labels is not None else {}) , cache_dir=a__ , **a__ , ) else: snake_case_ = config snake_case_ = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(self.hparams , a__ , a__ ): assert hasattr(self.config , a__ ), F'model config doesn\'t have a `{p}` attribute' setattr(self.config , a__ , getattr(self.hparams , a__ ) ) if tokenizer is None: snake_case_ = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=a__ , ) else: snake_case_ = tokenizer snake_case_ = MODEL_MODES[mode] if model is None: snake_case_ = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool(".ckpt" in self.hparams.model_name_or_path ) , config=self.config , cache_dir=a__ , ) else: snake_case_ = model def lowerCAmelCase__ ( self , *a__ , **a__ ) -> Any: '''simple docstring''' snake_case_ = self.model_type.from_pretrained(*a__ , **a__ ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = arg_to_scheduler[self.hparams.lr_scheduler] snake_case_ = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) snake_case_ = {"scheduler": scheduler, "interval": "step", "frequency": 1} return scheduler def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ = self.model snake_case_ = ["bias", "LayerNorm.weight"] snake_case_ = [ { "params": [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters "weight_decay": self.hparams.weight_decay, }, { "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] if self.hparams.adafactor: snake_case_ = Adafactor( a__ , lr=self.hparams.learning_rate , scale_parameter=a__ , relative_step=a__ ) else: snake_case_ = AdamW( a__ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) snake_case_ = optimizer snake_case_ = self.get_lr_scheduler() return [optimizer], [scheduler] def lowerCAmelCase__ ( self , a__ , a__ ) -> Dict: '''simple docstring''' return self.validation_step(a__ , a__ ) def lowerCAmelCase__ ( self , a__ ) -> Tuple: '''simple docstring''' return self.validation_end(a__ ) def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' snake_case_ = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores snake_case_ = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def lowerCAmelCase__ ( self , a__ ) -> str: '''simple docstring''' if stage == "test": snake_case_ = len(self.test_dataloader().dataset ) else: snake_case_ = self.get_dataloader("train" , self.hparams.train_batch_size , shuffle=a__ ) snake_case_ = len(self.train_dataloader().dataset ) def lowerCAmelCase__ ( self , a__ , a__ , a__ = False ) -> Optional[Any]: '''simple docstring''' raise NotImplementedError("You must implement this for your task" ) def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' return self.train_loader def lowerCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' return self.get_dataloader("dev" , self.hparams.eval_batch_size , shuffle=a__ ) def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' return self.get_dataloader("test" , self.hparams.eval_batch_size , shuffle=a__ ) def lowerCAmelCase__ ( self , a__ ) -> List[Any]: '''simple docstring''' return os.path.join( self.hparams.data_dir , "cached_{}_{}_{}".format( a__ , list(filter(a__ , self.hparams.model_name_or_path.split("/" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def lowerCAmelCase__ ( self , a__ ) -> None: '''simple docstring''' snake_case_ = self.output_dir.joinpath("best_tfmr" ) snake_case_ = self.step_count self.model.save_pretrained(a__ ) self.tokenizer.save_pretrained(a__ ) @staticmethod def lowerCAmelCase__ ( a__ , a__ ) -> List[Any]: '''simple docstring''' parser.add_argument( "--model_name_or_path" , default=a__ , type=a__ , required=a__ , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--config_name" , default="" , type=a__ , help="Pretrained config name or path if not the same as model_name" ) parser.add_argument( "--tokenizer_name" , default=a__ , type=a__ , help="Pretrained tokenizer name or path if not the same as model_name" , ) parser.add_argument( "--cache_dir" , default=str(Path(a__ ).parent / "test_run" / "cache" ) , type=a__ , help="Where do you want to store the pre-trained models downloaded from huggingface.co" , ) parser.add_argument( "--encoder_layerdrop" , type=a__ , help="Encoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--decoder_layerdrop" , type=a__ , help="Decoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--dropout" , type=a__ , help="Dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--attention_dropout" , type=a__ , help="Attention dropout probability (Optional). Goes into model.config" , ) parser.add_argument("--learning_rate" , default=5e-5 , type=a__ , help="The initial learning rate for Adam." ) parser.add_argument( "--lr_scheduler" , default="linear" , choices=a__ , metavar=a__ , type=a__ , help="Learning rate scheduler" , ) parser.add_argument("--weight_decay" , default=0.0 , type=a__ , help="Weight decay if we apply some." ) parser.add_argument("--adam_epsilon" , default=1e-8 , type=a__ , help="Epsilon for Adam optimizer." ) parser.add_argument("--warmup_steps" , default=0 , type=a__ , help="Linear warmup over warmup_steps." ) parser.add_argument("--num_workers" , default=4 , type=a__ , help="kwarg passed to DataLoader" ) parser.add_argument("--num_train_epochs" , dest="max_epochs" , default=3 , type=a__ ) parser.add_argument("--train_batch_size" , default=32 , type=a__ ) parser.add_argument("--eval_batch_size" , default=32 , type=a__ ) parser.add_argument("--adafactor" , action="store_true" ) class _snake_case ( pl.Callback ): def lowerCAmelCase__ ( self , a__ , a__ ) -> str: '''simple docstring''' if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class _snake_case ( pl.Callback ): def lowerCAmelCase__ ( self , a__ , a__ ) -> Any: '''simple docstring''' for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(a__ ) class _snake_case ( pl.Callback ): def lowerCAmelCase__ ( self , a__ , a__ ) -> List[str]: '''simple docstring''' snake_case_ = trainer.lr_schedulers[0]["scheduler"] snake_case_ = {F'lr_group_{i}': lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(a__ ) def lowerCAmelCase__ ( self , a__ , a__ ) -> List[str]: '''simple docstring''' rank_zero_info("***** Validation results *****" ) snake_case_ = trainer.callback_metrics # Log results for key in sorted(a__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(a__ , str(metrics[key] ) ) ) def lowerCAmelCase__ ( self , a__ , a__ ) -> Optional[int]: '''simple docstring''' rank_zero_info("***** Test results *****" ) snake_case_ = trainer.callback_metrics # Log and save results to file snake_case_ = os.path.join(pl_module.hparams.output_dir , "test_results.txt" ) with open(a__ , "w" ) as writer: for key in sorted(a__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(a__ , str(metrics[key] ) ) ) writer.write("{} = {}\n".format(a__ , str(metrics[key] ) ) ) def UpperCamelCase_( snake_case : List[Any] , snake_case : Optional[Any] ): '''simple docstring''' parser.add_argument( "--output_dir" , default=str(Path(snake_case ).parent / "test_run" / "model_checkpoints" ) , type=snake_case , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument( "--fp16" , action="store_true" , help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit" , ) parser.add_argument( "--fp16_opt_level" , type=snake_case , default="O2" , help=( "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html" ) , ) parser.add_argument("--n_tpu_cores" , dest="tpu_cores" , type=snake_case ) parser.add_argument("--max_grad_norm" , dest="gradient_clip_val" , default=1.0 , type=snake_case , help="Max gradient norm" ) parser.add_argument("--do_train" , action="store_true" , help="Whether to run training." ) parser.add_argument("--do_predict" , action="store_true" , help="Whether to run predictions on the test set." ) parser.add_argument( "--gradient_accumulation_steps" , dest="accumulate_grad_batches" , type=snake_case , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--seed" , type=snake_case , default=4_2 , help="random seed for initialization" ) parser.add_argument( "--data_dir" , default=str(Path(snake_case ).parent / "test_run" / "dummy-train-data" ) , type=snake_case , help="The input data dir. Should contain the training files for the CoNLL-2003 NER task." , ) def UpperCamelCase_( snake_case : BaseTransformer , snake_case : argparse.Namespace , snake_case : Optional[int]=None , snake_case : int=True , snake_case : List[str]=[] , snake_case : int=None , snake_case : Any=None , **snake_case : Dict , ): '''simple docstring''' pl.seed_everything(args.seed ) # init model snake_case_ = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=snake_case ) # add custom checkpoints if checkpoint_callback is None: snake_case_ = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix="checkpoint" , monitor="val_loss" , mode="min" , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(snake_case ) if logging_callback is None: snake_case_ = LoggingCallback() snake_case_ = {} if args.fpaa: snake_case_ = 1_6 if args.gpus > 1: snake_case_ = "auto" snake_case_ = "ddp" snake_case_ = args.accumulate_grad_batches snake_case_ = None snake_case_ = "auto" snake_case_ = pl.Trainer.from_argparse_args( snake_case , weights_summary=snake_case , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=snake_case , val_check_interval=1 , num_sanity_val_steps=2 , **snake_case , ) if args.do_train: trainer.fit(snake_case ) else: print("RAG modeling tests with new set functions successfuly executed!" ) return trainer
85
__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
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """SCUT-DLVCLab/lilt-roberta-en-base""": ( """https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json""" ), } class A__ ( _lowerCamelCase): A_ : Union[str, Any] = 'lilt' def __init__( self , _SCREAMING_SNAKE_CASE=3_05_22 , _SCREAMING_SNAKE_CASE=7_68 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=30_72 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=5_12 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=1E-12 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE="absolute" , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=10_24 , **_SCREAMING_SNAKE_CASE , ): super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[Any] = vocab_size __lowerCAmelCase : List[str] = hidden_size __lowerCAmelCase : Optional[int] = num_hidden_layers __lowerCAmelCase : str = num_attention_heads __lowerCAmelCase : Tuple = hidden_act __lowerCAmelCase : Any = intermediate_size __lowerCAmelCase : List[Any] = hidden_dropout_prob __lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob __lowerCAmelCase : Any = max_position_embeddings __lowerCAmelCase : List[str] = type_vocab_size __lowerCAmelCase : str = initializer_range __lowerCAmelCase : Tuple = layer_norm_eps __lowerCAmelCase : int = position_embedding_type __lowerCAmelCase : Dict = classifier_dropout __lowerCAmelCase : str = channel_shrink_ratio __lowerCAmelCase : Dict = max_ad_position_embeddings
86
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
0
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): __A : Tuple = ["speech"] def __init__( self : List[str] , *lowercase_ : Optional[Any] , **lowercase_ : str ) -> List[str]: requires_backends(self , ["speech"] ) class snake_case_ ( metaclass=__A ): __A : Any = ["speech"] def __init__( self : Optional[Any] , *lowercase_ : Optional[Any] , **lowercase_ : int ) -> Any: requires_backends(self , ["speech"] )
87
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
0
def a__ ( A_ ): '''simple docstring''' if not all(x.isalpha() for x in string ): raise ValueError("""String must only contain alphabetic characters.""" ) __magic_name__ = sorted(string.lower() ) return len(A_ ) == len(set(A_ ) ) if __name__ == "__main__": __lowerCAmelCase : Dict = input('Enter a string ').strip() __lowerCAmelCase : Union[str, Any] = is_isogram(input_str) print(F'''{input_str} is {"an" if isogram else "not an"} isogram.''')
88
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
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class __magic_name__ ( metaclass=_UpperCamelCase ): lowerCAmelCase : str = ['note_seq'] def __init__( self : Tuple ,*_UpperCAmelCase : List[Any] ,**_UpperCAmelCase : str ): requires_backends(self ,['note_seq'] ) @classmethod def __lowercase ( cls : List[Any] ,*_UpperCAmelCase : str ,**_UpperCAmelCase : Optional[Any] ): requires_backends(cls ,['note_seq'] ) @classmethod def __lowercase ( cls : Union[str, Any] ,*_UpperCAmelCase : Dict ,**_UpperCAmelCase : Any ): requires_backends(cls ,['note_seq'] )
89
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
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __A = {"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
90
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
0
"""simple docstring""" from __future__ import annotations def _A (__a , __a ) -> list[list[int]]: """simple docstring""" SCREAMING_SNAKE_CASE_ : list[list[int]] = [] create_all_state(1 , __a , __a , [] , __a ) return result def _A (__a , __a , __a , __a , __a , ) -> None: """simple docstring""" if level == 0: total_list.append(current_list[:] ) return for i in range(__a , total_number - level + 2 ): current_list.append(__a ) create_all_state(i + 1 , __a , level - 1 , __a , __a ) current_list.pop() def _A (__a ) -> None: """simple docstring""" for i in total_list: print(*__a ) if __name__ == "__main__": UpperCAmelCase_ : Dict = 4 UpperCAmelCase_ : int = 2 UpperCAmelCase_ : str = generate_all_combinations(n, k) print_all_state(total_list)
91
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
0
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = [[1, 2, 4], [1, 2, 3, 4]] __lowerCAmelCase = DisjunctiveConstraint(_A ) self.assertTrue(isinstance(dc.token_ids , _A ) ) with self.assertRaises(_A ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(_A ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(_A ): DisjunctiveConstraint(_A ) # fails here def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = [[1, 2, 3], [1, 2, 4]] __lowerCAmelCase = DisjunctiveConstraint(_A ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) __lowerCAmelCase = stepped is True and completed is False and reset is False self.assertTrue(_A ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) __lowerCAmelCase = stepped is True and completed is False and reset is False self.assertTrue(_A ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(3 ) __lowerCAmelCase = stepped is True and completed is True and reset is False self.assertTrue(_A ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] __lowerCAmelCase = DisjunctiveConstraint(_A ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
92
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
0
'''simple docstring''' from __future__ import annotations from collections import Counter from random import random class lowerCAmelCase__ : def __init__( self ): """simple docstring""" lowercase_ : int = {} def _snake_case ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase_ : Dict = {} def _snake_case ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" if nodea not in self.connections: self.add_node(__SCREAMING_SNAKE_CASE ) if nodea not in self.connections: self.add_node(__SCREAMING_SNAKE_CASE ) lowercase_ : Optional[Any] = probability def _snake_case ( self ): """simple docstring""" return list(self.connections ) def _snake_case ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase_ : Any = 0 lowercase_ : Tuple = random() for dest in self.connections[node]: current_probability += self.connections[node][dest] if current_probability > random_value: return dest return "" def snake_case_ ( __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : list[tuple[str, str, float]] , __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : List[Any] = MarkovChainGraphUndirectedUnweighted() for nodea, nodea, probability in transitions: graph.add_transition_probability(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowercase_ : str = Counter(graph.get_nodes() ) lowercase_ : Any = start for _ in range(__SCREAMING_SNAKE_CASE ): lowercase_ : int = graph.transition(__SCREAMING_SNAKE_CASE ) visited[node] += 1 return visited if __name__ == "__main__": import doctest doctest.testmod()
93
# 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
0
import heapq def __lowerCamelCase ( UpperCAmelCase_ : dict ): """simple docstring""" a :list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(UpperCAmelCase_ , [-1 * len(UpperCAmelCase_ ), (key, value)] ) # chosen_vertices = set of chosen vertices a :Optional[Any] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices a :Tuple = heapq.heappop(UpperCAmelCase_ )[1][0] chosen_vertices.add(UpperCAmelCase_ ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: a :Optional[Any] = elem[1][1].index(UpperCAmelCase_ ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(UpperCAmelCase_ ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() snake_case : Optional[Any] = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(F"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
94
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
0
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("""9.1.0"""): UpperCAmelCase : str = { """linear""": PIL.Image.Resampling.BILINEAR, """bilinear""": PIL.Image.Resampling.BILINEAR, """bicubic""": PIL.Image.Resampling.BICUBIC, """lanczos""": PIL.Image.Resampling.LANCZOS, """nearest""": PIL.Image.Resampling.NEAREST, } else: UpperCAmelCase : Tuple = { """linear""": PIL.Image.LINEAR, """bilinear""": PIL.Image.BILINEAR, """bicubic""": PIL.Image.BICUBIC, """lanczos""": PIL.Image.LANCZOS, """nearest""": PIL.Image.NEAREST, } def _A ( SCREAMING_SNAKE_CASE : List[Any] ): """simple docstring""" a__ : Tuple =(images / 2 + 0.5).clamp(0 , 1 ) a__ : Any =images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() a__ : Optional[Any] =numpy_to_pil(SCREAMING_SNAKE_CASE ) return images def _A ( SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" if images.ndim == 3: a__ : int =images[None, ...] a__ : List[str] =(images * 255).round().astype("uint8" ) if images.shape[-1] == 1: # special case for grayscale (single channel) images a__ : Optional[int] =[Image.fromarray(image.squeeze() , mode="L" ) for image in images] else: a__ : List[Any] =[Image.fromarray(SCREAMING_SNAKE_CASE ) for image in images] return pil_images
95
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
0
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def A_ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def A_ ( self ): _lowerCamelCase : Tuple = StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) _lowerCamelCase : Any = sd_pipe.to(lowercase ) sd_pipe.set_progress_bar_config(disable=lowercase ) sd_pipe.set_scheduler('sample_euler' ) _lowerCamelCase : Dict = 'A painting of a squirrel eating a burger' _lowerCamelCase : List[Any] = torch.manual_seed(0 ) _lowerCamelCase : Dict = sd_pipe([prompt] , generator=lowercase , guidance_scale=9.0 , num_inference_steps=20 , output_type='np' ) _lowerCamelCase : Union[str, Any] = output.images _lowerCamelCase : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _lowerCamelCase : Dict = np.array([0.04_47, 0.04_92, 0.04_68, 0.04_08, 0.03_83, 0.04_08, 0.03_54, 0.03_80, 0.03_39] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def A_ ( self ): _lowerCamelCase : Any = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) _lowerCamelCase : Any = sd_pipe.to(lowercase ) sd_pipe.set_progress_bar_config(disable=lowercase ) sd_pipe.set_scheduler('sample_euler' ) _lowerCamelCase : Union[str, Any] = 'A painting of a squirrel eating a burger' _lowerCamelCase : List[str] = torch.manual_seed(0 ) _lowerCamelCase : Tuple = sd_pipe([prompt] , generator=lowercase , guidance_scale=9.0 , num_inference_steps=20 , output_type='np' ) _lowerCamelCase : Optional[int] = output.images _lowerCamelCase : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _lowerCamelCase : List[str] = np.array([0.12_37, 0.13_20, 0.14_38, 0.13_59, 0.13_90, 0.11_32, 0.12_77, 0.11_75, 0.11_12] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def A_ ( self ): _lowerCamelCase : Union[str, Any] = StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) _lowerCamelCase : int = sd_pipe.to(lowercase ) sd_pipe.set_progress_bar_config(disable=lowercase ) sd_pipe.set_scheduler('sample_dpmpp_2m' ) _lowerCamelCase : int = 'A painting of a squirrel eating a burger' _lowerCamelCase : List[str] = torch.manual_seed(0 ) _lowerCamelCase : Any = sd_pipe( [prompt] , generator=lowercase , guidance_scale=7.5 , num_inference_steps=15 , output_type='np' , use_karras_sigmas=lowercase , ) _lowerCamelCase : List[Any] = output.images _lowerCamelCase : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _lowerCamelCase : Any = np.array( [0.11_38_16_89, 0.12_11_29_21, 0.1_38_94_57, 0.12_54_96_06, 0.1_24_49_64, 0.10_83_15_17, 0.11_56_28_66, 0.10_86_78_16, 0.10_49_90_48] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
96
# 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
0
'''simple docstring''' import shutil import tempfile import unittest from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast from transformers.testing_utils import require_sentencepiece, require_torchaudio from .test_feature_extraction_clap import floats_list @require_torchaudio @require_sentencepiece class lowercase ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Dict = '''laion/clap-htsat-unfused''' UpperCamelCase__ :Optional[int] = tempfile.mkdtemp() def lowerCAmelCase__ ( self , **UpperCamelCase_ ): '''simple docstring''' return RobertaTokenizer.from_pretrained(self.checkpoint , **UpperCamelCase_ ) def lowerCAmelCase__ ( self , **UpperCamelCase_ ): '''simple docstring''' return ClapFeatureExtractor.from_pretrained(self.checkpoint , **UpperCamelCase_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = self.get_tokenizer() UpperCamelCase__ :Tuple = self.get_feature_extractor() UpperCamelCase__ :Union[str, Any] = ClapProcessor(tokenizer=UpperCamelCase_ , feature_extractor=UpperCamelCase_ ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase__ :Optional[int] = ClapProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , UpperCamelCase_ ) self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , UpperCamelCase_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Union[str, Any] = ClapProcessor(tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase__ :Union[str, Any] = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) UpperCamelCase__ :Union[str, Any] = self.get_feature_extractor(do_normalize=UpperCamelCase_ , padding_value=1.0 ) UpperCamelCase__ :int = ClapProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=UpperCamelCase_ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , UpperCamelCase_ ) self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.feature_extractor , UpperCamelCase_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = self.get_feature_extractor() UpperCamelCase__ :int = self.get_tokenizer() UpperCamelCase__ :List[Any] = ClapProcessor(tokenizer=UpperCamelCase_ , feature_extractor=UpperCamelCase_ ) UpperCamelCase__ :Any = floats_list((3, 1000) ) UpperCamelCase__ :Union[str, Any] = feature_extractor(UpperCamelCase_ , return_tensors='''np''' ) UpperCamelCase__ :Dict = processor(audios=UpperCamelCase_ , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :List[Any] = self.get_feature_extractor() UpperCamelCase__ :Optional[int] = self.get_tokenizer() UpperCamelCase__ :Tuple = ClapProcessor(tokenizer=UpperCamelCase_ , feature_extractor=UpperCamelCase_ ) UpperCamelCase__ :List[Any] = '''This is a test string''' UpperCamelCase__ :int = processor(text=UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = tokenizer(UpperCamelCase_ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Tuple = self.get_feature_extractor() UpperCamelCase__ :List[str] = self.get_tokenizer() UpperCamelCase__ :int = ClapProcessor(tokenizer=UpperCamelCase_ , feature_extractor=UpperCamelCase_ ) UpperCamelCase__ :int = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCamelCase__ :str = processor.batch_decode(UpperCamelCase_ ) UpperCamelCase__ :List[Any] = tokenizer.batch_decode(UpperCamelCase_ ) self.assertListEqual(UpperCamelCase_ , UpperCamelCase_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Tuple = self.get_feature_extractor() UpperCamelCase__ :List[str] = self.get_tokenizer() UpperCamelCase__ :Tuple = ClapProcessor(tokenizer=UpperCamelCase_ , feature_extractor=UpperCamelCase_ ) self.assertListEqual( processor.model_input_names[2:] , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , )
97
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
0
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType lowerCAmelCase__ : int = logging.get_logger(__name__) lowerCAmelCase__ : Tuple = { 'openai/whisper-base': 'https://huggingface.co/openai/whisper-base/resolve/main/config.json', } # fmt: off lowerCAmelCase__ : str = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 357, 366, 438, 532, 685, 705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377, 1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211, 4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786, 11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791, 17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409, 34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361 ] lowerCAmelCase__ : Optional[Any] = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 359, 503, 522, 542, 873, 893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627, 3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647, 7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793, 14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675, 22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865, 42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362 ] class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = "whisper" snake_case__ = ["past_key_values"] snake_case__ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : str ,lowerCamelCase__ : Tuple=51_865 ,lowerCamelCase__ : Optional[Any]=80 ,lowerCamelCase__ : Tuple=6 ,lowerCamelCase__ : Optional[Any]=4 ,lowerCamelCase__ : int=6 ,lowerCamelCase__ : int=4 ,lowerCamelCase__ : Union[str, Any]=1_536 ,lowerCamelCase__ : Optional[int]=1_536 ,lowerCamelCase__ : str=0.0 ,lowerCamelCase__ : List[Any]=0.0 ,lowerCamelCase__ : Dict=50_257 ,lowerCamelCase__ : Union[str, Any]=True ,lowerCamelCase__ : Dict=True ,lowerCamelCase__ : List[str]="gelu" ,lowerCamelCase__ : Dict=256 ,lowerCamelCase__ : Optional[Any]=0.0 ,lowerCamelCase__ : List[str]=0.0 ,lowerCamelCase__ : List[Any]=0.0 ,lowerCamelCase__ : Union[str, Any]=0.0_2 ,lowerCamelCase__ : Optional[Any]=False ,lowerCamelCase__ : Optional[Any]=1_500 ,lowerCamelCase__ : Union[str, Any]=448 ,lowerCamelCase__ : Tuple=50_256 ,lowerCamelCase__ : List[str]=50_256 ,lowerCamelCase__ : List[Any]=50_256 ,lowerCamelCase__ : Tuple=None ,lowerCamelCase__ : Tuple=[220, 50_256] ,lowerCamelCase__ : List[Any]=False ,lowerCamelCase__ : Dict=256 ,lowerCamelCase__ : List[str]=False ,lowerCamelCase__ : Tuple=0.0_5 ,lowerCamelCase__ : Tuple=10 ,lowerCamelCase__ : str=2 ,lowerCamelCase__ : List[Any]=0.0 ,lowerCamelCase__ : int=10 ,lowerCamelCase__ : Any=0 ,lowerCamelCase__ : Optional[Any]=7 ,**lowerCamelCase__ : Dict ,): UpperCAmelCase__ = vocab_size UpperCAmelCase__ = num_mel_bins UpperCAmelCase__ = d_model UpperCAmelCase__ = encoder_layers UpperCAmelCase__ = encoder_attention_heads UpperCAmelCase__ = decoder_layers UpperCAmelCase__ = decoder_attention_heads UpperCAmelCase__ = decoder_ffn_dim UpperCAmelCase__ = encoder_ffn_dim UpperCAmelCase__ = dropout UpperCAmelCase__ = attention_dropout UpperCAmelCase__ = activation_dropout UpperCAmelCase__ = activation_function UpperCAmelCase__ = init_std UpperCAmelCase__ = encoder_layerdrop UpperCAmelCase__ = decoder_layerdrop UpperCAmelCase__ = use_cache UpperCAmelCase__ = encoder_layers UpperCAmelCase__ = scale_embedding # scale factor will be sqrt(d_model) if True UpperCAmelCase__ = max_source_positions UpperCAmelCase__ = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. UpperCAmelCase__ = classifier_proj_size UpperCAmelCase__ = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase__ = apply_spec_augment UpperCAmelCase__ = mask_time_prob UpperCAmelCase__ = mask_time_length UpperCAmelCase__ = mask_time_min_masks UpperCAmelCase__ = mask_feature_prob UpperCAmelCase__ = mask_feature_length UpperCAmelCase__ = mask_feature_min_masks UpperCAmelCase__ = median_filter_width super().__init__( pad_token_id=lowerCamelCase__ ,bos_token_id=lowerCamelCase__ ,eos_token_id=lowerCamelCase__ ,is_encoder_decoder=lowerCamelCase__ ,decoder_start_token_id=lowerCamelCase__ ,suppress_tokens=lowerCamelCase__ ,begin_suppress_tokens=lowerCamelCase__ ,**lowerCamelCase__ ,) class snake_case ( __UpperCAmelCase ): """simple docstring""" @property def __lowerCAmelCase ( self : Any ): UpperCAmelCase__ = OrderedDict( [ ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}), ] ) if self.use_past: UpperCAmelCase__ = {0: 'batch'} else: UpperCAmelCase__ = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase__ ,direction='inputs' ) return common_inputs def __lowerCAmelCase ( self : Any ,lowerCamelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] ,lowerCamelCase__ : int = -1 ,lowerCamelCase__ : int = -1 ,lowerCamelCase__ : bool = False ,lowerCamelCase__ : Optional["TensorType"] = None ,lowerCamelCase__ : int = 22_050 ,lowerCamelCase__ : float = 5.0 ,lowerCamelCase__ : int = 220 ,): UpperCAmelCase__ = OrderedDict() UpperCAmelCase__ = OnnxConfig.generate_dummy_inputs( self ,preprocessor=preprocessor.feature_extractor ,batch_size=lowerCamelCase__ ,framework=lowerCamelCase__ ,sampling_rate=lowerCamelCase__ ,time_duration=lowerCamelCase__ ,frequency=lowerCamelCase__ ,) UpperCAmelCase__ = encoder_inputs['input_features'].shape[2] UpperCAmelCase__ = encoder_sequence_length // 2 if self.use_past else seq_length UpperCAmelCase__ = super().generate_dummy_inputs( preprocessor.tokenizer ,lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ) UpperCAmelCase__ = encoder_inputs.pop('input_features' ) UpperCAmelCase__ = decoder_inputs.pop('decoder_input_ids' ) if "past_key_values" in decoder_inputs: UpperCAmelCase__ = decoder_inputs.pop('past_key_values' ) return dummy_inputs @property def __lowerCAmelCase ( self : str ): return 1e-3
98
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
0
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class A__ : """simple docstring""" @staticmethod def __lowercase ( *lowercase , **lowercase) -> Optional[int]: '''simple docstring''' pass @is_pipeline_test @require_torch @require_vision class A__ ( unittest.TestCase ): """simple docstring""" __A : str = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def __lowercase ( self , lowercase , lowercase , lowercase) -> Union[str, Any]: '''simple docstring''' a__ : str = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa') a__ : int = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def __lowercase ( self , lowercase , lowercase) -> Dict: '''simple docstring''' a__ : str = vqa_pipeline(lowercase , top_k=1) self.assertEqual( lowercase , [ [{'score': ANY(lowercase), 'answer': ANY(lowercase)}], [{'score': ANY(lowercase), 'answer': ANY(lowercase)}], ] , ) @require_torch def __lowercase ( self) -> str: '''simple docstring''' a__ : Tuple = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa') a__ : str = './tests/fixtures/tests_samples/COCO/000000039769.png' a__ : Tuple = 'How many cats are there?' a__ : int = vqa_pipeline(image=lowercase , question='How many cats are there?' , top_k=2) self.assertEqual( lowercase , [{'score': ANY(lowercase), 'answer': ANY(lowercase)}, {'score': ANY(lowercase), 'answer': ANY(lowercase)}]) a__ : List[Any] = vqa_pipeline({'image': image, 'question': question} , top_k=2) self.assertEqual( lowercase , [{'score': ANY(lowercase), 'answer': ANY(lowercase)}, {'score': ANY(lowercase), 'answer': ANY(lowercase)}]) @slow @require_torch def __lowercase ( self) -> Optional[int]: '''simple docstring''' a__ : Union[str, Any] = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa') a__ : Optional[Any] = './tests/fixtures/tests_samples/COCO/000000039769.png' a__ : Dict = 'How many cats are there?' a__ : Any = vqa_pipeline(image=lowercase , question=lowercase , top_k=2) self.assertEqual( nested_simplify(lowercase , decimals=4) , [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]) a__ : Union[str, Any] = vqa_pipeline({'image': image, 'question': question} , top_k=2) self.assertEqual( nested_simplify(lowercase , decimals=4) , [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]) a__ : Optional[int] = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2) self.assertEqual( nested_simplify(lowercase , decimals=4) , [[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF') def __lowercase ( self) -> Dict: '''simple docstring''' pass
99
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
0
"""simple docstring""" import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() __magic_name__ = logging.get_logger(__name__) set_seed(770) __magic_name__ = { "c_attn": "att_proj", "c_proj": "out_proj", "c_fc": "in_proj", "transformer.": "", "h.": "layers.", "ln_1": "layernorm_1", "ln_2": "layernorm_2", "ln_f": "layernorm_final", "wpe": "position_embeds_layer", "wte": "input_embeds_layer", } __magic_name__ = { "text_small": { "repo_id": "suno/bark", "file_name": "text.pt", }, "coarse_small": { "repo_id": "suno/bark", "file_name": "coarse.pt", }, "fine_small": { "repo_id": "suno/bark", "file_name": "fine.pt", }, "text": { "repo_id": "suno/bark", "file_name": "text_2.pt", }, "coarse": { "repo_id": "suno/bark", "file_name": "coarse_2.pt", }, "fine": { "repo_id": "suno/bark", "file_name": "fine_2.pt", }, } __magic_name__ = os.path.dirname(os.path.abspath(__file__)) __magic_name__ = os.path.join(os.path.expanduser("~"), ".cache") __magic_name__ = os.path.join(os.getenv("XDG_CACHE_HOME", default_cache_dir), "suno", "bark_v0") def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_=False ): __SCREAMING_SNAKE_CASE = model_type if use_small: key += "_small" return os.path.join(UpperCamelCase_ , REMOTE_MODEL_PATHS[key]["""file_name"""] ) def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): os.makedirs(UpperCamelCase_ , exist_ok=UpperCamelCase_ ) hf_hub_download(repo_id=UpperCamelCase_ , filename=UpperCamelCase_ , local_dir=UpperCamelCase_ ) def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_=False , UpperCamelCase_="text" ): if model_type == "text": __SCREAMING_SNAKE_CASE = BarkSemanticModel __SCREAMING_SNAKE_CASE = BarkSemanticConfig __SCREAMING_SNAKE_CASE = BarkSemanticGenerationConfig elif model_type == "coarse": __SCREAMING_SNAKE_CASE = BarkCoarseModel __SCREAMING_SNAKE_CASE = BarkCoarseConfig __SCREAMING_SNAKE_CASE = BarkCoarseGenerationConfig elif model_type == "fine": __SCREAMING_SNAKE_CASE = BarkFineModel __SCREAMING_SNAKE_CASE = BarkFineConfig __SCREAMING_SNAKE_CASE = BarkFineGenerationConfig else: raise NotImplementedError() __SCREAMING_SNAKE_CASE = f"{model_type}_small" if use_small else model_type __SCREAMING_SNAKE_CASE = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(UpperCamelCase_ ): logger.info(f"{model_type} model not found, downloading into `{CACHE_DIR}`." ) _download(model_info["""repo_id"""] , model_info["""file_name"""] ) __SCREAMING_SNAKE_CASE = torch.load(UpperCamelCase_ , map_location=UpperCamelCase_ ) # this is a hack __SCREAMING_SNAKE_CASE = checkpoint["""model_args"""] if "input_vocab_size" not in model_args: __SCREAMING_SNAKE_CASE = model_args["""vocab_size"""] __SCREAMING_SNAKE_CASE = model_args["""vocab_size"""] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments __SCREAMING_SNAKE_CASE = model_args.pop("""n_head""" ) __SCREAMING_SNAKE_CASE = model_args.pop("""n_embd""" ) __SCREAMING_SNAKE_CASE = model_args.pop("""n_layer""" ) __SCREAMING_SNAKE_CASE = ConfigClass(**checkpoint["""model_args"""] ) __SCREAMING_SNAKE_CASE = ModelClass(config=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = GenerationConfigClass() __SCREAMING_SNAKE_CASE = model_generation_config __SCREAMING_SNAKE_CASE = checkpoint["""model"""] # fixup checkpoint __SCREAMING_SNAKE_CASE = """_orig_mod.""" for k, v in list(state_dict.items() ): if k.startswith(UpperCamelCase_ ): # replace part of the key with corresponding layer name in HF implementation __SCREAMING_SNAKE_CASE = k[len(UpperCamelCase_ ) :] for old_layer_name in new_layer_name_dict: __SCREAMING_SNAKE_CASE = new_k.replace(UpperCamelCase_ , new_layer_name_dict[old_layer_name] ) __SCREAMING_SNAKE_CASE = state_dict.pop(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = set(state_dict.keys() ) - set(model.state_dict().keys() ) __SCREAMING_SNAKE_CASE = {k for k in extra_keys if not k.endswith(""".attn.bias""" )} __SCREAMING_SNAKE_CASE = set(model.state_dict().keys() ) - set(state_dict.keys() ) __SCREAMING_SNAKE_CASE = {k for k in missing_keys if not k.endswith(""".attn.bias""" )} if len(UpperCamelCase_ ) != 0: raise ValueError(f"extra keys found: {extra_keys}" ) if len(UpperCamelCase_ ) != 0: raise ValueError(f"missing keys: {missing_keys}" ) model.load_state_dict(UpperCamelCase_ , strict=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = model.num_parameters(exclude_embeddings=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = checkpoint["""best_val_loss"""].item() logger.info(f"model loaded: {round(n_params/1e6 , 1 )}M params, {round(UpperCamelCase_ , 3 )} loss" ) model.eval() model.to(UpperCamelCase_ ) del checkpoint, state_dict return model def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_=False , UpperCamelCase_="text" ): if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() __SCREAMING_SNAKE_CASE = """cpu""" # do conversion on cpu __SCREAMING_SNAKE_CASE = _get_ckpt_path(UpperCamelCase_ , use_small=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = _load_model(UpperCamelCase_ , UpperCamelCase_ , model_type=UpperCamelCase_ , use_small=UpperCamelCase_ ) # load bark initial model __SCREAMING_SNAKE_CASE = _bark_load_model(UpperCamelCase_ , """cpu""" , model_type=UpperCamelCase_ , use_small=UpperCamelCase_ ) if model_type == "text": __SCREAMING_SNAKE_CASE = bark_model["""model"""] if model.num_parameters(exclude_embeddings=UpperCamelCase_ ) != bark_model.get_num_params(): raise ValueError("""initial and new models don't have the same number of parameters""" ) # check if same output as the bark model __SCREAMING_SNAKE_CASE = 5 __SCREAMING_SNAKE_CASE = 10 if model_type in ["text", "coarse"]: __SCREAMING_SNAKE_CASE = torch.randint(256 , (batch_size, sequence_length) , dtype=torch.int ) __SCREAMING_SNAKE_CASE = bark_model(UpperCamelCase_ )[0] __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ ) # take last logits __SCREAMING_SNAKE_CASE = output_new_model_total.logits[:, [-1], :] else: __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = torch.randint(256 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) __SCREAMING_SNAKE_CASE = model(UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = bark_model(UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError("""initial and new outputs don't have the same shape""" ) if (output_new_model - output_old_model).abs().max().item() > 1e-3: raise ValueError("""initial and new outputs are not equal""" ) Path(UpperCamelCase_ ).mkdir(exist_ok=UpperCamelCase_ ) model.save_pretrained(UpperCamelCase_ ) def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , ): __SCREAMING_SNAKE_CASE = os.path.join(UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = BarkSemanticConfig.from_pretrained(os.path.join(UpperCamelCase_ , """config.json""" ) ) __SCREAMING_SNAKE_CASE = BarkCoarseConfig.from_pretrained(os.path.join(UpperCamelCase_ , """config.json""" ) ) __SCREAMING_SNAKE_CASE = BarkFineConfig.from_pretrained(os.path.join(UpperCamelCase_ , """config.json""" ) ) __SCREAMING_SNAKE_CASE = EncodecConfig.from_pretrained("""facebook/encodec_24khz""" ) __SCREAMING_SNAKE_CASE = BarkSemanticModel.from_pretrained(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = BarkCoarseModel.from_pretrained(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = BarkFineModel.from_pretrained(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = EncodecModel.from_pretrained("""facebook/encodec_24khz""" ) __SCREAMING_SNAKE_CASE = BarkConfig.from_sub_model_configs( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) __SCREAMING_SNAKE_CASE = BarkModel(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = semantic __SCREAMING_SNAKE_CASE = coarseAcoustic __SCREAMING_SNAKE_CASE = fineAcoustic __SCREAMING_SNAKE_CASE = codec __SCREAMING_SNAKE_CASE = bark_generation_config Path(UpperCamelCase_ ).mkdir(exist_ok=UpperCamelCase_ ) bark.save_pretrained(UpperCamelCase_ , repo_id=UpperCamelCase_ , push_to_hub=UpperCamelCase_ ) if __name__ == "__main__": __magic_name__ = argparse.ArgumentParser() # Required parameters parser.add_argument("model_type", type=str, help="text, coarse or fine.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--is_small", action="store_true", help="convert the small version instead of the large.") __magic_name__ = parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
100
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
0