code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import datasets from .evaluate import evaluate __UpperCamelCase : List[Any] = '\\n@inproceedings{Rajpurkar2016SQuAD10,\n title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n booktitle={EMNLP},\n year={2016}\n}\n' __UpperCamelCase : str = '\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n' __UpperCamelCase : Union[str, Any] = '\nComputes SQuAD scores (F1 and EM).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': the text of the answer\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the SQuAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\nExamples:\n\n >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}]\n >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]\n >>> squad_metric = datasets.load_metric("squad")\n >>> results = squad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowercase__ ( datasets.Metric): def __A ( self : str ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': {'''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.Value('''string''' )}, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , ) def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} SCREAMING_SNAKE_CASE : Optional[int] = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] SCREAMING_SNAKE_CASE : Optional[Any] = evaluate(dataset=UpperCamelCase__ , predictions=UpperCamelCase__ ) return score
34
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
1
def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = int(_lowercase ) if n_element < 1: SCREAMING_SNAKE_CASE : Dict = ValueError('''a should be a positive number''' ) raise my_error SCREAMING_SNAKE_CASE : Optional[int] = [1] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = (0, 0, 0) SCREAMING_SNAKE_CASE : int = 1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": __UpperCamelCase : List[str] = input('Enter the last number (nth term) of the Hamming Number Series: ') print('Formula of Hamming Number Series => 2^i * 3^j * 5^k') __UpperCamelCase : Dict = hamming(int(n)) print('-----------------------------------------------------') print(f"""The list with nth numbers is: {hamming_numbers}""") print('-----------------------------------------------------')
34
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
1
from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class lowercase__ ( UpperCamelCase_): def __A ( self : Optional[int] , UpperCamelCase__ : float ): '''simple docstring''' return 0.0 def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) SCREAMING_SNAKE_CASE : Dict = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = 512 SCREAMING_SNAKE_CASE : Dict = [1] + [0] * (size - 1) SCREAMING_SNAKE_CASE : Dict = [filter_type.process(_lowercase ) for item in inputs] SCREAMING_SNAKE_CASE : List[Any] = [0] * (samplerate - size) # zero-padding outputs += filler SCREAMING_SNAKE_CASE : Optional[int] = np.abs(np.fft.fft(_lowercase ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = 20 * np.logaa(_lowercase ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('''Frequency (Hz)''' ) plt.xscale('''log''' ) # Display within reasonable bounds SCREAMING_SNAKE_CASE : str = get_bounds(_lowercase , _lowercase ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel('''Gain (dB)''' ) plt.plot(_lowercase ) plt.show() def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : List[str] = 512 SCREAMING_SNAKE_CASE : Any = [1] + [0] * (size - 1) SCREAMING_SNAKE_CASE : Optional[Any] = [filter_type.process(_lowercase ) for item in inputs] SCREAMING_SNAKE_CASE : Any = [0] * (samplerate - size) # zero-padding outputs += filler SCREAMING_SNAKE_CASE : Union[str, Any] = np.angle(np.fft.fft(_lowercase ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('''Frequency (Hz)''' ) plt.xscale('''log''' ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel('''Phase shift (Radians)''' ) plt.plot(np.unwrap(_lowercase , -2 * pi ) ) plt.show()
34
import random def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowercase ) elif element > pivot: greater.append(_lowercase ) else: equal.append(_lowercase ) return less, equal, greater def A ( _lowercase , _lowercase ): # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(_lowercase ) or index < 0: return None SCREAMING_SNAKE_CASE : Dict = items[random.randint(0 , len(_lowercase ) - 1 )] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = _partition(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = len(_lowercase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowercase , _lowercase ) # must be in larger else: return quick_select(_lowercase , index - (m + count) )
34
1
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = DebertaTokenizer UpperCamelCase_ = True UpperCamelCase_ = DebertaTokenizerFast def __A ( self : List[Any] ): '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE : Any = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] SCREAMING_SNAKE_CASE : str = dict(zip(UpperCamelCase__ , range(len(UpperCamelCase__ ) ) ) ) SCREAMING_SNAKE_CASE : List[str] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] SCREAMING_SNAKE_CASE : Optional[Any] = {'''unk_token''': '''[UNK]'''} SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(UpperCamelCase__ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(UpperCamelCase__ ) ) def __A ( self : Any , **UpperCamelCase__ : str ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = '''lower newer''' SCREAMING_SNAKE_CASE : int = '''lower newer''' return input_text, output_text def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Dict = '''lower newer''' SCREAMING_SNAKE_CASE : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] SCREAMING_SNAKE_CASE : List[str] = tokenizer.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE : Optional[Any] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , UpperCamelCase__ ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer('''Hello''' , '''World''' ) SCREAMING_SNAKE_CASE : Dict = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , UpperCamelCase__ ) @slow def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ , UpperCamelCase__ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: SCREAMING_SNAKE_CASE : List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] SCREAMING_SNAKE_CASE : str = tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = [tokenizer.decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) for seq in encoding['''input_ids''']] # fmt: off SCREAMING_SNAKE_CASE : List[str] = { '''input_ids''': [ [1, 2118, 1_1126, 565, 35, 83, 2_5191, 163, 1_8854, 13, 1_2156, 12, 1_6101, 2_5376, 1_3807, 9, 2_2205, 2_7893, 1635, 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], [1, 2118, 1_1126, 565, 2_4536, 80, 4_3797, 4878, 7373, 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], [1, 133, 78, 65, 16, 10, 3724, 1538, 3_3183, 1_1303, 4_3797, 1938, 4, 870, 2_4165, 2_9105, 5, 739, 3_2644, 3_3183, 1_1303, 3_6173, 88, 80, 650, 7821, 4_5940, 6, 52, 2559, 5, 1836, 9, 5, 7397, 1_3171, 31, 5, 1836, 9, 3_2644, 3_3183, 1_1303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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] ] } # fmt: on SCREAMING_SNAKE_CASE : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , UpperCamelCase__ ) for expected, decoded in zip(UpperCamelCase__ , UpperCamelCase__ ): self.assertEqual(UpperCamelCase__ , UpperCamelCase__ )
34
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Tuple = logging.get_logger(__name__) # TODO Update this __UpperCamelCase : List[str] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """esm""" def __init__( self : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Any=768 , UpperCamelCase__ : Optional[Any]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : Optional[int]=3072 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1026 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Any=1E-12 , UpperCamelCase__ : Dict="absolute" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[int]=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Optional[Any]=None , **UpperCamelCase__ : Any , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : Dict = emb_layer_norm_before SCREAMING_SNAKE_CASE : List[str] = token_dropout SCREAMING_SNAKE_CASE : List[Any] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) SCREAMING_SNAKE_CASE : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = EsmFoldConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) SCREAMING_SNAKE_CASE : Optional[int] = get_default_vocab_list() else: SCREAMING_SNAKE_CASE : Optional[Any] = vocab_list else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = self.esmfold_config.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = None UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 0 UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Optional[int] ): '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE : Optional[Any] = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = TrunkConfig(**self.trunk ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = asdict(self ) SCREAMING_SNAKE_CASE : Tuple = self.trunk.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 48 UpperCamelCase_ = 1_024 UpperCamelCase_ = 128 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 0 UpperCamelCase_ = 0 UpperCamelCase_ = False UpperCamelCase_ = 4 UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Any ): '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE : Optional[int] = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) SCREAMING_SNAKE_CASE : Dict = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE : Tuple = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = asdict(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.structure_module.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 384 UpperCamelCase_ = 128 UpperCamelCase_ = 16 UpperCamelCase_ = 128 UpperCamelCase_ = 12 UpperCamelCase_ = 4 UpperCamelCase_ = 8 UpperCamelCase_ = 0.1 UpperCamelCase_ = 8 UpperCamelCase_ = 1 UpperCamelCase_ = 2 UpperCamelCase_ = 7 UpperCamelCase_ = 10 UpperCamelCase_ = 1E-8 UpperCamelCase_ = 1E5 def __A ( self : Dict ): '''simple docstring''' return asdict(self ) def A ( ): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
34
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : Any = { 'tiiuae/falcon-40b': 'https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json', 'tiiuae/falcon-7b': 'https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """falcon""" UpperCamelCase_ = ["""past_key_values"""] def __init__( self : Optional[int] , UpperCamelCase__ : List[Any]=6_5024 , UpperCamelCase__ : Tuple=4544 , UpperCamelCase__ : Any=32 , UpperCamelCase__ : Optional[int]=71 , UpperCamelCase__ : int=1E-5 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : str=0.0 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : int=None , UpperCamelCase__ : Union[str, Any]=False , UpperCamelCase__ : int=False , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Optional[Any]=False , UpperCamelCase__ : Dict=11 , UpperCamelCase__ : Optional[int]=11 , **UpperCamelCase__ : str , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = vocab_size # Backward compatibility with n_embed kwarg SCREAMING_SNAKE_CASE : Optional[int] = kwargs.pop('''n_embed''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = hidden_size if n_embed is None else n_embed SCREAMING_SNAKE_CASE : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = layer_norm_epsilon SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : int = use_cache SCREAMING_SNAKE_CASE : List[str] = hidden_dropout SCREAMING_SNAKE_CASE : Any = attention_dropout SCREAMING_SNAKE_CASE : int = bos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = eos_token_id SCREAMING_SNAKE_CASE : Any = num_attention_heads if num_kv_heads is None else num_kv_heads SCREAMING_SNAKE_CASE : int = alibi SCREAMING_SNAKE_CASE : Optional[Any] = new_decoder_architecture SCREAMING_SNAKE_CASE : Any = multi_query # Ignored when new_decoder_architecture is True SCREAMING_SNAKE_CASE : Tuple = parallel_attn SCREAMING_SNAKE_CASE : List[Any] = bias super().__init__(bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) @property def __A ( self : int ): '''simple docstring''' return self.hidden_size // self.num_attention_heads @property def __A ( self : List[str] ): '''simple docstring''' return not self.alibi
34
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowercase__ ( unittest.TestCase): def __A ( self : Any ): '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=UpperCamelCase__ , ) assert hasattr(self , '''env''' ) def __A ( self : str , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings SCREAMING_SNAKE_CASE : Any = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=UpperCamelCase__ , instance_count=UpperCamelCase__ , instance_type=self.instance_type , debugger_hook_config=UpperCamelCase__ , hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=UpperCamelCase__ , py_version='''py36''' , ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' TrainingJobAnalytics(UpperCamelCase__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def __A ( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.create_estimator(UpperCamelCase__ ) # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[Any] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : List[str] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : List[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_9999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , UpperCamelCase__ )
34
1
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 ( _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = [] SCREAMING_SNAKE_CASE : int = [] SCREAMING_SNAKE_CASE : str = [] for rt in rc.restypes: SCREAMING_SNAKE_CASE : str = 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] ) SCREAMING_SNAKE_CASE : List[Any] = {name: i for i, name in enumerate(_lowercase )} 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 ) SCREAMING_SNAKE_CASE : Dict = torch.tensor( _lowercase , dtype=torch.intaa , device=protein['''aatype'''].device , ) SCREAMING_SNAKE_CASE : int = torch.tensor( _lowercase , dtype=torch.intaa , device=protein['''aatype'''].device , ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor( _lowercase , dtype=torch.floataa , device=protein['''aatype'''].device , ) SCREAMING_SNAKE_CASE : Optional[int] = 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 SCREAMING_SNAKE_CASE : Optional[int] = restype_atomaa_to_atomaa[protein_aatype] SCREAMING_SNAKE_CASE : Optional[int] = restype_atomaa_mask[protein_aatype] SCREAMING_SNAKE_CASE : List[Any] = residx_atomaa_mask SCREAMING_SNAKE_CASE : Optional[int] = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back SCREAMING_SNAKE_CASE : List[str] = restype_atomaa_to_atomaa[protein_aatype] SCREAMING_SNAKE_CASE : Union[str, Any] = residx_atomaa_to_atomaa.long() # create the corresponding mask SCREAMING_SNAKE_CASE : Optional[Any] = torch.zeros([21, 37] , dtype=torch.floataa , device=protein['''aatype'''].device ) for restype, restype_letter in enumerate(rc.restypes ): SCREAMING_SNAKE_CASE : str = rc.restype_atoa[restype_letter] SCREAMING_SNAKE_CASE : Optional[Any] = rc.residue_atoms[restype_name] for atom_name in atom_names: SCREAMING_SNAKE_CASE : Union[str, Any] = rc.atom_order[atom_name] SCREAMING_SNAKE_CASE : Optional[int] = 1 SCREAMING_SNAKE_CASE : Optional[int] = restype_atomaa_mask[protein_aatype] SCREAMING_SNAKE_CASE : Union[str, Any] = residx_atomaa_mask return protein def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = tree_map(lambda _lowercase : torch.tensor(_lowercase , device=batch['''aatype'''].device ) , _lowercase , np.ndarray ) SCREAMING_SNAKE_CASE : Optional[int] = tensor_tree_map(lambda _lowercase : np.array(_lowercase ) , make_atomaa_masks(_lowercase ) ) return out
34
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore __UpperCamelCase : Dict = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" __UpperCamelCase : Tuple = [file for file in filepaths if file != file.lower()] if upper_files: print(f"""{len(upper_files)} files contain uppercase characters:""") print('\n'.join(upper_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if ' ' in file] if space_files: print(f"""{len(space_files)} files contain space characters:""") print('\n'.join(space_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if '-' in file] if hyphen_files: print(f"""{len(hyphen_files)} files contain hyphen characters:""") print('\n'.join(hyphen_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(f"""{len(nodir_files)} files are not in a directory:""") print('\n'.join(nodir_files) + '\n') __UpperCamelCase : Optional[Any] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
34
1
from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class lowercase__ : def __init__( self : List[str] , UpperCamelCase__ : str , UpperCamelCase__ : int=13 , UpperCamelCase__ : List[Any]=7 , UpperCamelCase__ : Union[str, Any]=True , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : int=True , UpperCamelCase__ : int=True , UpperCamelCase__ : Dict=99 , UpperCamelCase__ : Dict=32 , UpperCamelCase__ : Union[str, Any]=2 , UpperCamelCase__ : Union[str, Any]=4 , UpperCamelCase__ : Union[str, Any]=37 , UpperCamelCase__ : str="gelu" , UpperCamelCase__ : Union[str, Any]=0.1 , UpperCamelCase__ : Tuple=0.1 , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : List[str]=16 , UpperCamelCase__ : int=2 , UpperCamelCase__ : Optional[int]=0.02 , UpperCamelCase__ : Tuple=3 , UpperCamelCase__ : List[Any]=4 , UpperCamelCase__ : List[Any]=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = parent SCREAMING_SNAKE_CASE : List[str] = 13 SCREAMING_SNAKE_CASE : str = 7 SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : str = 99 SCREAMING_SNAKE_CASE : int = 384 SCREAMING_SNAKE_CASE : Optional[int] = 2 SCREAMING_SNAKE_CASE : str = 4 SCREAMING_SNAKE_CASE : List[Any] = 37 SCREAMING_SNAKE_CASE : List[Any] = '''gelu''' SCREAMING_SNAKE_CASE : Any = 0.1 SCREAMING_SNAKE_CASE : Union[str, Any] = 0.1 SCREAMING_SNAKE_CASE : List[str] = 512 SCREAMING_SNAKE_CASE : str = 16 SCREAMING_SNAKE_CASE : str = 2 SCREAMING_SNAKE_CASE : Union[str, Any] = 0.02 SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : Dict = 4 SCREAMING_SNAKE_CASE : Optional[Any] = 128 SCREAMING_SNAKE_CASE : Optional[Any] = 2 SCREAMING_SNAKE_CASE : Optional[int] = 9 SCREAMING_SNAKE_CASE : int = 1 SCREAMING_SNAKE_CASE : str = None def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Union[str, Any] = None SCREAMING_SNAKE_CASE : Any = None if self.use_labels: SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : int = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=UpperCamelCase__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : Any , UpperCamelCase__ : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[Any] , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TFConvBertModel(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} SCREAMING_SNAKE_CASE : Optional[int] = [input_ids, input_mask] SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = TFConvBertForMaskedLM(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : List[str] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.num_labels SCREAMING_SNAKE_CASE : Any = TFConvBertForSequenceClassification(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : int = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[int] = TFConvBertForMultipleChoice(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Optional[int] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Union[str, Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str , UpperCamelCase__ : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.num_labels SCREAMING_SNAKE_CASE : int = TFConvBertForTokenClassification(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : str = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self : str , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = TFConvBertForQuestionAnswering(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : Any = config_and_inputs SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase_ = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = TFConvBertModelTester(self ) SCREAMING_SNAKE_CASE : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=37 ) def __A ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__ ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__ ) def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__ ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__ ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__ ) @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Any = True if hasattr(UpperCamelCase__ , '''use_cache''' ): SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : int = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Optional[int] = getattr(self.model_tester , '''key_length''' , UpperCamelCase__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Any = self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = len(model(UpperCamelCase__ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(UpperCamelCase__ , saved_model=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(UpperCamelCase__ , '''saved_model''' , '''1''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = tf.keras.models.load_model(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = model(UpperCamelCase__ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Optional[Any] = outputs['''encoder_hidden_states'''] SCREAMING_SNAKE_CASE : str = outputs['''encoder_attentions'''] else: SCREAMING_SNAKE_CASE : str = outputs['''hidden_states'''] SCREAMING_SNAKE_CASE : List[str] = outputs['''attentions'''] self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) self.assertIsNotNone(UpperCamelCase__ ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : str = getattr(self.model_tester , '''decoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Optional[int] = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Union[str, Any] = getattr(self.model_tester , '''key_length''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = getattr(self.model_tester , '''key_length''' , UpperCamelCase__ ) def check_decoder_attentions_output(UpperCamelCase__ : str ): SCREAMING_SNAKE_CASE : str = len(UpperCamelCase__ ) self.assertEqual(out_len % 2 , 0 ) SCREAMING_SNAKE_CASE : int = outputs.decoder_attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(UpperCamelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE : Any = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[int] = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = model(self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[int] = len(UpperCamelCase__ ) self.assertEqual(config.output_hidden_states , UpperCamelCase__ ) check_encoder_attentions_output(UpperCamelCase__ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : int = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = model(self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(config.output_hidden_states , UpperCamelCase__ ) check_decoder_attentions_output(UpperCamelCase__ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = model(self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(config.output_hidden_states , UpperCamelCase__ ) check_encoder_attentions_output(UpperCamelCase__ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Tuple = True SCREAMING_SNAKE_CASE : int = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = model(self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(UpperCamelCase__ ) ) self.assertEqual(model.config.output_hidden_states , UpperCamelCase__ ) check_encoder_attentions_output(UpperCamelCase__ ) @require_tf class lowercase__ ( unittest.TestCase): @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) SCREAMING_SNAKE_CASE : Tuple = tf.constant([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE : int = model(UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : Any = [1, 6, 768] self.assertEqual(output.shape , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = tf.constant( [ [ [-0.0347_5493, -0.468_6034, -0.3063_8832], [0.2263_7248, -0.2698_8646, -0.742_3424], [0.1032_4868, -0.4501_3508, -0.5828_0784], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , UpperCamelCase__ , atol=1E-4 )
34
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: __UpperCamelCase : Dict = None __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Optional[int] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} __UpperCamelCase : Optional[int] = { '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 __UpperCamelCase : Union[str, Any] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = TaTokenizer UpperCamelCase_ = [] def __init__( self : str , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[Any]=100 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[str] = [f"""<extra_id_{i}>""" for i in range(UpperCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE : int = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id_''' in str(UpperCamelCase__ ) ) , UpperCamelCase__ ) ) ) 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__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , extra_ids=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : str = vocab_file SCREAMING_SNAKE_CASE : int = False if not self.vocab_file else True SCREAMING_SNAKE_CASE : str = extra_ids @staticmethod def __A ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE : List[str] = 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.''' , UpperCamelCase__ , ) return max_model_length def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : 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(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = 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__ ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE : Tuple = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, 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 __A ( self : Dict ): '''simple docstring''' return list( set(filter(lambda UpperCamelCase__ : bool(re.search(r'''<extra_id_\d+>''' , UpperCamelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def __A ( self : List[Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(UpperCamelCase__ ) for token in self.get_sentinel_tokens()]
34
1
# 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 numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ( """This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.""" """It takes two arguments named `image` which should be the original image, and `label` which should be a text """ """describing the elements what should be identified in the segmentation mask. The tool returns the mask.""" ) UpperCamelCase_ = """CIDAS/clipseg-rd64-refined""" UpperCamelCase_ = """image_segmenter""" UpperCamelCase_ = CLIPSegForImageSegmentation UpperCamelCase_ = ["""image""", """text"""] UpperCamelCase_ = ["""image"""] def __init__( self : Union[str, Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''vision'''] ) super().__init__(*UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : List[Any] , UpperCamelCase__ : "Image" , UpperCamelCase__ : str ): '''simple docstring''' return self.pre_processor(text=[label] , images=[image] , padding=UpperCamelCase__ , return_tensors='''pt''' ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[Any] ): '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[Any] = self.model(**UpperCamelCase__ ).logits return logits def __A ( self : Tuple , UpperCamelCase__ : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = outputs.cpu().detach().numpy() SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
34
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __UpperCamelCase : str = False class lowercase__ ( unittest.TestCase): pass @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Union[str, Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = VersatileDiffusionPipeline.from_pretrained(UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''cyberpunk 2077''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe.dual_guided( prompt=UpperCamelCase__ , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images SCREAMING_SNAKE_CASE : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : int = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe.text_to_image( prompt=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : List[str] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = pipe.image_variation(UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : Optional[int] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
34
1
from ..utils import DummyObject, requires_backends class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Tuple , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : str , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Any , *UpperCamelCase__ : Any , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[str] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Tuple , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Dict , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : str , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : str , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Dict , *UpperCamelCase__ : int , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Any , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Dict , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : Any , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Tuple , *UpperCamelCase__ : Any , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[str] , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Any , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Tuple , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : int , *UpperCamelCase__ : int , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : Optional[int] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""sentencepiece"""] def __init__( self : List[Any] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''sentencepiece'''] )
34
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def A ( _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = analyze_text(_lowercase ) SCREAMING_SNAKE_CASE : Any = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Tuple = sum(single_char_strings.values() ) # one length string SCREAMING_SNAKE_CASE : Tuple = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: SCREAMING_SNAKE_CASE : Tuple = single_char_strings[ch] SCREAMING_SNAKE_CASE : List[str] = my_str / all_sum my_fir_sum += prob * math.loga(_lowercase ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string SCREAMING_SNAKE_CASE : Optional[Any] = sum(two_char_strings.values() ) SCREAMING_SNAKE_CASE : List[str] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : Any = two_char_strings[sequence] SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) / all_sum my_sec_sum += prob * math.loga(_lowercase ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = Counter() # type: ignore SCREAMING_SNAKE_CASE : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_lowercase ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def A ( ): import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
34
1
import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('3.8'): import importlib_metadata else: import importlib.metadata as importlib_metadata def A ( _lowercase , _lowercase=False ): try: SCREAMING_SNAKE_CASE : Optional[Any] = os.environ[key] except KeyError: # KEY isn't set, default to `default`. SCREAMING_SNAKE_CASE : Union[str, Any] = default else: # KEY is set, convert it to True or False. try: SCREAMING_SNAKE_CASE : int = 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 __UpperCamelCase : Optional[int] = parse_flag_from_env('RUN_SLOW', default=False) __UpperCamelCase : Dict = parse_flag_from_env('RUN_REMOTE', default=False) __UpperCamelCase : Optional[int] = parse_flag_from_env('RUN_LOCAL', default=True) __UpperCamelCase : Optional[Any] = parse_flag_from_env('RUN_PACKAGED', default=True) # Compression __UpperCamelCase : Tuple = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='test requires lz4') __UpperCamelCase : Any = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='test requires py7zr') __UpperCamelCase : str = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='test requires zstandard') # Audio __UpperCamelCase : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('soundfile') is None or version.parse(importlib_metadata.version('soundfile')) < version.parse('0.12.0'), reason='test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ', ) # Beam __UpperCamelCase : List[str] = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('0.3.2'), reason='test requires apache-beam and a compatible dill version', ) # Dill-cloudpickle compatibility __UpperCamelCase : List[str] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('0.3.2'), reason='test requires dill>0.3.2 for cloudpickle compatibility', ) # Windows __UpperCamelCase : Dict = pytest.mark.skipif( sys.platform == 'win32', reason='test should not be run on Windows', ) def A ( _lowercase ): try: import faiss # noqa except ImportError: SCREAMING_SNAKE_CASE : Optional[Any] = unittest.skip('''test requires faiss''' )(_lowercase ) return test_case def A ( _lowercase ): try: import regex # noqa except ImportError: SCREAMING_SNAKE_CASE : Tuple = unittest.skip('''test requires regex''' )(_lowercase ) return test_case def A ( _lowercase ): try: import elasticsearch # noqa except ImportError: SCREAMING_SNAKE_CASE : str = unittest.skip('''test requires elasticsearch''' )(_lowercase ) return test_case def A ( _lowercase ): try: import sqlalchemy # noqa except ImportError: SCREAMING_SNAKE_CASE : Tuple = unittest.skip('''test requires sqlalchemy''' )(_lowercase ) return test_case def A ( _lowercase ): if not config.TORCH_AVAILABLE: SCREAMING_SNAKE_CASE : Dict = unittest.skip('''test requires PyTorch''' )(_lowercase ) return test_case def A ( _lowercase ): if not config.TF_AVAILABLE: SCREAMING_SNAKE_CASE : int = unittest.skip('''test requires TensorFlow''' )(_lowercase ) return test_case def A ( _lowercase ): if not config.JAX_AVAILABLE: SCREAMING_SNAKE_CASE : Optional[int] = unittest.skip('''test requires JAX''' )(_lowercase ) return test_case def A ( _lowercase ): if not config.PIL_AVAILABLE: SCREAMING_SNAKE_CASE : Optional[int] = unittest.skip('''test requires Pillow''' )(_lowercase ) return test_case def A ( _lowercase ): try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowercase ) else: return test_case def A ( _lowercase ): try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowercase ) else: return test_case def A ( _lowercase ): try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowercase ) else: return test_case def A ( _lowercase ): def _require_spacy_model(_lowercase ): try: import spacy # noqa F401 spacy.load(_lowercase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowercase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowercase ) )(_lowercase ) else: return test_case return _require_spacy_model def A ( _lowercase ): try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowercase ) else: return test_case def A ( _lowercase ): try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowercase ) else: return test_case def A ( _lowercase ): if not _run_slow_tests or _run_slow_tests == 0: SCREAMING_SNAKE_CASE : List[str] = unittest.skip('''test is slow''' )(_lowercase ) return test_case def A ( _lowercase ): if not _run_local_tests or _run_local_tests == 0: SCREAMING_SNAKE_CASE : int = unittest.skip('''test is local''' )(_lowercase ) return test_case def A ( _lowercase ): if not _run_packaged_tests or _run_packaged_tests == 0: SCREAMING_SNAKE_CASE : Optional[int] = unittest.skip('''test is packaged''' )(_lowercase ) return test_case def A ( _lowercase ): if not _run_remote_tests or _run_remote_tests == 0: SCREAMING_SNAKE_CASE : Dict = unittest.skip('''test requires remote''' )(_lowercase ) return test_case def A ( *_lowercase ): def decorate(cls ): for name, fn in cls.__dict__.items(): if callable(_lowercase ) and name.startswith('''test''' ): for decorator in decorators: SCREAMING_SNAKE_CASE : Any = decorator(_lowercase ) setattr(cls , _lowercase , _lowercase ) return cls return decorate class lowercase__ ( UpperCamelCase_): pass class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = 0 UpperCamelCase_ = 1 UpperCamelCase_ = 2 @contextmanager def A ( _lowercase=OfflineSimulationMode.CONNECTION_FAILS , _lowercase=1e-16 ): SCREAMING_SNAKE_CASE : Optional[int] = requests.Session().request def timeout_request(_lowercase , _lowercase , _lowercase , **_lowercase ): # Change the url to an invalid url so that the connection hangs SCREAMING_SNAKE_CASE : str = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f"""Tried a call to {url} in offline mode with no timeout set. Please set a timeout.""" ) SCREAMING_SNAKE_CASE : int = timeout try: return online_request(_lowercase , _lowercase , **_lowercase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier SCREAMING_SNAKE_CASE : str = url SCREAMING_SNAKE_CASE : Optional[int] = e.args[0] SCREAMING_SNAKE_CASE : Optional[int] = (max_retry_error.args[0].replace('''10.255.255.1''' , f"""OfflineMock[{url}]""" ),) SCREAMING_SNAKE_CASE : Optional[int] = (max_retry_error,) raise def raise_connection_error(_lowercase , _lowercase , **_lowercase ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowercase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowercase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowercase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowercase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def A ( *_lowercase , **_lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowercase , **_lowercase ) as tmp_dir: try: os.chdir(_lowercase ) yield finally: os.chdir(_lowercase ) @contextmanager def A ( ): import gc gc.collect() SCREAMING_SNAKE_CASE : Optional[int] = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def A ( ): import gc gc.collect() SCREAMING_SNAKE_CASE : str = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def A ( _lowercase , _lowercase ): return deepcopy(_lowercase ).integers(0 , 100 , 10 ).tolist() == deepcopy(_lowercase ).integers(0 , 100 , 10 ).tolist() def A ( _lowercase ): import decorator from requests.exceptions import HTTPError def _wrapper(_lowercase , *_lowercase , **_lowercase ): try: return func(*_lowercase , **_lowercase ) except HTTPError as err: if str(_lowercase ).startswith('''500''' ) or str(_lowercase ).startswith('''502''' ): pytest.xfail(str(_lowercase ) ) raise err return decorator.decorator(_wrapper , _lowercase ) class lowercase__ : def __init__( self : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Tuple , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = returncode SCREAMING_SNAKE_CASE : Any = stdout SCREAMING_SNAKE_CASE : List[Any] = stderr async def A ( _lowercase , _lowercase ): while True: SCREAMING_SNAKE_CASE : Any = await stream.readline() if line: callback(_lowercase ) else: break async def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None , _lowercase=False , _lowercase=False ): if echo: print('''\nRunning: ''' , ''' '''.join(_lowercase ) ) SCREAMING_SNAKE_CASE : List[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 : List[str] = [] SCREAMING_SNAKE_CASE : List[str] = [] def tee(_lowercase , _lowercase , _lowercase , _lowercase="" ): SCREAMING_SNAKE_CASE : List[str] = 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( [ _read_stream(p.stdout , lambda _lowercase : tee(_lowercase , _lowercase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowercase : tee(_lowercase , _lowercase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowercase , ) return _RunOutput(await p.wait() , _lowercase , _lowercase ) def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=180 , _lowercase=False , _lowercase=True ): SCREAMING_SNAKE_CASE : Dict = asyncio.get_event_loop() SCREAMING_SNAKE_CASE : List[str] = 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 : List[str] = '''\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}""" ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f"""'{cmd_str}' produced no output.""" ) return result def A ( ): SCREAMING_SNAKE_CASE : Tuple = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) SCREAMING_SNAKE_CASE : int = re.sub(R'''^gw''' , '''''' , _lowercase , 0 , re.M ) return int(_lowercase ) def A ( ): SCREAMING_SNAKE_CASE : Any = 29_500 SCREAMING_SNAKE_CASE : List[Any] = pytest_xdist_worker_id() return port + uniq_delta
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCamelCase : Tuple = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Any = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
1
from typing import Union import fire import torch from tqdm import tqdm def A ( _lowercase , _lowercase = "cpu" , _lowercase = None ): SCREAMING_SNAKE_CASE : str = torch.load(_lowercase , map_location=_lowercase ) for k, v in tqdm(state_dict.items() ): if not isinstance(_lowercase , torch.Tensor ): raise TypeError('''FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin''' ) SCREAMING_SNAKE_CASE : Any = v.half() if save_path is None: # overwrite src_path SCREAMING_SNAKE_CASE : str = src_path torch.save(_lowercase , _lowercase ) if __name__ == "__main__": fire.Fire(convert)
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
1
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __UpperCamelCase : Dict = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None ): SCREAMING_SNAKE_CASE : Union[str, Any] = True while ask_again: SCREAMING_SNAKE_CASE : Optional[Any] = input(_lowercase ) try: if default is not None and len(_lowercase ) == 0: return default return convert_value(_lowercase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowercase ) def A ( _lowercase , _lowercase=[] , _lowercase=None , _lowercase=0 ): SCREAMING_SNAKE_CASE : Dict = BulletMenu(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : str = menu.run(default_choice=_lowercase ) return convert_value(_lowercase ) if convert_value is not None else result def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = int(_lowercase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A ( _lowercase ): return {"yes": True, "no": False}[value.lower()] class lowercase__ ( argparse.RawDescriptionHelpFormatter): def __A ( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = super()._format_usage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
34
1
import inspect import unittest class lowercase__ ( unittest.TestCase): def __A ( self : str ): '''simple docstring''' try: import diffusers # noqa: F401 except ImportError: assert False def __A ( self : int ): '''simple docstring''' import diffusers from diffusers.dependency_versions_table import deps SCREAMING_SNAKE_CASE : List[str] = inspect.getmembers(UpperCamelCase__ , inspect.isclass ) for cls_name, cls_module in all_classes: if "dummy_" in cls_module.__module__: for backend in cls_module._backends: if backend == "k_diffusion": SCREAMING_SNAKE_CASE : int = '''k-diffusion''' elif backend == "invisible_watermark": SCREAMING_SNAKE_CASE : Any = '''invisible-watermark''' assert backend in deps, f"""{backend} is not in the deps table!"""
34
from __future__ import annotations from typing import Any class lowercase__ ( UpperCamelCase_): pass class lowercase__ : def __init__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = data SCREAMING_SNAKE_CASE : Node | None = None def __iter__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self SCREAMING_SNAKE_CASE : Tuple = [] while node: if node in visited: raise ContainsLoopError visited.append(UpperCamelCase__ ) yield node.data SCREAMING_SNAKE_CASE : Dict = node.next_node @property def __A ( self : Optional[int] ): '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": __UpperCamelCase : List[Any] = Node(1) __UpperCamelCase : str = Node(2) __UpperCamelCase : Dict = Node(3) __UpperCamelCase : List[Any] = Node(4) print(root_node.has_loop) # False __UpperCamelCase : int = root_node.next_node print(root_node.has_loop) # True __UpperCamelCase : Union[str, Any] = Node(5) __UpperCamelCase : Union[str, Any] = Node(6) __UpperCamelCase : List[Any] = Node(5) __UpperCamelCase : List[str] = Node(6) print(root_node.has_loop) # False __UpperCamelCase : List[Any] = Node(1) print(root_node.has_loop) # False
34
1
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Dict = {'vocab_file': 'spiece.model'} __UpperCamelCase : Dict = { 'vocab_file': { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model', } } __UpperCamelCase : Optional[Any] = { 'xlnet-base-cased': None, 'xlnet-large-cased': None, } # Segments (not really needed) __UpperCamelCase : List[str] = 0 __UpperCamelCase : Tuple = 1 __UpperCamelCase : Dict = 2 __UpperCamelCase : Optional[Any] = 3 __UpperCamelCase : List[Any] = 4 class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = """left""" def __init__( self : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Any=False , UpperCamelCase__ : str=True , UpperCamelCase__ : List[Any]=False , UpperCamelCase__ : str="<s>" , UpperCamelCase__ : Optional[int]="</s>" , UpperCamelCase__ : Optional[Any]="<unk>" , UpperCamelCase__ : Optional[int]="<sep>" , UpperCamelCase__ : List[str]="<pad>" , UpperCamelCase__ : Tuple="<cls>" , UpperCamelCase__ : Union[str, Any]="<mask>" , UpperCamelCase__ : List[Any]=["<eop>", "<eod>"] , UpperCamelCase__ : Optional[Dict[str, Any]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else mask_token SCREAMING_SNAKE_CASE : str = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCamelCase__ , remove_space=UpperCamelCase__ , keep_accents=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : Tuple = do_lower_case SCREAMING_SNAKE_CASE : Dict = remove_space SCREAMING_SNAKE_CASE : Optional[int] = keep_accents SCREAMING_SNAKE_CASE : List[Any] = vocab_file SCREAMING_SNAKE_CASE : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase__ ) @property def __A ( self : str ): '''simple docstring''' return len(self.sp_model ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = {self.convert_ids_to_tokens(UpperCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.__dict__.copy() SCREAMING_SNAKE_CASE : Tuple = None return state def __setstate__( self : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): SCREAMING_SNAKE_CASE : Optional[Any] = {} SCREAMING_SNAKE_CASE : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __A ( self : Tuple , UpperCamelCase__ : List[str] ): '''simple docstring''' if self.remove_space: SCREAMING_SNAKE_CASE : List[Any] = ''' '''.join(inputs.strip().split() ) else: SCREAMING_SNAKE_CASE : List[str] = inputs SCREAMING_SNAKE_CASE : List[str] = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: SCREAMING_SNAKE_CASE : Any = unicodedata.normalize('''NFKD''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = ''''''.join([c for c in outputs if not unicodedata.combining(UpperCamelCase__ )] ) if self.do_lower_case: SCREAMING_SNAKE_CASE : Union[str, Any] = outputs.lower() return outputs def __A ( self : Any , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.preprocess_text(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.sp_model.encode(UpperCamelCase__ , out_type=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = [] for piece in pieces: if len(UpperCamelCase__ ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): SCREAMING_SNAKE_CASE : str = self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCamelCase__ , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: SCREAMING_SNAKE_CASE : Tuple = cur_pieces[1:] else: SCREAMING_SNAKE_CASE : Union[str, Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(UpperCamelCase__ ) else: new_pieces.append(UpperCamelCase__ ) return new_pieces def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' return self.sp_model.PieceToId(UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[Any] ): '''simple docstring''' return self.sp_model.IdToPiece(UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = ''''''.join(UpperCamelCase__ ).replace(UpperCamelCase__ , ''' ''' ).strip() return out_string def __A ( self : Optional[int] , UpperCamelCase__ : List[int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = None , UpperCamelCase__ : bool = True , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = kwargs.pop('''use_source_tokenizer''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = self.convert_ids_to_tokens(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 SCREAMING_SNAKE_CASE : Tuple = [] SCREAMING_SNAKE_CASE : Optional[int] = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : List[str] = [] sub_texts.append(UpperCamelCase__ ) else: current_sub_text.append(UpperCamelCase__ ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(UpperCamelCase__ ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens SCREAMING_SNAKE_CASE : Any = ''''''.join(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: SCREAMING_SNAKE_CASE : Dict = self.clean_up_tokenization(UpperCamelCase__ ) return clean_text else: return text def __A ( self : List[str] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [self.sep_token_id] SCREAMING_SNAKE_CASE : List[Any] = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __A ( self : List[str] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None , UpperCamelCase__ : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__ , token_ids_a=UpperCamelCase__ , already_has_special_tokens=UpperCamelCase__ ) if token_ids_a is not None: return ([0] * len(UpperCamelCase__ )) + [1] + ([0] * len(UpperCamelCase__ )) + [1, 1] return ([0] * len(UpperCamelCase__ )) + [1, 1] def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [self.sep_token_id] SCREAMING_SNAKE_CASE : Dict = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __A ( self : List[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : List[Any] = os.path.join( UpperCamelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCamelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCamelCase__ , '''wb''' ) as fi: SCREAMING_SNAKE_CASE : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (out_vocab_file,)
34
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""input_features""", """is_longer"""] def __init__( self : Optional[Any] , UpperCamelCase__ : Dict=64 , UpperCamelCase__ : Optional[Any]=4_8000 , UpperCamelCase__ : Tuple=480 , UpperCamelCase__ : Union[str, Any]=10 , UpperCamelCase__ : List[Any]=1024 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : int=False , UpperCamelCase__ : float = 0 , UpperCamelCase__ : float = 1_4000 , UpperCamelCase__ : int = None , UpperCamelCase__ : str = "fusion" , UpperCamelCase__ : str = "repeatpad" , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' super().__init__( feature_size=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , padding_value=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = top_db SCREAMING_SNAKE_CASE : Union[str, Any] = truncation SCREAMING_SNAKE_CASE : str = padding SCREAMING_SNAKE_CASE : List[Any] = fft_window_size SCREAMING_SNAKE_CASE : Tuple = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE : List[str] = hop_length SCREAMING_SNAKE_CASE : List[Any] = max_length_s SCREAMING_SNAKE_CASE : Tuple = max_length_s * sampling_rate SCREAMING_SNAKE_CASE : List[Any] = sampling_rate SCREAMING_SNAKE_CASE : List[str] = frequency_min SCREAMING_SNAKE_CASE : Any = frequency_max SCREAMING_SNAKE_CASE : List[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm=UpperCamelCase__ , mel_scale='''htk''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm='''slaney''' , mel_scale='''slaney''' , ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __A ( self : Optional[int] , UpperCamelCase__ : np.array , UpperCamelCase__ : Optional[np.array] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = spectrogram( UpperCamelCase__ , window_function(self.fft_window_size , '''hann''' ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=UpperCamelCase__ , log_mel='''dB''' , ) return log_mel_spectrogram.T def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : int = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : Any = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE : List[Any] = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE : int = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE : Optional[int] = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE : Optional[Any] = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE : Tuple = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE : str = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.functional.interpolate( UpperCamelCase__ , size=[chunk_frames, 64] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __A ( self : Dict , UpperCamelCase__ : np.array , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] ): '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE : Optional[Any] = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) - max_length SCREAMING_SNAKE_CASE : Dict = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE : List[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE : Optional[Any] = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE : Tuple = False else: SCREAMING_SNAKE_CASE : str = self._random_mel_fusion(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = True else: raise NotImplementedError(f"""data_truncating {truncation} not implemented""" ) else: SCREAMING_SNAKE_CASE : List[str] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE : Tuple = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Any = np.stack(np.tile(UpperCamelCase__ , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE : List[Any] = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.stack(np.tile(UpperCamelCase__ , UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.pad(UpperCamelCase__ , (0, max_length - waveform.shape[0]) , mode='''constant''' , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE : List[Any] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE : List[str] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , UpperCamelCase__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , UpperCamelCase__ : str = None , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , **UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE : List[str] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" f""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE : List[str] = isinstance(UpperCamelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) SCREAMING_SNAKE_CASE : int = is_batched_numpy or ( isinstance(UpperCamelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE : Any = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase__ , np.ndarray ): SCREAMING_SNAKE_CASE : List[Any] = np.asarray(UpperCamelCase__ , dtype=np.floataa ) elif isinstance(UpperCamelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE : List[str] = [np.asarray(UpperCamelCase__ )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE : int = [ self._get_input_mel(UpperCamelCase__ , max_length if max_length else self.nb_max_samples , UpperCamelCase__ , UpperCamelCase__ ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : List[str] = [] for mel, longer in padded_inputs: input_mel.append(UpperCamelCase__ ) is_longer.append(UpperCamelCase__ ) if truncation == "fusion" and sum(UpperCamelCase__ ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.randint(0 , len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = True if isinstance(input_mel[0] , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE : Optional[Any] = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_features''': input_mel, '''is_longer''': is_longer} SCREAMING_SNAKE_CASE : int = BatchFeature(UpperCamelCase__ ) if return_tensors is not None: SCREAMING_SNAKE_CASE : int = input_features.convert_to_tensors(UpperCamelCase__ ) return input_features
34
1
import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def A ( _lowercase , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : str = AutoConfig.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE : str = FlaxAutoModelForSeqaSeqLM.from_config(config=_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = checkpoints.load_tax_checkpoint(_lowercase ) SCREAMING_SNAKE_CASE : Tuple = '''wi_0''' in tax_model['''target''']['''encoder''']['''layers_0''']['''mlp'''] if config.model_type == "t5": SCREAMING_SNAKE_CASE : int = '''SelfAttention''' if config.model_type == "longt5" and config.encoder_attention_type == "local": SCREAMING_SNAKE_CASE : str = '''LocalSelfAttention''' elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE : List[str] = '''TransientGlobalSelfAttention''' else: raise ValueError( '''Given config is expected to have `model_type=\'t5\'`, or `model_type=\'longt5` with `encoder_attention_type`''' ''' attribute with a value from [\'local\', \'transient-global].''' ) # Encoder for layer_index in range(config.num_layers ): SCREAMING_SNAKE_CASE : List[str] = f"""layers_{str(_lowercase )}""" # Self-Attention SCREAMING_SNAKE_CASE : List[Any] = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''key''']['''kernel'''] SCREAMING_SNAKE_CASE : str = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''out''']['''kernel'''] SCREAMING_SNAKE_CASE : List[Any] = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''query''']['''kernel'''] SCREAMING_SNAKE_CASE : List[str] = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''value''']['''kernel'''] # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE : List[str] = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''T5LayerNorm_0''']['''scale'''] # Layer Normalization SCREAMING_SNAKE_CASE : List[str] = tax_model['''target''']['''encoder'''][layer_name]['''pre_attention_layer_norm''']['''scale'''] if split_mlp_wi: SCREAMING_SNAKE_CASE : int = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi_0''']['''kernel'''] SCREAMING_SNAKE_CASE : Optional[int] = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi_1''']['''kernel'''] else: SCREAMING_SNAKE_CASE : int = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi''']['''kernel'''] SCREAMING_SNAKE_CASE : Tuple = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wo''']['''kernel'''] # Layer Normalization SCREAMING_SNAKE_CASE : Any = tax_model['''target''']['''encoder'''][layer_name]['''pre_mlp_layer_norm''']['''scale'''] # Assigning SCREAMING_SNAKE_CASE : List[str] = flax_model.params['''encoder''']['''block'''][str(_lowercase )]['''layer'''] SCREAMING_SNAKE_CASE : Dict = tax_attention_key SCREAMING_SNAKE_CASE : List[Any] = tax_attention_out SCREAMING_SNAKE_CASE : List[str] = tax_attention_query SCREAMING_SNAKE_CASE : List[str] = tax_attention_value SCREAMING_SNAKE_CASE : Any = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE : Dict = tax_global_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE : List[str] = tax_mlp_wi_a SCREAMING_SNAKE_CASE : Optional[Any] = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE : Optional[Any] = tax_mlp_wi SCREAMING_SNAKE_CASE : Tuple = tax_mlp_wo SCREAMING_SNAKE_CASE : Optional[Any] = tax_mlp_layer_norm SCREAMING_SNAKE_CASE : str = flax_model_encoder_layer_block # Only for layer 0: SCREAMING_SNAKE_CASE : List[Any] = tax_model['''target''']['''encoder''']['''relpos_bias''']['''rel_embedding'''].T SCREAMING_SNAKE_CASE : str = tax_encoder_rel_embedding # Side/global relative position_bias + layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE : int = tax_model['''target''']['''encoder''']['''side_relpos_bias''']['''rel_embedding'''].T SCREAMING_SNAKE_CASE : int = tax_encoder_global_rel_embedding # Assigning SCREAMING_SNAKE_CASE : Optional[int] = tax_model['''target''']['''encoder''']['''encoder_norm''']['''scale'''] SCREAMING_SNAKE_CASE : int = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): SCREAMING_SNAKE_CASE : List[Any] = f"""layers_{str(_lowercase )}""" # Self-Attention SCREAMING_SNAKE_CASE : List[str] = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''key''']['''kernel'''] SCREAMING_SNAKE_CASE : Any = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''out''']['''kernel'''] SCREAMING_SNAKE_CASE : List[Any] = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''query''']['''kernel'''] SCREAMING_SNAKE_CASE : Dict = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''value''']['''kernel'''] # Layer Normalization SCREAMING_SNAKE_CASE : Dict = tax_model['''target''']['''decoder'''][layer_name]['''pre_self_attention_layer_norm'''][ '''scale''' ] # Encoder-Decoder-Attention SCREAMING_SNAKE_CASE : str = tax_model['''target''']['''decoder'''][layer_name]['''encoder_decoder_attention'''] SCREAMING_SNAKE_CASE : str = tax_enc_dec_attention_module['''key''']['''kernel'''] SCREAMING_SNAKE_CASE : List[Any] = tax_enc_dec_attention_module['''out''']['''kernel'''] SCREAMING_SNAKE_CASE : int = tax_enc_dec_attention_module['''query''']['''kernel'''] SCREAMING_SNAKE_CASE : List[str] = tax_enc_dec_attention_module['''value''']['''kernel'''] # Layer Normalization SCREAMING_SNAKE_CASE : Tuple = tax_model['''target''']['''decoder'''][layer_name]['''pre_cross_attention_layer_norm''']['''scale'''] # MLP if split_mlp_wi: SCREAMING_SNAKE_CASE : Any = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi_0''']['''kernel'''] SCREAMING_SNAKE_CASE : str = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi_1''']['''kernel'''] else: SCREAMING_SNAKE_CASE : List[str] = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi''']['''kernel'''] SCREAMING_SNAKE_CASE : Dict = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wo''']['''kernel'''] # Layer Normalization SCREAMING_SNAKE_CASE : List[Any] = tax_model['''target''']['''decoder'''][layer_name]['''pre_mlp_layer_norm''']['''scale'''] # Assigning SCREAMING_SNAKE_CASE : Any = flax_model.params['''decoder''']['''block'''][str(_lowercase )]['''layer'''] SCREAMING_SNAKE_CASE : Optional[Any] = tax_attention_key SCREAMING_SNAKE_CASE : Union[str, Any] = tax_attention_out SCREAMING_SNAKE_CASE : Dict = tax_attention_query SCREAMING_SNAKE_CASE : Any = tax_attention_value SCREAMING_SNAKE_CASE : Optional[int] = tax_pre_attention_layer_norm SCREAMING_SNAKE_CASE : Union[str, Any] = tax_enc_dec_attention_key SCREAMING_SNAKE_CASE : List[Any] = tax_enc_dec_attention_out SCREAMING_SNAKE_CASE : List[Any] = tax_enc_dec_attention_query SCREAMING_SNAKE_CASE : List[Any] = tax_enc_dec_attention_value SCREAMING_SNAKE_CASE : Optional[Any] = tax_cross_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE : Any = tax_mlp_wi_a SCREAMING_SNAKE_CASE : Dict = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE : Optional[int] = tax_mlp_wi SCREAMING_SNAKE_CASE : int = tax_mlp_wo SCREAMING_SNAKE_CASE : Optional[int] = txa_mlp_layer_norm SCREAMING_SNAKE_CASE : Union[str, Any] = flax_model_decoder_layer_block # Decoder Normalization SCREAMING_SNAKE_CASE : Optional[Any] = tax_model['''target''']['''decoder''']['''decoder_norm''']['''scale'''] SCREAMING_SNAKE_CASE : Optional[int] = txa_decoder_norm # Only for layer 0: SCREAMING_SNAKE_CASE : Tuple = tax_model['''target''']['''decoder''']['''relpos_bias''']['''rel_embedding'''].T SCREAMING_SNAKE_CASE : Any = tax_decoder_rel_embedding # Token Embeddings SCREAMING_SNAKE_CASE : Any = tax_model['''target''']['''token_embedder''']['''embedding'''] SCREAMING_SNAKE_CASE : List[str] = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: SCREAMING_SNAKE_CASE : int = tax_model['''target''']['''decoder''']['''logits_dense''']['''kernel'''] flax_model.save_pretrained(_lowercase ) print('''T5X Model was sucessfully converted!''' ) if __name__ == "__main__": __UpperCamelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--t5x_checkpoint_path', default=None, type=str, required=True, help='Path the T5X checkpoint.' ) parser.add_argument('--config_name', default=None, type=str, required=True, help='Config name of LongT5/T5 model.') parser.add_argument( '--flax_dump_folder_path', default=None, type=str, required=True, help='Path to the output FLAX model.' ) __UpperCamelCase : List[str] = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __UpperCamelCase : str = logging.get_logger(__name__) __UpperCamelCase : str = { 'microsoft/layoutlmv3-base': 'https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """layoutlmv3""" def __init__( self : Optional[int] , UpperCamelCase__ : Union[str, Any]=5_0265 , UpperCamelCase__ : List[Any]=768 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Tuple=3072 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Any=512 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : Optional[Any]=1E-5 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : int=0 , UpperCamelCase__ : str=2 , UpperCamelCase__ : List[str]=1024 , UpperCamelCase__ : str=128 , UpperCamelCase__ : str=128 , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Optional[Any]=64 , UpperCamelCase__ : Dict=256 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Dict=224 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : Any=None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__( vocab_size=UpperCamelCase__ , hidden_size=UpperCamelCase__ , num_hidden_layers=UpperCamelCase__ , num_attention_heads=UpperCamelCase__ , intermediate_size=UpperCamelCase__ , hidden_act=UpperCamelCase__ , hidden_dropout_prob=UpperCamelCase__ , attention_probs_dropout_prob=UpperCamelCase__ , max_position_embeddings=UpperCamelCase__ , type_vocab_size=UpperCamelCase__ , initializer_range=UpperCamelCase__ , layer_norm_eps=UpperCamelCase__ , pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : List[str] = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : str = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : Union[str, Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Union[str, Any] = max_rel_ad_pos SCREAMING_SNAKE_CASE : Union[str, Any] = text_embed SCREAMING_SNAKE_CASE : List[str] = visual_embed SCREAMING_SNAKE_CASE : Optional[Any] = input_size SCREAMING_SNAKE_CASE : Optional[Any] = num_channels SCREAMING_SNAKE_CASE : List[Any] = patch_size SCREAMING_SNAKE_CASE : List[Any] = classifier_dropout class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.12""") @property def __A ( self : str ): '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __A ( self : int ): '''simple docstring''' return 1E-5 @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Optional[Any] , UpperCamelCase__ : "ProcessorMixin" , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , ): '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , UpperCamelCase__ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : List[Any] = processor.tokenizer.num_special_tokens_to_add(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=UpperCamelCase__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : Any = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = dict( processor( UpperCamelCase__ , text=UpperCamelCase__ , boxes=UpperCamelCase__ , return_tensors=UpperCamelCase__ , ) ) return inputs
34
1
import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class lowercase__ : def __init__( self : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Any=13 , UpperCamelCase__ : Any=7 , UpperCamelCase__ : Tuple=6 , UpperCamelCase__ : int=17 , UpperCamelCase__ : List[str]=23 , UpperCamelCase__ : Optional[Any]=11 , UpperCamelCase__ : List[Any]=True , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = parent SCREAMING_SNAKE_CASE : Optional[int] = batch_size SCREAMING_SNAKE_CASE : Any = seq_length SCREAMING_SNAKE_CASE : int = act_dim SCREAMING_SNAKE_CASE : Any = state_dim SCREAMING_SNAKE_CASE : Dict = hidden_size SCREAMING_SNAKE_CASE : Optional[Any] = max_length SCREAMING_SNAKE_CASE : List[Any] = is_training def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) SCREAMING_SNAKE_CASE : Tuple = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) SCREAMING_SNAKE_CASE : Dict = floats_tensor((self.batch_size, self.seq_length, 1) ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((self.batch_size, self.seq_length, 1) ) SCREAMING_SNAKE_CASE : str = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1000 ) SCREAMING_SNAKE_CASE : List[str] = random_attention_mask((self.batch_size, self.seq_length) ) SCREAMING_SNAKE_CASE : List[str] = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def __A ( self : Dict ): '''simple docstring''' return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def __A ( self : str , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = DecisionTransformerModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = model(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE : Optional[Any] = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = (DecisionTransformerModel,) if is_torch_available() else () UpperCamelCase_ = () UpperCamelCase_ = {"""feature-extraction""": DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids UpperCamelCase_ = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = DecisionTransformerModelTester(self ) SCREAMING_SNAKE_CASE : Any = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=37 ) def __A ( self : int ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) @slow def __A ( self : Optional[Any] ): '''simple docstring''' for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : List[str] = DecisionTransformerModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : int = model_class(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : str = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : str = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(UpperCamelCase__ )] , UpperCamelCase__ ) @require_torch class lowercase__ ( unittest.TestCase): @slow def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = 2 # number of steps of autoregressive prediction we will perform SCREAMING_SNAKE_CASE : Tuple = 10 # defined by the RL environment, may be normalized SCREAMING_SNAKE_CASE : Optional[int] = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) SCREAMING_SNAKE_CASE : Any = model.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = model.config torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = torch.randn(1 , 1 , config.state_dim ).to(device=UpperCamelCase__ , dtype=torch.floataa ) # env.reset() SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor( [[0.24_2793, -0.2869_3074, 0.874_2613], [0.6781_5274, -0.0810_1085, -0.1295_2147]] , device=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = torch.tensor(UpperCamelCase__ , device=UpperCamelCase__ , dtype=torch.floataa ).reshape(1 , 1 , 1 ) SCREAMING_SNAKE_CASE : Optional[int] = state SCREAMING_SNAKE_CASE : Any = torch.zeros(1 , 0 , config.act_dim , device=UpperCamelCase__ , dtype=torch.floataa ) SCREAMING_SNAKE_CASE : int = torch.zeros(1 , 0 , device=UpperCamelCase__ , dtype=torch.floataa ) SCREAMING_SNAKE_CASE : Any = torch.tensor(0 , device=UpperCamelCase__ , dtype=torch.long ).reshape(1 , 1 ) for step in range(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : str = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=UpperCamelCase__ )] , dim=1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat([rewards, torch.zeros(1 , 1 , device=UpperCamelCase__ )] , dim=1 ) SCREAMING_SNAKE_CASE : Tuple = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = model( states=UpperCamelCase__ , actions=UpperCamelCase__ , rewards=UpperCamelCase__ , returns_to_go=UpperCamelCase__ , timesteps=UpperCamelCase__ , attention_mask=UpperCamelCase__ , return_dict=UpperCamelCase__ , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=UpperCamelCase__ , dtype=torch.floataa ), 1.0, False, {}, ) SCREAMING_SNAKE_CASE : Optional[Any] = action_pred[0, -1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([states, state] , dim=1 ) SCREAMING_SNAKE_CASE : Tuple = returns_to_go[0, -1] - reward SCREAMING_SNAKE_CASE : List[Any] = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) SCREAMING_SNAKE_CASE : List[str] = torch.cat( [timesteps, torch.ones((1, 1) , device=UpperCamelCase__ , dtype=torch.long ) * (step + 1)] , dim=1 )
34
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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FunnelTokenizer UpperCamelCase_ = FunnelTokenizerFast UpperCamelCase_ = True UpperCamelCase_ = True def __A ( self : Union[str, Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[Any] = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : List[str] = 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 __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : int = '''unwanted, running''' return input_text, output_text def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCamelCase__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [7, 4, 5, 10, 8, 9] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_tokenizers(do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE : int = tokenizer('''UNwant\u00E9d,running''' ) SCREAMING_SNAKE_CASE : Optional[Any] = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
34
1
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowercase__ ( UpperCamelCase_ , UpperCamelCase_): UpperCamelCase_ = 1 @register_to_config def __init__( self : List[str] , UpperCamelCase__ : int = 1000 , UpperCamelCase__ : Optional[Union[np.ndarray, List[float]]] = None ): '''simple docstring''' self.set_timesteps(UpperCamelCase__ ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : str = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. SCREAMING_SNAKE_CASE : Tuple = 4 # running values SCREAMING_SNAKE_CASE : int = [] def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, torch.device] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = num_inference_steps SCREAMING_SNAKE_CASE : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: SCREAMING_SNAKE_CASE : int = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE : Optional[Any] = torch.sin(steps * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE : Dict = (1.0 - self.betas**2) ** 0.5 SCREAMING_SNAKE_CASE : Optional[Any] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] SCREAMING_SNAKE_CASE : List[str] = timesteps.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = [] def __A ( self : Tuple , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : int , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : bool = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) SCREAMING_SNAKE_CASE : Optional[int] = (self.timesteps == timestep).nonzero().item() SCREAMING_SNAKE_CASE : Union[str, Any] = timestep_index + 1 SCREAMING_SNAKE_CASE : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCamelCase__ ) if len(self.ets ) == 1: SCREAMING_SNAKE_CASE : Dict = self.ets[-1] elif len(self.ets ) == 2: SCREAMING_SNAKE_CASE : Optional[int] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: SCREAMING_SNAKE_CASE : str = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: SCREAMING_SNAKE_CASE : Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) SCREAMING_SNAKE_CASE : Optional[int] = self._get_prev_sample(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : torch.FloatTensor , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return sample def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.alphas[timestep_index] SCREAMING_SNAKE_CASE : List[str] = self.betas[timestep_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas[prev_timestep_index] SCREAMING_SNAKE_CASE : Tuple = self.betas[prev_timestep_index] SCREAMING_SNAKE_CASE : Dict = (sample - sigma * ets) / max(UpperCamelCase__ , 1E-8 ) SCREAMING_SNAKE_CASE : Optional[Any] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
34
import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = dataset SCREAMING_SNAKE_CASE : Optional[Any] = process SCREAMING_SNAKE_CASE : Union[str, Any] = params def __len__( self : Tuple ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dataset[i] SCREAMING_SNAKE_CASE : Optional[int] = self.process(UpperCamelCase__ , **self.params ) return processed class lowercase__ ( UpperCamelCase_): def __init__( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = loader SCREAMING_SNAKE_CASE : List[Any] = infer SCREAMING_SNAKE_CASE : int = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : List[str] = loader_batch_size # Internal bookkeeping SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None def __len__( self : int ): '''simple docstring''' return len(self.loader ) def __iter__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice SCREAMING_SNAKE_CASE : Optional[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Convert ModelOutput to tuple first SCREAMING_SNAKE_CASE : Dict = element.to_tuple() if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : List[str] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around SCREAMING_SNAKE_CASE : int = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Union[str, Any] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Tuple = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. SCREAMING_SNAKE_CASE : Tuple = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 SCREAMING_SNAKE_CASE : Any = self._loader_batch_data.__class__(UpperCamelCase__ ) self._loader_batch_index += 1 return result def __A ( self : Union[str, Any] ): '''simple docstring''' if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch SCREAMING_SNAKE_CASE : Tuple = next(self.iterator ) SCREAMING_SNAKE_CASE : List[Any] = self.infer(UpperCamelCase__ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Optional[int] = processed else: SCREAMING_SNAKE_CASE : int = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : int = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[Any] = observed_batch_size # Setting internal index to unwrap the batch SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : int = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=None ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __iter__( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = iter(self.loader ) SCREAMING_SNAKE_CASE : List[Any] = None return self def __A ( self : List[str] ): '''simple docstring''' if self.subiterator is None: SCREAMING_SNAKE_CASE : Dict = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item SCREAMING_SNAKE_CASE : Any = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators SCREAMING_SNAKE_CASE : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = next(self.subiterator ) return processed class lowercase__ ( UpperCamelCase_): def __iter__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Optional[int] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Tuple = self.loader_batch_item() SCREAMING_SNAKE_CASE : Any = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator while not is_last: SCREAMING_SNAKE_CASE : Any = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = processed else: SCREAMING_SNAKE_CASE : Union[str, Any] = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : List[str] = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[str] = observed_batch_size SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : str = 0 while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Any = self.loader_batch_item() SCREAMING_SNAKE_CASE : List[Any] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator else: SCREAMING_SNAKE_CASE : int = processed SCREAMING_SNAKE_CASE : List[str] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) return accumulator class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Dict = key def __len__( self : Optional[int] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' return self.dataset[i][self.key] class lowercase__ ( UpperCamelCase_): def __init__( self : List[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : List[str] = keya SCREAMING_SNAKE_CASE : Tuple = keya def __len__( self : List[str] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
34
1
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. __UpperCamelCase : str = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. __UpperCamelCase : Tuple = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. __UpperCamelCase : Optional[int] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = len([g for position, g in enumerate(_lowercase ) if g == main_target[position]] ) return (item, float(_lowercase )) def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = random.randint(0 , len(_lowercase ) - 1 ) SCREAMING_SNAKE_CASE : List[str] = parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE : Optional[int] = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Any = list(_lowercase ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE : List[Any] = random.choice(_lowercase ) return "".join(_lowercase ) def A ( _lowercase , _lowercase , _lowercase , ): SCREAMING_SNAKE_CASE : List[str] = [] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE : Optional[int] = int(parent_a[1] * 100 ) + 1 SCREAMING_SNAKE_CASE : Tuple = 10 if child_n >= 10 else child_n for _ in range(_lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = population_score[random.randint(0 , _lowercase )][0] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = crossover(parent_a[0] , _lowercase ) # Append new string to the population list. pop.append(mutate(_lowercase , _lowercase ) ) pop.append(mutate(_lowercase , _lowercase ) ) return pop def A ( _lowercase , _lowercase , _lowercase = True ): # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE : List[Any] = f"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(_lowercase ) # Verify that the target contains no genes besides the ones inside genes variable. SCREAMING_SNAKE_CASE : str = sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE : List[Any] = f"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(_lowercase ) # Generate random starting population. SCREAMING_SNAKE_CASE : Union[str, Any] = [] for _ in range(_lowercase ): population.append(''''''.join([random.choice(_lowercase ) for i in range(len(_lowercase ) )] ) ) # Just some logs to know what the algorithms is doing. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(_lowercase ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. SCREAMING_SNAKE_CASE : List[Any] = [evaluate(_lowercase , _lowercase ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE : Any = sorted(_lowercase , key=lambda _lowercase : x[1] , reverse=_lowercase ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( f"""\nGeneration: {generation}""" f"""\nTotal Population:{total_population}""" f"""\nBest score: {population_score[0][1]}""" f"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. SCREAMING_SNAKE_CASE : Union[str, Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(_lowercase ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE : List[Any] = [ (item, score / len(_lowercase )) for item, score in population_score ] # This is selection for i in range(_lowercase ): population.extend(select(population_score[int(_lowercase )] , _lowercase , _lowercase ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(_lowercase ) > N_POPULATION: break if __name__ == "__main__": __UpperCamelCase : str = ( 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!' ) __UpperCamelCase : Optional[int] = list( ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm' 'nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\' ) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : Optional[Any] = basic(target_str, genes_list) print( f"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}""" )
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """deberta-v2""" def __init__( self : Optional[Any] , UpperCamelCase__ : Any=12_8100 , UpperCamelCase__ : Optional[int]=1536 , UpperCamelCase__ : Dict=24 , UpperCamelCase__ : List[str]=24 , UpperCamelCase__ : Tuple=6144 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Optional[int]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : Optional[Any]=0 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : List[Any]=1E-7 , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=-1 , UpperCamelCase__ : List[Any]=0 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : str="gelu" , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention SCREAMING_SNAKE_CASE : Optional[Any] = max_relative_positions SCREAMING_SNAKE_CASE : Optional[int] = pad_token_id SCREAMING_SNAKE_CASE : Optional[Any] = position_biased_input # Backwards compatibility if type(UpperCamelCase__ ) == str: SCREAMING_SNAKE_CASE : Optional[int] = [x.strip() for x in pos_att_type.lower().split('''|''' )] SCREAMING_SNAKE_CASE : Any = pos_att_type SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = kwargs.get('''pooler_hidden_size''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pooler_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = pooler_hidden_act class lowercase__ ( UpperCamelCase_): @property def __A ( self : Union[str, Any] ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Dict , UpperCamelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().generate_dummy_inputs(preprocessor=UpperCamelCase__ , framework=UpperCamelCase__ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
34
1
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def A ( ): SCREAMING_SNAKE_CASE : Union[str, Any] = ArgumentParser( description=( '''PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes''' ) ) # Optional arguments for the launch helper parser.add_argument('''--num_cores''' , type=_lowercase , default=1 , help='''Number of TPU cores to use (1 or 8).''' ) # positional parser.add_argument( '''training_script''' , type=_lowercase , help=( '''The full path to the single TPU training ''' '''program/script to be launched in parallel, ''' '''followed by all the arguments for the ''' '''training script''' ) , ) # rest from the training program parser.add_argument('''training_script_args''' , nargs=_lowercase ) return parser.parse_args() def A ( ): SCREAMING_SNAKE_CASE : Optional[int] = parse_args() # Import training_script as a module. SCREAMING_SNAKE_CASE : Optional[Any] = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) SCREAMING_SNAKE_CASE : Dict = script_fpath.stem SCREAMING_SNAKE_CASE : List[str] = importlib.import_module(_lowercase ) # Patch sys.argv SCREAMING_SNAKE_CASE : Union[str, Any] = [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
34
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() __UpperCamelCase : str = logging.get_logger(__name__) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Any = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(_lowercase , _lowercase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(_lowercase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : str = '''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" SCREAMING_SNAKE_CASE : Optional[int] = BitConfig( conv_layer=_lowercase , num_labels=1_000 , idalabel=_lowercase , labelaid=_lowercase , ) return config def A ( _lowercase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''stem.conv''' , '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''blocks''' , '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''head.fc''' , '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE : str = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''bit.encoder.''' + name return name def A ( ): SCREAMING_SNAKE_CASE : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ) return im @torch.no_grad() def A ( _lowercase , _lowercase , _lowercase=False ): SCREAMING_SNAKE_CASE : List[Any] = get_config(_lowercase ) # load original model from timm SCREAMING_SNAKE_CASE : Optional[Any] = create_model(_lowercase , pretrained=_lowercase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE : Optional[int] = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Dict = state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE : str = BitForImageClassification(_lowercase ) model.eval() model.load_state_dict(_lowercase ) # create image processor SCREAMING_SNAKE_CASE : Optional[Any] = create_transform(**resolve_data_config({} , model=_lowercase ) ) SCREAMING_SNAKE_CASE : List[str] = transform.transforms SCREAMING_SNAKE_CASE : Union[str, Any] = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE : Tuple = BitImageProcessor( do_resize=_lowercase , size={'''shortest_edge''': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_lowercase , crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} , do_normalize=_lowercase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Union[str, Any] = transform(_lowercase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE : Optional[int] = processor(_lowercase , return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(_lowercase , _lowercase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = outputs.logits print('''Logits:''' , logits[0, :3] ) print('''Predicted class:''' , model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE : List[Any] = timm_model(_lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowercase , outputs.logits , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_lowercase ).mkdir(exist_ok=_lowercase ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowercase ) processor.save_pretrained(_lowercase ) 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__": __UpperCamelCase : 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.', ) __UpperCamelCase : Optional[int] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
1
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 __UpperCamelCase : List[str] = [ 'openmmlab/upernet-convnext-tiny', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCamelCase : Optional[Any] = 'UperNetConfig' class lowercase__ ( nn.Module): def __init__( self : Any , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : Union[int, Tuple[int, int]] , UpperCamelCase__ : Union[int, Tuple[int, int], str] = 0 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Union[int, Tuple[int, int]] = 1 , ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Convad( in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , bias=UpperCamelCase__ , dilation=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Optional[int] = nn.BatchNormad(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = nn.ReLU() def __A ( self : Tuple , UpperCamelCase__ : torch.Tensor ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.conv(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.batch_norm(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.activation(UpperCamelCase__ ) return output class lowercase__ ( nn.Module): def __init__( self : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Dict = [ nn.AdaptiveAvgPoolad(UpperCamelCase__ ), UperNetConvModule(UpperCamelCase__ , UpperCamelCase__ , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(UpperCamelCase__ ) , UpperCamelCase__ ) def __A ( self : List[Any] , UpperCamelCase__ : torch.Tensor ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = input for layer in self.layers: SCREAMING_SNAKE_CASE : int = layer(UpperCamelCase__ ) return hidden_state class lowercase__ ( nn.Module): def __init__( self : str , UpperCamelCase__ : Tuple[int, ...] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : bool ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Any = pool_scales SCREAMING_SNAKE_CASE : List[Any] = align_corners SCREAMING_SNAKE_CASE : List[Any] = in_channels SCREAMING_SNAKE_CASE : str = channels SCREAMING_SNAKE_CASE : List[Any] = [] for i, pool_scale in enumerate(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=UpperCamelCase__ , in_channels=UpperCamelCase__ , channels=UpperCamelCase__ ) self.blocks.append(UpperCamelCase__ ) self.add_module(str(UpperCamelCase__ ) , UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : torch.Tensor ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [] for ppm in self.blocks: SCREAMING_SNAKE_CASE : Tuple = ppm(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = nn.functional.interpolate( UpperCamelCase__ , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(UpperCamelCase__ ) return ppm_outs class lowercase__ ( nn.Module): def __init__( self : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : str = config SCREAMING_SNAKE_CASE : Dict = config.pool_scales # e.g. (1, 2, 3, 6) SCREAMING_SNAKE_CASE : Any = in_channels SCREAMING_SNAKE_CASE : Optional[Any] = config.hidden_size SCREAMING_SNAKE_CASE : Dict = False SCREAMING_SNAKE_CASE : int = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module SCREAMING_SNAKE_CASE : Tuple = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) SCREAMING_SNAKE_CASE : Optional[Any] = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module SCREAMING_SNAKE_CASE : Tuple = nn.ModuleList() SCREAMING_SNAKE_CASE : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer SCREAMING_SNAKE_CASE : int = UperNetConvModule(UpperCamelCase__ , self.channels , kernel_size=1 ) SCREAMING_SNAKE_CASE : List[str] = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(UpperCamelCase__ ) self.fpn_convs.append(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def __A ( self : Optional[int] ): '''simple docstring''' self.apply(self._init_weights ) def __A ( self : Union[str, Any] , UpperCamelCase__ : Tuple ): '''simple docstring''' if isinstance(UpperCamelCase__ , 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 __A ( self : Any , UpperCamelCase__ : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = inputs[-1] SCREAMING_SNAKE_CASE : List[Any] = [x] psp_outs.extend(self.psp_modules(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(UpperCamelCase__ , dim=1 ) SCREAMING_SNAKE_CASE : List[str] = self.bottleneck(UpperCamelCase__ ) return output def __A ( self : List[Any] , UpperCamelCase__ : torch.Tensor ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(UpperCamelCase__ ) ) # build top-down path SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) for i in range(used_backbone_levels - 1 , 0 , -1 ): SCREAMING_SNAKE_CASE : List[Any] = laterals[i - 1].shape[2:] SCREAMING_SNAKE_CASE : str = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=UpperCamelCase__ , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs SCREAMING_SNAKE_CASE : List[str] = [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 ): SCREAMING_SNAKE_CASE : List[Any] = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) SCREAMING_SNAKE_CASE : Any = torch.cat(UpperCamelCase__ , dim=1 ) SCREAMING_SNAKE_CASE : Optional[Any] = self.fpn_bottleneck(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.classifier(UpperCamelCase__ ) return output class lowercase__ ( nn.Module): def __init__( self : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : int = 2 , UpperCamelCase__ : int = 3 , UpperCamelCase__ : Union[int, Tuple[int, int]] = 1 ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Dict = config SCREAMING_SNAKE_CASE : List[str] = config.auxiliary_in_channels SCREAMING_SNAKE_CASE : Optional[Any] = config.auxiliary_channels SCREAMING_SNAKE_CASE : int = config.auxiliary_num_convs SCREAMING_SNAKE_CASE : Union[str, Any] = config.auxiliary_concat_input SCREAMING_SNAKE_CASE : List[str] = in_index SCREAMING_SNAKE_CASE : Tuple = (kernel_size // 2) * dilation SCREAMING_SNAKE_CASE : str = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , dilation=UpperCamelCase__ ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , dilation=UpperCamelCase__ ) ) if self.num_convs == 0: SCREAMING_SNAKE_CASE : Optional[int] = nn.Identity() else: SCREAMING_SNAKE_CASE : List[Any] = nn.Sequential(*UpperCamelCase__ ) if self.concat_input: SCREAMING_SNAKE_CASE : Any = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=UpperCamelCase__ , padding=kernel_size // 2 ) SCREAMING_SNAKE_CASE : List[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def __A ( self : List[Any] ): '''simple docstring''' self.apply(self._init_weights ) def __A ( self : Optional[Any] , UpperCamelCase__ : Tuple ): '''simple docstring''' if isinstance(UpperCamelCase__ , 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 __A ( self : Dict , UpperCamelCase__ : torch.Tensor ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = encoder_hidden_states[self.in_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.convs(UpperCamelCase__ ) if self.concat_input: SCREAMING_SNAKE_CASE : Optional[Any] = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) SCREAMING_SNAKE_CASE : Optional[int] = self.classifier(UpperCamelCase__ ) return output class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = UperNetConfig UpperCamelCase_ = """pixel_values""" UpperCamelCase_ = True def __A ( self : Optional[int] , UpperCamelCase__ : Dict ): '''simple docstring''' if isinstance(UpperCamelCase__ , UpperCamelCase__ ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def __A ( self : List[str] ): '''simple docstring''' self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def __A ( self : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : int=False ): '''simple docstring''' if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = value __UpperCamelCase : str = R'\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' __UpperCamelCase : int = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.""" , UpperCamelCase_ , ) class lowercase__ ( UpperCamelCase_): def __init__( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' super().__init__(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) SCREAMING_SNAKE_CASE : List[Any] = UperNetHead(UpperCamelCase__ , in_channels=self.backbone.channels ) SCREAMING_SNAKE_CASE : List[Any] = UperNetFCNHead(UpperCamelCase__ ) 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=UpperCamelCase__ , config_class=_CONFIG_FOR_DOC ) def __A ( self : List[str] , UpperCamelCase__ : Optional[torch.Tensor] = None , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : Optional[torch.Tensor] = None , UpperCamelCase__ : Optional[bool] = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE : Dict = output_attentions if output_attentions is not None else self.config.output_attentions SCREAMING_SNAKE_CASE : Optional[Any] = self.backbone.forward_with_filtered_kwargs( UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , output_attentions=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = outputs.feature_maps SCREAMING_SNAKE_CASE : Optional[int] = self.decode_head(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = nn.functional.interpolate(UpperCamelCase__ , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = None if self.auxiliary_head is not None: SCREAMING_SNAKE_CASE : Any = self.auxiliary_head(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = nn.functional.interpolate( UpperCamelCase__ , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = 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 SCREAMING_SNAKE_CASE : str = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) SCREAMING_SNAKE_CASE : Union[str, Any] = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: SCREAMING_SNAKE_CASE : Optional[Any] = (logits,) + outputs[1:] else: SCREAMING_SNAKE_CASE : Optional[Any] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=UpperCamelCase__ , logits=UpperCamelCase__ , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
34
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow __UpperCamelCase : str = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ 'text-classification', 'language-modeling', 'summarization', 'token-classification', 'question-answering', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) __UpperCamelCase : int = logging.getLogger() def A ( ): SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() parser.add_argument('''-f''' ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() return args.f def A ( _lowercase , _lowercase="eval" ): SCREAMING_SNAKE_CASE : Dict = os.path.join(_lowercase , f"""{split}_results.json""" ) if os.path.exists(_lowercase ): with open(_lowercase , '''r''' ) as f: return json.load(_lowercase ) raise ValueError(f"""can't find {path}""" ) __UpperCamelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase__ ( UpperCamelCase_): def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Tuple = f""" run_glue.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 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_glue.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : str = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_clm_flax.main() SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_summarization_flax.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f""" run_mlm.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} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_mlm_flax.main() SCREAMING_SNAKE_CASE : List[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_ta_mlm_flax.main() SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Any = f""" run_flax_ner.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} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_ner.main() SCREAMING_SNAKE_CASE : List[str] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_qa.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} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_qa.main() SCREAMING_SNAKE_CASE : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
34
1
import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = dataset SCREAMING_SNAKE_CASE : Optional[Any] = process SCREAMING_SNAKE_CASE : Union[str, Any] = params def __len__( self : Tuple ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dataset[i] SCREAMING_SNAKE_CASE : Optional[int] = self.process(UpperCamelCase__ , **self.params ) return processed class lowercase__ ( UpperCamelCase_): def __init__( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = loader SCREAMING_SNAKE_CASE : List[Any] = infer SCREAMING_SNAKE_CASE : int = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : List[str] = loader_batch_size # Internal bookkeeping SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None def __len__( self : int ): '''simple docstring''' return len(self.loader ) def __iter__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice SCREAMING_SNAKE_CASE : Optional[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Convert ModelOutput to tuple first SCREAMING_SNAKE_CASE : Dict = element.to_tuple() if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : List[str] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around SCREAMING_SNAKE_CASE : int = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Union[str, Any] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Tuple = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. SCREAMING_SNAKE_CASE : Tuple = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 SCREAMING_SNAKE_CASE : Any = self._loader_batch_data.__class__(UpperCamelCase__ ) self._loader_batch_index += 1 return result def __A ( self : Union[str, Any] ): '''simple docstring''' if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch SCREAMING_SNAKE_CASE : Tuple = next(self.iterator ) SCREAMING_SNAKE_CASE : List[Any] = self.infer(UpperCamelCase__ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Optional[int] = processed else: SCREAMING_SNAKE_CASE : int = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : int = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[Any] = observed_batch_size # Setting internal index to unwrap the batch SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : int = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=None ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __iter__( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = iter(self.loader ) SCREAMING_SNAKE_CASE : List[Any] = None return self def __A ( self : List[str] ): '''simple docstring''' if self.subiterator is None: SCREAMING_SNAKE_CASE : Dict = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item SCREAMING_SNAKE_CASE : Any = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators SCREAMING_SNAKE_CASE : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = next(self.subiterator ) return processed class lowercase__ ( UpperCamelCase_): def __iter__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Optional[int] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Tuple = self.loader_batch_item() SCREAMING_SNAKE_CASE : Any = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator while not is_last: SCREAMING_SNAKE_CASE : Any = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = processed else: SCREAMING_SNAKE_CASE : Union[str, Any] = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : List[str] = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[str] = observed_batch_size SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : str = 0 while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Any = self.loader_batch_item() SCREAMING_SNAKE_CASE : List[Any] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator else: SCREAMING_SNAKE_CASE : int = processed SCREAMING_SNAKE_CASE : List[str] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) return accumulator class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Dict = key def __len__( self : Optional[int] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' return self.dataset[i][self.key] class lowercase__ ( UpperCamelCase_): def __init__( self : List[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : List[str] = keya SCREAMING_SNAKE_CASE : Tuple = keya def __len__( self : List[str] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
34
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset __UpperCamelCase : Dict = random.Random() def A ( _lowercase , _lowercase=1.0 , _lowercase=None , _lowercase=None ): if rng is None: SCREAMING_SNAKE_CASE : Any = global_rng SCREAMING_SNAKE_CASE : int = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase__ ( unittest.TestCase): def __init__( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : str=7 , UpperCamelCase__ : Any=400 , UpperCamelCase__ : List[str]=2000 , UpperCamelCase__ : List[Any]=2048 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Union[str, Any]=1 , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : str=30 , UpperCamelCase__ : Tuple=4_4100 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : str = batch_size SCREAMING_SNAKE_CASE : str = min_seq_length SCREAMING_SNAKE_CASE : Dict = max_seq_length SCREAMING_SNAKE_CASE : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE : Optional[Any] = spectrogram_length SCREAMING_SNAKE_CASE : Optional[int] = feature_size SCREAMING_SNAKE_CASE : Tuple = num_audio_channels SCREAMING_SNAKE_CASE : Union[str, Any] = hop_length SCREAMING_SNAKE_CASE : List[Any] = chunk_length SCREAMING_SNAKE_CASE : str = sampling_rate def __A ( self : Optional[Any] ): '''simple docstring''' return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def __A ( self : Tuple , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Optional[int]=False ): '''simple docstring''' def _flatten(UpperCamelCase__ : str ): return list(itertools.chain(*UpperCamelCase__ ) ) if equal_length: SCREAMING_SNAKE_CASE : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE : int = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE : Optional[Any] = [np.asarray(UpperCamelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = TvltFeatureExtractor def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TvltFeatureExtractionTester(self ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''spectrogram_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''feature_size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''num_audio_channels''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''hop_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''chunk_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''sampling_rate''' ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : Optional[int] = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Optional[int] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Tuple = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : int = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Any = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] SCREAMING_SNAKE_CASE : Optional[int] = [np.asarray(UpperCamelCase__ ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched SCREAMING_SNAKE_CASE : Optional[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking SCREAMING_SNAKE_CASE : List[str] = feature_extractor( UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 , mask_audio=UpperCamelCase__ ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE : Optional[Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE : Dict = np.asarray(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def __A ( self : Optional[int] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE : Dict = ds.sort('''id''' ).select(range(UpperCamelCase__ ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE : int = TvltFeatureExtractor() SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) SCREAMING_SNAKE_CASE : str = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , UpperCamelCase__ , atol=1E-4 ) )
34
1
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 lowercase__ ( unittest.TestCase): def __init__( self : Union[str, Any] , UpperCamelCase__ : str , UpperCamelCase__ : Union[str, Any]=13 , UpperCamelCase__ : List[str]=7 , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : int=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=99 , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : str=5 , UpperCamelCase__ : Optional[Any]=4 , UpperCamelCase__ : int=37 , UpperCamelCase__ : List[str]="gelu" , UpperCamelCase__ : Union[str, Any]=0.1 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : str=512 , UpperCamelCase__ : Union[str, Any]=16 , UpperCamelCase__ : Tuple=2 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : Tuple=4 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = parent SCREAMING_SNAKE_CASE : Dict = batch_size SCREAMING_SNAKE_CASE : List[Any] = seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Optional[int] = use_attention_mask SCREAMING_SNAKE_CASE : str = use_token_type_ids SCREAMING_SNAKE_CASE : Union[str, Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : str = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Optional[int] = type_sequence_label_size SCREAMING_SNAKE_CASE : Dict = initializer_range SCREAMING_SNAKE_CASE : List[Any] = num_choices def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_attention_mask: SCREAMING_SNAKE_CASE : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = 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 __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = config_and_inputs SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : List[Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) SCREAMING_SNAKE_CASE : List[Any] = 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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = True UpperCamelCase_ = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = FlaxRobertaModelTester(self ) @slow def __A ( self : int ): '''simple docstring''' for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = model_class_name.from_pretrained('''roberta-base''' , from_pt=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCamelCase__ )
34
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowercase__ ( UpperCamelCase_ , UpperCamelCase_): UpperCamelCase_ = 1 @register_to_config def __init__( self : List[str] , UpperCamelCase__ : int = 1000 , UpperCamelCase__ : Optional[Union[np.ndarray, List[float]]] = None ): '''simple docstring''' self.set_timesteps(UpperCamelCase__ ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : str = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. SCREAMING_SNAKE_CASE : Tuple = 4 # running values SCREAMING_SNAKE_CASE : int = [] def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, torch.device] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = num_inference_steps SCREAMING_SNAKE_CASE : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: SCREAMING_SNAKE_CASE : int = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE : Optional[Any] = torch.sin(steps * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE : Dict = (1.0 - self.betas**2) ** 0.5 SCREAMING_SNAKE_CASE : Optional[Any] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] SCREAMING_SNAKE_CASE : List[str] = timesteps.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = [] def __A ( self : Tuple , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : int , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : bool = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) SCREAMING_SNAKE_CASE : Optional[int] = (self.timesteps == timestep).nonzero().item() SCREAMING_SNAKE_CASE : Union[str, Any] = timestep_index + 1 SCREAMING_SNAKE_CASE : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCamelCase__ ) if len(self.ets ) == 1: SCREAMING_SNAKE_CASE : Dict = self.ets[-1] elif len(self.ets ) == 2: SCREAMING_SNAKE_CASE : Optional[int] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: SCREAMING_SNAKE_CASE : str = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: SCREAMING_SNAKE_CASE : Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) SCREAMING_SNAKE_CASE : Optional[int] = self._get_prev_sample(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : torch.FloatTensor , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return sample def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.alphas[timestep_index] SCREAMING_SNAKE_CASE : List[str] = self.betas[timestep_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas[prev_timestep_index] SCREAMING_SNAKE_CASE : Tuple = self.betas[prev_timestep_index] SCREAMING_SNAKE_CASE : Dict = (sample - sigma * ets) / max(UpperCamelCase__ , 1E-8 ) SCREAMING_SNAKE_CASE : Optional[Any] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
34
1
import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[int] , UpperCamelCase__ : WhisperForConditionalGeneration , UpperCamelCase__ : WhisperProcessor , UpperCamelCase__ : AutoencoderKL , UpperCamelCase__ : CLIPTextModel , UpperCamelCase__ : CLIPTokenizer , UpperCamelCase__ : UNetaDConditionModel , UpperCamelCase__ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , UpperCamelCase__ : StableDiffusionSafetyChecker , UpperCamelCase__ : CLIPImageProcessor , ): '''simple docstring''' super().__init__() if safety_checker is None: logger.warning( f"""You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure""" ''' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered''' ''' results in services or applications open to the public. Both the diffusers team and Hugging Face''' ''' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling''' ''' it only for use-cases that involve analyzing network behavior or auditing its results. For more''' ''' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .''' ) self.register_modules( speech_model=UpperCamelCase__ , speech_processor=UpperCamelCase__ , vae=UpperCamelCase__ , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ , unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , feature_extractor=UpperCamelCase__ , ) def __A ( self : Any , UpperCamelCase__ : Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": SCREAMING_SNAKE_CASE : Dict = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCamelCase__ ) def __A ( self : Dict ): '''simple docstring''' self.enable_attention_slicing(UpperCamelCase__ ) @torch.no_grad() def __call__( self : List[str] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Any=1_6000 , UpperCamelCase__ : int = 512 , UpperCamelCase__ : int = 512 , UpperCamelCase__ : int = 50 , UpperCamelCase__ : float = 7.5 , UpperCamelCase__ : Optional[Union[str, List[str]]] = None , UpperCamelCase__ : Optional[int] = 1 , UpperCamelCase__ : float = 0.0 , UpperCamelCase__ : Optional[torch.Generator] = None , UpperCamelCase__ : Optional[torch.FloatTensor] = None , UpperCamelCase__ : Optional[str] = "pil" , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCamelCase__ : int = 1 , **UpperCamelCase__ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.speech_processor.feature_extractor( UpperCamelCase__ , return_tensors='''pt''' , sampling_rate=UpperCamelCase__ ).input_features.to(self.device ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.speech_model.generate(UpperCamelCase__ , max_length=48_0000 ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.speech_processor.tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ , normalize=UpperCamelCase__ )[ 0 ] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : int = 1 elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : int = len(UpperCamelCase__ ) else: raise ValueError(f"""`prompt` has to be of type `str` or `list` but is {type(UpperCamelCase__ )}""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or callback_steps <= 0) ): raise ValueError( f"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" f""" {type(UpperCamelCase__ )}.""" ) # get prompt text embeddings SCREAMING_SNAKE_CASE : Any = self.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : List[str] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = text_input_ids[:, : self.tokenizer.model_max_length] SCREAMING_SNAKE_CASE : Optional[int] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = text_embeddings.shape SCREAMING_SNAKE_CASE : int = text_embeddings.repeat(1 , UpperCamelCase__ , 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = text_embeddings.view(bs_embed * num_images_per_prompt , UpperCamelCase__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. SCREAMING_SNAKE_CASE : Tuple = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : List[str] if negative_prompt is None: SCREAMING_SNAKE_CASE : List[str] = [''''''] * batch_size elif type(UpperCamelCase__ ) is not type(UpperCamelCase__ ): raise TypeError( f"""`negative_prompt` should be the same type to `prompt`, but got {type(UpperCamelCase__ )} !=""" f""" {type(UpperCamelCase__ )}.""" ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [negative_prompt] elif batch_size != len(UpperCamelCase__ ): raise ValueError( f"""`negative_prompt`: {negative_prompt} has batch size {len(UpperCamelCase__ )}, but `prompt`:""" f""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" ''' the batch size of `prompt`.''' ) else: SCREAMING_SNAKE_CASE : Dict = negative_prompt SCREAMING_SNAKE_CASE : int = text_input_ids.shape[-1] SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE : Optional[Any] = uncond_embeddings.shape[1] SCREAMING_SNAKE_CASE : int = uncond_embeddings.repeat(1 , UpperCamelCase__ , 1 ) SCREAMING_SNAKE_CASE : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , UpperCamelCase__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes SCREAMING_SNAKE_CASE : str = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. SCREAMING_SNAKE_CASE : Dict = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) SCREAMING_SNAKE_CASE : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps SCREAMING_SNAKE_CASE : str = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device='''cpu''' , dtype=UpperCamelCase__ ).to( self.device ) else: SCREAMING_SNAKE_CASE : List[str] = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device=self.device , dtype=UpperCamelCase__ ) else: if latents.shape != latents_shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) SCREAMING_SNAKE_CASE : List[Any] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(UpperCamelCase__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand SCREAMING_SNAKE_CASE : Tuple = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler SCREAMING_SNAKE_CASE : Any = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] SCREAMING_SNAKE_CASE : Optional[Any] = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) SCREAMING_SNAKE_CASE : List[Any] = {} if accepts_eta: SCREAMING_SNAKE_CASE : str = eta for i, t in enumerate(self.progress_bar(UpperCamelCase__ ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler.scale_model_input(UpperCamelCase__ , UpperCamelCase__ ) # predict the noise residual SCREAMING_SNAKE_CASE : Optional[int] = self.unet(UpperCamelCase__ , UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ ).sample # perform guidance if do_classifier_free_guidance: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE : str = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE : List[str] = self.scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = 1 / 0.1_8215 * latents SCREAMING_SNAKE_CASE : Optional[Any] = self.vae.decode(UpperCamelCase__ ).sample SCREAMING_SNAKE_CASE : List[str] = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 SCREAMING_SNAKE_CASE : str = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Optional[int] = self.numpy_to_pil(UpperCamelCase__ ) if not return_dict: return image return StableDiffusionPipelineOutput(images=UpperCamelCase__ , nsfw_content_detected=UpperCamelCase__ )
34
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
1
import argparse import struct import unittest class lowercase__ : def __init__( self : List[str] , UpperCamelCase__ : bytes ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = data # Initialize hash values SCREAMING_SNAKE_CASE : int = [ 0X6a_09_e6_67, 0Xbb_67_ae_85, 0X3c_6e_f3_72, 0Xa5_4f_f5_3a, 0X51_0e_52_7f, 0X9b_05_68_8c, 0X1f_83_d9_ab, 0X5b_e0_cd_19, ] # Initialize round constants SCREAMING_SNAKE_CASE : List[str] = [ 0X42_8a_2f_98, 0X71_37_44_91, 0Xb5_c0_fb_cf, 0Xe9_b5_db_a5, 0X39_56_c2_5b, 0X59_f1_11_f1, 0X92_3f_82_a4, 0Xab_1c_5e_d5, 0Xd8_07_aa_98, 0X12_83_5b_01, 0X24_31_85_be, 0X55_0c_7d_c3, 0X72_be_5d_74, 0X80_de_b1_fe, 0X9b_dc_06_a7, 0Xc1_9b_f1_74, 0Xe4_9b_69_c1, 0Xef_be_47_86, 0X0f_c1_9d_c6, 0X24_0c_a1_cc, 0X2d_e9_2c_6f, 0X4a_74_84_aa, 0X5c_b0_a9_dc, 0X76_f9_88_da, 0X98_3e_51_52, 0Xa8_31_c6_6d, 0Xb0_03_27_c8, 0Xbf_59_7f_c7, 0Xc6_e0_0b_f3, 0Xd5_a7_91_47, 0X06_ca_63_51, 0X14_29_29_67, 0X27_b7_0a_85, 0X2e_1b_21_38, 0X4d_2c_6d_fc, 0X53_38_0d_13, 0X65_0a_73_54, 0X76_6a_0a_bb, 0X81_c2_c9_2e, 0X92_72_2c_85, 0Xa2_bf_e8_a1, 0Xa8_1a_66_4b, 0Xc2_4b_8b_70, 0Xc7_6c_51_a3, 0Xd1_92_e8_19, 0Xd6_99_06_24, 0Xf4_0e_35_85, 0X10_6a_a0_70, 0X19_a4_c1_16, 0X1e_37_6c_08, 0X27_48_77_4c, 0X34_b0_bc_b5, 0X39_1c_0c_b3, 0X4e_d8_aa_4a, 0X5b_9c_ca_4f, 0X68_2e_6f_f3, 0X74_8f_82_ee, 0X78_a5_63_6f, 0X84_c8_78_14, 0X8c_c7_02_08, 0X90_be_ff_fa, 0Xa4_50_6c_eb, 0Xbe_f9_a3_f7, 0Xc6_71_78_f2, ] SCREAMING_SNAKE_CASE : Optional[int] = self.preprocessing(self.data ) self.final_hash() @staticmethod def __A ( UpperCamelCase__ : bytes ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = B'''\x80''' + (B'''\x00''' * (63 - (len(UpperCamelCase__ ) + 8) % 64)) SCREAMING_SNAKE_CASE : int = struct.pack('''>Q''' , (len(UpperCamelCase__ ) * 8) ) return data + padding + big_endian_integer def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers SCREAMING_SNAKE_CASE : Optional[Any] = list(struct.unpack('''>16L''' , UpperCamelCase__ ) ) # add 48 0-ed integers words += [0] * 48 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array SCREAMING_SNAKE_CASE : Optional[int] = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) SCREAMING_SNAKE_CASE : int = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) SCREAMING_SNAKE_CASE : Any = ( words[index - 16] + sa + words[index - 7] + sa ) % 0X1_00_00_00_00 # Compression SCREAMING_SNAKE_CASE : Any = self.ror(UpperCamelCase__ , 6 ) ^ self.ror(UpperCamelCase__ , 11 ) ^ self.ror(UpperCamelCase__ , 25 ) SCREAMING_SNAKE_CASE : List[str] = (e & f) ^ ((~e & 0Xff_ff_ff_ff) & g) SCREAMING_SNAKE_CASE : Optional[int] = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0X1_00_00_00_00 SCREAMING_SNAKE_CASE : List[Any] = self.ror(UpperCamelCase__ , 2 ) ^ self.ror(UpperCamelCase__ , 13 ) ^ self.ror(UpperCamelCase__ , 22 ) SCREAMING_SNAKE_CASE : str = (a & b) ^ (a & c) ^ (b & c) SCREAMING_SNAKE_CASE : Tuple = (sa + maj) % 0X1_00_00_00_00 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = ( g, f, e, ((d + tempa) % 0X1_00_00_00_00), c, b, a, ((tempa + tempa) % 0X1_00_00_00_00), ) SCREAMING_SNAKE_CASE : Any = [a, b, c, d, e, f, g, h] # Modify final values SCREAMING_SNAKE_CASE : Tuple = [ ((element + mutated_hash_values[index]) % 0X1_00_00_00_00) for index, element in enumerate(self.hashes ) ] SCREAMING_SNAKE_CASE : Optional[Any] = ''''''.join([hex(UpperCamelCase__ )[2:].zfill(8 ) for value in self.hashes] ) def __A ( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' return 0Xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations) class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' import hashlib SCREAMING_SNAKE_CASE : Dict = bytes('''Test String''' , '''utf-8''' ) self.assertEqual(SHAaaa(UpperCamelCase__ ).hash , hashlib.shaaaa(UpperCamelCase__ ).hexdigest() ) def A ( ): import doctest doctest.testmod() SCREAMING_SNAKE_CASE : Optional[int] = argparse.ArgumentParser() parser.add_argument( '''-s''' , '''--string''' , dest='''input_string''' , default='''Hello World!! Welcome to Cryptography''' , help='''Hash the string''' , ) parser.add_argument( '''-f''' , '''--file''' , dest='''input_file''' , help='''Hash contents of a file''' ) SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE : Dict = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , '''rb''' ) as f: SCREAMING_SNAKE_CASE : int = f.read() else: SCREAMING_SNAKE_CASE : Dict = bytes(_lowercase , '''utf-8''' ) print(SHAaaa(_lowercase ).hash ) if __name__ == "__main__": main()
34
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
1
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 __UpperCamelCase : Optional[int] = logging.get_logger(__name__) @add_end_docstrings(UpperCamelCase_) class lowercase__ ( UpperCamelCase_): def __init__( self : Union[str, Any] , **UpperCamelCase__ : int ): '''simple docstring''' 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 : Tuple , UpperCamelCase__ : Union[str, List[str], "Image", List["Image"]] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return super().__call__(UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : List[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = {} if "candidate_labels" in kwargs: SCREAMING_SNAKE_CASE : Dict = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: SCREAMING_SNAKE_CASE : int = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Any="This is a photo of {}." ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = load_image(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.image_processor(images=[image] , return_tensors=self.framework ) SCREAMING_SNAKE_CASE : Optional[Any] = candidate_labels SCREAMING_SNAKE_CASE : List[Any] = [hypothesis_template.format(UpperCamelCase__ ) for x in candidate_labels] SCREAMING_SNAKE_CASE : Dict = self.tokenizer(UpperCamelCase__ , return_tensors=self.framework , padding=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = [text_inputs] return inputs def __A ( self : Optional[Any] , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = model_inputs.pop('''candidate_labels''' ) SCREAMING_SNAKE_CASE : Optional[int] = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = text_inputs[0] else: # Batching case. SCREAMING_SNAKE_CASE : int = text_inputs[0][0] SCREAMING_SNAKE_CASE : Any = self.model(**UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def __A ( self : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = model_outputs.pop('''candidate_labels''' ) SCREAMING_SNAKE_CASE : str = model_outputs['''logits'''][0] if self.framework == "pt": SCREAMING_SNAKE_CASE : Optional[int] = logits.softmax(dim=-1 ).squeeze(-1 ) SCREAMING_SNAKE_CASE : List[str] = probs.tolist() if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : str = [scores] elif self.framework == "tf": SCREAMING_SNAKE_CASE : Optional[int] = stable_softmax(UpperCamelCase__ , axis=-1 ) SCREAMING_SNAKE_CASE : List[Any] = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) SCREAMING_SNAKE_CASE : Any = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(UpperCamelCase__ , UpperCamelCase__ ) , key=lambda UpperCamelCase__ : -x[0] ) ] return result
34
import random def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowercase ) elif element > pivot: greater.append(_lowercase ) else: equal.append(_lowercase ) return less, equal, greater def A ( _lowercase , _lowercase ): # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(_lowercase ) or index < 0: return None SCREAMING_SNAKE_CASE : Dict = items[random.randint(0 , len(_lowercase ) - 1 )] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = _partition(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = len(_lowercase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowercase , _lowercase ) # must be in larger else: return quick_select(_lowercase , index - (m + count) )
34
1
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 __UpperCamelCase : Union[str, Any] = 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 lowercase__ ( UpperCamelCase_): def __init__( self : Union[str, Any] , UpperCamelCase__ : int = 101 ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = length def __len__( self : Dict ): '''simple docstring''' return self.length def __getitem__( self : int , UpperCamelCase__ : str ): '''simple docstring''' return i class lowercase__ : def __call__( self : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' return {"input_ids": torch.tensor(UpperCamelCase__ ), "labels": torch.tensor(UpperCamelCase__ )} class lowercase__ ( nn.Module): def __init__( self : List[str] ): '''simple docstring''' super().__init__() # Add some (unused) params otherwise DDP will complain. SCREAMING_SNAKE_CASE : Dict = nn.Linear(120 , 80 ) def __A ( self : List[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any=None ): '''simple docstring''' if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowercase__ ( UpperCamelCase_): @require_torch_neuroncore def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = f"""--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py """.split() SCREAMING_SNAKE_CASE : Tuple = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Optional[int] = f"""--output_dir {output_dir}""".split() SCREAMING_SNAKE_CASE : Optional[int] = ['''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 lowercase__ ( UpperCamelCase_): @require_torch_multi_gpu def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = f"""--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py """.split() SCREAMING_SNAKE_CASE : Any = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : List[Any] = f"""--output_dir {output_dir}""".split() SCREAMING_SNAKE_CASE : str = ['''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 __UpperCamelCase : Any = HfArgumentParser((TrainingArguments,)) __UpperCamelCase : 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]: __UpperCamelCase : List[str] = DummyDataset(dataset_length) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = list(range(len(_lowercase ) ) ) SCREAMING_SNAKE_CASE : List[str] = 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} __UpperCamelCase : Any = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __UpperCamelCase : str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __UpperCamelCase : Tuple = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __UpperCamelCase : Any = 2 __UpperCamelCase : Any = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __UpperCamelCase : Optional[int] = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __UpperCamelCase : List[str] = None
34
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Tuple = logging.get_logger(__name__) # TODO Update this __UpperCamelCase : List[str] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """esm""" def __init__( self : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Any=768 , UpperCamelCase__ : Optional[Any]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : Optional[int]=3072 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1026 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Any=1E-12 , UpperCamelCase__ : Dict="absolute" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[int]=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Optional[Any]=None , **UpperCamelCase__ : Any , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : Dict = emb_layer_norm_before SCREAMING_SNAKE_CASE : List[str] = token_dropout SCREAMING_SNAKE_CASE : List[Any] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) SCREAMING_SNAKE_CASE : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = EsmFoldConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) SCREAMING_SNAKE_CASE : Optional[int] = get_default_vocab_list() else: SCREAMING_SNAKE_CASE : Optional[Any] = vocab_list else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = self.esmfold_config.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = None UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 0 UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Optional[int] ): '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE : Optional[Any] = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = TrunkConfig(**self.trunk ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = asdict(self ) SCREAMING_SNAKE_CASE : Tuple = self.trunk.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 48 UpperCamelCase_ = 1_024 UpperCamelCase_ = 128 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 0 UpperCamelCase_ = 0 UpperCamelCase_ = False UpperCamelCase_ = 4 UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Any ): '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE : Optional[int] = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) SCREAMING_SNAKE_CASE : Dict = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE : Tuple = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = asdict(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.structure_module.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 384 UpperCamelCase_ = 128 UpperCamelCase_ = 16 UpperCamelCase_ = 128 UpperCamelCase_ = 12 UpperCamelCase_ = 4 UpperCamelCase_ = 8 UpperCamelCase_ = 0.1 UpperCamelCase_ = 8 UpperCamelCase_ = 1 UpperCamelCase_ = 2 UpperCamelCase_ = 7 UpperCamelCase_ = 10 UpperCamelCase_ = 1E-8 UpperCamelCase_ = 1E5 def __A ( self : Dict ): '''simple docstring''' return asdict(self ) def A ( ): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
34
1
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __UpperCamelCase : Dict = logging.get_logger(__name__) __UpperCamelCase : Dict[Optional[str], Type[Formatter]] = {} __UpperCamelCase : Dict[Optional[str], str] = {} __UpperCamelCase : Dict[Optional[str], Exception] = {} def A ( _lowercase , _lowercase , _lowercase = None , ): SCREAMING_SNAKE_CASE : List[Any] = 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__})""" ) SCREAMING_SNAKE_CASE : Optional[int] = 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})""" ) SCREAMING_SNAKE_CASE : Tuple = format_type def A ( _lowercase , _lowercase , _lowercase = None ): SCREAMING_SNAKE_CASE : Dict = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): SCREAMING_SNAKE_CASE : Tuple = 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: __UpperCamelCase : int = 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: __UpperCamelCase : Union[str, Any] = 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: __UpperCamelCase : str = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def A ( _lowercase ): if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def A ( _lowercase , **_lowercase ): SCREAMING_SNAKE_CASE : Dict = get_format_type_from_alias(_lowercase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**_lowercase ) 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}'""" )
34
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowercase__ ( unittest.TestCase): def __A ( self : Any ): '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=UpperCamelCase__ , ) assert hasattr(self , '''env''' ) def __A ( self : str , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings SCREAMING_SNAKE_CASE : Any = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=UpperCamelCase__ , instance_count=UpperCamelCase__ , instance_type=self.instance_type , debugger_hook_config=UpperCamelCase__ , hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=UpperCamelCase__ , py_version='''py36''' , ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' TrainingJobAnalytics(UpperCamelCase__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def __A ( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.create_estimator(UpperCamelCase__ ) # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[Any] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : List[str] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : List[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_9999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , UpperCamelCase__ )
34
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) __UpperCamelCase : int = { 'facebook/data2vec-text-base': 'https://huggingface.co/data2vec/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """data2vec-text""" def __init__( self : Optional[Any] , UpperCamelCase__ : List[str]=3_0522 , UpperCamelCase__ : Tuple=768 , UpperCamelCase__ : Optional[int]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : int=3072 , UpperCamelCase__ : Any="gelu" , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : Optional[Any]=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : Union[str, Any]=1E-12 , UpperCamelCase__ : Tuple=1 , UpperCamelCase__ : Union[str, Any]=0 , UpperCamelCase__ : Dict=2 , UpperCamelCase__ : Any="absolute" , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : str = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__ ( UpperCamelCase_): @property def __A ( self : Tuple ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : int = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
34
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore __UpperCamelCase : Dict = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" __UpperCamelCase : Tuple = [file for file in filepaths if file != file.lower()] if upper_files: print(f"""{len(upper_files)} files contain uppercase characters:""") print('\n'.join(upper_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if ' ' in file] if space_files: print(f"""{len(space_files)} files contain space characters:""") print('\n'.join(space_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if '-' in file] if hyphen_files: print(f"""{len(hyphen_files)} files contain hyphen characters:""") print('\n'.join(hyphen_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(f"""{len(nodir_files)} files are not in a directory:""") print('\n'.join(nodir_files) + '\n') __UpperCamelCase : Optional[Any] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
34
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __UpperCamelCase : int = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
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: __UpperCamelCase : Dict = None __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Optional[int] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} __UpperCamelCase : Optional[int] = { '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 __UpperCamelCase : Union[str, Any] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = TaTokenizer UpperCamelCase_ = [] def __init__( self : str , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[Any]=100 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[str] = [f"""<extra_id_{i}>""" for i in range(UpperCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE : int = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id_''' in str(UpperCamelCase__ ) ) , UpperCamelCase__ ) ) ) 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__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , extra_ids=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : str = vocab_file SCREAMING_SNAKE_CASE : int = False if not self.vocab_file else True SCREAMING_SNAKE_CASE : str = extra_ids @staticmethod def __A ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE : List[str] = 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.''' , UpperCamelCase__ , ) return max_model_length def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : 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(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = 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__ ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE : Tuple = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, 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 __A ( self : Dict ): '''simple docstring''' return list( set(filter(lambda UpperCamelCase__ : bool(re.search(r'''<extra_id_\d+>''' , UpperCamelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def __A ( self : List[Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(UpperCamelCase__ ) for token in self.get_sentinel_tokens()]
34
1
from ..utils import DummyObject, requires_backends class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Dict , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : Dict , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Any , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : int , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Tuple , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Tuple , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) def A ( *_lowercase , **_lowercase ): requires_backends(_lowercase , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : int , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : int , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Any , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Dict , *UpperCamelCase__ : Any , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : Any , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Union[str, Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : int , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : Any , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Any , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : int , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : int , *UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Any , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : int , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Dict , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : int , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Any , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Tuple , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Any , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : int , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : str , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Any , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : int , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Dict , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : int , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : int , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Union[str, Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : Any , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : Dict , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Any , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : int , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[str] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : Any , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[Any] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Dict , *UpperCamelCase__ : Any , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Union[str, Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Any , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[str] , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : int , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : int , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : str , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : List[str] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : Dict ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : List[Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : int , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[int] , *UpperCamelCase__ : Union[str, Any] , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : int , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : int ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : str , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Tuple , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : str , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Optional[int] ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : Optional[Any] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Any ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowercase__ ( metaclass=UpperCamelCase_): UpperCamelCase_ = ["""torch"""] def __init__( self : Optional[int] , *UpperCamelCase__ : str , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def __A ( cls : Any , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : str ): '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def __A ( cls : List[Any] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Tuple ): '''simple docstring''' requires_backends(cls , ['''torch'''] )
34
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __UpperCamelCase : str = False class lowercase__ ( unittest.TestCase): pass @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Union[str, Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = VersatileDiffusionPipeline.from_pretrained(UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''cyberpunk 2077''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe.dual_guided( prompt=UpperCamelCase__ , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images SCREAMING_SNAKE_CASE : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : int = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe.text_to_image( prompt=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : List[str] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = pipe.image_variation(UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : Optional[int] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
34
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __UpperCamelCase : Optional[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : List[str] = ['NllbTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Tuple = ['NllbTokenizerFast'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __UpperCamelCase : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def A ( _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = analyze_text(_lowercase ) SCREAMING_SNAKE_CASE : Any = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Tuple = sum(single_char_strings.values() ) # one length string SCREAMING_SNAKE_CASE : Tuple = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: SCREAMING_SNAKE_CASE : Tuple = single_char_strings[ch] SCREAMING_SNAKE_CASE : List[str] = my_str / all_sum my_fir_sum += prob * math.loga(_lowercase ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string SCREAMING_SNAKE_CASE : Optional[Any] = sum(two_char_strings.values() ) SCREAMING_SNAKE_CASE : List[str] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : Any = two_char_strings[sequence] SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) / all_sum my_sec_sum += prob * math.loga(_lowercase ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = Counter() # type: ignore SCREAMING_SNAKE_CASE : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_lowercase ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def A ( ): import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
34
1
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class lowercase__ : UpperCamelCase_ = None def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE : Any = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : List[Any] = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Dict = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCamelCase : Tuple = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Any = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
1
from __future__ import annotations from scipy.special import comb # type: ignore class lowercase__ : def __init__( self : Dict , UpperCamelCase__ : list[tuple[float, float]] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) - 1 def __A ( self : List[Any] , UpperCamelCase__ : float ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : list[float] = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree , UpperCamelCase__ ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(UpperCamelCase__ ) , 5 ) == 1 return output_values def __A ( self : Tuple , UpperCamelCase__ : float ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : Dict = self.basis_function(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = 0.0 SCREAMING_SNAKE_CASE : List[Any] = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def __A ( self : Dict , UpperCamelCase__ : float = 0.01 ): '''simple docstring''' from matplotlib import pyplot as plt # type: ignore SCREAMING_SNAKE_CASE : list[float] = [] # x coordinates of points to plot SCREAMING_SNAKE_CASE : list[float] = [] # y coordinates of points to plot SCREAMING_SNAKE_CASE : List[Any] = 0.0 while t <= 1: SCREAMING_SNAKE_CASE : int = self.bezier_curve_function(UpperCamelCase__ ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size SCREAMING_SNAKE_CASE : List[str] = [i[0] for i in self.list_of_points] SCREAMING_SNAKE_CASE : List[Any] = [i[1] for i in self.list_of_points] plt.plot( UpperCamelCase__ , UpperCamelCase__ , color='''blue''' , label='''Curve of Degree ''' + str(self.degree ) , ) plt.scatter(UpperCamelCase__ , UpperCamelCase__ , color='''red''' , label='''Control Points''' ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Optional[int] = logging.get_logger(__name__) __UpperCamelCase : Dict = { 'edbeeching/decision-transformer-gym-hopper-medium': ( 'https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """decision_transformer""" UpperCamelCase_ = ["""past_key_values"""] UpperCamelCase_ = { """max_position_embeddings""": """n_positions""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self : str , UpperCamelCase__ : Optional[int]=17 , UpperCamelCase__ : int=4 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Dict=4096 , UpperCamelCase__ : Union[str, Any]=True , UpperCamelCase__ : str=1 , UpperCamelCase__ : List[Any]=1024 , UpperCamelCase__ : Union[str, Any]=3 , UpperCamelCase__ : Optional[int]=1 , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : Union[str, Any]="relu" , UpperCamelCase__ : Tuple=0.1 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : Optional[int]=1E-5 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : Union[str, Any]=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : List[str]=5_0256 , UpperCamelCase__ : int=5_0256 , UpperCamelCase__ : List[Any]=False , UpperCamelCase__ : Any=False , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = state_dim SCREAMING_SNAKE_CASE : Optional[int] = act_dim SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Any = max_ep_len SCREAMING_SNAKE_CASE : List[str] = action_tanh SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Tuple = n_positions SCREAMING_SNAKE_CASE : List[Any] = n_layer SCREAMING_SNAKE_CASE : str = n_head SCREAMING_SNAKE_CASE : Union[str, Any] = n_inner SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : int = resid_pdrop SCREAMING_SNAKE_CASE : Any = embd_pdrop SCREAMING_SNAKE_CASE : str = attn_pdrop SCREAMING_SNAKE_CASE : Tuple = layer_norm_epsilon SCREAMING_SNAKE_CASE : int = initializer_range SCREAMING_SNAKE_CASE : List[str] = scale_attn_weights SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : List[Any] = scale_attn_by_inverse_layer_idx SCREAMING_SNAKE_CASE : Optional[int] = reorder_and_upcast_attn SCREAMING_SNAKE_CASE : Dict = bos_token_id SCREAMING_SNAKE_CASE : Tuple = eos_token_id super().__init__(bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ )
34
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __UpperCamelCase : Dict = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None ): SCREAMING_SNAKE_CASE : Union[str, Any] = True while ask_again: SCREAMING_SNAKE_CASE : Optional[Any] = input(_lowercase ) try: if default is not None and len(_lowercase ) == 0: return default return convert_value(_lowercase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowercase ) def A ( _lowercase , _lowercase=[] , _lowercase=None , _lowercase=0 ): SCREAMING_SNAKE_CASE : Dict = BulletMenu(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : str = menu.run(default_choice=_lowercase ) return convert_value(_lowercase ) if convert_value is not None else result def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = int(_lowercase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A ( _lowercase ): return {"yes": True, "no": False}[value.lower()] class lowercase__ ( argparse.RawDescriptionHelpFormatter): def __A ( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = super()._format_usage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
34
1
# XXX: we want transformers master here - in the absense of conftest manipulating sys.path: # hack it in for now: import sys from pathlib import Path __UpperCamelCase : Optional[Any] = Path(__file__).resolve().parents[3] / 'src' sys.path.insert(1, str(git_repo_path)) import dataclasses # noqa import io # noqa import itertools # noqa import json # noqa import os # noqa import unittest # noqa from copy import deepcopy # noqa from parameterized import parameterized # noqa from transformers import TrainingArguments, is_torch_available # noqa from transformers.deepspeed import is_deepspeed_available # noqa from transformers.file_utils import WEIGHTS_NAME # noqa from transformers.testing_utils import ( # noqa CaptureLogger, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, mockenv_context, require_deepspeed, require_torch_gpu, require_torch_multi_gpu, slow, ) from transformers.trainer_utils import set_seed # noqa set_seed(42) __UpperCamelCase : str = {'base': 'patrickvonplaten/wav2vec2_tiny_random', 'robust': 'patrickvonplaten/wav2vec2_tiny_random_robust'} __UpperCamelCase : List[str] = 'zero2' __UpperCamelCase : Any = 'zero3' __UpperCamelCase : Any = [ZEROa, ZEROa] def A ( _lowercase , _lowercase , _lowercase ): # customize the test name generator function as we want both params to appear in the sub-test # name, as by default it shows only the first param SCREAMING_SNAKE_CASE : int = parameterized.to_safe_name('''_'''.join(str(_lowercase ) for x in param.args ) ) return f"""{func.__name__}_{param_based_name}""" # Cartesian-product of zero stages with models to test __UpperCamelCase : Optional[int] = list(itertools.product(stages, models.keys())) @slow @require_deepspeed @require_torch_gpu class lowercase__ ( UpperCamelCase_): @parameterized.expand(UpperCamelCase__ , name_func=UpperCamelCase__ ) def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int ): '''simple docstring''' self.run_and_check( stage=UpperCamelCase__ , model=UpperCamelCase__ , distributed=UpperCamelCase__ , fpaa=UpperCamelCase__ , ) @require_torch_multi_gpu @parameterized.expand(UpperCamelCase__ , name_func=UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : int ): '''simple docstring''' self.run_and_check( stage=UpperCamelCase__ , model=UpperCamelCase__ , distributed=UpperCamelCase__ , fpaa=UpperCamelCase__ , ) @parameterized.expand(UpperCamelCase__ , name_func=UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any ): '''simple docstring''' self.run_and_check( stage=UpperCamelCase__ , model=UpperCamelCase__ , distributed=UpperCamelCase__ , fpaa=UpperCamelCase__ , ) @require_torch_multi_gpu @parameterized.expand(UpperCamelCase__ , name_func=UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' self.run_and_check( stage=UpperCamelCase__ , model=UpperCamelCase__ , distributed=UpperCamelCase__ , fpaa=UpperCamelCase__ , ) def __A ( self : Optional[Any] , UpperCamelCase__ : Tuple ): '''simple docstring''' pass def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int = 10 , UpperCamelCase__ : bool = True , UpperCamelCase__ : bool = True , UpperCamelCase__ : bool = True , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = models[model] SCREAMING_SNAKE_CASE : Optional[int] = self.run_trainer( stage=UpperCamelCase__ , model_name=UpperCamelCase__ , eval_steps=UpperCamelCase__ , num_train_epochs=1 , distributed=UpperCamelCase__ , fpaa=UpperCamelCase__ , ) self.do_checks(UpperCamelCase__ ) return output_dir def __A ( self : str , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int = 10 , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : bool = True , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.get_auto_remove_tmp_dir('''./xxx''' , after=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = f""" --model_name_or_path {model_name} --dataset_name hf-internal-testing/librispeech_asr_dummy --dataset_config_name clean --train_split_name validation --validation_split_name validation --output_dir {output_dir} --num_train_epochs {str(UpperCamelCase__ )} --per_device_train_batch_size 2 --per_device_eval_batch_size 2 --evaluation_strategy steps --learning_rate 5e-4 --warmup_steps 8 --orthography timit --preprocessing_num_workers 1 --group_by_length --freeze_feature_extractor --report_to none --save_steps 0 --eval_steps {eval_steps} --report_to none """.split() if fpaa: args.extend(['''--fp16'''] ) # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true, # hence the separate config files SCREAMING_SNAKE_CASE : Optional[int] = f"""--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json""".split() SCREAMING_SNAKE_CASE : Optional[int] = [f"""{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py"""] SCREAMING_SNAKE_CASE : List[str] = self.get_launcher(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = launcher + script + args + ds_args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(UpperCamelCase__ , env=self.get_env() ) return output_dir def __A ( self : Any , UpperCamelCase__ : Any=False ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = min(2 , get_gpu_count() ) if distributed else 1 return f"""deepspeed --num_nodes 1 --num_gpus {num_gpus}""".split()
34
from __future__ import annotations from typing import Any class lowercase__ ( UpperCamelCase_): pass class lowercase__ : def __init__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = data SCREAMING_SNAKE_CASE : Node | None = None def __iter__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self SCREAMING_SNAKE_CASE : Tuple = [] while node: if node in visited: raise ContainsLoopError visited.append(UpperCamelCase__ ) yield node.data SCREAMING_SNAKE_CASE : Dict = node.next_node @property def __A ( self : Optional[int] ): '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": __UpperCamelCase : List[Any] = Node(1) __UpperCamelCase : str = Node(2) __UpperCamelCase : Dict = Node(3) __UpperCamelCase : List[Any] = Node(4) print(root_node.has_loop) # False __UpperCamelCase : int = root_node.next_node print(root_node.has_loop) # True __UpperCamelCase : Union[str, Any] = Node(5) __UpperCamelCase : Union[str, Any] = Node(6) __UpperCamelCase : List[Any] = Node(5) __UpperCamelCase : List[str] = Node(6) print(root_node.has_loop) # False __UpperCamelCase : List[Any] = Node(1) print(root_node.has_loop) # False
34
1
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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FunnelTokenizer UpperCamelCase_ = FunnelTokenizerFast UpperCamelCase_ = True UpperCamelCase_ = True def __A ( self : Union[str, Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[Any] = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : List[str] = 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 __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : int = '''unwanted, running''' return input_text, output_text def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCamelCase__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [7, 4, 5, 10, 8, 9] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_tokenizers(do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE : int = tokenizer('''UNwant\u00E9d,running''' ) SCREAMING_SNAKE_CASE : Optional[Any] = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
34
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""input_features""", """is_longer"""] def __init__( self : Optional[Any] , UpperCamelCase__ : Dict=64 , UpperCamelCase__ : Optional[Any]=4_8000 , UpperCamelCase__ : Tuple=480 , UpperCamelCase__ : Union[str, Any]=10 , UpperCamelCase__ : List[Any]=1024 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : int=False , UpperCamelCase__ : float = 0 , UpperCamelCase__ : float = 1_4000 , UpperCamelCase__ : int = None , UpperCamelCase__ : str = "fusion" , UpperCamelCase__ : str = "repeatpad" , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' super().__init__( feature_size=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , padding_value=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = top_db SCREAMING_SNAKE_CASE : Union[str, Any] = truncation SCREAMING_SNAKE_CASE : str = padding SCREAMING_SNAKE_CASE : List[Any] = fft_window_size SCREAMING_SNAKE_CASE : Tuple = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE : List[str] = hop_length SCREAMING_SNAKE_CASE : List[Any] = max_length_s SCREAMING_SNAKE_CASE : Tuple = max_length_s * sampling_rate SCREAMING_SNAKE_CASE : List[Any] = sampling_rate SCREAMING_SNAKE_CASE : List[str] = frequency_min SCREAMING_SNAKE_CASE : Any = frequency_max SCREAMING_SNAKE_CASE : List[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm=UpperCamelCase__ , mel_scale='''htk''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm='''slaney''' , mel_scale='''slaney''' , ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __A ( self : Optional[int] , UpperCamelCase__ : np.array , UpperCamelCase__ : Optional[np.array] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = spectrogram( UpperCamelCase__ , window_function(self.fft_window_size , '''hann''' ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=UpperCamelCase__ , log_mel='''dB''' , ) return log_mel_spectrogram.T def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : int = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : Any = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE : List[Any] = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE : int = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE : Optional[int] = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE : Optional[Any] = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE : Tuple = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE : str = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.functional.interpolate( UpperCamelCase__ , size=[chunk_frames, 64] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __A ( self : Dict , UpperCamelCase__ : np.array , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] ): '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE : Optional[Any] = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) - max_length SCREAMING_SNAKE_CASE : Dict = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE : List[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE : Optional[Any] = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE : Tuple = False else: SCREAMING_SNAKE_CASE : str = self._random_mel_fusion(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = True else: raise NotImplementedError(f"""data_truncating {truncation} not implemented""" ) else: SCREAMING_SNAKE_CASE : List[str] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE : Tuple = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Any = np.stack(np.tile(UpperCamelCase__ , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE : List[Any] = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.stack(np.tile(UpperCamelCase__ , UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.pad(UpperCamelCase__ , (0, max_length - waveform.shape[0]) , mode='''constant''' , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE : List[Any] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE : List[str] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , UpperCamelCase__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , UpperCamelCase__ : str = None , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , **UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE : List[str] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" f""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE : List[str] = isinstance(UpperCamelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) SCREAMING_SNAKE_CASE : int = is_batched_numpy or ( isinstance(UpperCamelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE : Any = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase__ , np.ndarray ): SCREAMING_SNAKE_CASE : List[Any] = np.asarray(UpperCamelCase__ , dtype=np.floataa ) elif isinstance(UpperCamelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE : List[str] = [np.asarray(UpperCamelCase__ )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE : int = [ self._get_input_mel(UpperCamelCase__ , max_length if max_length else self.nb_max_samples , UpperCamelCase__ , UpperCamelCase__ ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : List[str] = [] for mel, longer in padded_inputs: input_mel.append(UpperCamelCase__ ) is_longer.append(UpperCamelCase__ ) if truncation == "fusion" and sum(UpperCamelCase__ ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.randint(0 , len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = True if isinstance(input_mel[0] , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE : Optional[Any] = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_features''': input_mel, '''is_longer''': is_longer} SCREAMING_SNAKE_CASE : int = BatchFeature(UpperCamelCase__ ) if return_tensors is not None: SCREAMING_SNAKE_CASE : int = input_features.convert_to_tensors(UpperCamelCase__ ) return input_features
34
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = StableDiffusionInpaintPipeline UpperCamelCase_ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS UpperCamelCase_ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS UpperCamelCase_ = frozenset( []) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase_ = frozenset([]) def __A ( self : Optional[int] ): '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : int = PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) 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 , hidden_act='''gelu''' , projection_dim=512 , ) SCREAMING_SNAKE_CASE : Dict = CLIPTextModel(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) SCREAMING_SNAKE_CASE : List[str] = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __A ( self : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any]=0 ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(UpperCamelCase__ ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] SCREAMING_SNAKE_CASE : List[Any] = Image.fromarray(np.uinta(UpperCamelCase__ ) ).convert('''RGB''' ).resize((64, 64) ) SCREAMING_SNAKE_CASE : List[str] = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Any = self.get_dummy_components() SCREAMING_SNAKE_CASE : int = StableDiffusionInpaintPipeline(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = sd_pipe.to(UpperCamelCase__ ) sd_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.get_dummy_inputs(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe(**UpperCamelCase__ ).images SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE : List[str] = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self : List[Any] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : List[str] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) SCREAMING_SNAKE_CASE : int = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = '''stabilityai/stable-diffusion-2-inpainting''' SCREAMING_SNAKE_CASE : Any = StableDiffusionInpaintPipeline.from_pretrained(UpperCamelCase__ , safety_checker=UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : Optional[Any] = '''Face of a yellow cat, high resolution, sitting on a park bench''' SCREAMING_SNAKE_CASE : int = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : List[Any] = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) SCREAMING_SNAKE_CASE : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) SCREAMING_SNAKE_CASE : List[str] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) SCREAMING_SNAKE_CASE : Optional[int] = '''stabilityai/stable-diffusion-2-inpainting''' SCREAMING_SNAKE_CASE : Union[str, Any] = StableDiffusionInpaintPipeline.from_pretrained( UpperCamelCase__ , torch_dtype=torch.floataa , safety_checker=UpperCamelCase__ , ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : Union[str, Any] = '''Face of a yellow cat, high resolution, sitting on a park bench''' SCREAMING_SNAKE_CASE : List[str] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : List[Any] = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __A ( self : Optional[int] ): '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) SCREAMING_SNAKE_CASE : Optional[Any] = '''stabilityai/stable-diffusion-2-inpainting''' SCREAMING_SNAKE_CASE : Tuple = PNDMScheduler.from_pretrained(UpperCamelCase__ , subfolder='''scheduler''' ) SCREAMING_SNAKE_CASE : Tuple = StableDiffusionInpaintPipeline.from_pretrained( UpperCamelCase__ , safety_checker=UpperCamelCase__ , scheduler=UpperCamelCase__ , torch_dtype=torch.floataa , ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE : Optional[int] = '''Face of a yellow cat, high resolution, sitting on a park bench''' SCREAMING_SNAKE_CASE : str = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __UpperCamelCase : str = logging.get_logger(__name__) __UpperCamelCase : str = { 'microsoft/layoutlmv3-base': 'https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """layoutlmv3""" def __init__( self : Optional[int] , UpperCamelCase__ : Union[str, Any]=5_0265 , UpperCamelCase__ : List[Any]=768 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Tuple=3072 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Any=512 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : Optional[Any]=1E-5 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : int=0 , UpperCamelCase__ : str=2 , UpperCamelCase__ : List[str]=1024 , UpperCamelCase__ : str=128 , UpperCamelCase__ : str=128 , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Optional[Any]=64 , UpperCamelCase__ : Dict=256 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Dict=224 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : Any=None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__( vocab_size=UpperCamelCase__ , hidden_size=UpperCamelCase__ , num_hidden_layers=UpperCamelCase__ , num_attention_heads=UpperCamelCase__ , intermediate_size=UpperCamelCase__ , hidden_act=UpperCamelCase__ , hidden_dropout_prob=UpperCamelCase__ , attention_probs_dropout_prob=UpperCamelCase__ , max_position_embeddings=UpperCamelCase__ , type_vocab_size=UpperCamelCase__ , initializer_range=UpperCamelCase__ , layer_norm_eps=UpperCamelCase__ , pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : List[str] = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : str = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : Union[str, Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Union[str, Any] = max_rel_ad_pos SCREAMING_SNAKE_CASE : Union[str, Any] = text_embed SCREAMING_SNAKE_CASE : List[str] = visual_embed SCREAMING_SNAKE_CASE : Optional[Any] = input_size SCREAMING_SNAKE_CASE : Optional[Any] = num_channels SCREAMING_SNAKE_CASE : List[Any] = patch_size SCREAMING_SNAKE_CASE : List[Any] = classifier_dropout class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.12""") @property def __A ( self : str ): '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __A ( self : int ): '''simple docstring''' return 1E-5 @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Optional[Any] , UpperCamelCase__ : "ProcessorMixin" , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , ): '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , UpperCamelCase__ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : List[Any] = processor.tokenizer.num_special_tokens_to_add(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=UpperCamelCase__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : Any = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = dict( processor( UpperCamelCase__ , text=UpperCamelCase__ , boxes=UpperCamelCase__ , return_tensors=UpperCamelCase__ , ) ) return inputs
34
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class lowercase__ ( UpperCamelCase_): def __init__( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = data def __iter__( self : Tuple ): '''simple docstring''' for element in self.data: yield element def A ( _lowercase=True ): SCREAMING_SNAKE_CASE : Dict = Accelerator(even_batches=_lowercase ) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def A ( _lowercase , _lowercase , _lowercase , _lowercase = False ): if iterable: SCREAMING_SNAKE_CASE : Union[str, Any] = DummyIterableDataset(torch.as_tensor(range(_lowercase ) ) ) else: SCREAMING_SNAKE_CASE : Optional[Any] = TensorDataset(torch.as_tensor(range(_lowercase ) ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = DataLoader(_lowercase , batch_size=_lowercase ) SCREAMING_SNAKE_CASE : List[str] = accelerator.prepare(_lowercase ) return dl def A ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , ): SCREAMING_SNAKE_CASE : Optional[int] = create_dataloader(accelerator=_lowercase , dataset_size=_lowercase , batch_size=_lowercase ) SCREAMING_SNAKE_CASE : List[Any] = [len(batch[0] ) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def A ( ): SCREAMING_SNAKE_CASE : Optional[Any] = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( _lowercase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( _lowercase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def A ( ): SCREAMING_SNAKE_CASE : Dict = create_accelerator(even_batches=_lowercase ) verify_dataloader_batch_sizes( _lowercase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( _lowercase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def A ( ): SCREAMING_SNAKE_CASE : str = create_accelerator(even_batches=_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : Optional[int] = accelerator.prepare(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = create_dataloader(_lowercase , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE : Tuple = [] with accelerator.join_uneven_inputs([ddp_model] ): for batch_idx, batch in enumerate(_lowercase ): SCREAMING_SNAKE_CASE : int = ddp_model(batch[0].float() ) SCREAMING_SNAKE_CASE : Any = output.sum() loss.backward() batch_idxs.append(_lowercase ) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def A ( _lowercase ): with warnings.catch_warnings(record=_lowercase ) as w: with accelerator.join_uneven_inputs([Mock()] ): pass assert issubclass(w[-1].category , _lowercase ) assert "only supported for multi-GPU" in str(w[-1].message ) def A ( ): SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : Tuple = False SCREAMING_SNAKE_CASE : List[Any] = create_accelerator(even_batches=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : List[str] = accelerator.prepare(_lowercase ) SCREAMING_SNAKE_CASE : Any = create_dataloader(_lowercase , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE : str = create_dataloader(_lowercase , dataset_size=3 , batch_size=1 ) with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowercase ): SCREAMING_SNAKE_CASE : List[str] = train_dl.batch_sampler.even_batches SCREAMING_SNAKE_CASE : Tuple = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def A ( ): SCREAMING_SNAKE_CASE : Tuple = True SCREAMING_SNAKE_CASE : int = False SCREAMING_SNAKE_CASE : Union[str, Any] = create_accelerator(even_batches=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = accelerator.prepare(_lowercase ) create_dataloader(_lowercase , dataset_size=3 , batch_size=1 , iterable=_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(_lowercase , dataset_size=3 , batch_size=1 ) with warnings.catch_warnings(): warnings.filterwarnings('''ignore''' ) try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowercase ): SCREAMING_SNAKE_CASE : int = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def A ( ): SCREAMING_SNAKE_CASE : Tuple = create_accelerator() SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : List[Any] = accelerator.prepare(_lowercase ) create_dataloader(_lowercase , dataset_size=3 , batch_size=1 , iterable=_lowercase ) with warnings.catch_warnings(record=_lowercase ) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowercase ): pass assert issubclass(w[-1].category , _lowercase ) assert "only supported for map-style datasets" in str(w[-1].message ) def A ( ): SCREAMING_SNAKE_CASE : Union[str, Any] = create_accelerator() accelerator.print('''Test that even_batches variable ensures uniform batches across processes''' ) test_default_ensures_even_batch_sizes() accelerator.print('''Run tests with even_batches disabled''' ) test_can_disable_even_batches() accelerator.print('''Test joining uneven inputs''' ) test_can_join_uneven_inputs() accelerator.print('''Test overriding even_batches when joining uneven inputs''' ) test_join_can_override_even_batches() accelerator.print('''Test overriding even_batches for mixed dataloader types''' ) test_join_can_override_for_mixed_type_dataloaders() accelerator.print('''Test overriding even_batches raises a warning for iterable dataloaders''' ) test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print('''Test join with non DDP distributed raises warning''' ) SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.state.distributed_type SCREAMING_SNAKE_CASE : List[str] = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(_lowercase ) SCREAMING_SNAKE_CASE : List[str] = original_state if __name__ == "__main__": main()
34
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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FunnelTokenizer UpperCamelCase_ = FunnelTokenizerFast UpperCamelCase_ = True UpperCamelCase_ = True def __A ( self : Union[str, Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[Any] = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : List[str] = 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 __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : int = '''unwanted, running''' return input_text, output_text def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCamelCase__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [7, 4, 5, 10, 8, 9] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_tokenizers(do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE : int = tokenizer('''UNwant\u00E9d,running''' ) SCREAMING_SNAKE_CASE : Optional[Any] = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
34
1
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""image_processor""", """tokenizer"""] UpperCamelCase_ = """AutoImageProcessor""" UpperCamelCase_ = """AutoTokenizer""" def __init__( self : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.image_processor def __call__( self : Union[str, Any] , UpperCamelCase__ : int=None , UpperCamelCase__ : List[Any]=None , UpperCamelCase__ : Any=None , **UpperCamelCase__ : Any ): '''simple docstring''' if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer(UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if images is not None: SCREAMING_SNAKE_CASE : List[Any] = self.image_processor(UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if text is not None and images is not None: SCREAMING_SNAKE_CASE : List[Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCamelCase__ ) , tensor_type=UpperCamelCase__ ) def __A ( self : Union[str, Any] , *UpperCamelCase__ : str , **UpperCamelCase__ : List[str] ): '''simple docstring''' return self.tokenizer.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , *UpperCamelCase__ : Any , **UpperCamelCase__ : Any ): '''simple docstring''' return self.tokenizer.decode(*UpperCamelCase__ , **UpperCamelCase__ ) @property def __A ( self : Optional[int] ): '''simple docstring''' return ["input_ids", "attention_mask", "pixel_values"]
34
import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = dataset SCREAMING_SNAKE_CASE : Optional[Any] = process SCREAMING_SNAKE_CASE : Union[str, Any] = params def __len__( self : Tuple ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dataset[i] SCREAMING_SNAKE_CASE : Optional[int] = self.process(UpperCamelCase__ , **self.params ) return processed class lowercase__ ( UpperCamelCase_): def __init__( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = loader SCREAMING_SNAKE_CASE : List[Any] = infer SCREAMING_SNAKE_CASE : int = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : List[str] = loader_batch_size # Internal bookkeeping SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None def __len__( self : int ): '''simple docstring''' return len(self.loader ) def __iter__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice SCREAMING_SNAKE_CASE : Optional[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Convert ModelOutput to tuple first SCREAMING_SNAKE_CASE : Dict = element.to_tuple() if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : List[str] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around SCREAMING_SNAKE_CASE : int = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Union[str, Any] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Tuple = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. SCREAMING_SNAKE_CASE : Tuple = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 SCREAMING_SNAKE_CASE : Any = self._loader_batch_data.__class__(UpperCamelCase__ ) self._loader_batch_index += 1 return result def __A ( self : Union[str, Any] ): '''simple docstring''' if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch SCREAMING_SNAKE_CASE : Tuple = next(self.iterator ) SCREAMING_SNAKE_CASE : List[Any] = self.infer(UpperCamelCase__ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Optional[int] = processed else: SCREAMING_SNAKE_CASE : int = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : int = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[Any] = observed_batch_size # Setting internal index to unwrap the batch SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : int = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=None ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __iter__( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = iter(self.loader ) SCREAMING_SNAKE_CASE : List[Any] = None return self def __A ( self : List[str] ): '''simple docstring''' if self.subiterator is None: SCREAMING_SNAKE_CASE : Dict = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item SCREAMING_SNAKE_CASE : Any = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators SCREAMING_SNAKE_CASE : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = next(self.subiterator ) return processed class lowercase__ ( UpperCamelCase_): def __iter__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Optional[int] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Tuple = self.loader_batch_item() SCREAMING_SNAKE_CASE : Any = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator while not is_last: SCREAMING_SNAKE_CASE : Any = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = processed else: SCREAMING_SNAKE_CASE : Union[str, Any] = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : List[str] = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[str] = observed_batch_size SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : str = 0 while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Any = self.loader_batch_item() SCREAMING_SNAKE_CASE : List[Any] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator else: SCREAMING_SNAKE_CASE : int = processed SCREAMING_SNAKE_CASE : List[str] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) return accumulator class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Dict = key def __len__( self : Optional[int] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' return self.dataset[i][self.key] class lowercase__ ( UpperCamelCase_): def __init__( self : List[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : List[str] = keya SCREAMING_SNAKE_CASE : Tuple = keya def __len__( self : List[str] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
34
1
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowercase__ ( unittest.TestCase): def __A ( self : Dict ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1 SCREAMING_SNAKE_CASE : Union[str, Any] = 3 SCREAMING_SNAKE_CASE : Tuple = (32, 32) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase__ ) return image @property def __A ( self : Optional[int] ): '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : str = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) return model @property def __A ( self : str ): '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) return model @property def __A ( self : Any ): '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5006 , ) return RobertaSeriesModelWithTransformation(UpperCamelCase__ ) @property def __A ( self : Optional[Any] ): '''simple docstring''' def extract(*UpperCamelCase__ : str , **UpperCamelCase__ : str ): class lowercase__ : def __init__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = torch.ones([0] ) def __A ( self : int , UpperCamelCase__ : int ): '''simple docstring''' self.pixel_values.to(UpperCamelCase__ ) return self return Out() return extract def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_cond_unet SCREAMING_SNAKE_CASE : Tuple = PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_vae SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder SCREAMING_SNAKE_CASE : Optional[int] = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) SCREAMING_SNAKE_CASE : Dict = 77 SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_image.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : Any = AltDiffusionImgaImgPipeline( unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , vae=UpperCamelCase__ , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ , safety_checker=UpperCamelCase__ , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE : str = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Any = torch.Generator(device=UpperCamelCase__ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = alt_pipe( [prompt] , generator=UpperCamelCase__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , image=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Dict = output.images SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = alt_pipe( [prompt] , generator=UpperCamelCase__ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , image=UpperCamelCase__ , return_dict=UpperCamelCase__ , )[0] SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : str = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dummy_cond_unet SCREAMING_SNAKE_CASE : str = PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = self.dummy_vae SCREAMING_SNAKE_CASE : Dict = self.dummy_text_encoder SCREAMING_SNAKE_CASE : Any = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) SCREAMING_SNAKE_CASE : Tuple = 77 SCREAMING_SNAKE_CASE : Dict = self.dummy_image.to(UpperCamelCase__ ) # put models in fp16 SCREAMING_SNAKE_CASE : int = unet.half() SCREAMING_SNAKE_CASE : Optional[int] = vae.half() SCREAMING_SNAKE_CASE : Tuple = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : Tuple = AltDiffusionImgaImgPipeline( unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , vae=UpperCamelCase__ , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ , safety_checker=UpperCamelCase__ , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE : Any = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = alt_pipe( [prompt] , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , image=UpperCamelCase__ , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE : Union[str, Any] = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE : Any = '''BAAI/AltDiffusion''' SCREAMING_SNAKE_CASE : int = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase__ , safety_checker=UpperCamelCase__ , ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : Optional[int] = '''A fantasy landscape, trending on artstation''' SCREAMING_SNAKE_CASE : str = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE : Optional[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE : List[Any] = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Tuple ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) SCREAMING_SNAKE_CASE : int = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE : List[str] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy''' ) SCREAMING_SNAKE_CASE : Any = '''BAAI/AltDiffusion''' SCREAMING_SNAKE_CASE : Tuple = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase__ , safety_checker=UpperCamelCase__ , ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : int = '''A fantasy landscape, trending on artstation''' SCREAMING_SNAKE_CASE : Union[str, Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Dict = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """deberta-v2""" def __init__( self : Optional[Any] , UpperCamelCase__ : Any=12_8100 , UpperCamelCase__ : Optional[int]=1536 , UpperCamelCase__ : Dict=24 , UpperCamelCase__ : List[str]=24 , UpperCamelCase__ : Tuple=6144 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Optional[int]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : Optional[Any]=0 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : List[Any]=1E-7 , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=-1 , UpperCamelCase__ : List[Any]=0 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : str="gelu" , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention SCREAMING_SNAKE_CASE : Optional[Any] = max_relative_positions SCREAMING_SNAKE_CASE : Optional[int] = pad_token_id SCREAMING_SNAKE_CASE : Optional[Any] = position_biased_input # Backwards compatibility if type(UpperCamelCase__ ) == str: SCREAMING_SNAKE_CASE : Optional[int] = [x.strip() for x in pos_att_type.lower().split('''|''' )] SCREAMING_SNAKE_CASE : Any = pos_att_type SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = kwargs.get('''pooler_hidden_size''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pooler_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = pooler_hidden_act class lowercase__ ( UpperCamelCase_): @property def __A ( self : Union[str, Any] ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Dict , UpperCamelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().generate_dummy_inputs(preprocessor=UpperCamelCase__ , framework=UpperCamelCase__ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
34
1
import math import qiskit def A ( _lowercase = 1 , _lowercase = 1 , _lowercase = 1 ): if ( isinstance(_lowercase , _lowercase ) or isinstance(_lowercase , _lowercase ) or isinstance(_lowercase , _lowercase ) ): raise TypeError('''inputs must be integers.''' ) if (input_a < 0) or (input_a < 0) or (carry_in < 0): raise ValueError('''inputs must be positive.''' ) if ( (math.floor(_lowercase ) != input_a) or (math.floor(_lowercase ) != input_a) or (math.floor(_lowercase ) != carry_in) ): raise ValueError('''inputs must be exact integers.''' ) if (input_a > 2) or (input_a > 2) or (carry_in > 2): raise ValueError('''inputs must be less or equal to 2.''' ) # build registers SCREAMING_SNAKE_CASE : List[str] = qiskit.QuantumRegister(4 , '''qr''' ) SCREAMING_SNAKE_CASE : Tuple = qiskit.ClassicalRegister(2 , '''cr''' ) # list the entries SCREAMING_SNAKE_CASE : Optional[int] = [input_a, input_a, carry_in] SCREAMING_SNAKE_CASE : Tuple = qiskit.QuantumCircuit(_lowercase , _lowercase ) for i in range(0 , 3 ): if entry[i] == 2: quantum_circuit.h(_lowercase ) # for hadamard entries elif entry[i] == 1: quantum_circuit.x(_lowercase ) # for 1 entries elif entry[i] == 0: quantum_circuit.i(_lowercase ) # for 0 entries # build the circuit quantum_circuit.ccx(0 , 1 , 3 ) # ccx = toffoli gate quantum_circuit.cx(0 , 1 ) quantum_circuit.ccx(1 , 2 , 3 ) quantum_circuit.cx(1 , 2 ) quantum_circuit.cx(0 , 1 ) quantum_circuit.measure([2, 3] , _lowercase ) # measure the last two qbits SCREAMING_SNAKE_CASE : Optional[int] = qiskit.Aer.get_backend('''aer_simulator''' ) SCREAMING_SNAKE_CASE : Dict = qiskit.execute(_lowercase , _lowercase , shots=1_000 ) return job.result().get_counts(_lowercase ) if __name__ == "__main__": print(f"""Total sum count for state is: {quantum_full_adder(1, 1, 1)}""")
34
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() __UpperCamelCase : str = logging.get_logger(__name__) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Any = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(_lowercase , _lowercase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(_lowercase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : str = '''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" SCREAMING_SNAKE_CASE : Optional[int] = BitConfig( conv_layer=_lowercase , num_labels=1_000 , idalabel=_lowercase , labelaid=_lowercase , ) return config def A ( _lowercase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''stem.conv''' , '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''blocks''' , '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''head.fc''' , '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE : str = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''bit.encoder.''' + name return name def A ( ): SCREAMING_SNAKE_CASE : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ) return im @torch.no_grad() def A ( _lowercase , _lowercase , _lowercase=False ): SCREAMING_SNAKE_CASE : List[Any] = get_config(_lowercase ) # load original model from timm SCREAMING_SNAKE_CASE : Optional[Any] = create_model(_lowercase , pretrained=_lowercase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE : Optional[int] = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Dict = state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE : str = BitForImageClassification(_lowercase ) model.eval() model.load_state_dict(_lowercase ) # create image processor SCREAMING_SNAKE_CASE : Optional[Any] = create_transform(**resolve_data_config({} , model=_lowercase ) ) SCREAMING_SNAKE_CASE : List[str] = transform.transforms SCREAMING_SNAKE_CASE : Union[str, Any] = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE : Tuple = BitImageProcessor( do_resize=_lowercase , size={'''shortest_edge''': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_lowercase , crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} , do_normalize=_lowercase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Union[str, Any] = transform(_lowercase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE : Optional[int] = processor(_lowercase , return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(_lowercase , _lowercase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = outputs.logits print('''Logits:''' , logits[0, :3] ) print('''Predicted class:''' , model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE : List[Any] = timm_model(_lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowercase , outputs.logits , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_lowercase ).mkdir(exist_ok=_lowercase ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowercase ) processor.save_pretrained(_lowercase ) 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__": __UpperCamelCase : 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.', ) __UpperCamelCase : Optional[int] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
1
import argparse import os import gluonnlp as nlp import mxnet as mx import numpy as np import torch from gluonnlp.base import get_home_dir from gluonnlp.model.bert import BERTEncoder from gluonnlp.model.utils import _load_vocab from gluonnlp.vocab import Vocab from packaging import version from torch import nn from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging if version.parse(nlp.__version__) != version.parse('0.8.3'): raise Exception('requires gluonnlp == 0.8.3') if version.parse(mx.__version__) != version.parse('1.5.0'): raise Exception('requires mxnet == 1.5.0') logging.set_verbosity_info() __UpperCamelCase : Dict = logging.get_logger(__name__) __UpperCamelCase : List[Any] = 'The Nymphenburg Palace is a beautiful palace in Munich!' def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = { '''attention_cell''': '''multi_head''', '''num_layers''': 4, '''units''': 1_024, '''hidden_size''': 768, '''max_length''': 512, '''num_heads''': 8, '''scaled''': True, '''dropout''': 0.1, '''use_residual''': True, '''embed_size''': 1_024, '''embed_dropout''': 0.1, '''word_embed''': None, '''layer_norm_eps''': 1e-5, '''token_type_vocab_size''': 2, } SCREAMING_SNAKE_CASE : List[str] = bort_4_8_768_1024_hparams # Let's construct the original Bort model here # Taken from official BERT implementation, see: # https://github.com/alexa/bort/blob/master/bort/bort.py SCREAMING_SNAKE_CASE : Optional[Any] = BERTEncoder( attention_cell=predefined_args['''attention_cell'''] , num_layers=predefined_args['''num_layers'''] , units=predefined_args['''units'''] , hidden_size=predefined_args['''hidden_size'''] , max_length=predefined_args['''max_length'''] , num_heads=predefined_args['''num_heads'''] , scaled=predefined_args['''scaled'''] , dropout=predefined_args['''dropout'''] , output_attention=_lowercase , output_all_encodings=_lowercase , use_residual=predefined_args['''use_residual'''] , activation=predefined_args.get('''activation''' , '''gelu''' ) , layer_norm_eps=predefined_args.get('''layer_norm_eps''' , _lowercase ) , ) # Vocab information needs to be fetched first # It's the same as RoBERTa, so RobertaTokenizer can be used later SCREAMING_SNAKE_CASE : int = '''openwebtext_ccnews_stories_books_cased''' # Specify download folder to Gluonnlp's vocab SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(get_home_dir() , '''models''' ) SCREAMING_SNAKE_CASE : Optional[Any] = _load_vocab(_lowercase , _lowercase , _lowercase , cls=_lowercase ) SCREAMING_SNAKE_CASE : int = nlp.model.BERTModel( _lowercase , len(_lowercase ) , units=predefined_args['''units'''] , embed_size=predefined_args['''embed_size'''] , embed_dropout=predefined_args['''embed_dropout'''] , word_embed=predefined_args['''word_embed'''] , use_pooler=_lowercase , use_token_type_embed=_lowercase , token_type_vocab_size=predefined_args['''token_type_vocab_size'''] , use_classifier=_lowercase , use_decoder=_lowercase , ) original_bort.load_parameters(_lowercase , cast_dtype=_lowercase , ignore_extra=_lowercase ) SCREAMING_SNAKE_CASE : int = original_bort._collect_params_with_prefix() # Build our config 🤗 SCREAMING_SNAKE_CASE : Any = { '''architectures''': ['''BertForMaskedLM'''], '''attention_probs_dropout_prob''': predefined_args['''dropout'''], '''hidden_act''': '''gelu''', '''hidden_dropout_prob''': predefined_args['''dropout'''], '''hidden_size''': predefined_args['''embed_size'''], '''initializer_range''': 0.02, '''intermediate_size''': predefined_args['''hidden_size'''], '''layer_norm_eps''': predefined_args['''layer_norm_eps'''], '''max_position_embeddings''': predefined_args['''max_length'''], '''model_type''': '''bort''', '''num_attention_heads''': predefined_args['''num_heads'''], '''num_hidden_layers''': predefined_args['''num_layers'''], '''pad_token_id''': 1, # 2 = BERT, 1 = RoBERTa '''type_vocab_size''': 1, # 2 = BERT, 1 = RoBERTa '''vocab_size''': len(_lowercase ), } SCREAMING_SNAKE_CASE : List[str] = BertConfig.from_dict(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = BertForMaskedLM(_lowercase ) hf_bort_model.eval() # Parameter mapping table (Gluonnlp to Transformers) # * denotes layer index # # | Gluon Parameter | Transformers Parameter # | -------------------------------------------------------------- | ---------------------- # | `encoder.layer_norm.beta` | `bert.embeddings.LayerNorm.bias` # | `encoder.layer_norm.gamma` | `bert.embeddings.LayerNorm.weight` # | `encoder.position_weight` | `bert.embeddings.position_embeddings.weight` # | `word_embed.0.weight` | `bert.embeddings.word_embeddings.weight` # | `encoder.transformer_cells.*.attention_cell.proj_key.bias` | `bert.encoder.layer.*.attention.self.key.bias` # | `encoder.transformer_cells.*.attention_cell.proj_key.weight` | `bert.encoder.layer.*.attention.self.key.weight` # | `encoder.transformer_cells.*.attention_cell.proj_query.bias` | `bert.encoder.layer.*.attention.self.query.bias` # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight` # | `encoder.transformer_cells.*.attention_cell.proj_value.bias` | `bert.encoder.layer.*.attention.self.value.bias` # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight` # | `encoder.transformer_cells.*.ffn.ffn_2.bias` | `bert.encoder.layer.*.attention.output.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_2.weight` | `bert.encoder.layer.*.attention.output.dense.weight` # | `encoder.transformer_cells.*.layer_norm.beta` | `bert.encoder.layer.*.attention.output.LayerNorm.bias` # | `encoder.transformer_cells.*.layer_norm.gamma` | `bert.encoder.layer.*.attention.output.LayerNorm.weight` # | `encoder.transformer_cells.*.ffn.ffn_1.bias` | `bert.encoder.layer.*.intermediate.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_1.weight` | `bert.encoder.layer.*.intermediate.dense.weight` # | `encoder.transformer_cells.*.ffn.layer_norm.beta` | `bert.encoder.layer.*.output.LayerNorm.bias` # | `encoder.transformer_cells.*.ffn.layer_norm.gamma` | `bert.encoder.layer.*.output.LayerNorm.weight` # | `encoder.transformer_cells.*.proj.bias` | `bert.encoder.layer.*.output.dense.bias` # | `encoder.transformer_cells.*.proj.weight` | `bert.encoder.layer.*.output.dense.weight` # Helper function to convert MXNET Arrays to PyTorch def to_torch(_lowercase ) -> nn.Parameter: return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy() ) ) # Check param shapes and map new HF param back def check_and_map_params(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = hf_param.shape SCREAMING_SNAKE_CASE : Union[str, Any] = to_torch(params[gluon_param] ) SCREAMING_SNAKE_CASE : Tuple = gluon_param.shape assert ( shape_hf == shape_gluon ), f"""The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers""" return gluon_param SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( hf_bort_model.bert.embeddings.word_embeddings.weight , '''word_embed.0.weight''' ) SCREAMING_SNAKE_CASE : int = check_and_map_params( hf_bort_model.bert.embeddings.position_embeddings.weight , '''encoder.position_weight''' ) SCREAMING_SNAKE_CASE : Dict = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.bias , '''encoder.layer_norm.beta''' ) SCREAMING_SNAKE_CASE : int = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.weight , '''encoder.layer_norm.gamma''' ) # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.zeros_like( hf_bort_model.bert.embeddings.token_type_embeddings.weight.data ) for i in range(hf_bort_config.num_hidden_layers ): SCREAMING_SNAKE_CASE : BertLayer = hf_bort_model.bert.encoder.layer[i] # self attention SCREAMING_SNAKE_CASE : BertSelfAttention = layer.attention.self SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( self_attn.key.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_key.bias""" ) SCREAMING_SNAKE_CASE : int = check_and_map_params( self_attn.key.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_key.weight""" ) SCREAMING_SNAKE_CASE : Optional[int] = check_and_map_params( self_attn.query.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_query.bias""" ) SCREAMING_SNAKE_CASE : Tuple = check_and_map_params( self_attn.query.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_query.weight""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = check_and_map_params( self_attn.value.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_value.bias""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = check_and_map_params( self_attn.value.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_value.weight""" ) # self attention output SCREAMING_SNAKE_CASE : BertSelfOutput = layer.attention.output SCREAMING_SNAKE_CASE : List[str] = check_and_map_params( self_output.dense.bias , f"""encoder.transformer_cells.{i}.proj.bias""" ) SCREAMING_SNAKE_CASE : Any = check_and_map_params( self_output.dense.weight , f"""encoder.transformer_cells.{i}.proj.weight""" ) SCREAMING_SNAKE_CASE : str = check_and_map_params( self_output.LayerNorm.bias , f"""encoder.transformer_cells.{i}.layer_norm.beta""" ) SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( self_output.LayerNorm.weight , f"""encoder.transformer_cells.{i}.layer_norm.gamma""" ) # intermediate SCREAMING_SNAKE_CASE : BertIntermediate = layer.intermediate SCREAMING_SNAKE_CASE : Tuple = check_and_map_params( intermediate.dense.bias , f"""encoder.transformer_cells.{i}.ffn.ffn_1.bias""" ) SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( intermediate.dense.weight , f"""encoder.transformer_cells.{i}.ffn.ffn_1.weight""" ) # output SCREAMING_SNAKE_CASE : BertOutput = layer.output SCREAMING_SNAKE_CASE : int = check_and_map_params( bert_output.dense.bias , f"""encoder.transformer_cells.{i}.ffn.ffn_2.bias""" ) SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( bert_output.dense.weight , f"""encoder.transformer_cells.{i}.ffn.ffn_2.weight""" ) SCREAMING_SNAKE_CASE : Any = check_and_map_params( bert_output.LayerNorm.bias , f"""encoder.transformer_cells.{i}.ffn.layer_norm.beta""" ) SCREAMING_SNAKE_CASE : Optional[Any] = check_and_map_params( bert_output.LayerNorm.weight , f"""encoder.transformer_cells.{i}.ffn.layer_norm.gamma""" ) # Save space and energy 🎄 hf_bort_model.half() # Compare output of both models SCREAMING_SNAKE_CASE : int = RobertaTokenizer.from_pretrained('''roberta-base''' ) SCREAMING_SNAKE_CASE : Any = tokenizer.encode_plus(_lowercase )['''input_ids'''] # Get gluon output SCREAMING_SNAKE_CASE : Tuple = mx.nd.array([input_ids] ) SCREAMING_SNAKE_CASE : List[Any] = original_bort(inputs=_lowercase , token_types=[] ) # Get Transformer output (save and reload model again) hf_bort_model.save_pretrained(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = BertModel.from_pretrained(_lowercase ) hf_bort_model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.encode_plus(_lowercase , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : str = hf_bort_model(**_lowercase )[0] SCREAMING_SNAKE_CASE : Dict = output_gluon[0].asnumpy() SCREAMING_SNAKE_CASE : List[str] = output_hf[0].detach().numpy() SCREAMING_SNAKE_CASE : str = np.max(np.abs(hf_layer - gluon_layer ) ).item() SCREAMING_SNAKE_CASE : List[str] = np.allclose(_lowercase , _lowercase , atol=1e-3 ) if success: print('''✔️ Both model do output the same tensors''' ) else: print('''❌ Both model do **NOT** output the same tensors''' ) print('''Absolute difference is:''' , _lowercase ) if __name__ == "__main__": __UpperCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bort_checkpoint_path', default=None, type=str, required=True, help='Path the official Bort params file.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __UpperCamelCase : Optional[Any] = parser.parse_args() convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)
34
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow __UpperCamelCase : str = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ 'text-classification', 'language-modeling', 'summarization', 'token-classification', 'question-answering', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) __UpperCamelCase : int = logging.getLogger() def A ( ): SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() parser.add_argument('''-f''' ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() return args.f def A ( _lowercase , _lowercase="eval" ): SCREAMING_SNAKE_CASE : Dict = os.path.join(_lowercase , f"""{split}_results.json""" ) if os.path.exists(_lowercase ): with open(_lowercase , '''r''' ) as f: return json.load(_lowercase ) raise ValueError(f"""can't find {path}""" ) __UpperCamelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase__ ( UpperCamelCase_): def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Tuple = f""" run_glue.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 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_glue.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : str = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_clm_flax.main() SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_summarization_flax.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f""" run_mlm.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} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_mlm_flax.main() SCREAMING_SNAKE_CASE : List[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_ta_mlm_flax.main() SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Any = f""" run_flax_ner.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} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_ner.main() SCREAMING_SNAKE_CASE : List[str] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_qa.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} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_qa.main() SCREAMING_SNAKE_CASE : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
34
1
def A ( _lowercase ): if n == 1 or not isinstance(_lowercase , _lowercase ): return 0 elif n == 2: return 1 else: SCREAMING_SNAKE_CASE : Tuple = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def A ( _lowercase ): SCREAMING_SNAKE_CASE : List[str] = 0 SCREAMING_SNAKE_CASE : Optional[int] = 2 while digits < n: index += 1 SCREAMING_SNAKE_CASE : Optional[Any] = len(str(fibonacci(_lowercase ) ) ) return index def A ( _lowercase = 1_000 ): return fibonacci_digits_index(_lowercase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
34
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset __UpperCamelCase : Dict = random.Random() def A ( _lowercase , _lowercase=1.0 , _lowercase=None , _lowercase=None ): if rng is None: SCREAMING_SNAKE_CASE : Any = global_rng SCREAMING_SNAKE_CASE : int = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase__ ( unittest.TestCase): def __init__( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : str=7 , UpperCamelCase__ : Any=400 , UpperCamelCase__ : List[str]=2000 , UpperCamelCase__ : List[Any]=2048 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Union[str, Any]=1 , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : str=30 , UpperCamelCase__ : Tuple=4_4100 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : str = batch_size SCREAMING_SNAKE_CASE : str = min_seq_length SCREAMING_SNAKE_CASE : Dict = max_seq_length SCREAMING_SNAKE_CASE : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE : Optional[Any] = spectrogram_length SCREAMING_SNAKE_CASE : Optional[int] = feature_size SCREAMING_SNAKE_CASE : Tuple = num_audio_channels SCREAMING_SNAKE_CASE : Union[str, Any] = hop_length SCREAMING_SNAKE_CASE : List[Any] = chunk_length SCREAMING_SNAKE_CASE : str = sampling_rate def __A ( self : Optional[Any] ): '''simple docstring''' return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def __A ( self : Tuple , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Optional[int]=False ): '''simple docstring''' def _flatten(UpperCamelCase__ : str ): return list(itertools.chain(*UpperCamelCase__ ) ) if equal_length: SCREAMING_SNAKE_CASE : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE : int = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE : Optional[Any] = [np.asarray(UpperCamelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = TvltFeatureExtractor def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TvltFeatureExtractionTester(self ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''spectrogram_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''feature_size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''num_audio_channels''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''hop_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''chunk_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''sampling_rate''' ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : Optional[int] = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Optional[int] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Tuple = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : int = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Any = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] SCREAMING_SNAKE_CASE : Optional[int] = [np.asarray(UpperCamelCase__ ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched SCREAMING_SNAKE_CASE : Optional[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking SCREAMING_SNAKE_CASE : List[str] = feature_extractor( UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 , mask_audio=UpperCamelCase__ ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE : Optional[Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE : Dict = np.asarray(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def __A ( self : Optional[int] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE : Dict = ds.sort('''id''' ).select(range(UpperCamelCase__ ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE : int = TvltFeatureExtractor() SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) SCREAMING_SNAKE_CASE : str = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , UpperCamelCase__ , atol=1E-4 ) )
34
1
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 __UpperCamelCase : Optional[int] = True except ImportError: __UpperCamelCase : str = False try: from torch.hub import _get_torch_home __UpperCamelCase : int = _get_torch_home() except ImportError: __UpperCamelCase : Dict = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __UpperCamelCase : List[Any] = os.path.join(torch_cache_home, 'transformers') __UpperCamelCase : Optional[int] = 'https://cdn.huggingface.co' __UpperCamelCase : Tuple = 'https://s3.amazonaws.com/models.huggingface.co/bert' __UpperCamelCase : List[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __UpperCamelCase : str = os.path.join(PATH, 'config.yaml') __UpperCamelCase : Optional[Any] = os.path.join(PATH, 'attributes.txt') __UpperCamelCase : Any = os.path.join(PATH, 'objects.txt') __UpperCamelCase : str = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __UpperCamelCase : List[Any] = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __UpperCamelCase : str = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __UpperCamelCase : int = 'pytorch_model.bin' __UpperCamelCase : Any = 'config.yaml' def A ( _lowercase=OBJECTS , _lowercase=ATTRIBUTES ): SCREAMING_SNAKE_CASE : Tuple = [] with open(_lowercase ) as f: for object in f.readlines(): vg_classes.append(object.split(''',''' )[0].lower().strip() ) SCREAMING_SNAKE_CASE : Optional[Any] = [] with open(_lowercase ) as f: for object in f.readlines(): vg_attrs.append(object.split(''',''' )[0].lower().strip() ) return vg_classes, vg_attrs def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = OrderedDict() with open(_lowercase , '''rb''' ) as f: SCREAMING_SNAKE_CASE : List[Any] = pkl.load(_lowercase )['''model'''] for k in copy.deepcopy(list(ckp.keys() ) ): SCREAMING_SNAKE_CASE : Dict = ckp.pop(_lowercase ) if isinstance(_lowercase , np.ndarray ): SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor(_lowercase ) else: assert isinstance(_lowercase , torch.tensor ), type(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = v return r class lowercase__ : UpperCamelCase_ = {} def __init__( self : List[Any] , UpperCamelCase__ : dict , UpperCamelCase__ : str = "root" , UpperCamelCase__ : Dict=0 ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = name SCREAMING_SNAKE_CASE : Tuple = level SCREAMING_SNAKE_CASE : int = {} for k, v in dictionary.items(): if v is None: raise ValueError() SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = copy.deepcopy(UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = Config(UpperCamelCase__ , name=UpperCamelCase__ , level=level + 1 ) SCREAMING_SNAKE_CASE : Optional[int] = v setattr(self , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = d def __repr__( self : List[Any] ): '''simple docstring''' return str(list((self._pointer.keys()) ) ) def __setattr__( self : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = val SCREAMING_SNAKE_CASE : Optional[Any] = val SCREAMING_SNAKE_CASE : Optional[int] = key.split('''.''' ) SCREAMING_SNAKE_CASE : List[str] = len(UpperCamelCase__ ) - 1 SCREAMING_SNAKE_CASE : List[str] = 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: SCREAMING_SNAKE_CASE : Tuple = val else: SCREAMING_SNAKE_CASE : Any = pointer[l] def __A ( self : List[Any] ): '''simple docstring''' return self._pointer def __A ( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' with open(f"""{file_name}""" , '''w''' ) as stream: dump(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' with open(f"""{file_name}""" , '''w''' ) as stream: json.dump(UpperCamelCase__ , UpperCamelCase__ ) @staticmethod def __A ( UpperCamelCase__ : Dict ): '''simple docstring''' with open(UpperCamelCase__ ) as stream: SCREAMING_SNAKE_CASE : Tuple = load(UpperCamelCase__ , Loader=UpperCamelCase__ ) return data def __str__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = ''' ''' if self._name != "root": SCREAMING_SNAKE_CASE : Optional[int] = f"""{t * (self._level-1)}{self._name}:\n""" else: SCREAMING_SNAKE_CASE : int = '''''' SCREAMING_SNAKE_CASE : Optional[int] = 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""" SCREAMING_SNAKE_CASE : Optional[Any] = level return r[:-1] @classmethod def __A ( cls : Dict , UpperCamelCase__ : str , **UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = cls.get_config_dict(UpperCamelCase__ , **UpperCamelCase__ ) return cls(UpperCamelCase__ ) @classmethod def __A ( cls : Union[str, Any] , UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = kwargs.pop('''cache_dir''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = kwargs.pop('''force_download''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = kwargs.pop('''resume_download''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = kwargs.pop('''proxies''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = kwargs.pop('''local_files_only''' , UpperCamelCase__ ) if os.path.isdir(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(UpperCamelCase__ , UpperCamelCase__ ) elif os.path.isfile(UpperCamelCase__ ) or is_remote_url(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Any = pretrained_model_name_or_path else: SCREAMING_SNAKE_CASE : Optional[Any] = hf_bucket_url(UpperCamelCase__ , filename=UpperCamelCase__ , use_cdn=UpperCamelCase__ ) try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE : Optional[Any] = 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 SCREAMING_SNAKE_CASE : List[Any] = Config.load_yaml(UpperCamelCase__ ) except EnvironmentError: SCREAMING_SNAKE_CASE : Dict = '''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 A ( _lowercase ): SCREAMING_SNAKE_CASE : List[Any] = torch.load('''dump.pt''' , map_location=in_tensor.device ) SCREAMING_SNAKE_CASE : Tuple = in_tensor.numpy() SCREAMING_SNAKE_CASE : Optional[int] = out_tensor.numpy()[0] print(na.shape , na[0, 0, :5] ) print(na.shape , na[0, 0, :5] ) assert np.allclose(_lowercase , _lowercase , rtol=0.01 , atol=0.1 ), ( f"""{sum([1 for x in np.isclose(_lowercase , _lowercase , 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 A ( _lowercase ): SCREAMING_SNAKE_CASE : List[str] = urlparse(_lowercase ) return parsed.scheme in ("http", "https") def A ( _lowercase , _lowercase , _lowercase=True ): SCREAMING_SNAKE_CASE : Optional[Any] = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX SCREAMING_SNAKE_CASE : Optional[int] = '''/''' not in model_id if legacy_format: return f"""{endpoint}/{model_id}-{filename}""" else: return f"""{endpoint}/{model_id}/{filename}""" def A ( _lowercase , _lowercase , _lowercase=None , _lowercase=0 , _lowercase=None , ): SCREAMING_SNAKE_CASE : List[str] = '''python/{}'''.format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(_lowercase , _lowercase ): ua += "; " + "; ".join('''{}/{}'''.format(_lowercase , _lowercase ) for k, v in user_agent.items() ) elif isinstance(_lowercase , _lowercase ): ua += "; " + user_agent SCREAMING_SNAKE_CASE : List[str] = {'''user-agent''': ua} if resume_size > 0: SCREAMING_SNAKE_CASE : Dict = '''bytes=%d-''' % (resume_size,) SCREAMING_SNAKE_CASE : Tuple = requests.get(_lowercase , stream=_lowercase , proxies=_lowercase , headers=_lowercase ) if response.status_code == 416: # Range not satisfiable return SCREAMING_SNAKE_CASE : Dict = response.headers.get('''Content-Length''' ) SCREAMING_SNAKE_CASE : List[Any] = resume_size + int(_lowercase ) if content_length is not None else None SCREAMING_SNAKE_CASE : Optional[Any] = tqdm( unit='''B''' , unit_scale=_lowercase , total=_lowercase , initial=_lowercase , desc='''Downloading''' , ) for chunk in response.iter_content(chunk_size=1_024 ): if chunk: # filter out keep-alive new chunks progress.update(len(_lowercase ) ) temp_file.write(_lowercase ) progress.close() def A ( _lowercase , _lowercase=None , _lowercase=False , _lowercase=None , _lowercase=10 , _lowercase=False , _lowercase=None , _lowercase=False , ): if cache_dir is None: SCREAMING_SNAKE_CASE : List[Any] = TRANSFORMERS_CACHE if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = str(_lowercase ) os.makedirs(_lowercase , exist_ok=_lowercase ) SCREAMING_SNAKE_CASE : str = None if not local_files_only: try: SCREAMING_SNAKE_CASE : int = requests.head(_lowercase , allow_redirects=_lowercase , proxies=_lowercase , timeout=_lowercase ) if response.status_code == 200: SCREAMING_SNAKE_CASE : List[Any] = response.headers.get('''ETag''' ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass SCREAMING_SNAKE_CASE : Optional[int] = url_to_filename(_lowercase , _lowercase ) # get cache path to put the file SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(_lowercase , _lowercase ) # 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(_lowercase ): return cache_path else: SCREAMING_SNAKE_CASE : Any = [ file for file in fnmatch.filter(os.listdir(_lowercase ) , filename + '''.*''' ) if not file.endswith('''.json''' ) and not file.endswith('''.lock''' ) ] if len(_lowercase ) > 0: return os.path.join(_lowercase , 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(_lowercase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. SCREAMING_SNAKE_CASE : List[str] = cache_path + '''.lock''' with FileLock(_lowercase ): # If the download just completed while the lock was activated. if os.path.exists(_lowercase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: SCREAMING_SNAKE_CASE : int = cache_path + '''.incomplete''' @contextmanager def _resumable_file_manager(): with open(_lowercase , '''a+b''' ) as f: yield f SCREAMING_SNAKE_CASE : List[Any] = _resumable_file_manager if os.path.exists(_lowercase ): SCREAMING_SNAKE_CASE : List[Any] = os.stat(_lowercase ).st_size else: SCREAMING_SNAKE_CASE : Dict = 0 else: SCREAMING_SNAKE_CASE : Any = partial(tempfile.NamedTemporaryFile , dir=_lowercase , delete=_lowercase ) SCREAMING_SNAKE_CASE : List[Any] = 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''' , _lowercase , temp_file.name , ) http_get( _lowercase , _lowercase , proxies=_lowercase , resume_size=_lowercase , user_agent=_lowercase , ) os.replace(temp_file.name , _lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = {'''url''': url, '''etag''': etag} SCREAMING_SNAKE_CASE : int = cache_path + '''.json''' with open(_lowercase , '''w''' ) as meta_file: json.dump(_lowercase , _lowercase ) return cache_path def A ( _lowercase , _lowercase=None ): SCREAMING_SNAKE_CASE : Optional[int] = url.encode('''utf-8''' ) SCREAMING_SNAKE_CASE : Optional[Any] = shaaaa(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = url_hash.hexdigest() if etag: SCREAMING_SNAKE_CASE : Any = etag.encode('''utf-8''' ) SCREAMING_SNAKE_CASE : Dict = shaaaa(_lowercase ) filename += "." + etag_hash.hexdigest() if url.endswith('''.h5''' ): filename += ".h5" return filename def A ( _lowercase , _lowercase=None , _lowercase=False , _lowercase=None , _lowercase=False , _lowercase=None , _lowercase=False , _lowercase=False , _lowercase=False , ): if cache_dir is None: SCREAMING_SNAKE_CASE : List[Any] = TRANSFORMERS_CACHE if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = str(_lowercase ) if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = str(_lowercase ) if is_remote_url(_lowercase ): # URL, so get it from the cache (downloading if necessary) SCREAMING_SNAKE_CASE : List[Any] = get_from_cache( _lowercase , cache_dir=_lowercase , force_download=_lowercase , proxies=_lowercase , resume_download=_lowercase , user_agent=_lowercase , local_files_only=_lowercase , ) elif os.path.exists(_lowercase ): # File, and it exists. SCREAMING_SNAKE_CASE : Optional[Any] = url_or_filename elif urlparse(_lowercase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError('''file {} not found'''.format(_lowercase ) ) else: # Something unknown raise ValueError('''unable to parse {} as a URL or as a local path'''.format(_lowercase ) ) if extract_compressed_file: if not is_zipfile(_lowercase ) and not tarfile.is_tarfile(_lowercase ): 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/" SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = os.path.split(_lowercase ) SCREAMING_SNAKE_CASE : Any = output_file.replace('''.''' , '''-''' ) + '''-extracted''' SCREAMING_SNAKE_CASE : List[str] = os.path.join(_lowercase , _lowercase ) if os.path.isdir(_lowercase ) and os.listdir(_lowercase ) and not force_extract: return output_path_extracted # Prevent parallel extractions SCREAMING_SNAKE_CASE : int = output_path + '''.lock''' with FileLock(_lowercase ): shutil.rmtree(_lowercase , ignore_errors=_lowercase ) os.makedirs(_lowercase ) if is_zipfile(_lowercase ): with ZipFile(_lowercase , '''r''' ) as zip_file: zip_file.extractall(_lowercase ) zip_file.close() elif tarfile.is_tarfile(_lowercase ): SCREAMING_SNAKE_CASE : Dict = tarfile.open(_lowercase ) tar_file.extractall(_lowercase ) tar_file.close() else: raise EnvironmentError('''Archive format of {} could not be identified'''.format(_lowercase ) ) return output_path_extracted return output_path def A ( _lowercase , _lowercase="," ): assert isinstance(_lowercase , _lowercase ) if os.path.isfile(_lowercase ): with open(_lowercase ) as f: SCREAMING_SNAKE_CASE : Union[str, Any] = eval(f.read() ) else: SCREAMING_SNAKE_CASE : Tuple = requests.get(_lowercase ) try: SCREAMING_SNAKE_CASE : str = requests.json() except Exception: SCREAMING_SNAKE_CASE : Tuple = req.content.decode() assert data is not None, "could not connect" try: SCREAMING_SNAKE_CASE : List[str] = eval(_lowercase ) except Exception: SCREAMING_SNAKE_CASE : Optional[int] = data.split('''\n''' ) req.close() return data def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = requests.get(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = np.array(Image.open(BytesIO(response.content ) ) ) return img def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = url.split('''/''' )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(_lowercase ) with open(_lowercase , '''rb''' ) as stream: SCREAMING_SNAKE_CASE : int = pkl.load(_lowercase ) SCREAMING_SNAKE_CASE : str = weights.pop('''model''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, v in model.items(): SCREAMING_SNAKE_CASE : List[Any] = torch.from_numpy(_lowercase ) if "running_var" in k: SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([0] ) SCREAMING_SNAKE_CASE : int = k.replace('''running_var''' , '''num_batches_tracked''' ) SCREAMING_SNAKE_CASE : int = zero return new def A ( ): print(f"""{os.path.abspath(os.path.join(_lowercase , os.pardir ) )}/demo.ipynb""" ) def A ( _lowercase , _lowercase="RGB" ): assert isinstance(_lowercase , _lowercase ) if os.path.isfile(_lowercase ): SCREAMING_SNAKE_CASE : Any = cva.imread(_lowercase ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = get_image_from_url(_lowercase ) assert img is not None, f"""could not connect to: {im}""" SCREAMING_SNAKE_CASE : int = cva.cvtColor(_lowercase , cva.COLOR_BGR2RGB ) if input_format == "RGB": SCREAMING_SNAKE_CASE : Optional[Any] = img[:, :, ::-1] return img def A ( _lowercase , _lowercase=1 ): return (images[i : i + batch] for i in range(0 , len(_lowercase ) , _lowercase ))
34
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowercase__ ( UpperCamelCase_ , UpperCamelCase_): UpperCamelCase_ = 1 @register_to_config def __init__( self : List[str] , UpperCamelCase__ : int = 1000 , UpperCamelCase__ : Optional[Union[np.ndarray, List[float]]] = None ): '''simple docstring''' self.set_timesteps(UpperCamelCase__ ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : str = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. SCREAMING_SNAKE_CASE : Tuple = 4 # running values SCREAMING_SNAKE_CASE : int = [] def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, torch.device] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = num_inference_steps SCREAMING_SNAKE_CASE : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: SCREAMING_SNAKE_CASE : int = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE : Optional[Any] = torch.sin(steps * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE : Dict = (1.0 - self.betas**2) ** 0.5 SCREAMING_SNAKE_CASE : Optional[Any] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] SCREAMING_SNAKE_CASE : List[str] = timesteps.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = [] def __A ( self : Tuple , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : int , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : bool = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) SCREAMING_SNAKE_CASE : Optional[int] = (self.timesteps == timestep).nonzero().item() SCREAMING_SNAKE_CASE : Union[str, Any] = timestep_index + 1 SCREAMING_SNAKE_CASE : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCamelCase__ ) if len(self.ets ) == 1: SCREAMING_SNAKE_CASE : Dict = self.ets[-1] elif len(self.ets ) == 2: SCREAMING_SNAKE_CASE : Optional[int] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: SCREAMING_SNAKE_CASE : str = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: SCREAMING_SNAKE_CASE : Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) SCREAMING_SNAKE_CASE : Optional[int] = self._get_prev_sample(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : torch.FloatTensor , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return sample def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.alphas[timestep_index] SCREAMING_SNAKE_CASE : List[str] = self.betas[timestep_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas[prev_timestep_index] SCREAMING_SNAKE_CASE : Tuple = self.betas[prev_timestep_index] SCREAMING_SNAKE_CASE : Dict = (sample - sigma * ets) / max(UpperCamelCase__ , 1E-8 ) SCREAMING_SNAKE_CASE : Optional[Any] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
34
1
import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FlaxAutoencoderKL @property def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = 4 SCREAMING_SNAKE_CASE : str = 3 SCREAMING_SNAKE_CASE : Tuple = (32, 32) SCREAMING_SNAKE_CASE : Optional[int] = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE : int = jax.random.uniform(UpperCamelCase__ , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } SCREAMING_SNAKE_CASE : Tuple = self.dummy_input return init_dict, inputs_dict
34
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
1
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class lowercase__ : UpperCamelCase_ = 42 UpperCamelCase_ = 42 class lowercase__ : def __init__( self : int , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : list[list[Edge]] = [[] for _ in range(UpperCamelCase__ )] SCREAMING_SNAKE_CASE : Union[str, Any] = size def __getitem__( self : Optional[Any] , UpperCamelCase__ : int ): '''simple docstring''' return iter(self._graph[vertex] ) @property def __A ( self : Dict ): '''simple docstring''' return self._size def __A ( self : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' if weight not in (0, 1): raise ValueError('''Edge weight must be either 0 or 1.''' ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError('''Vertex indexes must be in [0; size).''' ) self._graph[from_vertex].append(Edge(UpperCamelCase__ , UpperCamelCase__ ) ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = deque([start_vertex] ) SCREAMING_SNAKE_CASE : list[int | None] = [None] * self.size SCREAMING_SNAKE_CASE : str = 0 while queue: SCREAMING_SNAKE_CASE : Dict = queue.popleft() SCREAMING_SNAKE_CASE : str = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: SCREAMING_SNAKE_CASE : List[str] = current_distance + edge.weight SCREAMING_SNAKE_CASE : List[str] = distances[edge.destination_vertex] if ( isinstance(UpperCamelCase__ , UpperCamelCase__ ) and new_distance >= dest_vertex_distance ): continue SCREAMING_SNAKE_CASE : Tuple = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError('''No path from start_vertex to finish_vertex.''' ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
34
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
1
from __future__ import annotations import unittest import numpy as np from transformers import OPTConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel def A ( _lowercase , _lowercase , _lowercase=None , _lowercase=None ): if attention_mask is None: SCREAMING_SNAKE_CASE : Tuple = tf.cast(tf.math.not_equal(_lowercase , config.pad_token_id ) , tf.inta ) return {"input_ids": input_ids, "attention_mask": attention_mask} @require_tf class lowercase__ : UpperCamelCase_ = OPTConfig UpperCamelCase_ = {} UpperCamelCase_ = """gelu""" def __init__( self : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : int=13 , UpperCamelCase__ : Optional[int]=7 , UpperCamelCase__ : int=True , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Union[str, Any]=99 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : List[str]=2 , UpperCamelCase__ : Tuple=4 , UpperCamelCase__ : Dict=4 , UpperCamelCase__ : Optional[int]="gelu" , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Dict=0.1 , UpperCamelCase__ : Any=20 , UpperCamelCase__ : Optional[Any]=2 , UpperCamelCase__ : Optional[int]=1 , UpperCamelCase__ : Optional[int]=0 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : Any=16 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = parent SCREAMING_SNAKE_CASE : List[Any] = batch_size SCREAMING_SNAKE_CASE : Optional[int] = seq_length SCREAMING_SNAKE_CASE : Tuple = is_training SCREAMING_SNAKE_CASE : Any = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size SCREAMING_SNAKE_CASE : Tuple = hidden_size SCREAMING_SNAKE_CASE : str = num_hidden_layers SCREAMING_SNAKE_CASE : int = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : int = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : Union[str, Any] = eos_token_id SCREAMING_SNAKE_CASE : List[str] = pad_token_id SCREAMING_SNAKE_CASE : Optional[int] = bos_token_id SCREAMING_SNAKE_CASE : int = embed_dim SCREAMING_SNAKE_CASE : str = word_embed_proj_dim SCREAMING_SNAKE_CASE : str = False def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE : Optional[int] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE : List[str] = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE : Dict = self.config_cls( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , embed_dim=self.embed_dim , word_embed_proj_dim=self.word_embed_proj_dim , is_encoder_decoder=UpperCamelCase__ , **self.config_updates , ) SCREAMING_SNAKE_CASE : Tuple = prepare_opt_inputs_dict(UpperCamelCase__ , UpperCamelCase__ ) return config, inputs_dict def __A ( self : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFOPTModel(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = inputs_dict['''input_ids'''] SCREAMING_SNAKE_CASE : Tuple = input_ids[:1, :] SCREAMING_SNAKE_CASE : int = inputs_dict['''attention_mask'''][:1, :] SCREAMING_SNAKE_CASE : Optional[int] = 1 # first forward pass SCREAMING_SNAKE_CASE : int = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , use_cache=UpperCamelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE : int = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE : Optional[int] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE : Tuple = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , past_key_values=UpperCamelCase__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE : List[Any] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE : List[str] = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE : List[str] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(UpperCamelCase__ , UpperCamelCase__ , rtol=1E-3 ) @require_tf class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else () UpperCamelCase_ = (TFOPTForCausalLM,) if is_tf_available() else () UpperCamelCase_ = ( {"""feature-extraction""": TFOPTModel, """text-generation""": TFOPTForCausalLM} if is_tf_available() else {} ) UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 10 def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = TFOPTModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=UpperCamelCase__ ) def __A ( self : Dict ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() def _get_word_embedding_weight(UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Any ): if hasattr(UpperCamelCase__ , '''weight''' ): return embedding_layer.weight else: # Here we build the word embeddings weights if not exists. # And then we retry to get the attribute once built. model.build() if hasattr(UpperCamelCase__ , '''weight''' ): return embedding_layer.weight else: return None for model_class in self.all_model_classes: for size in [config.vocab_size - 10, config.vocab_size + 10]: # build the embeddings SCREAMING_SNAKE_CASE : Tuple = model_class(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = _get_word_embedding_weight(UpperCamelCase__ , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE : List[Any] = _get_word_embedding_weight(UpperCamelCase__ , model.get_output_embeddings() ) # reshape the embeddings model.resize_token_embeddings(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = _get_word_embedding_weight(UpperCamelCase__ , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE : str = _get_word_embedding_weight(UpperCamelCase__ , model.get_output_embeddings() ) # check that the resized embeddings size matches the desired size. SCREAMING_SNAKE_CASE : str = size if size is not None else config.vocab_size self.assertEqual(new_input_embeddings.shape[0] , UpperCamelCase__ ) # check that weights remain the same after resizing SCREAMING_SNAKE_CASE : Tuple = True for pa, pa in zip(old_input_embeddings.value() , new_input_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE : Any = False self.assertTrue(UpperCamelCase__ ) if old_output_embeddings is not None and new_output_embeddings is not None: self.assertEqual(new_output_embeddings.shape[0] , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = True for pa, pa in zip(old_output_embeddings.value() , new_output_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE : Tuple = False self.assertTrue(UpperCamelCase__ ) def A ( _lowercase ): return tf.constant(_lowercase , dtype=tf.intaa ) @require_tf class lowercase__ ( unittest.TestCase): UpperCamelCase_ = 99 def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = tf.ones((4, 1) , dtype=tf.intaa ) * 2 SCREAMING_SNAKE_CASE : Union[str, Any] = tf.concat([ids_tensor((4, 6) , self.vocab_size - 3 ) + 3, eos_column_vector] , axis=1 ) SCREAMING_SNAKE_CASE : Dict = input_ids.shape[0] SCREAMING_SNAKE_CASE : Union[str, Any] = OPTConfig( vocab_size=self.vocab_size , hidden_size=24 , num_hidden_layers=2 , num_attention_heads=2 , ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size @require_sentencepiece @require_tf class lowercase__ ( unittest.TestCase): @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = TFOPTModel.from_pretrained('''facebook/opt-350m''' ) SCREAMING_SNAKE_CASE : Optional[int] = _long_tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] ) SCREAMING_SNAKE_CASE : Union[str, Any] = tf.not_equal(UpperCamelCase__ , model.config.pad_token_id ) with tf.GradientTape(): SCREAMING_SNAKE_CASE : str = model(input_ids=UpperCamelCase__ , attention_mask=UpperCamelCase__ ).last_hidden_state SCREAMING_SNAKE_CASE : Tuple = (1, 11, 512) self.assertEqual(output.shape , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = tf.constant( [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]] ) self.assertTrue(np.allclose(output[:, :3, :3] , UpperCamelCase__ , atol=4E-3 ) ) SCREAMING_SNAKE_CASE : Dict = tf.function(UpperCamelCase__ , jit_compile=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = xla_generate(UpperCamelCase__ , UpperCamelCase__ )[0] self.assertTrue(np.allclose(output[:, :3, :3] , UpperCamelCase__ , atol=4E-2 ) ) @require_tf @slow class lowercase__ ( unittest.TestCase): def __A ( self : List[Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Dict = '''facebook/opt-350m''' def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TFOPTForCausalLM.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE : Optional[int] = GPTaTokenizer.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE : Optional[int] = [ '''Today is a beautiful day and I want to''', '''In the city of''', '''Paris is the capital of France and''', '''Computers and mobile phones have taken''', ] # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False SCREAMING_SNAKE_CASE : int = tokenizer(UpperCamelCase__ , return_tensors='''tf''' , padding=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = tf.math.reduce_mean(model(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) SCREAMING_SNAKE_CASE : str = tf.constant( [ [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670], [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822], [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703], [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477], ] ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ , atol=1E-4 ) ) SCREAMING_SNAKE_CASE : Dict = tf.function(UpperCamelCase__ , jit_compile=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = tf.math.reduce_mean(xla_generate(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ , atol=1E-4 ) ) @require_tf @slow class lowercase__ ( unittest.TestCase): @property def __A ( self : Any ): '''simple docstring''' return [ "Today is a beautiful day and I want", "In the city of", "Paris is the capital of France and", "Computers and mobile phones have taken", ] def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = '''facebook/opt-125m''' SCREAMING_SNAKE_CASE : Optional[Any] = [ '''Today is a beautiful day and I want to''', '''In the city of New York, the city''', '''Paris is the capital of France and the capital''', '''Computers and mobile phones have taken over the''', ] SCREAMING_SNAKE_CASE : Optional[int] = [] SCREAMING_SNAKE_CASE : Dict = GPTaTokenizer.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = TFOPTForCausalLM.from_pretrained(UpperCamelCase__ ) for prompt in self.prompts: SCREAMING_SNAKE_CASE : List[Any] = tokenizer(UpperCamelCase__ , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE : Dict = model.generate(UpperCamelCase__ , max_length=10 ) SCREAMING_SNAKE_CASE : int = tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''facebook/opt-350m''' SCREAMING_SNAKE_CASE : Union[str, Any] = GPTaTokenizer.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = TFOPTForCausalLM.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = '''left''' # use different length sentences to test batching SCREAMING_SNAKE_CASE : Dict = [ '''Hello, my dog is a little''', '''Today, I''', ] SCREAMING_SNAKE_CASE : Dict = tokenizer(UpperCamelCase__ , return_tensors='''tf''' , padding=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = inputs['''input_ids'''] SCREAMING_SNAKE_CASE : Union[str, Any] = model.generate(input_ids=UpperCamelCase__ , attention_mask=inputs['''attention_mask'''] ) SCREAMING_SNAKE_CASE : str = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE : Union[str, Any] = model.generate(input_ids=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = inputs_non_padded.shape[-1] - tf.math.reduce_sum( tf.cast(inputs['''attention_mask'''][-1] , tf.intaa ) ) SCREAMING_SNAKE_CASE : int = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE : Union[str, Any] = model.generate(input_ids=UpperCamelCase__ , max_length=model.config.max_length - num_paddings ) SCREAMING_SNAKE_CASE : Any = tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = tokenizer.decode(output_non_padded[0] , skip_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer.decode(output_padded[0] , skip_special_tokens=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = [ '''Hello, my dog is a little bit of a dork.\nI\'m a little bit''', '''Today, I was in the middle of a conversation with a friend about the''', ] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , [non_padded_sentence, padded_sentence] ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''facebook/opt-350m''' SCREAMING_SNAKE_CASE : int = [ '''Today is a beautiful day and I want to''', '''In the city of San Francisco, the city''', '''Paris is the capital of France and the capital''', '''Computers and mobile phones have taken over the''', ] SCREAMING_SNAKE_CASE : Optional[int] = [] SCREAMING_SNAKE_CASE : Union[str, Any] = GPTaTokenizer.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = TFOPTForCausalLM.from_pretrained(UpperCamelCase__ ) for prompt in self.prompts: SCREAMING_SNAKE_CASE : Tuple = tokenizer(UpperCamelCase__ , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE : Dict = model.generate(UpperCamelCase__ , max_length=10 ) SCREAMING_SNAKE_CASE : Tuple = tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ )
34
import random def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowercase ) elif element > pivot: greater.append(_lowercase ) else: equal.append(_lowercase ) return less, equal, greater def A ( _lowercase , _lowercase ): # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(_lowercase ) or index < 0: return None SCREAMING_SNAKE_CASE : Dict = items[random.randint(0 , len(_lowercase ) - 1 )] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = _partition(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = len(_lowercase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowercase , _lowercase ) # must be in larger else: return quick_select(_lowercase , index - (m + count) )
34
1
from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging __UpperCamelCase : Any = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json', # See all umt5 models at https://huggingface.co/models?filter=umt5 } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """umt5""" UpperCamelCase_ = ["""past_key_values"""] def __init__( self : Optional[Any] , UpperCamelCase__ : Dict=25_0112 , UpperCamelCase__ : List[str]=512 , UpperCamelCase__ : Tuple=64 , UpperCamelCase__ : Any=1024 , UpperCamelCase__ : Optional[Any]=8 , UpperCamelCase__ : Dict=None , UpperCamelCase__ : List[Any]=6 , UpperCamelCase__ : Dict=32 , UpperCamelCase__ : Union[str, Any]=128 , UpperCamelCase__ : Tuple=0.1 , UpperCamelCase__ : Optional[int]=1E-6 , UpperCamelCase__ : List[str]=1.0 , UpperCamelCase__ : Dict="gated-gelu" , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Any="T5Tokenizer" , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Union[str, Any]=0 , UpperCamelCase__ : str=1 , UpperCamelCase__ : List[Any]=0 , **UpperCamelCase__ : List[str] , ): '''simple docstring''' super().__init__( is_encoder_decoder=UpperCamelCase__ , tokenizer_class=UpperCamelCase__ , tie_word_embeddings=UpperCamelCase__ , pad_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , decoder_start_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = d_model SCREAMING_SNAKE_CASE : Optional[int] = d_kv SCREAMING_SNAKE_CASE : List[Any] = d_ff SCREAMING_SNAKE_CASE : Dict = num_layers SCREAMING_SNAKE_CASE : List[Any] = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry SCREAMING_SNAKE_CASE : int = num_heads SCREAMING_SNAKE_CASE : str = relative_attention_num_buckets SCREAMING_SNAKE_CASE : Optional[int] = relative_attention_max_distance SCREAMING_SNAKE_CASE : int = dropout_rate SCREAMING_SNAKE_CASE : Tuple = layer_norm_epsilon SCREAMING_SNAKE_CASE : Any = initializer_factor SCREAMING_SNAKE_CASE : Optional[int] = feed_forward_proj SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : str = self.feed_forward_proj.split('''-''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = act_info[-1] SCREAMING_SNAKE_CASE : int = act_info[0] == '''gated''' if len(UpperCamelCase__ ) > 1 and act_info[0] != "gated" or len(UpperCamelCase__ ) > 2: raise ValueError( f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.""" '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) if feed_forward_proj == "gated-gelu": SCREAMING_SNAKE_CASE : Optional[int] = '''gelu_new''' @property def __A ( self : Dict ): '''simple docstring''' return self.d_model @property def __A ( self : str ): '''simple docstring''' return self.num_heads @property def __A ( self : Optional[int] ): '''simple docstring''' return self.num_layers class lowercase__ ( UpperCamelCase_): @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: SCREAMING_SNAKE_CASE : List[Any] = '''past_encoder_sequence + sequence''' SCREAMING_SNAKE_CASE : Dict = {0: '''batch'''} SCREAMING_SNAKE_CASE : Tuple = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(UpperCamelCase__ , direction='''inputs''' ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def __A ( self : Dict ): '''simple docstring''' return 13 @property def __A ( self : Optional[int] ): '''simple docstring''' return 5E-4
34
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Tuple = logging.get_logger(__name__) # TODO Update this __UpperCamelCase : List[str] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """esm""" def __init__( self : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Any=768 , UpperCamelCase__ : Optional[Any]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : Optional[int]=3072 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1026 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Any=1E-12 , UpperCamelCase__ : Dict="absolute" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[int]=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Optional[Any]=None , **UpperCamelCase__ : Any , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : Dict = emb_layer_norm_before SCREAMING_SNAKE_CASE : List[str] = token_dropout SCREAMING_SNAKE_CASE : List[Any] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) SCREAMING_SNAKE_CASE : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = EsmFoldConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) SCREAMING_SNAKE_CASE : Optional[int] = get_default_vocab_list() else: SCREAMING_SNAKE_CASE : Optional[Any] = vocab_list else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = self.esmfold_config.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = None UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 0 UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Optional[int] ): '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE : Optional[Any] = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = TrunkConfig(**self.trunk ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = asdict(self ) SCREAMING_SNAKE_CASE : Tuple = self.trunk.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 48 UpperCamelCase_ = 1_024 UpperCamelCase_ = 128 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 0 UpperCamelCase_ = 0 UpperCamelCase_ = False UpperCamelCase_ = 4 UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Any ): '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE : Optional[int] = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) SCREAMING_SNAKE_CASE : Dict = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE : Tuple = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = asdict(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.structure_module.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 384 UpperCamelCase_ = 128 UpperCamelCase_ = 16 UpperCamelCase_ = 128 UpperCamelCase_ = 12 UpperCamelCase_ = 4 UpperCamelCase_ = 8 UpperCamelCase_ = 0.1 UpperCamelCase_ = 8 UpperCamelCase_ = 1 UpperCamelCase_ = 2 UpperCamelCase_ = 7 UpperCamelCase_ = 10 UpperCamelCase_ = 1E-8 UpperCamelCase_ = 1E5 def __A ( self : Dict ): '''simple docstring''' return asdict(self ) def A ( ): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
34
1
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowercase__ ( unittest.TestCase): def __A ( self : Any ): '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=UpperCamelCase__ , ) assert hasattr(self , '''env''' ) def __A ( self : str , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings SCREAMING_SNAKE_CASE : Any = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=UpperCamelCase__ , instance_count=UpperCamelCase__ , instance_type=self.instance_type , debugger_hook_config=UpperCamelCase__ , hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=UpperCamelCase__ , py_version='''py36''' , ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' TrainingJobAnalytics(UpperCamelCase__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def __A ( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.create_estimator(UpperCamelCase__ ) # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[Any] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : List[str] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : List[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_9999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , UpperCamelCase__ )
34
1
import hashlib import unittest from typing import Dict import numpy as np from transformers import ( MODEL_FOR_MASK_GENERATION_MAPPING, TF_MODEL_FOR_MASK_GENERATION_MAPPING, is_vision_available, pipeline, ) from transformers.pipelines import MaskGenerationPipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) if is_vision_available(): from PIL import Image else: class lowercase__ : @staticmethod def __A ( *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : Dict ): '''simple docstring''' pass def A ( _lowercase ): SCREAMING_SNAKE_CASE : str = hashlib.mda(image.tobytes() ) return m.hexdigest()[:10] def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = np.array(_lowercase ) SCREAMING_SNAKE_CASE : Any = npimg.shape return {"hash": hashimage(_lowercase ), "shape": shape} @is_pipeline_test @require_vision @require_torch class lowercase__ ( unittest.TestCase): UpperCamelCase_ = dict( (list(MODEL_FOR_MASK_GENERATION_MAPPING.items()) if MODEL_FOR_MASK_GENERATION_MAPPING else [])) UpperCamelCase_ = dict( (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items()) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else [])) def __A ( self : List[str] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = MaskGenerationPipeline(model=UpperCamelCase__ , image_processor=UpperCamelCase__ ) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def __A ( self : Dict , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' pass @require_tf @unittest.skip('''Image segmentation not implemented in TF''' ) def __A ( self : int ): '''simple docstring''' pass @slow @require_torch def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = pipeline('''mask-generation''' , model='''facebook/sam-vit-huge''' ) SCREAMING_SNAKE_CASE : List[str] = image_segmenter('''http://images.cocodataset.org/val2017/000000039769.jpg''' , points_per_batch=256 ) # Shortening by hashing SCREAMING_SNAKE_CASE : int = [] for i, o in enumerate(outputs['''masks'''] ): new_outupt += [{"mask": mask_to_test_readable(UpperCamelCase__ ), "scores": outputs["scores"][i]}] # fmt: off self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0444}, {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.021}, {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0167}, {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0132}, {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0053}, {'''mask''': {'''hash''': '''e2d0b7a0b7''', '''shape''': (480, 640)}, '''scores''': 0.9967}, {'''mask''': {'''hash''': '''453c7844bd''', '''shape''': (480, 640)}, '''scores''': 0.993}, {'''mask''': {'''hash''': '''3d44f2926d''', '''shape''': (480, 640)}, '''scores''': 0.9909}, {'''mask''': {'''hash''': '''64033ddc3f''', '''shape''': (480, 640)}, '''scores''': 0.9879}, {'''mask''': {'''hash''': '''801064ff79''', '''shape''': (480, 640)}, '''scores''': 0.9834}, {'''mask''': {'''hash''': '''6172f276ef''', '''shape''': (480, 640)}, '''scores''': 0.9716}, {'''mask''': {'''hash''': '''b49e60e084''', '''shape''': (480, 640)}, '''scores''': 0.9612}, {'''mask''': {'''hash''': '''a811e775fd''', '''shape''': (480, 640)}, '''scores''': 0.9599}, {'''mask''': {'''hash''': '''a6a8ebcf4b''', '''shape''': (480, 640)}, '''scores''': 0.9552}, {'''mask''': {'''hash''': '''9d8257e080''', '''shape''': (480, 640)}, '''scores''': 0.9532}, {'''mask''': {'''hash''': '''32de6454a8''', '''shape''': (480, 640)}, '''scores''': 0.9516}, {'''mask''': {'''hash''': '''af3d4af2c8''', '''shape''': (480, 640)}, '''scores''': 0.9499}, {'''mask''': {'''hash''': '''3c6db475fb''', '''shape''': (480, 640)}, '''scores''': 0.9483}, {'''mask''': {'''hash''': '''c290813fb9''', '''shape''': (480, 640)}, '''scores''': 0.9464}, {'''mask''': {'''hash''': '''b6f0b8f606''', '''shape''': (480, 640)}, '''scores''': 0.943}, {'''mask''': {'''hash''': '''92ce16bfdf''', '''shape''': (480, 640)}, '''scores''': 0.943}, {'''mask''': {'''hash''': '''c749b25868''', '''shape''': (480, 640)}, '''scores''': 0.9408}, {'''mask''': {'''hash''': '''efb6cab859''', '''shape''': (480, 640)}, '''scores''': 0.9335}, {'''mask''': {'''hash''': '''1ff2eafb30''', '''shape''': (480, 640)}, '''scores''': 0.9326}, {'''mask''': {'''hash''': '''788b798e24''', '''shape''': (480, 640)}, '''scores''': 0.9262}, {'''mask''': {'''hash''': '''abea804f0e''', '''shape''': (480, 640)}, '''scores''': 0.8999}, {'''mask''': {'''hash''': '''7b9e8ddb73''', '''shape''': (480, 640)}, '''scores''': 0.8986}, {'''mask''': {'''hash''': '''cd24047c8a''', '''shape''': (480, 640)}, '''scores''': 0.8984}, {'''mask''': {'''hash''': '''6943e6bcbd''', '''shape''': (480, 640)}, '''scores''': 0.8873}, {'''mask''': {'''hash''': '''b5f47c9191''', '''shape''': (480, 640)}, '''scores''': 0.8871} ] , ) # fmt: on @require_torch @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = '''facebook/sam-vit-huge''' SCREAMING_SNAKE_CASE : Tuple = pipeline('''mask-generation''' , model=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = image_segmenter( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , pred_iou_thresh=1 , points_per_batch=256 ) # Shortening by hashing SCREAMING_SNAKE_CASE : Optional[Any] = [] for i, o in enumerate(outputs['''masks'''] ): new_outupt += [{"mask": mask_to_test_readable(UpperCamelCase__ ), "scores": outputs["scores"][i]}] self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0444}, {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.0210}, {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0167}, {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0132}, {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0053}, ] , )
34
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore __UpperCamelCase : Dict = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" __UpperCamelCase : Tuple = [file for file in filepaths if file != file.lower()] if upper_files: print(f"""{len(upper_files)} files contain uppercase characters:""") print('\n'.join(upper_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if ' ' in file] if space_files: print(f"""{len(space_files)} files contain space characters:""") print('\n'.join(space_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if '-' in file] if hyphen_files: print(f"""{len(hyphen_files)} files contain hyphen characters:""") print('\n'.join(hyphen_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(f"""{len(nodir_files)} files are not in a directory:""") print('\n'.join(nodir_files) + '\n') __UpperCamelCase : Optional[Any] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
34
1
from __future__ import annotations from typing import Any class lowercase__ : def __init__( self : Any , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = num_of_nodes SCREAMING_SNAKE_CASE : list[list[int]] = [] SCREAMING_SNAKE_CASE : dict[int, int] = {} def __A ( self : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' self.m_edges.append([u_node, v_node, weight] ) def __A ( self : Tuple , UpperCamelCase__ : int ): '''simple docstring''' if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def __A ( self : Tuple , UpperCamelCase__ : int ): '''simple docstring''' if self.m_component[u_node] != u_node: for k in self.m_component: SCREAMING_SNAKE_CASE : Optional[int] = self.find_component(UpperCamelCase__ ) def __A ( self : str , UpperCamelCase__ : list[int] , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' if component_size[u_node] <= component_size[v_node]: SCREAMING_SNAKE_CASE : int = v_node component_size[v_node] += component_size[u_node] self.set_component(UpperCamelCase__ ) elif component_size[u_node] >= component_size[v_node]: SCREAMING_SNAKE_CASE : Optional[int] = self.find_component(UpperCamelCase__ ) component_size[u_node] += component_size[v_node] self.set_component(UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : list[Any] = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) SCREAMING_SNAKE_CASE : List[str] = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = edge SCREAMING_SNAKE_CASE : Union[str, Any] = self.m_component[u] SCREAMING_SNAKE_CASE : List[str] = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): SCREAMING_SNAKE_CASE : Union[str, Any] = [u, v, w] for edge in minimum_weight_edge: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = edge SCREAMING_SNAKE_CASE : List[str] = self.m_component[u] SCREAMING_SNAKE_CASE : Optional[Any] = self.m_component[v] if u_component != v_component: mst_weight += w self.union(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) print(f"""Added edge [{u} - {v}]\nAdded weight: {w}\n""" ) num_of_components -= 1 SCREAMING_SNAKE_CASE : Union[str, Any] = [-1] * self.m_num_of_nodes print(f"""The total weight of the minimal spanning tree is: {mst_weight}""" ) def A ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
34
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: __UpperCamelCase : Dict = None __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Optional[int] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} __UpperCamelCase : Optional[int] = { '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 __UpperCamelCase : Union[str, Any] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = TaTokenizer UpperCamelCase_ = [] def __init__( self : str , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[Any]=100 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[str] = [f"""<extra_id_{i}>""" for i in range(UpperCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE : int = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id_''' in str(UpperCamelCase__ ) ) , UpperCamelCase__ ) ) ) 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__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , extra_ids=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : str = vocab_file SCREAMING_SNAKE_CASE : int = False if not self.vocab_file else True SCREAMING_SNAKE_CASE : str = extra_ids @staticmethod def __A ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE : List[str] = 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.''' , UpperCamelCase__ , ) return max_model_length def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : 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(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = 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__ ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE : Tuple = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, 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 __A ( self : Dict ): '''simple docstring''' return list( set(filter(lambda UpperCamelCase__ : bool(re.search(r'''<extra_id_\d+>''' , UpperCamelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def __A ( self : List[Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(UpperCamelCase__ ) for token in self.get_sentinel_tokens()]
34
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCamelCase : List[str] = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'google/mobilenet_v1_1.0_224': 'https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json', 'google/mobilenet_v1_0.75_192': 'https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """mobilenet_v1""" def __init__( self : List[str] , UpperCamelCase__ : Tuple=3 , UpperCamelCase__ : List[str]=224 , UpperCamelCase__ : Optional[int]=1.0 , UpperCamelCase__ : Optional[Any]=8 , UpperCamelCase__ : Dict="relu6" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=0.999 , UpperCamelCase__ : Optional[Any]=0.02 , UpperCamelCase__ : int=0.001 , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) SCREAMING_SNAKE_CASE : Dict = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = image_size SCREAMING_SNAKE_CASE : Any = depth_multiplier SCREAMING_SNAKE_CASE : List[Any] = min_depth SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Tuple = tf_padding SCREAMING_SNAKE_CASE : Any = classifier_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : str = layer_norm_eps class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.11""") @property def __A ( self : Optional[int] ): '''simple docstring''' return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __A ( self : Optional[Any] ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __A ( self : Any ): '''simple docstring''' return 1E-4
34
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __UpperCamelCase : str = False class lowercase__ ( unittest.TestCase): pass @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Union[str, Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = VersatileDiffusionPipeline.from_pretrained(UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''cyberpunk 2077''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe.dual_guided( prompt=UpperCamelCase__ , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images SCREAMING_SNAKE_CASE : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : int = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe.text_to_image( prompt=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : List[str] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = pipe.image_variation(UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : Optional[int] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
34
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase : str = logging.get_logger(__name__) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Any = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(_lowercase , _lowercase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(_lowercase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : str = '''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" SCREAMING_SNAKE_CASE : Optional[int] = BitConfig( conv_layer=_lowercase , num_labels=1_000 , idalabel=_lowercase , labelaid=_lowercase , ) return config def A ( _lowercase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''stem.conv''' , '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''blocks''' , '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''head.fc''' , '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE : str = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''bit.encoder.''' + name return name def A ( ): SCREAMING_SNAKE_CASE : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ) return im @torch.no_grad() def A ( _lowercase , _lowercase , _lowercase=False ): SCREAMING_SNAKE_CASE : List[Any] = get_config(_lowercase ) # load original model from timm SCREAMING_SNAKE_CASE : Optional[Any] = create_model(_lowercase , pretrained=_lowercase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE : Optional[int] = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Dict = state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE : str = BitForImageClassification(_lowercase ) model.eval() model.load_state_dict(_lowercase ) # create image processor SCREAMING_SNAKE_CASE : Optional[Any] = create_transform(**resolve_data_config({} , model=_lowercase ) ) SCREAMING_SNAKE_CASE : List[str] = transform.transforms SCREAMING_SNAKE_CASE : Union[str, Any] = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE : Tuple = BitImageProcessor( do_resize=_lowercase , size={'''shortest_edge''': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_lowercase , crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} , do_normalize=_lowercase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Union[str, Any] = transform(_lowercase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE : Optional[int] = processor(_lowercase , return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(_lowercase , _lowercase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = outputs.logits print('''Logits:''' , logits[0, :3] ) print('''Predicted class:''' , model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE : List[Any] = timm_model(_lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowercase , outputs.logits , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_lowercase ).mkdir(exist_ok=_lowercase ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowercase ) processor.save_pretrained(_lowercase ) 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__": __UpperCamelCase : 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.', ) __UpperCamelCase : Optional[int] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def A ( _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = analyze_text(_lowercase ) SCREAMING_SNAKE_CASE : Any = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Tuple = sum(single_char_strings.values() ) # one length string SCREAMING_SNAKE_CASE : Tuple = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: SCREAMING_SNAKE_CASE : Tuple = single_char_strings[ch] SCREAMING_SNAKE_CASE : List[str] = my_str / all_sum my_fir_sum += prob * math.loga(_lowercase ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string SCREAMING_SNAKE_CASE : Optional[Any] = sum(two_char_strings.values() ) SCREAMING_SNAKE_CASE : List[str] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : Any = two_char_strings[sequence] SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) / all_sum my_sec_sum += prob * math.loga(_lowercase ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = Counter() # type: ignore SCREAMING_SNAKE_CASE : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_lowercase ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def A ( ): import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
34
1
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class lowercase__ : UpperCamelCase_ = field( metadata={"""help""": """The output directory where the model will be written."""} , ) UpperCamelCase_ = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } , ) UpperCamelCase_ = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""}) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""}) def A ( ): SCREAMING_SNAKE_CASE : Optional[Any] = HfArgumentParser((ModelArguments,) ) ((SCREAMING_SNAKE_CASE) , ) : int = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: SCREAMING_SNAKE_CASE : Union[str, Any] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: SCREAMING_SNAKE_CASE : str = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[Any] = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=_lowercase , decoder_config=_lowercase , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens SCREAMING_SNAKE_CASE : Tuple = decoder_config.decoder_start_token_id SCREAMING_SNAKE_CASE : str = decoder_config.pad_token_id if decoder_start_token_id is None: SCREAMING_SNAKE_CASE : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: SCREAMING_SNAKE_CASE : str = decoder_config.eos_token_id # This is necessary to make Flax's generate() work SCREAMING_SNAKE_CASE : List[str] = decoder_config.eos_token_id SCREAMING_SNAKE_CASE : Optional[int] = decoder_start_token_id SCREAMING_SNAKE_CASE : Tuple = pad_token_id SCREAMING_SNAKE_CASE : int = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) SCREAMING_SNAKE_CASE : Tuple = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) SCREAMING_SNAKE_CASE : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCamelCase : Tuple = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Any = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
1
from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup __UpperCamelCase : int = 'https://www.indeed.co.in/jobs?q=mobile+app+development&l=' def A ( _lowercase = "mumbai" ): SCREAMING_SNAKE_CASE : Tuple = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): SCREAMING_SNAKE_CASE : Dict = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() SCREAMING_SNAKE_CASE : Optional[int] = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs('Bangalore'), 1): print(f"""Job {i:>2} is {job[0]} at {job[1]}""")
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
1
import csv from collections import defaultdict from dataclasses import dataclass, field from typing import List, Optional import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import ScalarFormatter from transformers import HfArgumentParser def A ( _lowercase=None , _lowercase=None ): return field(default_factory=lambda: default , metadata=_lowercase ) @dataclass class lowercase__ : UpperCamelCase_ = field( metadata={"""help""": """The csv file to plot."""} , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Whether to plot along batch size or sequence length. Defaults to sequence length."""} , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Whether the csv file has time results or memory results. Defaults to memory results."""} , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Disable logarithmic scale when plotting"""} , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={ """help""": """Whether the csv file has training results or inference results. Defaults to inference results.""" } , ) UpperCamelCase_ = field( default=UpperCamelCase_ , metadata={"""help""": """Filename under which the plot will be saved. If unused no plot is saved."""} , ) UpperCamelCase_ = list_field( default=UpperCamelCase_ , metadata={"""help""": """List of model names that are used instead of the ones in the csv file."""}) def A ( _lowercase ): try: int(_lowercase ) return True except ValueError: return False def A ( _lowercase ): try: float(_lowercase ) return True except ValueError: return False class lowercase__ : def __init__( self : Any , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = args SCREAMING_SNAKE_CASE : Optional[Any] = defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}} ) with open(self.args.csv_file , newline='''''' ) as csv_file: SCREAMING_SNAKE_CASE : List[Any] = csv.DictReader(UpperCamelCase__ ) for row in reader: SCREAMING_SNAKE_CASE : Dict = row['''model'''] self.result_dict[model_name]["bsz"].append(int(row['''batch_size'''] ) ) self.result_dict[model_name]["seq_len"].append(int(row['''sequence_length'''] ) ) if can_convert_to_int(row['''result'''] ): # value is not None SCREAMING_SNAKE_CASE : Tuple = int(row['''result'''] ) elif can_convert_to_float(row['''result'''] ): # value is not None SCREAMING_SNAKE_CASE : Dict = float(row['''result'''] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = plt.subplots() SCREAMING_SNAKE_CASE : List[str] = '''Time usage''' if self.args.is_time else '''Memory usage''' SCREAMING_SNAKE_CASE : Tuple = title_str + ''' for training''' if self.args.is_train else title_str + ''' for inference''' if not self.args.no_log_scale: # set logarithm scales ax.set_xscale('''log''' ) ax.set_yscale('''log''' ) for axis in [ax.xaxis, ax.yaxis]: axis.set_major_formatter(ScalarFormatter() ) for model_name_idx, model_name in enumerate(self.result_dict.keys() ): SCREAMING_SNAKE_CASE : Any = sorted(set(self.result_dict[model_name]['''bsz'''] ) ) SCREAMING_SNAKE_CASE : int = sorted(set(self.result_dict[model_name]['''seq_len'''] ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.result_dict[model_name]['''result'''] ((SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE)) : List[str] = ( (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes) ) SCREAMING_SNAKE_CASE : str = ( model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx] ) for inner_loop_value in inner_loop_array: if self.args.plot_along_batch: SCREAMING_SNAKE_CASE : str = np.asarray( [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results] , dtype=UpperCamelCase__ , ) else: SCREAMING_SNAKE_CASE : Optional[Any] = np.asarray( [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results] , dtype=np.floataa , ) ((SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE)) : Any = ( ('''batch_size''', '''len''') if self.args.plot_along_batch else ('''in #tokens''', '''bsz''') ) SCREAMING_SNAKE_CASE : List[str] = np.asarray(UpperCamelCase__ , UpperCamelCase__ )[: len(UpperCamelCase__ )] plt.scatter( UpperCamelCase__ , UpperCamelCase__ , label=f"""{label_model_name} - {inner_loop_label}: {inner_loop_value}""" ) plt.plot(UpperCamelCase__ , UpperCamelCase__ , '''--''' ) title_str += f""" {label_model_name} vs.""" SCREAMING_SNAKE_CASE : List[Any] = title_str[:-4] SCREAMING_SNAKE_CASE : str = '''Time in s''' if self.args.is_time else '''Memory in MB''' # plot plt.title(UpperCamelCase__ ) plt.xlabel(UpperCamelCase__ ) plt.ylabel(UpperCamelCase__ ) plt.legend() if self.args.figure_png_file is not None: plt.savefig(self.args.figure_png_file ) else: plt.show() def A ( ): SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(_lowercase ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses()[0] SCREAMING_SNAKE_CASE : str = Plot(args=_lowercase ) plot.plot() if __name__ == "__main__": main()
34
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __UpperCamelCase : Dict = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None ): SCREAMING_SNAKE_CASE : Union[str, Any] = True while ask_again: SCREAMING_SNAKE_CASE : Optional[Any] = input(_lowercase ) try: if default is not None and len(_lowercase ) == 0: return default return convert_value(_lowercase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowercase ) def A ( _lowercase , _lowercase=[] , _lowercase=None , _lowercase=0 ): SCREAMING_SNAKE_CASE : Dict = BulletMenu(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : str = menu.run(default_choice=_lowercase ) return convert_value(_lowercase ) if convert_value is not None else result def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = int(_lowercase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A ( _lowercase ): return {"yes": True, "no": False}[value.lower()] class lowercase__ ( argparse.RawDescriptionHelpFormatter): def __A ( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = super()._format_usage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
34
1
from functools import lru_cache @lru_cache def A ( _lowercase ): if num < 0: raise ValueError('''Number should not be negative.''' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
34
from __future__ import annotations from typing import Any class lowercase__ ( UpperCamelCase_): pass class lowercase__ : def __init__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = data SCREAMING_SNAKE_CASE : Node | None = None def __iter__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self SCREAMING_SNAKE_CASE : Tuple = [] while node: if node in visited: raise ContainsLoopError visited.append(UpperCamelCase__ ) yield node.data SCREAMING_SNAKE_CASE : Dict = node.next_node @property def __A ( self : Optional[int] ): '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": __UpperCamelCase : List[Any] = Node(1) __UpperCamelCase : str = Node(2) __UpperCamelCase : Dict = Node(3) __UpperCamelCase : List[Any] = Node(4) print(root_node.has_loop) # False __UpperCamelCase : int = root_node.next_node print(root_node.has_loop) # True __UpperCamelCase : Union[str, Any] = Node(5) __UpperCamelCase : Union[str, Any] = Node(6) __UpperCamelCase : List[Any] = Node(5) __UpperCamelCase : List[str] = Node(6) print(root_node.has_loop) # False __UpperCamelCase : List[Any] = Node(1) print(root_node.has_loop) # False
34
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Any = logging.get_logger(__name__) __UpperCamelCase : Dict = { 'google/pegasus-large': 'https://huggingface.co/google/pegasus-large/resolve/main/config.json', # See all PEGASUS models at https://huggingface.co/models?filter=pegasus } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """pegasus""" UpperCamelCase_ = ["""past_key_values"""] UpperCamelCase_ = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self : Any , UpperCamelCase__ : Dict=5_0265 , UpperCamelCase__ : Dict=1024 , UpperCamelCase__ : str=12 , UpperCamelCase__ : List[Any]=4096 , UpperCamelCase__ : Optional[Any]=16 , UpperCamelCase__ : str=12 , UpperCamelCase__ : List[str]=4096 , UpperCamelCase__ : Dict=16 , UpperCamelCase__ : int=0.0 , UpperCamelCase__ : int=0.0 , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Any="gelu" , UpperCamelCase__ : str=1024 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : List[str]=0.0 , UpperCamelCase__ : str=0.0 , UpperCamelCase__ : Tuple=0.02 , UpperCamelCase__ : str=0 , UpperCamelCase__ : str=False , UpperCamelCase__ : Optional[int]=0 , UpperCamelCase__ : Optional[int]=1 , UpperCamelCase__ : List[str]=1 , **UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = d_model SCREAMING_SNAKE_CASE : List[Any] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Dict = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layers SCREAMING_SNAKE_CASE : Tuple = decoder_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = dropout SCREAMING_SNAKE_CASE : str = attention_dropout SCREAMING_SNAKE_CASE : int = activation_dropout SCREAMING_SNAKE_CASE : List[Any] = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : Optional[int] = encoder_layerdrop SCREAMING_SNAKE_CASE : Optional[Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Dict = encoder_layers SCREAMING_SNAKE_CASE : Tuple = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , is_encoder_decoder=UpperCamelCase__ , decoder_start_token_id=UpperCamelCase__ , forced_eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) @property def __A ( self : Optional[Any] ): '''simple docstring''' return self.encoder_attention_heads @property def __A ( self : Dict ): '''simple docstring''' return self.d_model
34
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""input_features""", """is_longer"""] def __init__( self : Optional[Any] , UpperCamelCase__ : Dict=64 , UpperCamelCase__ : Optional[Any]=4_8000 , UpperCamelCase__ : Tuple=480 , UpperCamelCase__ : Union[str, Any]=10 , UpperCamelCase__ : List[Any]=1024 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : int=False , UpperCamelCase__ : float = 0 , UpperCamelCase__ : float = 1_4000 , UpperCamelCase__ : int = None , UpperCamelCase__ : str = "fusion" , UpperCamelCase__ : str = "repeatpad" , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' super().__init__( feature_size=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , padding_value=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = top_db SCREAMING_SNAKE_CASE : Union[str, Any] = truncation SCREAMING_SNAKE_CASE : str = padding SCREAMING_SNAKE_CASE : List[Any] = fft_window_size SCREAMING_SNAKE_CASE : Tuple = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE : List[str] = hop_length SCREAMING_SNAKE_CASE : List[Any] = max_length_s SCREAMING_SNAKE_CASE : Tuple = max_length_s * sampling_rate SCREAMING_SNAKE_CASE : List[Any] = sampling_rate SCREAMING_SNAKE_CASE : List[str] = frequency_min SCREAMING_SNAKE_CASE : Any = frequency_max SCREAMING_SNAKE_CASE : List[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm=UpperCamelCase__ , mel_scale='''htk''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm='''slaney''' , mel_scale='''slaney''' , ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __A ( self : Optional[int] , UpperCamelCase__ : np.array , UpperCamelCase__ : Optional[np.array] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = spectrogram( UpperCamelCase__ , window_function(self.fft_window_size , '''hann''' ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=UpperCamelCase__ , log_mel='''dB''' , ) return log_mel_spectrogram.T def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : int = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : Any = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE : List[Any] = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE : int = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE : Optional[int] = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE : Optional[Any] = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE : Tuple = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE : str = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.functional.interpolate( UpperCamelCase__ , size=[chunk_frames, 64] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __A ( self : Dict , UpperCamelCase__ : np.array , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] ): '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE : Optional[Any] = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) - max_length SCREAMING_SNAKE_CASE : Dict = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE : List[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE : Optional[Any] = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE : Tuple = False else: SCREAMING_SNAKE_CASE : str = self._random_mel_fusion(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = True else: raise NotImplementedError(f"""data_truncating {truncation} not implemented""" ) else: SCREAMING_SNAKE_CASE : List[str] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE : Tuple = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Any = np.stack(np.tile(UpperCamelCase__ , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE : List[Any] = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.stack(np.tile(UpperCamelCase__ , UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.pad(UpperCamelCase__ , (0, max_length - waveform.shape[0]) , mode='''constant''' , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE : List[Any] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE : List[str] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , UpperCamelCase__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , UpperCamelCase__ : str = None , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , **UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE : List[str] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" f""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE : List[str] = isinstance(UpperCamelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) SCREAMING_SNAKE_CASE : int = is_batched_numpy or ( isinstance(UpperCamelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE : Any = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase__ , np.ndarray ): SCREAMING_SNAKE_CASE : List[Any] = np.asarray(UpperCamelCase__ , dtype=np.floataa ) elif isinstance(UpperCamelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE : List[str] = [np.asarray(UpperCamelCase__ )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE : int = [ self._get_input_mel(UpperCamelCase__ , max_length if max_length else self.nb_max_samples , UpperCamelCase__ , UpperCamelCase__ ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : List[str] = [] for mel, longer in padded_inputs: input_mel.append(UpperCamelCase__ ) is_longer.append(UpperCamelCase__ ) if truncation == "fusion" and sum(UpperCamelCase__ ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.randint(0 , len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = True if isinstance(input_mel[0] , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE : Optional[Any] = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_features''': input_mel, '''is_longer''': is_longer} SCREAMING_SNAKE_CASE : int = BatchFeature(UpperCamelCase__ ) if return_tensors is not None: SCREAMING_SNAKE_CASE : int = input_features.convert_to_tensors(UpperCamelCase__ ) return input_features
34
1
import requests from bsa import BeautifulSoup def A ( _lowercase = "AAPL" ): SCREAMING_SNAKE_CASE : Tuple = f"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" SCREAMING_SNAKE_CASE : Tuple = BeautifulSoup(requests.get(_lowercase ).text , '''html.parser''' ) SCREAMING_SNAKE_CASE : Optional[Any] = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __UpperCamelCase : str = logging.get_logger(__name__) __UpperCamelCase : str = { 'microsoft/layoutlmv3-base': 'https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """layoutlmv3""" def __init__( self : Optional[int] , UpperCamelCase__ : Union[str, Any]=5_0265 , UpperCamelCase__ : List[Any]=768 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Tuple=3072 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Any=512 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : Optional[Any]=1E-5 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : int=0 , UpperCamelCase__ : str=2 , UpperCamelCase__ : List[str]=1024 , UpperCamelCase__ : str=128 , UpperCamelCase__ : str=128 , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Optional[Any]=64 , UpperCamelCase__ : Dict=256 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Dict=224 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : Any=None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__( vocab_size=UpperCamelCase__ , hidden_size=UpperCamelCase__ , num_hidden_layers=UpperCamelCase__ , num_attention_heads=UpperCamelCase__ , intermediate_size=UpperCamelCase__ , hidden_act=UpperCamelCase__ , hidden_dropout_prob=UpperCamelCase__ , attention_probs_dropout_prob=UpperCamelCase__ , max_position_embeddings=UpperCamelCase__ , type_vocab_size=UpperCamelCase__ , initializer_range=UpperCamelCase__ , layer_norm_eps=UpperCamelCase__ , pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : List[str] = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : str = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : Union[str, Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Union[str, Any] = max_rel_ad_pos SCREAMING_SNAKE_CASE : Union[str, Any] = text_embed SCREAMING_SNAKE_CASE : List[str] = visual_embed SCREAMING_SNAKE_CASE : Optional[Any] = input_size SCREAMING_SNAKE_CASE : Optional[Any] = num_channels SCREAMING_SNAKE_CASE : List[Any] = patch_size SCREAMING_SNAKE_CASE : List[Any] = classifier_dropout class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.12""") @property def __A ( self : str ): '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __A ( self : int ): '''simple docstring''' return 1E-5 @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Optional[Any] , UpperCamelCase__ : "ProcessorMixin" , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , ): '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , UpperCamelCase__ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : List[Any] = processor.tokenizer.num_special_tokens_to_add(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=UpperCamelCase__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : Any = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = dict( processor( UpperCamelCase__ , text=UpperCamelCase__ , boxes=UpperCamelCase__ , return_tensors=UpperCamelCase__ , ) ) return inputs
34
1
__UpperCamelCase : List[str] = 9.8_0665 def A ( _lowercase , _lowercase , _lowercase = g ): if fluid_density <= 0: raise ValueError('''Impossible fluid density''' ) if volume < 0: raise ValueError('''Impossible Object volume''' ) if gravity <= 0: raise ValueError('''Impossible Gravity''' ) return fluid_density * gravity * volume if __name__ == "__main__": import doctest # run doctest doctest.testmod()
34
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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FunnelTokenizer UpperCamelCase_ = FunnelTokenizerFast UpperCamelCase_ = True UpperCamelCase_ = True def __A ( self : Union[str, Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[Any] = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : List[str] = 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 __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : int = '''unwanted, running''' return input_text, output_text def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCamelCase__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [7, 4, 5, 10, 8, 9] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_tokenizers(do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE : int = tokenizer('''UNwant\u00E9d,running''' ) SCREAMING_SNAKE_CASE : Optional[Any] = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
34
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowercase__ : def __init__( self : List[str] , UpperCamelCase__ : List[str] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = parent SCREAMING_SNAKE_CASE : Optional[Any] = 13 SCREAMING_SNAKE_CASE : Any = 7 SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : str = False SCREAMING_SNAKE_CASE : Optional[int] = False SCREAMING_SNAKE_CASE : Tuple = False SCREAMING_SNAKE_CASE : int = 2 SCREAMING_SNAKE_CASE : Any = 99 SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Optional[Any] = 32 SCREAMING_SNAKE_CASE : List[Any] = 2 SCREAMING_SNAKE_CASE : Any = 4 SCREAMING_SNAKE_CASE : Dict = 0.1 SCREAMING_SNAKE_CASE : Tuple = 0.1 SCREAMING_SNAKE_CASE : Dict = 512 SCREAMING_SNAKE_CASE : Tuple = 16 SCREAMING_SNAKE_CASE : Any = 2 SCREAMING_SNAKE_CASE : int = 0.02 SCREAMING_SNAKE_CASE : Tuple = 3 SCREAMING_SNAKE_CASE : List[Any] = 4 SCREAMING_SNAKE_CASE : int = '''last''' SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Optional[Any] = None SCREAMING_SNAKE_CASE : Optional[int] = 0 def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : int = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) SCREAMING_SNAKE_CASE : int = None if self.use_input_lengths: SCREAMING_SNAKE_CASE : Dict = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length SCREAMING_SNAKE_CASE : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) SCREAMING_SNAKE_CASE : Union[str, Any] = None SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[str] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __A ( self : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TFFlaubertModel(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids} SCREAMING_SNAKE_CASE : Optional[Any] = model(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = [input_ids, input_mask] SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , UpperCamelCase__ : int , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = TFFlaubertWithLMHeadModel(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids} SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : List[str] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Dict , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFFlaubertForQuestionAnsweringSimple(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''lengths''': input_lengths} SCREAMING_SNAKE_CASE : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self : str , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFFlaubertForSequenceClassification(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = {'''input_ids''': input_ids, '''lengths''': input_lengths} SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : List[str] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.num_labels SCREAMING_SNAKE_CASE : Optional[int] = TFFlaubertForTokenClassification(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} SCREAMING_SNAKE_CASE : Union[str, Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_choices SCREAMING_SNAKE_CASE : int = TFFlaubertForMultipleChoice(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Dict = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Optional[Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : int = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : Optional[Any] = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''langs''': token_type_ids, '''lengths''': input_lengths, } return config, inputs_dict @require_tf class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase_ = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable UpperCamelCase_ = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase_ = False UpperCamelCase_ = False def __A ( self : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str ): '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFFlaubertModelTester(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = ConfigTester(self , config_class=UpperCamelCase__ , emb_dim=37 ) def __A ( self : int ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__ ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__ ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCamelCase__ ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCamelCase__ ) @slow def __A ( self : Optional[int] ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Union[str, Any] = TFFlaubertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowercase__ ( unittest.TestCase): @slow def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased''' ) SCREAMING_SNAKE_CASE : Optional[Any] = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" SCREAMING_SNAKE_CASE : Tuple = model(UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : str = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape , UpperCamelCase__ ) # compare the actual values for a slice. SCREAMING_SNAKE_CASE : Optional[Any] = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
34
import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = dataset SCREAMING_SNAKE_CASE : Optional[Any] = process SCREAMING_SNAKE_CASE : Union[str, Any] = params def __len__( self : Tuple ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dataset[i] SCREAMING_SNAKE_CASE : Optional[int] = self.process(UpperCamelCase__ , **self.params ) return processed class lowercase__ ( UpperCamelCase_): def __init__( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = loader SCREAMING_SNAKE_CASE : List[Any] = infer SCREAMING_SNAKE_CASE : int = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : List[str] = loader_batch_size # Internal bookkeeping SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None def __len__( self : int ): '''simple docstring''' return len(self.loader ) def __iter__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice SCREAMING_SNAKE_CASE : Optional[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Convert ModelOutput to tuple first SCREAMING_SNAKE_CASE : Dict = element.to_tuple() if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : List[str] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around SCREAMING_SNAKE_CASE : int = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Union[str, Any] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Tuple = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. SCREAMING_SNAKE_CASE : Tuple = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 SCREAMING_SNAKE_CASE : Any = self._loader_batch_data.__class__(UpperCamelCase__ ) self._loader_batch_index += 1 return result def __A ( self : Union[str, Any] ): '''simple docstring''' if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch SCREAMING_SNAKE_CASE : Tuple = next(self.iterator ) SCREAMING_SNAKE_CASE : List[Any] = self.infer(UpperCamelCase__ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Optional[int] = processed else: SCREAMING_SNAKE_CASE : int = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : int = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[Any] = observed_batch_size # Setting internal index to unwrap the batch SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : int = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=None ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __iter__( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = iter(self.loader ) SCREAMING_SNAKE_CASE : List[Any] = None return self def __A ( self : List[str] ): '''simple docstring''' if self.subiterator is None: SCREAMING_SNAKE_CASE : Dict = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item SCREAMING_SNAKE_CASE : Any = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators SCREAMING_SNAKE_CASE : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = next(self.subiterator ) return processed class lowercase__ ( UpperCamelCase_): def __iter__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Optional[int] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Tuple = self.loader_batch_item() SCREAMING_SNAKE_CASE : Any = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator while not is_last: SCREAMING_SNAKE_CASE : Any = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = processed else: SCREAMING_SNAKE_CASE : Union[str, Any] = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : List[str] = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[str] = observed_batch_size SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : str = 0 while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Any = self.loader_batch_item() SCREAMING_SNAKE_CASE : List[Any] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator else: SCREAMING_SNAKE_CASE : int = processed SCREAMING_SNAKE_CASE : List[str] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) return accumulator class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Dict = key def __len__( self : Optional[int] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' return self.dataset[i][self.key] class lowercase__ ( UpperCamelCase_): def __init__( self : List[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : List[str] = keya SCREAMING_SNAKE_CASE : Tuple = keya def __len__( self : List[str] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
34
1
import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class lowercase__ ( unittest.TestCase): def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = tempfile.mkdtemp() # fmt: off SCREAMING_SNAKE_CASE : Optional[int] = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest'''] # fmt: on SCREAMING_SNAKE_CASE : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) SCREAMING_SNAKE_CASE : int = { '''do_resize''': True, '''size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.5, 0.5, 0.5], '''image_std''': [0.5, 0.5, 0.5], } SCREAMING_SNAKE_CASE : List[str] = os.path.join(self.tmpdirname , UpperCamelCase__ ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Dict , **UpperCamelCase__ : List[str] ): '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Tuple , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE : List[Any] = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Any = self.get_image_processor() SCREAMING_SNAKE_CASE : Dict = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE : Union[str, Any] = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE : str = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) SCREAMING_SNAKE_CASE : Any = self.get_image_processor(do_normalize=UpperCamelCase__ , padding_value=1.0 ) SCREAMING_SNAKE_CASE : Optional[int] = VisionTextDualEncoderProcessor.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 , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.get_image_processor() SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE : Tuple = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : Optional[Any] = image_processor(UpperCamelCase__ , return_tensors='''np''' ) SCREAMING_SNAKE_CASE : List[str] = processor(images=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 __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_image_processor() SCREAMING_SNAKE_CASE : int = self.get_tokenizer() SCREAMING_SNAKE_CASE : str = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''lower newer''' SCREAMING_SNAKE_CASE : List[Any] = processor(text=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer(UpperCamelCase__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.get_image_processor() SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE : Tuple = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''lower newer''' SCREAMING_SNAKE_CASE : int = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : List[str] = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with self.assertRaises(UpperCamelCase__ ): processor() def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_image_processor() SCREAMING_SNAKE_CASE : Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE : Optional[Any] = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE : Dict = processor.batch_decode(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.batch_decode(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.get_image_processor() SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE : List[str] = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = '''lower newer''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : str = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
34
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """deberta-v2""" def __init__( self : Optional[Any] , UpperCamelCase__ : Any=12_8100 , UpperCamelCase__ : Optional[int]=1536 , UpperCamelCase__ : Dict=24 , UpperCamelCase__ : List[str]=24 , UpperCamelCase__ : Tuple=6144 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Optional[int]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : Optional[Any]=0 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : List[Any]=1E-7 , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=-1 , UpperCamelCase__ : List[Any]=0 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : str="gelu" , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention SCREAMING_SNAKE_CASE : Optional[Any] = max_relative_positions SCREAMING_SNAKE_CASE : Optional[int] = pad_token_id SCREAMING_SNAKE_CASE : Optional[Any] = position_biased_input # Backwards compatibility if type(UpperCamelCase__ ) == str: SCREAMING_SNAKE_CASE : Optional[int] = [x.strip() for x in pos_att_type.lower().split('''|''' )] SCREAMING_SNAKE_CASE : Any = pos_att_type SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = kwargs.get('''pooler_hidden_size''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pooler_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = pooler_hidden_act class lowercase__ ( UpperCamelCase_): @property def __A ( self : Union[str, Any] ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Dict , UpperCamelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().generate_dummy_inputs(preprocessor=UpperCamelCase__ , framework=UpperCamelCase__ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
34
1
import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCamelCase : int = logging.get_logger(__name__) __UpperCamelCase : List[Any] = { 'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json', # See all DETR models at https://huggingface.co/models?filter=detr } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """detr""" UpperCamelCase_ = ["""past_key_values"""] UpperCamelCase_ = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : int , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : str=3 , UpperCamelCase__ : Union[str, Any]=100 , UpperCamelCase__ : Dict=6 , UpperCamelCase__ : str=2048 , UpperCamelCase__ : Dict=8 , UpperCamelCase__ : List[str]=6 , UpperCamelCase__ : Dict=2048 , UpperCamelCase__ : List[Any]=8 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : Optional[int]=0.0 , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int="relu" , UpperCamelCase__ : Optional[Any]=256 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : int=0.0 , UpperCamelCase__ : Optional[int]=0.0 , UpperCamelCase__ : str=0.02 , UpperCamelCase__ : int=1.0 , UpperCamelCase__ : List[Any]=False , UpperCamelCase__ : Optional[Any]="sine" , UpperCamelCase__ : List[str]="resnet50" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Any=False , UpperCamelCase__ : Optional[Any]=1 , UpperCamelCase__ : List[Any]=5 , UpperCamelCase__ : Any=2 , UpperCamelCase__ : List[Any]=1 , UpperCamelCase__ : Tuple=1 , UpperCamelCase__ : str=5 , UpperCamelCase__ : int=2 , UpperCamelCase__ : Tuple=0.1 , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) SCREAMING_SNAKE_CASE : Any = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = backbone_config.get('''model_type''' ) SCREAMING_SNAKE_CASE : Tuple = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE : int = config_class.from_dict(UpperCamelCase__ ) # set timm attributes to None SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = None, None, None SCREAMING_SNAKE_CASE : Optional[Any] = use_timm_backbone SCREAMING_SNAKE_CASE : Tuple = backbone_config SCREAMING_SNAKE_CASE : int = num_channels SCREAMING_SNAKE_CASE : List[str] = num_queries SCREAMING_SNAKE_CASE : Any = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Any = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = decoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = decoder_layers SCREAMING_SNAKE_CASE : int = decoder_attention_heads SCREAMING_SNAKE_CASE : Tuple = dropout SCREAMING_SNAKE_CASE : Tuple = attention_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = activation_dropout SCREAMING_SNAKE_CASE : Dict = activation_function SCREAMING_SNAKE_CASE : List[Any] = init_std SCREAMING_SNAKE_CASE : Union[str, Any] = init_xavier_std SCREAMING_SNAKE_CASE : Optional[int] = encoder_layerdrop SCREAMING_SNAKE_CASE : List[str] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[str] = encoder_layers SCREAMING_SNAKE_CASE : Optional[int] = auxiliary_loss SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Optional[Any] = backbone SCREAMING_SNAKE_CASE : List[str] = use_pretrained_backbone SCREAMING_SNAKE_CASE : Any = dilation # Hungarian matcher SCREAMING_SNAKE_CASE : Optional[int] = class_cost SCREAMING_SNAKE_CASE : str = bbox_cost SCREAMING_SNAKE_CASE : List[str] = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE : List[str] = mask_loss_coefficient SCREAMING_SNAKE_CASE : Optional[Any] = dice_loss_coefficient SCREAMING_SNAKE_CASE : Dict = bbox_loss_coefficient SCREAMING_SNAKE_CASE : List[Any] = giou_loss_coefficient SCREAMING_SNAKE_CASE : Optional[Any] = eos_coefficient super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __A ( self : Optional[int] ): '''simple docstring''' return self.encoder_attention_heads @property def __A ( self : Any ): '''simple docstring''' return self.d_model @classmethod def __A ( cls : Union[str, Any] , UpperCamelCase__ : PretrainedConfig , **UpperCamelCase__ : List[str] ): '''simple docstring''' return cls(backbone_config=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: SCREAMING_SNAKE_CASE : int = self.backbone_config.to_dict() SCREAMING_SNAKE_CASE : Optional[Any] = self.__class__.model_type return output class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.11""") @property def __A ( self : Optional[Any] ): '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def __A ( self : Optional[Any] ): '''simple docstring''' return 1E-5 @property def __A ( self : int ): '''simple docstring''' return 12
34
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() __UpperCamelCase : str = logging.get_logger(__name__) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Any = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(_lowercase , _lowercase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(_lowercase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : str = '''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" SCREAMING_SNAKE_CASE : Optional[int] = BitConfig( conv_layer=_lowercase , num_labels=1_000 , idalabel=_lowercase , labelaid=_lowercase , ) return config def A ( _lowercase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''stem.conv''' , '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''blocks''' , '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''head.fc''' , '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE : str = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''bit.encoder.''' + name return name def A ( ): SCREAMING_SNAKE_CASE : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ) return im @torch.no_grad() def A ( _lowercase , _lowercase , _lowercase=False ): SCREAMING_SNAKE_CASE : List[Any] = get_config(_lowercase ) # load original model from timm SCREAMING_SNAKE_CASE : Optional[Any] = create_model(_lowercase , pretrained=_lowercase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE : Optional[int] = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Dict = state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE : str = BitForImageClassification(_lowercase ) model.eval() model.load_state_dict(_lowercase ) # create image processor SCREAMING_SNAKE_CASE : Optional[Any] = create_transform(**resolve_data_config({} , model=_lowercase ) ) SCREAMING_SNAKE_CASE : List[str] = transform.transforms SCREAMING_SNAKE_CASE : Union[str, Any] = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE : Tuple = BitImageProcessor( do_resize=_lowercase , size={'''shortest_edge''': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_lowercase , crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} , do_normalize=_lowercase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Union[str, Any] = transform(_lowercase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE : Optional[int] = processor(_lowercase , return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(_lowercase , _lowercase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = outputs.logits print('''Logits:''' , logits[0, :3] ) print('''Predicted class:''' , model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE : List[Any] = timm_model(_lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowercase , outputs.logits , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_lowercase ).mkdir(exist_ok=_lowercase ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowercase ) processor.save_pretrained(_lowercase ) 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__": __UpperCamelCase : 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.', ) __UpperCamelCase : Optional[int] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
1
from __future__ import annotations from fractions import Fraction def A ( _lowercase , _lowercase ): return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[int] = 11 SCREAMING_SNAKE_CASE : Optional[Any] = int('''1''' + '''0''' * digit_len ) for num in range(_lowercase , _lowercase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(_lowercase , _lowercase ): solutions.append(f"""{num}/{den}""" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Any = 10 return solutions def A ( _lowercase = 2 ): SCREAMING_SNAKE_CASE : Tuple = 1.0 for fraction in fraction_list(_lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = Fraction(_lowercase ) result *= frac.denominator / frac.numerator return int(_lowercase ) if __name__ == "__main__": print(solution())
34
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow __UpperCamelCase : str = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ 'text-classification', 'language-modeling', 'summarization', 'token-classification', 'question-answering', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) __UpperCamelCase : int = logging.getLogger() def A ( ): SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() parser.add_argument('''-f''' ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() return args.f def A ( _lowercase , _lowercase="eval" ): SCREAMING_SNAKE_CASE : Dict = os.path.join(_lowercase , f"""{split}_results.json""" ) if os.path.exists(_lowercase ): with open(_lowercase , '''r''' ) as f: return json.load(_lowercase ) raise ValueError(f"""can't find {path}""" ) __UpperCamelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase__ ( UpperCamelCase_): def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Tuple = f""" run_glue.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 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_glue.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : str = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_clm_flax.main() SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_summarization_flax.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f""" run_mlm.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} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_mlm_flax.main() SCREAMING_SNAKE_CASE : List[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_ta_mlm_flax.main() SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Any = f""" run_flax_ner.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} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_ner.main() SCREAMING_SNAKE_CASE : List[str] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_qa.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} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_qa.main() SCREAMING_SNAKE_CASE : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
34
1
from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = 42 class lowercase__ ( nn.Module): def __init__( self : List[Any] , UpperCamelCase__ : int=3 , UpperCamelCase__ : Optional[int]=3 , UpperCamelCase__ : Union[str, Any]=("DownEncoderBlock2D",) , UpperCamelCase__ : Tuple=(64,) , UpperCamelCase__ : List[str]=2 , UpperCamelCase__ : Union[str, Any]=32 , UpperCamelCase__ : str="silu" , UpperCamelCase__ : int=True , ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : int = layers_per_block SCREAMING_SNAKE_CASE : Dict = torch.nn.Convad( UpperCamelCase__ , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) SCREAMING_SNAKE_CASE : Optional[Any] = None SCREAMING_SNAKE_CASE : Union[str, Any] = nn.ModuleList([] ) # down SCREAMING_SNAKE_CASE : Union[str, Any] = block_out_channels[0] for i, down_block_type in enumerate(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[int] = output_channel SCREAMING_SNAKE_CASE : List[str] = block_out_channels[i] SCREAMING_SNAKE_CASE : Tuple = i == len(UpperCamelCase__ ) - 1 SCREAMING_SNAKE_CASE : Any = get_down_block( UpperCamelCase__ , num_layers=self.layers_per_block , in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , add_downsample=not is_final_block , resnet_eps=1E-6 , downsample_padding=0 , resnet_act_fn=UpperCamelCase__ , resnet_groups=UpperCamelCase__ , attention_head_dim=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) self.down_blocks.append(UpperCamelCase__ ) # mid SCREAMING_SNAKE_CASE : Optional[int] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=UpperCamelCase__ , output_scale_factor=1 , resnet_time_scale_shift='''default''' , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) # out SCREAMING_SNAKE_CASE : Any = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=UpperCamelCase__ , eps=1E-6 ) SCREAMING_SNAKE_CASE : Optional[int] = nn.SiLU() SCREAMING_SNAKE_CASE : int = 2 * out_channels if double_z else out_channels SCREAMING_SNAKE_CASE : int = nn.Convad(block_out_channels[-1] , UpperCamelCase__ , 3 , padding=1 ) SCREAMING_SNAKE_CASE : List[Any] = False def __A ( self : Tuple , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = x SCREAMING_SNAKE_CASE : Tuple = self.conv_in(UpperCamelCase__ ) if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCamelCase__ : Union[str, Any] ): def custom_forward(*UpperCamelCase__ : List[str] ): return module(*UpperCamelCase__ ) return custom_forward # down if is_torch_version('''>=''' , '''1.11.0''' ): for down_block in self.down_blocks: SCREAMING_SNAKE_CASE : Optional[int] = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) # middle SCREAMING_SNAKE_CASE : Any = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) else: for down_block in self.down_blocks: SCREAMING_SNAKE_CASE : Union[str, Any] = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ ) # middle SCREAMING_SNAKE_CASE : Tuple = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , UpperCamelCase__ ) else: # down for down_block in self.down_blocks: SCREAMING_SNAKE_CASE : str = down_block(UpperCamelCase__ ) # middle SCREAMING_SNAKE_CASE : Optional[Any] = self.mid_block(UpperCamelCase__ ) # post-process SCREAMING_SNAKE_CASE : Dict = self.conv_norm_out(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.conv_act(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = self.conv_out(UpperCamelCase__ ) return sample class lowercase__ ( nn.Module): def __init__( self : List[str] , UpperCamelCase__ : int=3 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : Any=("UpDecoderBlock2D",) , UpperCamelCase__ : Union[str, Any]=(64,) , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : List[Any]="silu" , UpperCamelCase__ : Optional[Any]="group" , ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Dict = layers_per_block SCREAMING_SNAKE_CASE : List[Any] = nn.Convad( UpperCamelCase__ , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : Tuple = nn.ModuleList([] ) SCREAMING_SNAKE_CASE : int = in_channels if norm_type == '''spatial''' else None # mid SCREAMING_SNAKE_CASE : Any = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=UpperCamelCase__ , output_scale_factor=1 , resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) # up SCREAMING_SNAKE_CASE : int = list(reversed(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : List[Any] = reversed_block_out_channels[0] for i, up_block_type in enumerate(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[str] = output_channel SCREAMING_SNAKE_CASE : Optional[int] = reversed_block_out_channels[i] SCREAMING_SNAKE_CASE : int = i == len(UpperCamelCase__ ) - 1 SCREAMING_SNAKE_CASE : Dict = get_up_block( UpperCamelCase__ , num_layers=self.layers_per_block + 1 , in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , prev_output_channel=UpperCamelCase__ , add_upsample=not is_final_block , resnet_eps=1E-6 , resnet_act_fn=UpperCamelCase__ , resnet_groups=UpperCamelCase__ , attention_head_dim=UpperCamelCase__ , temb_channels=UpperCamelCase__ , resnet_time_scale_shift=UpperCamelCase__ , ) self.up_blocks.append(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = output_channel # out if norm_type == "spatial": SCREAMING_SNAKE_CASE : List[str] = SpatialNorm(block_out_channels[0] , UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=UpperCamelCase__ , eps=1E-6 ) SCREAMING_SNAKE_CASE : List[Any] = nn.SiLU() SCREAMING_SNAKE_CASE : str = nn.Convad(block_out_channels[0] , UpperCamelCase__ , 3 , padding=1 ) SCREAMING_SNAKE_CASE : Any = False def __A ( self : Optional[int] , UpperCamelCase__ : Any , UpperCamelCase__ : Dict=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = z SCREAMING_SNAKE_CASE : Any = self.conv_in(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCamelCase__ : Optional[int] ): def custom_forward(*UpperCamelCase__ : Dict ): return module(*UpperCamelCase__ ) return custom_forward if is_torch_version('''>=''' , '''1.11.0''' ): # middle SCREAMING_SNAKE_CASE : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: SCREAMING_SNAKE_CASE : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) else: # middle SCREAMING_SNAKE_CASE : Dict = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: SCREAMING_SNAKE_CASE : Tuple = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , UpperCamelCase__ ) else: # middle SCREAMING_SNAKE_CASE : Optional[Any] = self.mid_block(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: SCREAMING_SNAKE_CASE : Any = up_block(UpperCamelCase__ , UpperCamelCase__ ) # post-process if latent_embeds is None: SCREAMING_SNAKE_CASE : str = self.conv_norm_out(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = self.conv_norm_out(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = self.conv_act(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.conv_out(UpperCamelCase__ ) return sample class lowercase__ ( nn.Module): def __init__( self : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple="random" , UpperCamelCase__ : Dict=False , UpperCamelCase__ : Optional[Any]=True ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[Any] = n_e SCREAMING_SNAKE_CASE : Tuple = vq_embed_dim SCREAMING_SNAKE_CASE : Union[str, Any] = beta SCREAMING_SNAKE_CASE : List[str] = legacy SCREAMING_SNAKE_CASE : Dict = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) SCREAMING_SNAKE_CASE : str = remap if self.remap is not None: self.register_buffer('''used''' , torch.tensor(np.load(self.remap ) ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.used.shape[0] SCREAMING_SNAKE_CASE : List[str] = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": SCREAMING_SNAKE_CASE : List[str] = self.re_embed SCREAMING_SNAKE_CASE : Tuple = self.re_embed + 1 print( f"""Remapping {self.n_e} indices to {self.re_embed} indices. """ f"""Using {self.unknown_index} for unknown indices.""" ) else: SCREAMING_SNAKE_CASE : Dict = n_e SCREAMING_SNAKE_CASE : int = sane_index_shape def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = inds.shape assert len(UpperCamelCase__ ) > 1 SCREAMING_SNAKE_CASE : Optional[int] = inds.reshape(ishape[0] , -1 ) SCREAMING_SNAKE_CASE : int = self.used.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = (inds[:, :, None] == used[None, None, ...]).long() SCREAMING_SNAKE_CASE : Union[str, Any] = match.argmax(-1 ) SCREAMING_SNAKE_CASE : List[Any] = match.sum(2 ) < 1 if self.unknown_index == "random": SCREAMING_SNAKE_CASE : Optional[Any] = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: SCREAMING_SNAKE_CASE : str = self.unknown_index return new.reshape(UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = inds.shape assert len(UpperCamelCase__ ) > 1 SCREAMING_SNAKE_CASE : List[Any] = inds.reshape(ishape[0] , -1 ) SCREAMING_SNAKE_CASE : Optional[Any] = self.used.to(UpperCamelCase__ ) if self.re_embed > self.used.shape[0]: # extra token SCREAMING_SNAKE_CASE : Any = 0 # simply set to zero SCREAMING_SNAKE_CASE : Any = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , UpperCamelCase__ ) return back.reshape(UpperCamelCase__ ) def __A ( self : int , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = z.permute(0 , 2 , 3 , 1 ).contiguous() SCREAMING_SNAKE_CASE : Any = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z SCREAMING_SNAKE_CASE : Dict = torch.argmin(torch.cdist(UpperCamelCase__ , self.embedding.weight ) , dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = self.embedding(UpperCamelCase__ ).view(z.shape ) SCREAMING_SNAKE_CASE : Union[str, Any] = None SCREAMING_SNAKE_CASE : Any = None # compute loss for embedding if not self.legacy: SCREAMING_SNAKE_CASE : Optional[int] = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients SCREAMING_SNAKE_CASE : List[Any] = z + (z_q - z).detach() # reshape back to match original input shape SCREAMING_SNAKE_CASE : Optional[Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: SCREAMING_SNAKE_CASE : Optional[Any] = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis SCREAMING_SNAKE_CASE : Optional[Any] = self.remap_to_used(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: SCREAMING_SNAKE_CASE : List[Any] = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __A ( self : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any ): '''simple docstring''' if self.remap is not None: SCREAMING_SNAKE_CASE : Any = indices.reshape(shape[0] , -1 ) # add batch axis SCREAMING_SNAKE_CASE : List[str] = self.unmap_to_all(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = indices.reshape(-1 ) # flatten again # get quantized latent vectors SCREAMING_SNAKE_CASE : Any = self.embedding(UpperCamelCase__ ) if shape is not None: SCREAMING_SNAKE_CASE : List[Any] = z_q.view(UpperCamelCase__ ) # reshape back to match original input shape SCREAMING_SNAKE_CASE : Dict = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class lowercase__ ( UpperCamelCase_): def __init__( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str]=False ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = parameters SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = torch.chunk(UpperCamelCase__ , 2 , dim=1 ) SCREAMING_SNAKE_CASE : Any = torch.clamp(self.logvar , -30.0 , 20.0 ) SCREAMING_SNAKE_CASE : Optional[int] = deterministic SCREAMING_SNAKE_CASE : Optional[Any] = torch.exp(0.5 * self.logvar ) SCREAMING_SNAKE_CASE : Tuple = torch.exp(self.logvar ) if self.deterministic: SCREAMING_SNAKE_CASE : int = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __A ( self : Any , UpperCamelCase__ : Optional[torch.Generator] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = randn_tensor( self.mean.shape , generator=UpperCamelCase__ , device=self.parameters.device , dtype=self.parameters.dtype ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.mean + self.std * sample return x def __A ( self : str , UpperCamelCase__ : str=None ): '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __A ( self : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : str=[1, 2, 3] ): '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) SCREAMING_SNAKE_CASE : Optional[Any] = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=UpperCamelCase__ ) def __A ( self : List[str] ): '''simple docstring''' return self.mean
34
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset __UpperCamelCase : Dict = random.Random() def A ( _lowercase , _lowercase=1.0 , _lowercase=None , _lowercase=None ): if rng is None: SCREAMING_SNAKE_CASE : Any = global_rng SCREAMING_SNAKE_CASE : int = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase__ ( unittest.TestCase): def __init__( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : str=7 , UpperCamelCase__ : Any=400 , UpperCamelCase__ : List[str]=2000 , UpperCamelCase__ : List[Any]=2048 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Union[str, Any]=1 , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : str=30 , UpperCamelCase__ : Tuple=4_4100 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : str = batch_size SCREAMING_SNAKE_CASE : str = min_seq_length SCREAMING_SNAKE_CASE : Dict = max_seq_length SCREAMING_SNAKE_CASE : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE : Optional[Any] = spectrogram_length SCREAMING_SNAKE_CASE : Optional[int] = feature_size SCREAMING_SNAKE_CASE : Tuple = num_audio_channels SCREAMING_SNAKE_CASE : Union[str, Any] = hop_length SCREAMING_SNAKE_CASE : List[Any] = chunk_length SCREAMING_SNAKE_CASE : str = sampling_rate def __A ( self : Optional[Any] ): '''simple docstring''' return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def __A ( self : Tuple , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Optional[int]=False ): '''simple docstring''' def _flatten(UpperCamelCase__ : str ): return list(itertools.chain(*UpperCamelCase__ ) ) if equal_length: SCREAMING_SNAKE_CASE : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE : int = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE : Optional[Any] = [np.asarray(UpperCamelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = TvltFeatureExtractor def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TvltFeatureExtractionTester(self ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''spectrogram_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''feature_size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''num_audio_channels''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''hop_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''chunk_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''sampling_rate''' ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : Optional[int] = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Optional[int] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Tuple = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : int = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Any = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] SCREAMING_SNAKE_CASE : Optional[int] = [np.asarray(UpperCamelCase__ ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched SCREAMING_SNAKE_CASE : Optional[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking SCREAMING_SNAKE_CASE : List[str] = feature_extractor( UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 , mask_audio=UpperCamelCase__ ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE : Optional[Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE : Dict = np.asarray(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def __A ( self : Optional[int] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE : Dict = ds.sort('''id''' ).select(range(UpperCamelCase__ ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE : int = TvltFeatureExtractor() SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) SCREAMING_SNAKE_CASE : str = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , UpperCamelCase__ , atol=1E-4 ) )
34
1
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. __UpperCamelCase : Union[str, Any] = abspath(join(dirname(dirname(__file__)), 'src')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='ignore', category=FutureWarning) def A ( _lowercase ): from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(_lowercase ) def A ( _lowercase ): from diffusers.utils.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE : str = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(_lowercase , id=_lowercase )
34
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowercase__ ( UpperCamelCase_ , UpperCamelCase_): UpperCamelCase_ = 1 @register_to_config def __init__( self : List[str] , UpperCamelCase__ : int = 1000 , UpperCamelCase__ : Optional[Union[np.ndarray, List[float]]] = None ): '''simple docstring''' self.set_timesteps(UpperCamelCase__ ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : str = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. SCREAMING_SNAKE_CASE : Tuple = 4 # running values SCREAMING_SNAKE_CASE : int = [] def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, torch.device] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = num_inference_steps SCREAMING_SNAKE_CASE : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: SCREAMING_SNAKE_CASE : int = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE : Optional[Any] = torch.sin(steps * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE : Dict = (1.0 - self.betas**2) ** 0.5 SCREAMING_SNAKE_CASE : Optional[Any] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] SCREAMING_SNAKE_CASE : List[str] = timesteps.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = [] def __A ( self : Tuple , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : int , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : bool = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) SCREAMING_SNAKE_CASE : Optional[int] = (self.timesteps == timestep).nonzero().item() SCREAMING_SNAKE_CASE : Union[str, Any] = timestep_index + 1 SCREAMING_SNAKE_CASE : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCamelCase__ ) if len(self.ets ) == 1: SCREAMING_SNAKE_CASE : Dict = self.ets[-1] elif len(self.ets ) == 2: SCREAMING_SNAKE_CASE : Optional[int] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: SCREAMING_SNAKE_CASE : str = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: SCREAMING_SNAKE_CASE : Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) SCREAMING_SNAKE_CASE : Optional[int] = self._get_prev_sample(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : torch.FloatTensor , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return sample def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.alphas[timestep_index] SCREAMING_SNAKE_CASE : List[str] = self.betas[timestep_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas[prev_timestep_index] SCREAMING_SNAKE_CASE : Tuple = self.betas[prev_timestep_index] SCREAMING_SNAKE_CASE : Dict = (sample - sigma * ets) / max(UpperCamelCase__ , 1E-8 ) SCREAMING_SNAKE_CASE : Optional[Any] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
34
1
def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = [] SCREAMING_SNAKE_CASE : List[str] = [] SCREAMING_SNAKE_CASE : List[str] = { '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator SCREAMING_SNAKE_CASE : Dict = len(_lowercase ) if (len(_lowercase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(_lowercase ) , '''Postfix'''.center(_lowercase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowercase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowercase ) # 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(_lowercase ) == 0: stack.append(_lowercase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowercase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowercase ) # push x to stack print( x.center(8 ) , (''''''.join(_lowercase )).ljust(_lowercase ) , (''''''.join(_lowercase )).ljust(_lowercase ) , sep=''' | ''' , ) # Output in tabular format while len(_lowercase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(_lowercase )).ljust(_lowercase ) , (''''''.join(_lowercase )).ljust(_lowercase ) , sep=''' | ''' , ) # Output in tabular format return "".join(_lowercase ) # return Postfix as str def A ( _lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowercase ) ): if infix[i] == "(": SCREAMING_SNAKE_CASE : Tuple = ''')''' # change "(" to ")" elif infix[i] == ")": SCREAMING_SNAKE_CASE : int = '''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(_lowercase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": __UpperCamelCase : str = input('\nEnter an Infix Equation = ') # Input an Infix equation __UpperCamelCase : Optional[Any] = ''.join(Infix.split()) # Remove spaces from the input print('\n\t', Infix, '(Infix) -> ', infix_2_prefix(Infix), '(Prefix)')
34
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
1
from abc import ABC, abstractmethod from argparse import ArgumentParser class lowercase__ ( UpperCamelCase_): @staticmethod @abstractmethod def __A ( UpperCamelCase__ : ArgumentParser ): '''simple docstring''' raise NotImplementedError() @abstractmethod def __A ( self : Optional[Any] ): '''simple docstring''' raise NotImplementedError()
34
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
1
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __UpperCamelCase : Any = logging.get_logger(__name__) __UpperCamelCase : Tuple = {'vocab_file': 'spiece.model'} __UpperCamelCase : Optional[Any] = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } __UpperCamelCase : Tuple = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] def __init__( self : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=False , UpperCamelCase__ : List[Any]=None , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : str=None , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : Optional[Dict[str, Any]] = None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs SCREAMING_SNAKE_CASE : Optional[int] = kwargs.get('''name_or_path''' ) if name_or_path is None: logger.warning( '''name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,''' ''' you are testing the model, this can safely be ignored''' ) SCREAMING_SNAKE_CASE : List[Any] = '''None''' # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing SCREAMING_SNAKE_CASE : Dict = '''<|endoftext|>''' if eos_token is None else eos_token SCREAMING_SNAKE_CASE : List[str] = '''<unk>''' if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: SCREAMING_SNAKE_CASE : Optional[int] = unk_token if pad_token is None else pad_token SCREAMING_SNAKE_CASE : Optional[int] = eos_token if bos_token is None else bos_token else: SCREAMING_SNAKE_CASE : int = '''<pad>''' if pad_token is None else pad_token SCREAMING_SNAKE_CASE : Union[str, Any] = '''<s>''' if bos_token is None else bos_token super().__init__( do_lower_case=UpperCamelCase__ , remove_space=UpperCamelCase__ , keep_accents=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = do_lower_case SCREAMING_SNAKE_CASE : List[str] = remove_space SCREAMING_SNAKE_CASE : Dict = keep_accents SCREAMING_SNAKE_CASE : List[str] = vocab_file SCREAMING_SNAKE_CASE : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase__ ) # Used for whitespace normalization in input texts # fmt : off SCREAMING_SNAKE_CASE : Optional[Any] = {''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', '''''', '''„'''} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing SCREAMING_SNAKE_CASE : str = re.compile( f"""[{''.join(map(UpperCamelCase__ , list(range(0 , 9 ) ) + list(range(11 , 32 ) ) + list(range(127 , 160 ) ) + [160, 173, 8203] ) )}]""" ) def __getstate__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.__dict__.copy() SCREAMING_SNAKE_CASE : Optional[Any] = None return state def __setstate__( self : Optional[int] , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): SCREAMING_SNAKE_CASE : str = {} SCREAMING_SNAKE_CASE : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def __A ( self : Dict ): '''simple docstring''' return len(self.sp_model ) def __A ( self : Dict , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.non_printing_characters_re.sub('''''' , UpperCamelCase__ ) # Normalize whitespaces SCREAMING_SNAKE_CASE : Optional[int] = ''''''.join([char if char not in self.whitespaces else ''' ''' for char in text] ) # NFC Unicode normalization SCREAMING_SNAKE_CASE : Optional[int] = unicodedata.normalize('''NFC''' , UpperCamelCase__ ) return text def __A ( self : str , UpperCamelCase__ : str , **UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.preprocess_text(UpperCamelCase__ ) return self.sp_model.encode(UpperCamelCase__ , out_type=UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : str ): '''simple docstring''' return self.sp_model.PieceToId(UpperCamelCase__ ) def __A ( self : str , UpperCamelCase__ : int ): '''simple docstring''' return self.sp_model.IdToPiece(UpperCamelCase__ ) @staticmethod def __A ( UpperCamelCase__ : str ): '''simple docstring''' return out_string def __A ( self : Optional[int] , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [] SCREAMING_SNAKE_CASE : Optional[int] = '''''' SCREAMING_SNAKE_CASE : Union[str, Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCamelCase__ ) + token SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Union[str, Any] = [] else: current_sub_tokens.append(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = False out_string += self.sp_model.decode(UpperCamelCase__ ) return out_string def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = {self.convert_ids_to_tokens(UpperCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : 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__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCamelCase__ , '''wb''' ) as fi: SCREAMING_SNAKE_CASE : List[Any] = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (out_vocab_file,) def __A ( self : Union[str, Any] , UpperCamelCase__ : Union[str, List[str]] , UpperCamelCase__ : Union[str, bool] = False ): '''simple docstring''' if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[str] = self.preprocess_text(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.sp_model.encode(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Optional[int] = [self.preprocess_text(UpperCamelCase__ ) for t in text] SCREAMING_SNAKE_CASE : Union[str, Any] = self.sp_model.encode(UpperCamelCase__ ) if return_tensors is True or return_tensors == "pt": SCREAMING_SNAKE_CASE : int = torch.tensor(UpperCamelCase__ ) return token_ids def __A ( self : Any , UpperCamelCase__ : Union[int, List[int]] ): '''simple docstring''' return self.sp_model.decode(UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : "Conversation" ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [f"""User: {text}""" if is_user else f"""Bot: {text}""" for is_user, text in conversation.iter_texts()] SCREAMING_SNAKE_CASE : Dict = ( f"""{self.eos_token}{self.bos_token}""" + f"""{self.bos_token}""".join(UpperCamelCase__ ) + f"""{self.bos_token}Bot:""" ) return self.encode(text=UpperCamelCase__ )
34
import random def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowercase ) elif element > pivot: greater.append(_lowercase ) else: equal.append(_lowercase ) return less, equal, greater def A ( _lowercase , _lowercase ): # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(_lowercase ) or index < 0: return None SCREAMING_SNAKE_CASE : Dict = items[random.randint(0 , len(_lowercase ) - 1 )] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = _partition(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = len(_lowercase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowercase , _lowercase ) # must be in larger else: return quick_select(_lowercase , index - (m + count) )
34
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCamelCase : Any = {'configuration_focalnet': ['FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FocalNetConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FocalNetForImageClassification', 'FocalNetForMaskedImageModeling', 'FocalNetBackbone', 'FocalNetModel', 'FocalNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys __UpperCamelCase : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Tuple = logging.get_logger(__name__) # TODO Update this __UpperCamelCase : List[str] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """esm""" def __init__( self : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Any=768 , UpperCamelCase__ : Optional[Any]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : Optional[int]=3072 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1026 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Any=1E-12 , UpperCamelCase__ : Dict="absolute" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[int]=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Optional[Any]=None , **UpperCamelCase__ : Any , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : Dict = emb_layer_norm_before SCREAMING_SNAKE_CASE : List[str] = token_dropout SCREAMING_SNAKE_CASE : List[Any] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) SCREAMING_SNAKE_CASE : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = EsmFoldConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) SCREAMING_SNAKE_CASE : Optional[int] = get_default_vocab_list() else: SCREAMING_SNAKE_CASE : Optional[Any] = vocab_list else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = self.esmfold_config.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = None UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 0 UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Optional[int] ): '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE : Optional[Any] = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = TrunkConfig(**self.trunk ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = asdict(self ) SCREAMING_SNAKE_CASE : Tuple = self.trunk.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 48 UpperCamelCase_ = 1_024 UpperCamelCase_ = 128 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 0 UpperCamelCase_ = 0 UpperCamelCase_ = False UpperCamelCase_ = 4 UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Any ): '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE : Optional[int] = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) SCREAMING_SNAKE_CASE : Dict = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE : Tuple = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = asdict(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.structure_module.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 384 UpperCamelCase_ = 128 UpperCamelCase_ = 16 UpperCamelCase_ = 128 UpperCamelCase_ = 12 UpperCamelCase_ = 4 UpperCamelCase_ = 8 UpperCamelCase_ = 0.1 UpperCamelCase_ = 8 UpperCamelCase_ = 1 UpperCamelCase_ = 2 UpperCamelCase_ = 7 UpperCamelCase_ = 10 UpperCamelCase_ = 1E-8 UpperCamelCase_ = 1E5 def __A ( self : Dict ): '''simple docstring''' return asdict(self ) def A ( ): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
34
1
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __UpperCamelCase : Any = logging.get_logger(__name__) __UpperCamelCase : Dict = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} # See all BART models at https://huggingface.co/models?filter=bart __UpperCamelCase : Any = { 'vocab_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json', }, 'merges_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt', }, } __UpperCamelCase : List[str] = { 'facebook/bart-base': 1024, 'facebook/bart-large': 1024, 'facebook/bart-large-mnli': 1024, 'facebook/bart-large-cnn': 1024, 'facebook/bart-large-xsum': 1024, 'yjernite/bart_eli5': 1024, } @lru_cache() def A ( ): SCREAMING_SNAKE_CASE : Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) SCREAMING_SNAKE_CASE : Dict = bs[:] SCREAMING_SNAKE_CASE : str = 0 for b in range(2**8 ): if b not in bs: bs.append(_lowercase ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE : str = [chr(_lowercase ) for n in cs] return dict(zip(_lowercase , _lowercase ) ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = set() SCREAMING_SNAKE_CASE : Dict = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE : List[Any] = char return pairs class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] def __init__( self : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : Union[str, Any]="replace" , UpperCamelCase__ : Any="<s>" , UpperCamelCase__ : List[str]="</s>" , UpperCamelCase__ : Optional[Any]="</s>" , UpperCamelCase__ : Union[str, Any]="<s>" , UpperCamelCase__ : Dict="<unk>" , UpperCamelCase__ : Union[str, Any]="<pad>" , UpperCamelCase__ : List[str]="<mask>" , UpperCamelCase__ : int=False , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else bos_token SCREAMING_SNAKE_CASE : int = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else eos_token SCREAMING_SNAKE_CASE : Dict = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else sep_token SCREAMING_SNAKE_CASE : int = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else cls_token SCREAMING_SNAKE_CASE : List[Any] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else unk_token SCREAMING_SNAKE_CASE : int = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE : Union[str, Any] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else mask_token super().__init__( errors=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) with open(UpperCamelCase__ , encoding='''utf-8''' ) as vocab_handle: SCREAMING_SNAKE_CASE : Any = json.load(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE : Any = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE : List[str] = bytes_to_unicode() SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in self.byte_encoder.items()} with open(UpperCamelCase__ , encoding='''utf-8''' ) as merges_handle: SCREAMING_SNAKE_CASE : Optional[int] = merges_handle.read().split('''\n''' )[1:-1] SCREAMING_SNAKE_CASE : Optional[int] = [tuple(merge.split() ) for merge in bpe_merges] SCREAMING_SNAKE_CASE : Optional[Any] = dict(zip(UpperCamelCase__ , range(len(UpperCamelCase__ ) ) ) ) SCREAMING_SNAKE_CASE : Optional[Any] = {} SCREAMING_SNAKE_CASE : Optional[int] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE : Dict = re.compile(r'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def __A ( self : Dict ): '''simple docstring''' return len(self.encoder ) def __A ( self : str ): '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __A ( self : str , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE : str = tuple(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = get_pairs(UpperCamelCase__ ) if not pairs: return token while True: SCREAMING_SNAKE_CASE : Union[str, Any] = min(UpperCamelCase__ , key=lambda UpperCamelCase__ : self.bpe_ranks.get(UpperCamelCase__ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = bigram SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : Union[str, Any] = 0 while i < len(UpperCamelCase__ ): try: SCREAMING_SNAKE_CASE : Union[str, Any] = word.index(UpperCamelCase__ , UpperCamelCase__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE : int = j if word[i] == first and i < len(UpperCamelCase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE : Any = tuple(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = new_word if len(UpperCamelCase__ ) == 1: break else: SCREAMING_SNAKE_CASE : List[Any] = get_pairs(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = ''' '''.join(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = word return word def __A ( self : Tuple , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] for token in re.findall(self.pat , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[str] = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(UpperCamelCase__ ).split(''' ''' ) ) return bpe_tokens def __A ( self : List[Any] , UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' return self.encoder.get(UpperCamelCase__ , self.encoder.get(self.unk_token ) ) def __A ( self : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' return self.decoder.get(UpperCamelCase__ ) def __A ( self : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ''''''.join(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def __A ( self : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = os.path.join( UpperCamelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) SCREAMING_SNAKE_CASE : Dict = os.path.join( UpperCamelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=UpperCamelCase__ , ensure_ascii=UpperCamelCase__ ) + '''\n''' ) SCREAMING_SNAKE_CASE : List[Any] = 0 with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda UpperCamelCase__ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) SCREAMING_SNAKE_CASE : Optional[Any] = token_index writer.write(''' '''.join(UpperCamelCase__ ) + '''\n''' ) index += 1 return vocab_file, merge_file def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE : Union[str, Any] = [self.cls_token_id] SCREAMING_SNAKE_CASE : List[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A ( self : str , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None , UpperCamelCase__ : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__ , token_ids_a=UpperCamelCase__ , already_has_special_tokens=UpperCamelCase__ ) if token_ids_a is None: return [1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1, 1] + ([0] * len(UpperCamelCase__ )) + [1] def __A ( self : Optional[int] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [self.sep_token_id] SCREAMING_SNAKE_CASE : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __A ( self : str , UpperCamelCase__ : Any , UpperCamelCase__ : int=False , **UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(UpperCamelCase__ ) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE : Union[str, Any] = ''' ''' + text return (text, kwargs)
34
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowercase__ ( unittest.TestCase): def __A ( self : Any ): '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=UpperCamelCase__ , ) assert hasattr(self , '''env''' ) def __A ( self : str , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings SCREAMING_SNAKE_CASE : Any = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=UpperCamelCase__ , instance_count=UpperCamelCase__ , instance_type=self.instance_type , debugger_hook_config=UpperCamelCase__ , hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=UpperCamelCase__ , py_version='''py36''' , ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' TrainingJobAnalytics(UpperCamelCase__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def __A ( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.create_estimator(UpperCamelCase__ ) # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[Any] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : List[str] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : List[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_9999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , UpperCamelCase__ )
34
1
def A ( _lowercase ): SCREAMING_SNAKE_CASE : int = [0 for i in range(len(_lowercase ) )] # initialize interval's left pointer and right pointer SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = 0, 0 for i in range(1 , len(_lowercase ) ): # case when current index is inside the interval if i <= right_pointer: SCREAMING_SNAKE_CASE : Any = min(right_pointer - i + 1 , z_result[i - left_pointer] ) SCREAMING_SNAKE_CASE : Optional[int] = min_edge while go_next(_lowercase , _lowercase , _lowercase ): z_result[i] += 1 # if new index's result gives us more right interval, # we've to update left_pointer and right_pointer if i + z_result[i] - 1 > right_pointer: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = i, i + z_result[i] - 1 return z_result def A ( _lowercase , _lowercase , _lowercase ): return i + z_result[i] < len(_lowercase ) and s[z_result[i]] == s[i + z_result[i]] def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : List[Any] = 0 # concatenate 'pattern' and 'input_str' and call z_function # with concatenated string SCREAMING_SNAKE_CASE : Optional[int] = z_function(pattern + input_str ) for val in z_result: # if value is greater then length of the pattern string # that means this index is starting position of substring # which is equal to pattern string if val >= len(_lowercase ): answer += 1 return answer if __name__ == "__main__": import doctest doctest.testmod()
34
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore __UpperCamelCase : Dict = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" __UpperCamelCase : Tuple = [file for file in filepaths if file != file.lower()] if upper_files: print(f"""{len(upper_files)} files contain uppercase characters:""") print('\n'.join(upper_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if ' ' in file] if space_files: print(f"""{len(space_files)} files contain space characters:""") print('\n'.join(space_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if '-' in file] if hyphen_files: print(f"""{len(hyphen_files)} files contain hyphen characters:""") print('\n'.join(hyphen_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(f"""{len(nodir_files)} files are not in a directory:""") print('\n'.join(nodir_files) + '\n') __UpperCamelCase : Optional[Any] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
34
1
from scipy.stats import pearsonr import datasets __UpperCamelCase : Tuple = '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n' __UpperCamelCase : Any = '\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n [\'p-value\', \'pearsonr\']\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n >>> print(round(results[\'p-value\'], 2))\n 0.15\n' __UpperCamelCase : Tuple = '\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowercase__ ( datasets.Metric): def __A ( self : List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'''] , ) def __A ( self : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Dict=False ): '''simple docstring''' if return_pvalue: SCREAMING_SNAKE_CASE : List[Any] = pearsonr(UpperCamelCase__ , UpperCamelCase__ ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(UpperCamelCase__ , UpperCamelCase__ )[0] )}
34
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: __UpperCamelCase : Dict = None __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Optional[int] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} __UpperCamelCase : Optional[int] = { '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 __UpperCamelCase : Union[str, Any] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = TaTokenizer UpperCamelCase_ = [] def __init__( self : str , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[Any]=100 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[str] = [f"""<extra_id_{i}>""" for i in range(UpperCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE : int = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id_''' in str(UpperCamelCase__ ) ) , UpperCamelCase__ ) ) ) 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__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , extra_ids=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : str = vocab_file SCREAMING_SNAKE_CASE : int = False if not self.vocab_file else True SCREAMING_SNAKE_CASE : str = extra_ids @staticmethod def __A ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE : List[str] = 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.''' , UpperCamelCase__ , ) return max_model_length def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : 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(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = 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__ ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE : Tuple = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, 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 __A ( self : Dict ): '''simple docstring''' return list( set(filter(lambda UpperCamelCase__ : bool(re.search(r'''<extra_id_\d+>''' , UpperCamelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def __A ( self : List[Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(UpperCamelCase__ ) for token in self.get_sentinel_tokens()]
34
1
import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration __UpperCamelCase : Optional[int] = [ # tf -> hf ('/', '.'), ('layer_', 'layers.'), ('kernel', 'weight'), ('beta', 'bias'), ('gamma', 'weight'), ('pegasus', 'model'), ] __UpperCamelCase : int = [ ('.output.dense', '.fc2'), ('intermediate.LayerNorm', 'final_layer_norm'), ('intermediate.dense', 'fc1'), ] __UpperCamelCase : List[Any] = ( INIT_COMMON + [ ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.out_proj'), ('attention.self', 'self_attn'), ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'), ('attention.encdec_output.dense', 'encoder_attn.out_proj'), ('attention.encdec', 'encoder_attn'), ('key', 'k_proj'), ('value', 'v_proj'), ('query', 'q_proj'), ('decoder.LayerNorm', 'decoder.layernorm_embedding'), ] + END_COMMON ) __UpperCamelCase : str = ( INIT_COMMON + [ ('embeddings.word_embeddings', 'shared.weight'), ('embeddings.position_embeddings', 'embed_positions.weight'), ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.output'), ('attention.self', 'self_attn.self'), ('encoder.LayerNorm', 'encoder.layernorm_embedding'), ] + END_COMMON ) __UpperCamelCase : Optional[Any] = [ 'encdec/key/bias', 'encdec/query/bias', 'encdec/value/bias', 'self/key/bias', 'self/query/bias', 'self/value/bias', 'encdec_output/dense/bias', 'attention/output/dense/bias', ] def A ( _lowercase , _lowercase ): for tf_name, hf_name in patterns: SCREAMING_SNAKE_CASE : str = k.replace(_lowercase , _lowercase ) return k def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = BigBirdPegasusConfig(**_lowercase ) SCREAMING_SNAKE_CASE : Tuple = BigBirdPegasusForConditionalGeneration(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = torch_model.state_dict() SCREAMING_SNAKE_CASE : Any = {} # separating decoder weights SCREAMING_SNAKE_CASE : str = {k: tf_weights[k] for k in tf_weights if k.startswith('''pegasus/decoder''' )} SCREAMING_SNAKE_CASE : Dict = {k: tf_weights[k] for k in tf_weights if not k.startswith('''pegasus/decoder''' )} for k, v in tqdm(decoder_weights.items() , '''tf -> hf conversion''' ): SCREAMING_SNAKE_CASE : int = [k.endswith(_lowercase ) for ending in KEYS_TO_IGNORE] if any(_lowercase ): continue SCREAMING_SNAKE_CASE : Tuple = DECODER_PATTERNS SCREAMING_SNAKE_CASE : Tuple = rename_state_dict_key(_lowercase , _lowercase ) if new_k not in state_dict: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value'''] ): SCREAMING_SNAKE_CASE : Union[str, Any] = v.T SCREAMING_SNAKE_CASE : int = torch.from_numpy(_lowercase ) assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" for k, v in tqdm(remaining_weights.items() , '''tf -> hf conversion''' ): SCREAMING_SNAKE_CASE : str = [k.endswith(_lowercase ) for ending in KEYS_TO_IGNORE] if any(_lowercase ): continue SCREAMING_SNAKE_CASE : Optional[int] = REMAINING_PATTERNS SCREAMING_SNAKE_CASE : Dict = rename_state_dict_key(_lowercase , _lowercase ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value'''] ): SCREAMING_SNAKE_CASE : Optional[Any] = v.T SCREAMING_SNAKE_CASE : List[str] = torch.from_numpy(_lowercase ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" SCREAMING_SNAKE_CASE : Dict = mapping['''model.embed_positions.weight'''] SCREAMING_SNAKE_CASE : Dict = mapping.pop('''model.embed_positions.weight''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = torch_model.load_state_dict(_lowercase , strict=_lowercase ) SCREAMING_SNAKE_CASE : str = [ k for k in missing if k not in [ '''final_logits_bias''', '''model.encoder.embed_tokens.weight''', '''model.decoder.embed_tokens.weight''', '''lm_head.weight''', ] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def A ( _lowercase ): SCREAMING_SNAKE_CASE : List[str] = tf.train.list_variables(_lowercase ) SCREAMING_SNAKE_CASE : Any = {} SCREAMING_SNAKE_CASE : Optional[Any] = ['''global_step'''] for name, shape in tqdm(_lowercase , desc='''converting tf checkpoint to dict''' ): SCREAMING_SNAKE_CASE : List[Any] = any(pat in name for pat in ignore_name ) if skip_key: continue SCREAMING_SNAKE_CASE : List[Any] = tf.train.load_variable(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[str] = array return tf_weights def A ( _lowercase , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[Any] = get_tf_weights_as_numpy(_lowercase ) SCREAMING_SNAKE_CASE : List[str] = convert_bigbird_pegasus(_lowercase , _lowercase ) torch_model.save_pretrained(_lowercase ) if __name__ == "__main__": __UpperCamelCase : str = argparse.ArgumentParser() parser.add_argument('--tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('--save_dir', default=None, type=str, help='Path to the output PyTorch model.') __UpperCamelCase : str = parser.parse_args() __UpperCamelCase : List[Any] = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
34
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __UpperCamelCase : str = False class lowercase__ ( unittest.TestCase): pass @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Union[str, Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = VersatileDiffusionPipeline.from_pretrained(UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''cyberpunk 2077''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe.dual_guided( prompt=UpperCamelCase__ , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images SCREAMING_SNAKE_CASE : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : int = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe.text_to_image( prompt=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : List[str] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = pipe.image_variation(UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : Optional[int] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
34
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def A ( _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = analyze_text(_lowercase ) SCREAMING_SNAKE_CASE : Any = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Tuple = sum(single_char_strings.values() ) # one length string SCREAMING_SNAKE_CASE : Tuple = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: SCREAMING_SNAKE_CASE : Tuple = single_char_strings[ch] SCREAMING_SNAKE_CASE : List[str] = my_str / all_sum my_fir_sum += prob * math.loga(_lowercase ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string SCREAMING_SNAKE_CASE : Optional[Any] = sum(two_char_strings.values() ) SCREAMING_SNAKE_CASE : List[str] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : Any = two_char_strings[sequence] SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) / all_sum my_sec_sum += prob * math.loga(_lowercase ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = Counter() # type: ignore SCREAMING_SNAKE_CASE : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_lowercase ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def A ( ): import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
34
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : List[str] = { 'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json', 'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json', 'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json', 'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json', 'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json', 'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json', 'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json', 'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json', 'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """xmod""" def __init__( self : Optional[Any] , UpperCamelCase__ : List[Any]=3_0522 , UpperCamelCase__ : Dict=768 , UpperCamelCase__ : Tuple=12 , UpperCamelCase__ : List[Any]=12 , UpperCamelCase__ : Union[str, Any]=3072 , UpperCamelCase__ : Union[str, Any]="gelu" , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : List[str]=512 , UpperCamelCase__ : Tuple=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : List[Any]=1E-12 , UpperCamelCase__ : Optional[int]=1 , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : Any=2 , UpperCamelCase__ : int="absolute" , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : List[Any]=None , UpperCamelCase__ : Any=False , UpperCamelCase__ : Dict=2 , UpperCamelCase__ : Dict=False , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Optional[int]=("en_XX",) , UpperCamelCase__ : Tuple=None , **UpperCamelCase__ : List[Any] , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : str = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : int = hidden_act SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Optional[int] = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range SCREAMING_SNAKE_CASE : List[Any] = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = position_embedding_type SCREAMING_SNAKE_CASE : List[str] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = classifier_dropout SCREAMING_SNAKE_CASE : Tuple = pre_norm SCREAMING_SNAKE_CASE : List[Any] = adapter_reduction_factor SCREAMING_SNAKE_CASE : List[Any] = adapter_layer_norm SCREAMING_SNAKE_CASE : Tuple = adapter_reuse_layer_norm SCREAMING_SNAKE_CASE : Dict = ln_before_adapter SCREAMING_SNAKE_CASE : Union[str, Any] = list(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = default_language class lowercase__ ( UpperCamelCase_): @property def __A ( self : int ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Any = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCamelCase : Tuple = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Any = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
1
import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = np.inf def set_batch_size(_lowercase ) -> None: nonlocal batch_size if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = min(_lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Tuple = min(_lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_lowercase , _lowercase ) and feature.dtype == "binary": SCREAMING_SNAKE_CASE : Optional[Any] = min(_lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_lowercase , _lowercase ) return None if batch_size is np.inf else batch_size class lowercase__ ( UpperCamelCase_): def __init__( self : Tuple , UpperCamelCase__ : NestedDataStructureLike[PathLike] , UpperCamelCase__ : Optional[NamedSplit] = None , UpperCamelCase__ : Optional[Features] = None , UpperCamelCase__ : str = None , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional[int] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' super().__init__( UpperCamelCase__ , split=UpperCamelCase__ , features=UpperCamelCase__ , cache_dir=UpperCamelCase__ , keep_in_memory=UpperCamelCase__ , streaming=UpperCamelCase__ , num_proc=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Optional[int] = path_or_paths if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else {self.split: path_or_paths} SCREAMING_SNAKE_CASE : Union[str, Any] = _PACKAGED_DATASETS_MODULES['''parquet'''][1] SCREAMING_SNAKE_CASE : Optional[Any] = Parquet( cache_dir=UpperCamelCase__ , data_files=UpperCamelCase__ , features=UpperCamelCase__ , hash=UpperCamelCase__ , **UpperCamelCase__ , ) def __A ( self : str ): '''simple docstring''' if self.streaming: SCREAMING_SNAKE_CASE : int = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : Optional[Any] = None self.builder.download_and_prepare( download_config=UpperCamelCase__ , download_mode=UpperCamelCase__ , verification_mode=UpperCamelCase__ , base_path=UpperCamelCase__ , num_proc=self.num_proc , ) SCREAMING_SNAKE_CASE : str = self.builder.as_dataset( split=self.split , verification_mode=UpperCamelCase__ , in_memory=self.keep_in_memory ) return dataset class lowercase__ : def __init__( self : List[str] , UpperCamelCase__ : Dataset , UpperCamelCase__ : Union[PathLike, BinaryIO] , UpperCamelCase__ : Optional[int] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Any = path_or_buf SCREAMING_SNAKE_CASE : Any = batch_size or get_writer_batch_size(dataset.features ) SCREAMING_SNAKE_CASE : Tuple = parquet_writer_kwargs def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , '''wb+''' ) as buffer: SCREAMING_SNAKE_CASE : Optional[int] = self._write(file_obj=UpperCamelCase__ , batch_size=UpperCamelCase__ , **self.parquet_writer_kwargs ) else: SCREAMING_SNAKE_CASE : List[Any] = self._write(file_obj=self.path_or_buf , batch_size=UpperCamelCase__ , **self.parquet_writer_kwargs ) return written def __A ( self : Tuple , UpperCamelCase__ : BinaryIO , UpperCamelCase__ : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : str = parquet_writer_kwargs.pop('''path_or_buf''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.dataset.features.arrow_schema SCREAMING_SNAKE_CASE : List[Any] = pq.ParquetWriter(UpperCamelCase__ , schema=UpperCamelCase__ , **UpperCamelCase__ ) for offset in logging.tqdm( range(0 , len(self.dataset ) , UpperCamelCase__ ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): SCREAMING_SNAKE_CASE : List[Any] = query_table( table=self.dataset._data , key=slice(UpperCamelCase__ , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(UpperCamelCase__ ) written += batch.nbytes writer.close() return written
34
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
1
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __UpperCamelCase : Optional[Any] = ['text', 'image', 'audio'] def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = [] for input_type in input_types: if input_type == "text": inputs.append('''Text input''' ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3_000 ) ) elif isinstance(_lowercase , _lowercase ): inputs.append(create_inputs(_lowercase ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = [] for output in outputs: if isinstance(_lowercase , (str, AgentText) ): output_types.append('''text''' ) elif isinstance(_lowercase , (Image.Image, AgentImage) ): output_types.append('''image''' ) elif isinstance(_lowercase , (torch.Tensor, AgentAudio) ): output_types.append('''audio''' ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class lowercase__ : def __A ( self : Union[str, Any] ): '''simple docstring''' self.assertTrue(hasattr(self.tool , '''inputs''' ) ) self.assertTrue(hasattr(self.tool , '''outputs''' ) ) SCREAMING_SNAKE_CASE : List[str] = self.tool.inputs for _input in inputs: if isinstance(_input , UpperCamelCase__ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) SCREAMING_SNAKE_CASE : Dict = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.tool(*UpperCamelCase__ ) # There is a single output if len(self.tool.outputs ) == 1: SCREAMING_SNAKE_CASE : Optional[int] = [outputs] self.assertListEqual(output_types(UpperCamelCase__ ) , self.tool.outputs ) def __A ( self : Any ): '''simple docstring''' self.assertTrue(hasattr(self.tool , '''description''' ) ) self.assertTrue(hasattr(self.tool , '''default_checkpoint''' ) ) self.assertTrue(self.tool.description.startswith('''This is a tool that''' ) ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE : str = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [outputs] self.assertEqual(len(UpperCamelCase__ ) , len(self.tool.outputs ) ) for output, output_type in zip(UpperCamelCase__ , self.tool.outputs ): SCREAMING_SNAKE_CASE : Optional[int] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(UpperCamelCase__ , UpperCamelCase__ ) ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE : Dict = [] for _input, input_type in zip(UpperCamelCase__ , self.tool.inputs ): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error SCREAMING_SNAKE_CASE : List[Any] = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = [outputs] self.assertEqual(len(UpperCamelCase__ ) , len(self.tool.outputs ) )
34
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __UpperCamelCase : Dict = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None ): SCREAMING_SNAKE_CASE : Union[str, Any] = True while ask_again: SCREAMING_SNAKE_CASE : Optional[Any] = input(_lowercase ) try: if default is not None and len(_lowercase ) == 0: return default return convert_value(_lowercase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowercase ) def A ( _lowercase , _lowercase=[] , _lowercase=None , _lowercase=0 ): SCREAMING_SNAKE_CASE : Dict = BulletMenu(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : str = menu.run(default_choice=_lowercase ) return convert_value(_lowercase ) if convert_value is not None else result def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = int(_lowercase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A ( _lowercase ): return {"yes": True, "no": False}[value.lower()] class lowercase__ ( argparse.RawDescriptionHelpFormatter): def __A ( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = super()._format_usage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
34
1
import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC __UpperCamelCase : Optional[Any] = parse(importlib.metadata.version('torch')) def A ( _lowercase , _lowercase , _lowercase ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(f"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) SCREAMING_SNAKE_CASE : List[Any] = STR_OPERATION_TO_FUNC[operation] if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Optional[int] = parse(importlib.metadata.version(_lowercase ) ) return operation(_lowercase , parse(_lowercase ) ) def A ( _lowercase , _lowercase ): return compare_versions(_lowercase , _lowercase , _lowercase )
34
from __future__ import annotations from typing import Any class lowercase__ ( UpperCamelCase_): pass class lowercase__ : def __init__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = data SCREAMING_SNAKE_CASE : Node | None = None def __iter__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self SCREAMING_SNAKE_CASE : Tuple = [] while node: if node in visited: raise ContainsLoopError visited.append(UpperCamelCase__ ) yield node.data SCREAMING_SNAKE_CASE : Dict = node.next_node @property def __A ( self : Optional[int] ): '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": __UpperCamelCase : List[Any] = Node(1) __UpperCamelCase : str = Node(2) __UpperCamelCase : Dict = Node(3) __UpperCamelCase : List[Any] = Node(4) print(root_node.has_loop) # False __UpperCamelCase : int = root_node.next_node print(root_node.has_loop) # True __UpperCamelCase : Union[str, Any] = Node(5) __UpperCamelCase : Union[str, Any] = Node(6) __UpperCamelCase : List[Any] = Node(5) __UpperCamelCase : List[str] = Node(6) print(root_node.has_loop) # False __UpperCamelCase : List[Any] = Node(1) print(root_node.has_loop) # False
34
1
import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class lowercase__ ( _lowerCamelCase): UpperCamelCase_ = 42 UpperCamelCase_ = None def A ( _lowercase , _lowercase=0.999 , _lowercase="cosine" , ): if alpha_transform_type == "cosine": def alpha_bar_fn(_lowercase ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(_lowercase ): return math.exp(t * -12.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) SCREAMING_SNAKE_CASE : int = [] for i in range(SCREAMING_SNAKE_CASE_ ): SCREAMING_SNAKE_CASE : str = i / num_diffusion_timesteps SCREAMING_SNAKE_CASE : List[Any] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) return torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa ) class lowercase__ ( _lowerCamelCase , _lowerCamelCase): UpperCamelCase_ = 1 @register_to_config def __init__( self : Dict , UpperCamelCase__ : str = 1000 , UpperCamelCase__ : List[Any] = 0.0001 , UpperCamelCase__ : Dict = 0.02 , UpperCamelCase__ : Union[str, Any] = "linear" , UpperCamelCase__ : int = None , UpperCamelCase__ : Optional[int] = True , UpperCamelCase__ : List[Any] = True , UpperCamelCase__ : Tuple = 0 , UpperCamelCase__ : List[Any] = "epsilon" , UpperCamelCase__ : int = 1.0 , **UpperCamelCase__ : Tuple , ): '''simple docstring''' if kwargs.get('''set_alpha_to_one''' , A__ ) is not None: SCREAMING_SNAKE_CASE : Any = ( '''The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.''' ) deprecate('''set_alpha_to_one''' , '''1.0.0''' , A__ , standard_warn=A__ ) SCREAMING_SNAKE_CASE : Any = kwargs['''set_alpha_to_one'''] if trained_betas is not None: SCREAMING_SNAKE_CASE : List[Any] = torch.tensor(A__ , dtype=torch.floataa ) elif beta_schedule == "linear": SCREAMING_SNAKE_CASE : Tuple = torch.linspace(A__ , A__ , A__ , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. SCREAMING_SNAKE_CASE : str = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , A__ , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule SCREAMING_SNAKE_CASE : Union[str, Any] = betas_for_alpha_bar(A__ ) else: raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""" ) SCREAMING_SNAKE_CASE : Dict = 1.0 - self.betas SCREAMING_SNAKE_CASE : Dict = torch.cumprod(self.alphas , dim=0 ) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. SCREAMING_SNAKE_CASE : List[str] = torch.tensor(0.0 ) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : Any = 1.0 # setable values SCREAMING_SNAKE_CASE : Optional[Any] = None SCREAMING_SNAKE_CASE : Optional[Any] = torch.from_numpy(np.arange(0 , A__ ).copy().astype(np.intaa ) ) def __A ( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] = None ): '''simple docstring''' return sample def __A ( self : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] = None ): '''simple docstring''' if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"""`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:""" f""" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle""" f""" maximal {self.config.num_train_timesteps} timesteps.""" ) SCREAMING_SNAKE_CASE : Optional[Any] = num_inference_steps SCREAMING_SNAKE_CASE : List[str] = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 SCREAMING_SNAKE_CASE : Dict = (np.arange(0 , A__ ) * step_ratio).round().copy().astype(np.intaa ) SCREAMING_SNAKE_CASE : List[Any] = torch.from_numpy(A__ ).to(A__ ) self.timesteps += self.config.steps_offset def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] = 0.0 , UpperCamelCase__ : Union[str, Any] = False , UpperCamelCase__ : Dict = None , UpperCamelCase__ : List[Any] = True , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process SCREAMING_SNAKE_CASE : List[Any] = self.alphas_cumprod[timestep] SCREAMING_SNAKE_CASE : Tuple = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) SCREAMING_SNAKE_CASE : str = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": SCREAMING_SNAKE_CASE : Dict = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 SCREAMING_SNAKE_CASE : Any = model_output elif self.config.prediction_type == "sample": SCREAMING_SNAKE_CASE : List[str] = model_output SCREAMING_SNAKE_CASE : Dict = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": SCREAMING_SNAKE_CASE : Tuple = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output SCREAMING_SNAKE_CASE : Optional[Any] = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or""" ''' `v_prediction`''' ) # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: SCREAMING_SNAKE_CASE : Any = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf SCREAMING_SNAKE_CASE : List[str] = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf SCREAMING_SNAKE_CASE : Optional[int] = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=A__ , pred_original_sample=A__ ) def __len__( self : List[str] ): '''simple docstring''' return self.config.num_train_timesteps
700
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""input_features""", """is_longer"""] def __init__( self : Optional[Any] , UpperCamelCase__ : Dict=64 , UpperCamelCase__ : Optional[Any]=4_8000 , UpperCamelCase__ : Tuple=480 , UpperCamelCase__ : Union[str, Any]=10 , UpperCamelCase__ : List[Any]=1024 , UpperCamelCase__ : Tuple=0.0 , UpperCamelCase__ : int=False , UpperCamelCase__ : float = 0 , UpperCamelCase__ : float = 1_4000 , UpperCamelCase__ : int = None , UpperCamelCase__ : str = "fusion" , UpperCamelCase__ : str = "repeatpad" , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' super().__init__( feature_size=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , padding_value=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = top_db SCREAMING_SNAKE_CASE : Union[str, Any] = truncation SCREAMING_SNAKE_CASE : str = padding SCREAMING_SNAKE_CASE : List[Any] = fft_window_size SCREAMING_SNAKE_CASE : Tuple = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE : List[str] = hop_length SCREAMING_SNAKE_CASE : List[Any] = max_length_s SCREAMING_SNAKE_CASE : Tuple = max_length_s * sampling_rate SCREAMING_SNAKE_CASE : List[Any] = sampling_rate SCREAMING_SNAKE_CASE : List[str] = frequency_min SCREAMING_SNAKE_CASE : Any = frequency_max SCREAMING_SNAKE_CASE : List[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm=UpperCamelCase__ , mel_scale='''htk''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=UpperCamelCase__ , min_frequency=UpperCamelCase__ , max_frequency=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , norm='''slaney''' , mel_scale='''slaney''' , ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE : Optional[Any] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __A ( self : Optional[int] , UpperCamelCase__ : np.array , UpperCamelCase__ : Optional[np.array] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = spectrogram( UpperCamelCase__ , window_function(self.fft_window_size , '''hann''' ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=UpperCamelCase__ , log_mel='''dB''' , ) return log_mel_spectrogram.T def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : int = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE : Any = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE : List[Any] = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE : int = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE : Optional[int] = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE : Optional[Any] = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE : Tuple = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE : str = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE : Optional[int] = torch.nn.functional.interpolate( UpperCamelCase__ , size=[chunk_frames, 64] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __A ( self : Dict , UpperCamelCase__ : np.array , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] ): '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE : Optional[Any] = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) - max_length SCREAMING_SNAKE_CASE : Dict = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE : Any = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE : List[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE : Optional[Any] = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE : Tuple = False else: SCREAMING_SNAKE_CASE : str = self._random_mel_fusion(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = True else: raise NotImplementedError(f"""data_truncating {truncation} not implemented""" ) else: SCREAMING_SNAKE_CASE : List[str] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE : Tuple = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Any = np.stack(np.tile(UpperCamelCase__ , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE : List[Any] = int(max_length / len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.stack(np.tile(UpperCamelCase__ , UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Dict = np.pad(UpperCamelCase__ , (0, max_length - waveform.shape[0]) , mode='''constant''' , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE : List[Any] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE : List[str] = self._np_extract_fbank_features(UpperCamelCase__ , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , UpperCamelCase__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , UpperCamelCase__ : str = None , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , **UpperCamelCase__ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE : List[str] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" f""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE : List[str] = isinstance(UpperCamelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) SCREAMING_SNAKE_CASE : int = is_batched_numpy or ( isinstance(UpperCamelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE : Any = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase__ , np.ndarray ): SCREAMING_SNAKE_CASE : List[Any] = np.asarray(UpperCamelCase__ , dtype=np.floataa ) elif isinstance(UpperCamelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE : List[str] = [np.asarray(UpperCamelCase__ )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE : int = [ self._get_input_mel(UpperCamelCase__ , max_length if max_length else self.nb_max_samples , UpperCamelCase__ , UpperCamelCase__ ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : List[str] = [] for mel, longer in padded_inputs: input_mel.append(UpperCamelCase__ ) is_longer.append(UpperCamelCase__ ) if truncation == "fusion" and sum(UpperCamelCase__ ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE : Union[str, Any] = np.random.randint(0 , len(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = True if isinstance(input_mel[0] , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE : Optional[Any] = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_features''': input_mel, '''is_longer''': is_longer} SCREAMING_SNAKE_CASE : int = BatchFeature(UpperCamelCase__ ) if return_tensors is not None: SCREAMING_SNAKE_CASE : int = input_features.convert_to_tensors(UpperCamelCase__ ) return input_features
34
0
from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse('3.8'): import importlib_metadata else: import importlib.metadata as importlib_metadata __UpperCamelCase : List[Any] = '' if version.parse(importlib_metadata.version('jiwer')) < version.parse('2.3.0'): class lowercase__ ( tr.AbstractTransform): def __init__( self : Union[str, Any] , UpperCamelCase__ : str = " " ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = sentence_delimiter def __A ( self : List[str] , UpperCamelCase__ : str ): '''simple docstring''' return list(lowerCAmelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [] for sent_idx, sentence in enumerate(lowerCAmelCase__ ): chars.extend(self.process_string(lowerCAmelCase__ ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(lowerCAmelCase__ ) - 1: chars.append(self.sentence_delimiter ) return chars __UpperCamelCase : Optional[Any] = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: __UpperCamelCase : Union[str, Any] = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) __UpperCamelCase : Dict = '\\n@inproceedings{inproceedings,\n author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n year = {2004},\n month = {01},\n pages = {},\n title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n' __UpperCamelCase : Union[str, Any] = '\\nCharacter error rate (CER) is a common metric of the performance of an automatic speech recognition system.\n\nCER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.\n\nCharacter error rate can be computed as:\n\nCER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct characters,\nN is the number of characters in the reference (N=S+D+C).\n\nCER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the\nperformance of the ASR system with a CER of 0 being a perfect score.\n' __UpperCamelCase : Tuple = '\nComputes CER score of transcribed segments against references.\nArgs:\n references: list of references for each speech input.\n predictions: list of transcribtions to score.\n concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.\nReturns:\n (float): the character error rate\n\nExamples:\n\n >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n >>> references = [\"this is the reference\", \"there is another one\"]\n >>> cer = datasets.load_metric(\"cer\")\n >>> cer_score = cer.compute(predictions=predictions, references=references)\n >>> print(cer_score)\n 0.34146341463414637\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowercase__ ( datasets.Metric): def __A ( self : List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/jitsi/jiwer/'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/Word_error_rate''', '''https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates''', ] , ) def __A ( self : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict=False ): '''simple docstring''' if concatenate_texts: return jiwer.compute_measures( lowerCAmelCase__ , lowerCAmelCase__ , truth_transform=lowerCAmelCase__ , hypothesis_transform=lowerCAmelCase__ , )["wer"] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : List[str] = 0 for prediction, reference in zip(lowerCAmelCase__ , lowerCAmelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = jiwer.compute_measures( lowerCAmelCase__ , lowerCAmelCase__ , truth_transform=lowerCAmelCase__ , hypothesis_transform=lowerCAmelCase__ , ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
701
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __UpperCamelCase : str = logging.get_logger(__name__) __UpperCamelCase : str = { 'microsoft/layoutlmv3-base': 'https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json', } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """layoutlmv3""" def __init__( self : Optional[int] , UpperCamelCase__ : Union[str, Any]=5_0265 , UpperCamelCase__ : List[Any]=768 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Union[str, Any]=12 , UpperCamelCase__ : Tuple=3072 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Any=512 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : List[Any]=0.02 , UpperCamelCase__ : Optional[Any]=1E-5 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : int=0 , UpperCamelCase__ : str=2 , UpperCamelCase__ : List[str]=1024 , UpperCamelCase__ : str=128 , UpperCamelCase__ : str=128 , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Optional[int]=32 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Optional[Any]=64 , UpperCamelCase__ : Dict=256 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Dict=224 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : Any=16 , UpperCamelCase__ : Any=None , **UpperCamelCase__ : Optional[Any] , ): '''simple docstring''' super().__init__( vocab_size=UpperCamelCase__ , hidden_size=UpperCamelCase__ , num_hidden_layers=UpperCamelCase__ , num_attention_heads=UpperCamelCase__ , intermediate_size=UpperCamelCase__ , hidden_act=UpperCamelCase__ , hidden_dropout_prob=UpperCamelCase__ , attention_probs_dropout_prob=UpperCamelCase__ , max_position_embeddings=UpperCamelCase__ , type_vocab_size=UpperCamelCase__ , initializer_range=UpperCamelCase__ , layer_norm_eps=UpperCamelCase__ , pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : List[str] = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : str = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : Union[str, Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Union[str, Any] = max_rel_ad_pos SCREAMING_SNAKE_CASE : Union[str, Any] = text_embed SCREAMING_SNAKE_CASE : List[str] = visual_embed SCREAMING_SNAKE_CASE : Optional[Any] = input_size SCREAMING_SNAKE_CASE : Optional[Any] = num_channels SCREAMING_SNAKE_CASE : List[Any] = patch_size SCREAMING_SNAKE_CASE : List[Any] = classifier_dropout class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.12""") @property def __A ( self : str ): '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __A ( self : int ): '''simple docstring''' return 1E-5 @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Optional[Any] , UpperCamelCase__ : "ProcessorMixin" , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , ): '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , UpperCamelCase__ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : List[Any] = processor.tokenizer.num_special_tokens_to_add(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = compute_effective_axis_dimension( UpperCamelCase__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=UpperCamelCase__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : Any = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = dict( processor( UpperCamelCase__ , text=UpperCamelCase__ , boxes=UpperCamelCase__ , return_tensors=UpperCamelCase__ , ) ) return inputs
34
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCamelCase : Any = { 'configuration_instructblip': [ 'INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'InstructBlipConfig', 'InstructBlipQFormerConfig', 'InstructBlipVisionConfig', ], 'processing_instructblip': ['InstructBlipProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : str = [ 'INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'InstructBlipQFormerModel', 'InstructBlipPreTrainedModel', 'InstructBlipForConditionalGeneration', 'InstructBlipVisionModel', ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys __UpperCamelCase : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
702
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 lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = FunnelTokenizer UpperCamelCase_ = FunnelTokenizerFast UpperCamelCase_ = True UpperCamelCase_ = True def __A ( self : Union[str, Any] ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[Any] = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : List[str] = 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 __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : int , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : int = '''unwanted, running''' return input_text, output_text def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCamelCase__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [7, 4, 5, 10, 8, 9] ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_tokenizers(do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE : int = tokenizer('''UNwant\u00E9d,running''' ) SCREAMING_SNAKE_CASE : Optional[Any] = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
34
0
from manim import * class lowercase__ ( _SCREAMING_SNAKE_CASE): def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = Rectangle(height=0.5 , width=0.5 ) SCREAMING_SNAKE_CASE : List[Any] = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE : List[Any] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE : Union[str, Any] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE : str = VGroup(*A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : List[Any] = VGroup(*A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : Dict = VGroup(A_ , A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : Dict = Text('''CPU''' , font_size=24 ) SCREAMING_SNAKE_CASE : Union[str, Any] = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(A_ ) SCREAMING_SNAKE_CASE : Dict = [mem.copy() for i in range(4 )] SCREAMING_SNAKE_CASE : Union[str, Any] = VGroup(*A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : Tuple = Text('''GPU''' , font_size=24 ) SCREAMING_SNAKE_CASE : List[str] = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) gpu.move_to([-1, -1, 0] ) self.add(A_ ) SCREAMING_SNAKE_CASE : Dict = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE : Any = VGroup(*A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : Dict = Text('''Model''' , font_size=24 ) SCREAMING_SNAKE_CASE : List[Any] = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) model.move_to([3, -1.0, 0] ) self.add(A_ ) SCREAMING_SNAKE_CASE : int = [] for i, rect in enumerate(A_ ): rect.set_stroke(A_ ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) SCREAMING_SNAKE_CASE : List[Any] = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(A_ , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=A_ ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=A_ , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=A_ , buff=0.0 ) self.add(A_ ) cpu_targs.append(A_ ) SCREAMING_SNAKE_CASE : List[Any] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE : Optional[int] = VGroup(*A_ ).arrange(A_ , buff=0 ) SCREAMING_SNAKE_CASE : List[str] = Text('''Loaded Checkpoint''' , font_size=24 ) SCREAMING_SNAKE_CASE : Optional[int] = Group(A_ , A_ ).arrange(A_ , aligned_edge=A_ , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) SCREAMING_SNAKE_CASE : Optional[int] = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE : str = MarkupText( f"""<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(A_ , A_ ) SCREAMING_SNAKE_CASE : List[Any] = MarkupText( f"""<span fgcolor=\'{BLUE}\'>●</span> Checkpoint""" , font_size=18 , ) blue_text.next_to(A_ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) SCREAMING_SNAKE_CASE : Tuple = MarkupText( f"""Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>.""" , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(A_ ) , Write(A_ ) ) self.play(Write(A_ , run_time=1 ) , Create(A_ , run_time=1 ) ) SCREAMING_SNAKE_CASE : List[str] = [] SCREAMING_SNAKE_CASE : Any = [] for i, rect in enumerate(A_ ): SCREAMING_SNAKE_CASE : str = fill.copy().set_fill(A_ , opacity=0.7 ) target.move_to(A_ ) first_animations.append(GrowFromCenter(A_ , run_time=1 ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(A_ , run_time=1.5 ) ) self.play(*A_ ) self.play(*A_ ) self.wait()
703
import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = dataset SCREAMING_SNAKE_CASE : Optional[Any] = process SCREAMING_SNAKE_CASE : Union[str, Any] = params def __len__( self : Tuple ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.dataset[i] SCREAMING_SNAKE_CASE : Optional[int] = self.process(UpperCamelCase__ , **self.params ) return processed class lowercase__ ( UpperCamelCase_): def __init__( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = loader SCREAMING_SNAKE_CASE : List[Any] = infer SCREAMING_SNAKE_CASE : int = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : List[str] = loader_batch_size # Internal bookkeeping SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None def __len__( self : int ): '''simple docstring''' return len(self.loader ) def __iter__( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice SCREAMING_SNAKE_CASE : Optional[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) SCREAMING_SNAKE_CASE : Union[str, Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Convert ModelOutput to tuple first SCREAMING_SNAKE_CASE : Dict = element.to_tuple() if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCamelCase__ , UpperCamelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): SCREAMING_SNAKE_CASE : List[str] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around SCREAMING_SNAKE_CASE : int = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Union[str, Any] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers SCREAMING_SNAKE_CASE : Tuple = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. SCREAMING_SNAKE_CASE : Tuple = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 SCREAMING_SNAKE_CASE : Any = self._loader_batch_data.__class__(UpperCamelCase__ ) self._loader_batch_index += 1 return result def __A ( self : Union[str, Any] ): '''simple docstring''' if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch SCREAMING_SNAKE_CASE : Tuple = next(self.iterator ) SCREAMING_SNAKE_CASE : List[Any] = self.infer(UpperCamelCase__ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Optional[int] = processed else: SCREAMING_SNAKE_CASE : int = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : int = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[Any] = observed_batch_size # Setting internal index to unwrap the batch SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : int = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class lowercase__ ( UpperCamelCase_): def __init__( self : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=None ): '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __iter__( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = iter(self.loader ) SCREAMING_SNAKE_CASE : List[Any] = None return self def __A ( self : List[str] ): '''simple docstring''' if self.subiterator is None: SCREAMING_SNAKE_CASE : Dict = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item SCREAMING_SNAKE_CASE : Any = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators SCREAMING_SNAKE_CASE : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = next(self.subiterator ) return processed class lowercase__ ( UpperCamelCase_): def __iter__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = iter(self.loader ) return self def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Optional[int] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Tuple = self.loader_batch_item() SCREAMING_SNAKE_CASE : Any = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator while not is_last: SCREAMING_SNAKE_CASE : Any = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCamelCase__ , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = processed else: SCREAMING_SNAKE_CASE : Union[str, Any] = list(processed.keys() )[0] SCREAMING_SNAKE_CASE : List[str] = processed[key] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = len(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. SCREAMING_SNAKE_CASE : List[str] = observed_batch_size SCREAMING_SNAKE_CASE : List[Any] = processed SCREAMING_SNAKE_CASE : str = 0 while self._loader_batch_index < self.loader_batch_size: SCREAMING_SNAKE_CASE : Any = self.loader_batch_item() SCREAMING_SNAKE_CASE : List[Any] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) if is_last: return accumulator else: SCREAMING_SNAKE_CASE : int = processed SCREAMING_SNAKE_CASE : List[str] = item.pop('''is_last''' ) accumulator.append(UpperCamelCase__ ) return accumulator class lowercase__ ( UpperCamelCase_): def __init__( self : Optional[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dataset SCREAMING_SNAKE_CASE : Dict = key def __len__( self : Optional[int] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' return self.dataset[i][self.key] class lowercase__ ( UpperCamelCase_): def __init__( self : List[Any] , UpperCamelCase__ : Dataset , UpperCamelCase__ : str , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : List[str] = keya SCREAMING_SNAKE_CASE : Tuple = keya def __len__( self : List[str] ): '''simple docstring''' return len(self.dataset ) def __getitem__( self : Union[str, Any] , UpperCamelCase__ : Any ): '''simple docstring''' return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
34
0
import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowercase__ : def __init__( self : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[str]=99 , UpperCamelCase__ : List[str]=13 , UpperCamelCase__ : int=16 , UpperCamelCase__ : str=7 , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Union[str, Any]=False , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=2 , UpperCamelCase__ : Dict=32 , UpperCamelCase__ : str=4 , UpperCamelCase__ : Dict=4 , UpperCamelCase__ : str=30 , UpperCamelCase__ : Optional[int]=0 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : Tuple=2 , UpperCamelCase__ : Optional[Any]=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = parent SCREAMING_SNAKE_CASE : Tuple = batch_size SCREAMING_SNAKE_CASE : Dict = decoder_seq_length # For common tests SCREAMING_SNAKE_CASE : Optional[Any] = self.decoder_seq_length SCREAMING_SNAKE_CASE : str = is_training SCREAMING_SNAKE_CASE : Optional[Any] = use_attention_mask SCREAMING_SNAKE_CASE : int = use_labels SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : str = d_model SCREAMING_SNAKE_CASE : Any = d_model SCREAMING_SNAKE_CASE : Dict = decoder_layers SCREAMING_SNAKE_CASE : Dict = decoder_layers SCREAMING_SNAKE_CASE : Optional[int] = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : str = eos_token_id SCREAMING_SNAKE_CASE : Dict = bos_token_id SCREAMING_SNAKE_CASE : Dict = pad_token_id SCREAMING_SNAKE_CASE : Tuple = decoder_start_token_id SCREAMING_SNAKE_CASE : Optional[Any] = use_cache SCREAMING_SNAKE_CASE : int = max_position_embeddings SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : str = decoder_seq_length SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : str = 1 def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_attention_mask: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_labels: SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def __A ( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : Union[str, Any] = TrOCRDecoder(config=UpperCamelCase_ ).to(UpperCamelCase_ ).eval() SCREAMING_SNAKE_CASE : int = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase_ , use_cache=UpperCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = model(UpperCamelCase_ , use_cache=UpperCamelCase_ ) self.parent.assertTrue(len(UpperCamelCase_ ) == len(UpperCamelCase_ ) ) self.parent.assertTrue(len(UpperCamelCase_ ) == len(UpperCamelCase_ ) + 1 ) SCREAMING_SNAKE_CASE : List[Any] = outputs['past_key_values'] # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE : int = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and SCREAMING_SNAKE_CASE : Dict = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE : int = model(UpperCamelCase_ )['last_hidden_state'] SCREAMING_SNAKE_CASE : Dict = model(UpperCamelCase_ , past_key_values=UpperCamelCase_ )['last_hidden_state'] # select random slice SCREAMING_SNAKE_CASE : List[str] = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE : Union[str, Any] = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() SCREAMING_SNAKE_CASE : Optional[int] = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(UpperCamelCase_ , UpperCamelCase_ , atol=1E-3 ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'input_ids': input_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_torch class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () UpperCamelCase_ = (TrOCRForCausalLM,) if is_torch_available() else () UpperCamelCase_ = {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {} UpperCamelCase_ = True UpperCamelCase_ = False def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = TrOCRStandaloneDecoderModelTester(self , is_training=UpperCamelCase_ ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=UpperCamelCase_ ) def __A ( self : Optional[int] ): '''simple docstring''' pass def __A ( self : Tuple ): '''simple docstring''' pass def __A ( self : List[Any] ): '''simple docstring''' pass def __A ( self : Union[str, Any] ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_ ) def __A ( self : Union[str, Any] ): '''simple docstring''' return @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __A ( self : int ): '''simple docstring''' pass
704
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """deberta-v2""" def __init__( self : Optional[Any] , UpperCamelCase__ : Any=12_8100 , UpperCamelCase__ : Optional[int]=1536 , UpperCamelCase__ : Dict=24 , UpperCamelCase__ : List[str]=24 , UpperCamelCase__ : Tuple=6144 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Optional[int]=0.1 , UpperCamelCase__ : List[str]=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : Optional[Any]=0 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : List[Any]=1E-7 , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=-1 , UpperCamelCase__ : List[Any]=0 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : str="gelu" , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention SCREAMING_SNAKE_CASE : Optional[Any] = max_relative_positions SCREAMING_SNAKE_CASE : Optional[int] = pad_token_id SCREAMING_SNAKE_CASE : Optional[Any] = position_biased_input # Backwards compatibility if type(UpperCamelCase__ ) == str: SCREAMING_SNAKE_CASE : Optional[int] = [x.strip() for x in pos_att_type.lower().split('''|''' )] SCREAMING_SNAKE_CASE : Any = pos_att_type SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = kwargs.get('''pooler_hidden_size''' , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pooler_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = pooler_hidden_act class lowercase__ ( UpperCamelCase_): @property def __A ( self : Union[str, Any] ): '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 12 def __A ( self : Dict , UpperCamelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : int = -1 , UpperCamelCase__ : bool = False , UpperCamelCase__ : Optional["TensorType"] = None , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : int = 40 , UpperCamelCase__ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().generate_dummy_inputs(preprocessor=UpperCamelCase__ , framework=UpperCamelCase__ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
34
0
import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( lowercase__ , lowercase__ , unittest.TestCase): UpperCamelCase_ = IFInpaintingSuperResolutionPipeline UpperCamelCase_ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} UpperCamelCase_ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"""original_image"""}) UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Optional[int] ): '''simple docstring''' return self._get_superresolution_dummy_components() def __A ( self : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any]=0 ): '''simple docstring''' if str(__lowerCamelCase ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : str = torch.manual_seed(__lowerCamelCase ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=__lowerCamelCase ).manual_seed(__lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 16, 16) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE : Any = { "prompt": "A painting of a squirrel eating a burger", "image": image, "original_image": original_image, "mask_image": mask_image, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Optional[int] ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __A ( self : Dict ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Optional[int] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : int ): '''simple docstring''' self._test_save_load_local() def __A ( self : Optional[int] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
705
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() __UpperCamelCase : str = logging.get_logger(__name__) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Any = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(_lowercase , _lowercase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(_lowercase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : str = '''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" SCREAMING_SNAKE_CASE : Optional[int] = BitConfig( conv_layer=_lowercase , num_labels=1_000 , idalabel=_lowercase , labelaid=_lowercase , ) return config def A ( _lowercase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''stem.conv''' , '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''blocks''' , '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''head.fc''' , '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE : str = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''bit.encoder.''' + name return name def A ( ): SCREAMING_SNAKE_CASE : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ) return im @torch.no_grad() def A ( _lowercase , _lowercase , _lowercase=False ): SCREAMING_SNAKE_CASE : List[Any] = get_config(_lowercase ) # load original model from timm SCREAMING_SNAKE_CASE : Optional[Any] = create_model(_lowercase , pretrained=_lowercase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE : Optional[int] = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Dict = state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE : str = BitForImageClassification(_lowercase ) model.eval() model.load_state_dict(_lowercase ) # create image processor SCREAMING_SNAKE_CASE : Optional[Any] = create_transform(**resolve_data_config({} , model=_lowercase ) ) SCREAMING_SNAKE_CASE : List[str] = transform.transforms SCREAMING_SNAKE_CASE : Union[str, Any] = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE : Tuple = BitImageProcessor( do_resize=_lowercase , size={'''shortest_edge''': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_lowercase , crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} , do_normalize=_lowercase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Union[str, Any] = transform(_lowercase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE : Optional[int] = processor(_lowercase , return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(_lowercase , _lowercase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = outputs.logits print('''Logits:''' , logits[0, :3] ) print('''Predicted class:''' , model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE : List[Any] = timm_model(_lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowercase , outputs.logits , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_lowercase ).mkdir(exist_ok=_lowercase ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowercase ) processor.save_pretrained(_lowercase ) 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__": __UpperCamelCase : 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.', ) __UpperCamelCase : Optional[int] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
0
import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 __UpperCamelCase : List[Any] = data_utils.TransfoXLTokenizer __UpperCamelCase : Dict = data_utils.TransfoXLCorpus __UpperCamelCase : Optional[int] = data_utils __UpperCamelCase : int = data_utils def A ( _lowercase , _lowercase , _lowercase , _lowercase ): if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(lowerCamelCase_ , '''rb''' ) as fp: SCREAMING_SNAKE_CASE : List[str] = pickle.load(lowerCamelCase_ , encoding='''latin1''' ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) SCREAMING_SNAKE_CASE : str = pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['pretrained_vocab_file'] print(f"""Save vocabulary to {pytorch_vocab_dump_path}""" ) SCREAMING_SNAKE_CASE : Any = corpus.vocab.__dict__ torch.save(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = corpus.__dict__ corpus_dict_no_vocab.pop('''vocab''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = pytorch_dump_folder_path + '/' + CORPUS_NAME print(f"""Save dataset to {pytorch_dataset_dump_path}""" ) torch.save(lowerCamelCase_ , lowerCamelCase_ ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model SCREAMING_SNAKE_CASE : Optional[int] = os.path.abspath(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.abspath(lowerCamelCase_ ) print(f"""Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.""" ) # Initialise PyTorch model if transfo_xl_config_file == "": SCREAMING_SNAKE_CASE : List[Any] = TransfoXLConfig() else: SCREAMING_SNAKE_CASE : str = TransfoXLConfig.from_json_file(lowerCamelCase_ ) print(f"""Building PyTorch model from configuration: {config}""" ) SCREAMING_SNAKE_CASE : Optional[int] = TransfoXLLMHeadModel(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = load_tf_weights_in_transfo_xl(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Save pytorch-model SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) print(f"""Save PyTorch model to {os.path.abspath(lowerCamelCase_ )}""" ) torch.save(model.state_dict() , lowerCamelCase_ ) print(f"""Save configuration file to {os.path.abspath(lowerCamelCase_ )}""" ) with open(lowerCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __UpperCamelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the folder to store the PyTorch model or dataset/vocab.', ) parser.add_argument( '--tf_checkpoint_path', default='', type=str, help='An optional path to a TensorFlow checkpoint path to be converted.', ) parser.add_argument( '--transfo_xl_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--transfo_xl_dataset_file', default='', type=str, help='An optional dataset file to be converted in a vocabulary.', ) __UpperCamelCase : Union[str, Any] = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
706
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow __UpperCamelCase : str = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ 'text-classification', 'language-modeling', 'summarization', 'token-classification', 'question-answering', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) __UpperCamelCase : int = logging.getLogger() def A ( ): SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() parser.add_argument('''-f''' ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() return args.f def A ( _lowercase , _lowercase="eval" ): SCREAMING_SNAKE_CASE : Dict = os.path.join(_lowercase , f"""{split}_results.json""" ) if os.path.exists(_lowercase ): with open(_lowercase , '''r''' ) as f: return json.load(_lowercase ) raise ValueError(f"""can't find {path}""" ) __UpperCamelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase__ ( UpperCamelCase_): def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Tuple = f""" run_glue.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 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_glue.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : str = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_clm_flax.main() SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_summarization_flax.main() SCREAMING_SNAKE_CASE : Union[str, Any] = get_results(UpperCamelCase__ , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f""" run_mlm.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} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_mlm_flax.main() SCREAMING_SNAKE_CASE : List[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_ta_mlm_flax.main() SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Any = f""" run_flax_ner.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} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_flax_ner.main() SCREAMING_SNAKE_CASE : List[str] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Union[str, Any] = f""" run_qa.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} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(UpperCamelCase__ , '''argv''' , UpperCamelCase__ ): run_qa.main() SCREAMING_SNAKE_CASE : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
34
0
import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class lowercase__ ( unittest.TestCase): def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowercase ) ) def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowercase ) ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_lowercase ) ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowercase ) ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', # Removed: 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_lowercase ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE : Union[str, Any] = 'fp16' self.assertTrue(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [ 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE : Dict = 'fp16' self.assertTrue(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] SCREAMING_SNAKE_CASE : Dict = 'fp16' self.assertTrue(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE : Dict = 'fp16' self.assertFalse(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [ 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', ] SCREAMING_SNAKE_CASE : str = 'fp16' self.assertTrue(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] SCREAMING_SNAKE_CASE : Dict = 'fp16' self.assertTrue(is_safetensors_compatible(_lowercase , variant=_lowercase ) ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', # 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE : Dict = 'fp16' self.assertFalse(is_safetensors_compatible(_lowercase , variant=_lowercase ) )
707
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset __UpperCamelCase : Dict = random.Random() def A ( _lowercase , _lowercase=1.0 , _lowercase=None , _lowercase=None ): if rng is None: SCREAMING_SNAKE_CASE : Any = global_rng SCREAMING_SNAKE_CASE : int = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase__ ( unittest.TestCase): def __init__( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : str=7 , UpperCamelCase__ : Any=400 , UpperCamelCase__ : List[str]=2000 , UpperCamelCase__ : List[Any]=2048 , UpperCamelCase__ : Any=128 , UpperCamelCase__ : Union[str, Any]=1 , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : str=30 , UpperCamelCase__ : Tuple=4_4100 , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : str = batch_size SCREAMING_SNAKE_CASE : str = min_seq_length SCREAMING_SNAKE_CASE : Dict = max_seq_length SCREAMING_SNAKE_CASE : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE : Optional[Any] = spectrogram_length SCREAMING_SNAKE_CASE : Optional[int] = feature_size SCREAMING_SNAKE_CASE : Tuple = num_audio_channels SCREAMING_SNAKE_CASE : Union[str, Any] = hop_length SCREAMING_SNAKE_CASE : List[Any] = chunk_length SCREAMING_SNAKE_CASE : str = sampling_rate def __A ( self : Optional[Any] ): '''simple docstring''' return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def __A ( self : Tuple , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Optional[int]=False ): '''simple docstring''' def _flatten(UpperCamelCase__ : str ): return list(itertools.chain(*UpperCamelCase__ ) ) if equal_length: SCREAMING_SNAKE_CASE : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE : int = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE : Optional[Any] = [np.asarray(UpperCamelCase__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase__ ( UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = TvltFeatureExtractor def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = TvltFeatureExtractionTester(self ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''spectrogram_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''feature_size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''num_audio_channels''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''hop_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''chunk_length''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''sampling_rate''' ) ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : Optional[int] = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Optional[int] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Tuple = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = feat_extract_first.to_dict() SCREAMING_SNAKE_CASE : str = feat_extract_second.to_dict() SCREAMING_SNAKE_CASE : int = dict_first.pop('''mel_filters''' ) SCREAMING_SNAKE_CASE : Any = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] SCREAMING_SNAKE_CASE : Optional[int] = [np.asarray(UpperCamelCase__ ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched SCREAMING_SNAKE_CASE : Optional[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking SCREAMING_SNAKE_CASE : List[str] = feature_extractor( UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 , mask_audio=UpperCamelCase__ ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE : Optional[Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE : Dict = np.asarray(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = feature_extractor(UpperCamelCase__ , return_tensors='''np''' , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def __A ( self : Optional[int] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE : Dict = ds.sort('''id''' ).select(range(UpperCamelCase__ ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE : int = TvltFeatureExtractor() SCREAMING_SNAKE_CASE : List[Any] = feature_extractor(UpperCamelCase__ , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) SCREAMING_SNAKE_CASE : str = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , UpperCamelCase__ , atol=1E-4 ) )
34
0
from __future__ import annotations import unittest from transformers import DistilBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.distilbert.modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertModel, ) class lowercase__ : def __init__( self : Tuple , UpperCamelCase__ : List[str] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = parent SCREAMING_SNAKE_CASE : Any = 13 SCREAMING_SNAKE_CASE : Any = 7 SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : Tuple = False SCREAMING_SNAKE_CASE : Optional[Any] = True SCREAMING_SNAKE_CASE : Dict = 99 SCREAMING_SNAKE_CASE : Union[str, Any] = 32 SCREAMING_SNAKE_CASE : Union[str, Any] = 2 SCREAMING_SNAKE_CASE : Union[str, Any] = 4 SCREAMING_SNAKE_CASE : List[str] = 37 SCREAMING_SNAKE_CASE : str = '''gelu''' SCREAMING_SNAKE_CASE : Dict = 0.1 SCREAMING_SNAKE_CASE : List[Any] = 0.1 SCREAMING_SNAKE_CASE : List[str] = 512 SCREAMING_SNAKE_CASE : Optional[int] = 16 SCREAMING_SNAKE_CASE : Optional[Any] = 2 SCREAMING_SNAKE_CASE : List[str] = 0.02 SCREAMING_SNAKE_CASE : Any = 3 SCREAMING_SNAKE_CASE : Optional[Any] = 4 SCREAMING_SNAKE_CASE : int = None def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Any = None if self.use_input_mask: SCREAMING_SNAKE_CASE : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Union[str, Any] = None SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : str = None if self.use_labels: SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : Dict = DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = TFDistilBertModel(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = {'''input_ids''': input_ids, '''attention_mask''': input_mask} SCREAMING_SNAKE_CASE : List[str] = model(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = [input_ids, input_mask] SCREAMING_SNAKE_CASE : Tuple = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = TFDistilBertForMaskedLM(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = {'''input_ids''': input_ids, '''attention_mask''': input_mask} SCREAMING_SNAKE_CASE : Union[str, Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : List[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = TFDistilBertForQuestionAnswering(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, } SCREAMING_SNAKE_CASE : List[str] = model(UpperCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self : str , UpperCamelCase__ : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.num_labels SCREAMING_SNAKE_CASE : Optional[Any] = TFDistilBertForSequenceClassification(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = {'''input_ids''': input_ids, '''attention_mask''': input_mask} SCREAMING_SNAKE_CASE : Union[str, Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self : int , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : Any , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[int] = TFDistilBertForMultipleChoice(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, } SCREAMING_SNAKE_CASE : Tuple = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self : List[Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.num_labels SCREAMING_SNAKE_CASE : Tuple = TFDistilBertForTokenClassification(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} SCREAMING_SNAKE_CASE : str = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.prepare_config_and_inputs() (SCREAMING_SNAKE_CASE) : Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE : Tuple = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowercase__ ( __A , __A , unittest.TestCase): UpperCamelCase_ = ( ( TFDistilBertModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertForMultipleChoice, ) if is_tf_available() else None ) UpperCamelCase_ = ( { """feature-extraction""": TFDistilBertModel, """fill-mask""": TFDistilBertForMaskedLM, """question-answering""": TFDistilBertForQuestionAnswering, """text-classification""": TFDistilBertForSequenceClassification, """token-classification""": TFDistilBertForTokenClassification, """zero-shot""": TFDistilBertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase_ = False UpperCamelCase_ = False def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = TFDistilBertModelTester(self ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=UpperCamelCase__ , dim=37 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__ ) def __A ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__ ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__ ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__ ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__ ) @slow def __A ( self : List[Any] ): '''simple docstring''' for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ): SCREAMING_SNAKE_CASE : Union[str, Any] = TFDistilBertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_tf class lowercase__ ( unittest.TestCase): @slow def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFDistilBertModel.from_pretrained('''distilbert-base-uncased''' ) SCREAMING_SNAKE_CASE : Tuple = tf.constant([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE : List[Any] = model(UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : str = [1, 6, 768] self.assertEqual(output.shape , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = tf.constant( [ [ [0.1926_1885, -0.1373_2955, 0.411_9799], [0.2215_0156, -0.0742_2661, 0.3903_7204], [0.2275_6018, -0.089_6414, 0.370_1467], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , UpperCamelCase__ , atol=1E-4 )
708
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowercase__ ( UpperCamelCase_ , UpperCamelCase_): UpperCamelCase_ = 1 @register_to_config def __init__( self : List[str] , UpperCamelCase__ : int = 1000 , UpperCamelCase__ : Optional[Union[np.ndarray, List[float]]] = None ): '''simple docstring''' self.set_timesteps(UpperCamelCase__ ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : str = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. SCREAMING_SNAKE_CASE : Tuple = 4 # running values SCREAMING_SNAKE_CASE : int = [] def __A ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, torch.device] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = num_inference_steps SCREAMING_SNAKE_CASE : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] SCREAMING_SNAKE_CASE : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: SCREAMING_SNAKE_CASE : int = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE : Optional[Any] = torch.sin(steps * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE : Dict = (1.0 - self.betas**2) ** 0.5 SCREAMING_SNAKE_CASE : Optional[Any] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] SCREAMING_SNAKE_CASE : List[str] = timesteps.to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = [] def __A ( self : Tuple , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : int , UpperCamelCase__ : torch.FloatTensor , UpperCamelCase__ : bool = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) SCREAMING_SNAKE_CASE : Optional[int] = (self.timesteps == timestep).nonzero().item() SCREAMING_SNAKE_CASE : Union[str, Any] = timestep_index + 1 SCREAMING_SNAKE_CASE : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCamelCase__ ) if len(self.ets ) == 1: SCREAMING_SNAKE_CASE : Dict = self.ets[-1] elif len(self.ets ) == 2: SCREAMING_SNAKE_CASE : Optional[int] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: SCREAMING_SNAKE_CASE : str = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: SCREAMING_SNAKE_CASE : Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) SCREAMING_SNAKE_CASE : Optional[int] = self._get_prev_sample(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : torch.FloatTensor , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return sample def __A ( self : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.alphas[timestep_index] SCREAMING_SNAKE_CASE : List[str] = self.betas[timestep_index] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas[prev_timestep_index] SCREAMING_SNAKE_CASE : Tuple = self.betas[prev_timestep_index] SCREAMING_SNAKE_CASE : Dict = (sample - sigma * ets) / max(UpperCamelCase__ , 1E-8 ) SCREAMING_SNAKE_CASE : Optional[Any] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : int ): '''simple docstring''' return self.config.num_train_timesteps
34
0
from ....utils import logging __UpperCamelCase : List[str] = logging.get_logger(__name__) class lowercase__ ( UpperCamelCase_): def __init__( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : List[str]=2048 ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = config.__dict__ SCREAMING_SNAKE_CASE : Optional[int] = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE : int = num_labels
709
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class lowercase__ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): UpperCamelCase_ = IFPipeline UpperCamelCase_ = TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} UpperCamelCase_ = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ = PipelineTesterMixin.required_optional_params - {"""latents"""} def __A ( self : Tuple ): '''simple docstring''' return self._get_dummy_components() def __A ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : int=0 ): '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def __A ( self : List[str] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __A ( self : Any ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __A ( self : Union[str, Any] ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __A ( self : List[Any] ): '''simple docstring''' self._test_save_load_local() def __A ( self : List[str] ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __A ( self : Tuple ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : str = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : str = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE : Tuple = IFInpaintingPipeline(**pipe_a.components ) SCREAMING_SNAKE_CASE : Optional[int] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : List[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , num_inference_steps=2 , generator=UpperCamelCase__ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = pipe_a( prompt_embeds=UpperCamelCase__ , negative_prompt_embeds=UpperCamelCase__ , image=UpperCamelCase__ , mask_image=UpperCamelCase__ , original_image=UpperCamelCase__ , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(UpperCamelCase__ , UpperCamelCase__ ) def A ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
34
0
import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py __UpperCamelCase : str = "src/diffusers" __UpperCamelCase : Dict = "." # This is to make sure the diffusers module imported is the one in the repo. __UpperCamelCase : Any = importlib.util.spec_from_file_location( 'diffusers', os.path.join(DIFFUSERS_PATH, '__init__.py'), submodule_search_locations=[DIFFUSERS_PATH], ) __UpperCamelCase : Optional[Any] = spec.loader.load_module() def A ( _lowercase , _lowercase ) -> Union[str, Any]: return line.startswith(lowerCamelCase__ ) or len(lowerCamelCase__ ) <= 1 or re.search(R'''^\s*\)(\s*->.*:|:)\s*$''' , lowerCamelCase__ ) is not None def A ( _lowercase ) -> str: SCREAMING_SNAKE_CASE : Any = object_name.split('''.''' ) SCREAMING_SNAKE_CASE : List[Any] = 0 # First let's find the module where our object lives. SCREAMING_SNAKE_CASE : List[Any] = parts[i] while i < len(lowerCamelCase__ ) and not os.path.isfile(os.path.join(lowerCamelCase__ , f"""{module}.py""" ) ): i += 1 if i < len(lowerCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(lowerCamelCase__ , parts[i] ) if i >= len(lowerCamelCase__ ): raise ValueError(f"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(lowerCamelCase__ , f"""{module}.py""" ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: SCREAMING_SNAKE_CASE : List[str] = f.readlines() # Now let's find the class / func in the code! SCREAMING_SNAKE_CASE : Optional[Any] = "" SCREAMING_SNAKE_CASE : Optional[Any] = 0 for name in parts[i + 1 :]: while ( line_index < len(lowerCamelCase__ ) and re.search(Rf"""^{indent}(class|def)\s+{name}(\(|\:)""" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(lowerCamelCase__ ): raise ValueError(f""" {object_name} does not match any function or class in {module}.""" ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). SCREAMING_SNAKE_CASE : Any = line_index while line_index < len(lowerCamelCase__ ) and _should_continue(lines[line_index] , lowerCamelCase__ ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 SCREAMING_SNAKE_CASE : Any = lines[start_index:line_index] return "".join(lowerCamelCase__ ) __UpperCamelCase : List[Any] = re.compile(R'^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)') __UpperCamelCase : Tuple = re.compile(R'^\s*(\S+)->(\S+)(\s+.*|$)') __UpperCamelCase : Tuple = re.compile(R'<FILL\s+[^>]*>') def A ( _lowercase ) -> Dict: SCREAMING_SNAKE_CASE : Optional[int] = code.split('''\n''' ) SCREAMING_SNAKE_CASE : Any = 0 while idx < len(lowerCamelCase__ ) and len(lines[idx] ) == 0: idx += 1 if idx < len(lowerCamelCase__ ): return re.search(R'''^(\s*)\S''' , lines[idx] ).groups()[0] return "" def A ( _lowercase ) -> Optional[Any]: SCREAMING_SNAKE_CASE : Dict = len(get_indent(lowerCamelCase__ ) ) > 0 if has_indent: SCREAMING_SNAKE_CASE : Tuple = f"""class Bla:\n{code}""" SCREAMING_SNAKE_CASE : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = black.format_str(lowerCamelCase__ , mode=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = style_docstrings_in_code(lowerCamelCase__ ) return result[len('''class Bla:\n''' ) :] if has_indent else result def A ( _lowercase , _lowercase=False ) -> Any: with open(lowerCamelCase__ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: SCREAMING_SNAKE_CASE : Optional[Any] = f.readlines() SCREAMING_SNAKE_CASE : List[str] = [] SCREAMING_SNAKE_CASE : Dict = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(lowerCamelCase__ ): SCREAMING_SNAKE_CASE : Dict = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. SCREAMING_SNAKE_CASE : Any = search.groups() SCREAMING_SNAKE_CASE : List[Any] = find_code_in_diffusers(lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = get_indent(lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Tuple = line_index + 1 if indent == theoretical_indent else line_index + 2 SCREAMING_SNAKE_CASE : str = theoretical_indent SCREAMING_SNAKE_CASE : Any = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. SCREAMING_SNAKE_CASE : Any = True while line_index < len(lowerCamelCase__ ) and should_continue: line_index += 1 if line_index >= len(lowerCamelCase__ ): break SCREAMING_SNAKE_CASE : Optional[int] = lines[line_index] SCREAMING_SNAKE_CASE : int = _should_continue(lowerCamelCase__ , lowerCamelCase__ ) and re.search(f"""^{indent}# End copy""" , lowerCamelCase__ ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 SCREAMING_SNAKE_CASE : Dict = lines[start_index:line_index] SCREAMING_SNAKE_CASE : List[Any] = "".join(lowerCamelCase__ ) # Remove any nested `Copied from` comments to avoid circular copies SCREAMING_SNAKE_CASE : str = [line for line in theoretical_code.split('''\n''' ) if _re_copy_warning.search(lowerCamelCase__ ) is None] SCREAMING_SNAKE_CASE : int = "\n".join(lowerCamelCase__ ) # Before comparing, use the `replace_pattern` on the original code. if len(lowerCamelCase__ ) > 0: SCREAMING_SNAKE_CASE : Tuple = replace_pattern.replace('''with''' , '''''' ).split(''',''' ) SCREAMING_SNAKE_CASE : int = [_re_replace_pattern.search(lowerCamelCase__ ) for p in patterns] for pattern in patterns: if pattern is None: continue SCREAMING_SNAKE_CASE : Dict = pattern.groups() SCREAMING_SNAKE_CASE : Union[str, Any] = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) if option.strip() == "all-casing": SCREAMING_SNAKE_CASE : Optional[int] = re.sub(obja.lower() , obja.lower() , lowerCamelCase__ ) SCREAMING_SNAKE_CASE : str = re.sub(obja.upper() , obja.upper() , lowerCamelCase__ ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line SCREAMING_SNAKE_CASE : List[Any] = blackify(lines[start_index - 1] + theoretical_code ) SCREAMING_SNAKE_CASE : Any = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: SCREAMING_SNAKE_CASE : List[Any] = lines[:start_index] + [theoretical_code] + lines[line_index:] SCREAMING_SNAKE_CASE : Optional[Any] = start_index + 1 if overwrite and len(lowerCamelCase__ ) > 0: # Warn the user a file has been modified. print(f"""Detected changes, rewriting {filename}.""" ) with open(lowerCamelCase__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(lowerCamelCase__ ) return diffs def A ( _lowercase = False ) -> Optional[int]: SCREAMING_SNAKE_CASE : Optional[int] = glob.glob(os.path.join(lowerCamelCase__ , '''**/*.py''' ) , recursive=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = [] for filename in all_files: SCREAMING_SNAKE_CASE : Tuple = is_copy_consistent(lowerCamelCase__ , lowerCamelCase__ ) diffs += [f"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(lowerCamelCase__ ) > 0: SCREAMING_SNAKE_CASE : Dict = "\n".join(lowerCamelCase__ ) raise Exception( '''Found the following copy inconsistencies:\n''' + diff + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.''' ) if __name__ == "__main__": __UpperCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') __UpperCamelCase : Any = parser.parse_args() check_copies(args.fix_and_overwrite)
710
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __UpperCamelCase : int = logging.get_logger(__name__) def A ( _lowercase , _lowercase , _lowercase , _lowercase ): def constraint_to_multiple_of(_lowercase , _lowercase , _lowercase=0 , _lowercase=None ): SCREAMING_SNAKE_CASE : int = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE : Dict = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE : Optional[Any] = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE : Optional[Any] = (output_size, output_size) if isinstance(_lowercase , _lowercase ) else output_size SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = get_image_size(_lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = output_size # determine new height and width SCREAMING_SNAKE_CASE : Dict = output_height / input_height SCREAMING_SNAKE_CASE : Optional[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE : List[Any] = scale_width else: # fit height SCREAMING_SNAKE_CASE : List[Any] = scale_height SCREAMING_SNAKE_CASE : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = constraint_to_multiple_of(scale_width * input_width , multiple=_lowercase ) return (new_height, new_width) class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = ["""pixel_values"""] def __init__( self : int , UpperCamelCase__ : bool = True , UpperCamelCase__ : Dict[str, int] = None , UpperCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[int, float] = 1 / 255 , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , UpperCamelCase__ : Optional[Union[float, List[float]]] = None , **UpperCamelCase__ : Optional[int] , ): '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 384, '''width''': 384} SCREAMING_SNAKE_CASE : Any = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : str = keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of SCREAMING_SNAKE_CASE : int = resample SCREAMING_SNAKE_CASE : Any = do_rescale SCREAMING_SNAKE_CASE : List[Any] = rescale_factor SCREAMING_SNAKE_CASE : Optional[int] = do_normalize SCREAMING_SNAKE_CASE : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : Optional[Any] , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Dict[str, int] , UpperCamelCase__ : bool = False , UpperCamelCase__ : int = 1 , UpperCamelCase__ : PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE : Any = get_resize_output_image_size( UpperCamelCase__ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=UpperCamelCase__ , multiple=UpperCamelCase__ , ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Dict , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[int, float] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : str , ): '''simple docstring''' return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : np.ndarray , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Union[float, List[float]] , UpperCamelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase__ : List[str] , ): '''simple docstring''' return normalize(UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __A ( self : Optional[Any] , UpperCamelCase__ : ImageInput , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : int = None , UpperCamelCase__ : PILImageResampling = None , UpperCamelCase__ : bool = None , UpperCamelCase__ : float = 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__ : Optional[int] , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : str = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : List[Any] = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Dict = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None 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.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Tuple = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE : Dict = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE : Any = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE : Any = [self.normalize(image=UpperCamelCase__ , mean=UpperCamelCase__ , std=UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] SCREAMING_SNAKE_CASE : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __A ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Tuple] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): SCREAMING_SNAKE_CASE : List[Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE : Optional[int] = [] for idx in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: SCREAMING_SNAKE_CASE : List[Any] = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE : List[Any] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
34
0
import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCamelCase : int = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { 'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json', # See all DETR models at https://huggingface.co/models?filter=detr } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """detr""" UpperCamelCase_ = ["""past_key_values"""] UpperCamelCase_ = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : Optional[int] , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : List[Any]=None , UpperCamelCase__ : List[str]=3 , UpperCamelCase__ : Any=100 , UpperCamelCase__ : Union[str, Any]=6 , UpperCamelCase__ : Union[str, Any]=2048 , UpperCamelCase__ : str=8 , UpperCamelCase__ : Dict=6 , UpperCamelCase__ : Tuple=2048 , UpperCamelCase__ : Optional[int]=8 , UpperCamelCase__ : int=0.0 , UpperCamelCase__ : Union[str, Any]=0.0 , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Union[str, Any]="relu" , UpperCamelCase__ : str=256 , UpperCamelCase__ : int=0.1 , UpperCamelCase__ : List[str]=0.0 , UpperCamelCase__ : Union[str, Any]=0.0 , UpperCamelCase__ : int=0.02 , UpperCamelCase__ : Union[str, Any]=1.0 , UpperCamelCase__ : str=False , UpperCamelCase__ : int="sine" , UpperCamelCase__ : List[str]="resnet50" , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : Dict=1 , UpperCamelCase__ : Optional[Any]=5 , UpperCamelCase__ : Optional[int]=2 , UpperCamelCase__ : List[str]=1 , UpperCamelCase__ : int=1 , UpperCamelCase__ : str=5 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : Union[str, Any]=0.1 , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) SCREAMING_SNAKE_CASE : int = CONFIG_MAPPING["resnet"](out_features=['''stage4'''] ) elif isinstance(__A , __A ): SCREAMING_SNAKE_CASE : Union[str, Any] = backbone_config.get('''model_type''' ) SCREAMING_SNAKE_CASE : Optional[Any] = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE : Optional[int] = config_class.from_dict(__A ) # set timm attributes to None SCREAMING_SNAKE_CASE : List[Any] = None, None, None SCREAMING_SNAKE_CASE : Tuple = use_timm_backbone SCREAMING_SNAKE_CASE : Dict = backbone_config SCREAMING_SNAKE_CASE : List[Any] = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = num_queries SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Any = encoder_ffn_dim SCREAMING_SNAKE_CASE : Optional[Any] = encoder_layers SCREAMING_SNAKE_CASE : int = encoder_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : Dict = decoder_layers SCREAMING_SNAKE_CASE : Dict = decoder_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = dropout SCREAMING_SNAKE_CASE : Tuple = attention_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = activation_dropout SCREAMING_SNAKE_CASE : List[Any] = activation_function SCREAMING_SNAKE_CASE : str = init_std SCREAMING_SNAKE_CASE : Dict = init_xavier_std SCREAMING_SNAKE_CASE : Tuple = encoder_layerdrop SCREAMING_SNAKE_CASE : List[str] = decoder_layerdrop SCREAMING_SNAKE_CASE : Optional[int] = encoder_layers SCREAMING_SNAKE_CASE : List[str] = auxiliary_loss SCREAMING_SNAKE_CASE : Optional[int] = position_embedding_type SCREAMING_SNAKE_CASE : Union[str, Any] = backbone SCREAMING_SNAKE_CASE : str = use_pretrained_backbone SCREAMING_SNAKE_CASE : Union[str, Any] = dilation # Hungarian matcher SCREAMING_SNAKE_CASE : Any = class_cost SCREAMING_SNAKE_CASE : int = bbox_cost SCREAMING_SNAKE_CASE : Tuple = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE : Union[str, Any] = mask_loss_coefficient SCREAMING_SNAKE_CASE : Tuple = dice_loss_coefficient SCREAMING_SNAKE_CASE : str = bbox_loss_coefficient SCREAMING_SNAKE_CASE : Dict = giou_loss_coefficient SCREAMING_SNAKE_CASE : Any = eos_coefficient super().__init__(is_encoder_decoder=__A , **__A ) @property def __A ( self : Tuple ): '''simple docstring''' return self.encoder_attention_heads @property def __A ( self : str ): '''simple docstring''' return self.d_model @classmethod def __A ( cls : str , UpperCamelCase__ : PretrainedConfig , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return cls(backbone_config=__A , **__A ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: SCREAMING_SNAKE_CASE : List[Any] = self.backbone_config.to_dict() SCREAMING_SNAKE_CASE : List[str] = self.__class__.model_type return output class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = version.parse("""1.11""") @property def __A ( self : Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def __A ( self : Union[str, Any] ): '''simple docstring''' return 1E-5 @property def __A ( self : Optional[int] ): '''simple docstring''' return 12
711
import random def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowercase ) elif element > pivot: greater.append(_lowercase ) else: equal.append(_lowercase ) return less, equal, greater def A ( _lowercase , _lowercase ): # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(_lowercase ) or index < 0: return None SCREAMING_SNAKE_CASE : Dict = items[random.randint(0 , len(_lowercase ) - 1 )] SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = _partition(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = len(_lowercase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowercase , _lowercase ) # must be in larger else: return quick_select(_lowercase , index - (m + count) )
34
0
import math from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Any = logging.get_logger(__name__) __UpperCamelCase : str = { "facebook/data2vec-base-960h": "https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """data2vec-audio""" def __init__( self : Optional[Any] , UpperCamelCase__ : List[Any]=32 , UpperCamelCase__ : List[Any]=768 , UpperCamelCase__ : List[Any]=12 , UpperCamelCase__ : int=12 , UpperCamelCase__ : List[str]=3072 , UpperCamelCase__ : Any="gelu" , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : int=0.1 , UpperCamelCase__ : Optional[int]=0.0 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[str]=0.02 , UpperCamelCase__ : Tuple=1E-5 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Tuple=(512, 512, 512, 512, 512, 512, 512) , UpperCamelCase__ : Tuple=(5, 2, 2, 2, 2, 2, 2) , UpperCamelCase__ : Dict=(10, 3, 3, 3, 3, 2, 2) , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : List[str]=16 , UpperCamelCase__ : int=19 , UpperCamelCase__ : Optional[Any]=5 , UpperCamelCase__ : int=0.05 , UpperCamelCase__ : str=10 , UpperCamelCase__ : Optional[Any]=2 , UpperCamelCase__ : List[str]=0.0 , UpperCamelCase__ : int=10 , UpperCamelCase__ : str=0 , UpperCamelCase__ : Union[str, Any]="sum" , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=256 , UpperCamelCase__ : Union[str, Any]=(512, 512, 512, 512, 1500) , UpperCamelCase__ : Union[str, Any]=(5, 3, 3, 1, 1) , UpperCamelCase__ : Optional[Any]=(1, 2, 3, 1, 1) , UpperCamelCase__ : List[Any]=512 , UpperCamelCase__ : Dict=0 , UpperCamelCase__ : Any=1 , UpperCamelCase__ : Union[str, Any]=2 , UpperCamelCase__ : int=False , UpperCamelCase__ : List[Any]=3 , UpperCamelCase__ : List[Any]=2 , UpperCamelCase__ : Optional[Any]=3 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : int , ): '''simple docstring''' super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Any = feat_extract_activation SCREAMING_SNAKE_CASE : Dict = list(_a ) SCREAMING_SNAKE_CASE : int = list(_a ) SCREAMING_SNAKE_CASE : str = list(_a ) SCREAMING_SNAKE_CASE : str = conv_bias SCREAMING_SNAKE_CASE : Optional[int] = num_conv_pos_embeddings SCREAMING_SNAKE_CASE : List[str] = num_conv_pos_embedding_groups SCREAMING_SNAKE_CASE : List[str] = conv_pos_kernel_size SCREAMING_SNAKE_CASE : Tuple = len(self.conv_dim ) SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Tuple = hidden_dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = activation_dropout SCREAMING_SNAKE_CASE : int = feat_proj_dropout SCREAMING_SNAKE_CASE : List[Any] = final_dropout SCREAMING_SNAKE_CASE : Tuple = layerdrop SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Tuple = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 SCREAMING_SNAKE_CASE : int = mask_time_prob SCREAMING_SNAKE_CASE : Any = mask_time_length SCREAMING_SNAKE_CASE : List[str] = mask_time_min_masks SCREAMING_SNAKE_CASE : List[Any] = mask_feature_prob SCREAMING_SNAKE_CASE : Tuple = mask_feature_length SCREAMING_SNAKE_CASE : Union[str, Any] = mask_feature_min_masks # ctc loss SCREAMING_SNAKE_CASE : Dict = ctc_loss_reduction SCREAMING_SNAKE_CASE : List[Any] = ctc_zero_infinity # adapter SCREAMING_SNAKE_CASE : int = add_adapter SCREAMING_SNAKE_CASE : List[str] = adapter_kernel_size SCREAMING_SNAKE_CASE : List[Any] = adapter_stride SCREAMING_SNAKE_CASE : Union[str, Any] = num_adapter_layers SCREAMING_SNAKE_CASE : Dict = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE : List[Any] = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE : Tuple = list(_a ) SCREAMING_SNAKE_CASE : Optional[Any] = list(_a ) SCREAMING_SNAKE_CASE : Optional[Any] = list(_a ) SCREAMING_SNAKE_CASE : Dict = xvector_output_dim @property def __A ( self : Dict ): '''simple docstring''' return math.prod(self.conv_stride )
712
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Tuple = logging.get_logger(__name__) # TODO Update this __UpperCamelCase : List[str] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = """esm""" def __init__( self : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Any=768 , UpperCamelCase__ : Optional[Any]=12 , UpperCamelCase__ : str=12 , UpperCamelCase__ : Optional[int]=3072 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1026 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Any=1E-12 , UpperCamelCase__ : Dict="absolute" , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[int]=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Optional[Any]=None , **UpperCamelCase__ : Any , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = position_embedding_type SCREAMING_SNAKE_CASE : Any = use_cache SCREAMING_SNAKE_CASE : Dict = emb_layer_norm_before SCREAMING_SNAKE_CASE : List[str] = token_dropout SCREAMING_SNAKE_CASE : List[Any] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) SCREAMING_SNAKE_CASE : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = EsmFoldConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) SCREAMING_SNAKE_CASE : Optional[int] = get_default_vocab_list() else: SCREAMING_SNAKE_CASE : Optional[Any] = vocab_list else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = self.esmfold_config.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = None UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = 0 UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Optional[int] ): '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE : Optional[Any] = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = TrunkConfig(**self.trunk ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = asdict(self ) SCREAMING_SNAKE_CASE : Tuple = self.trunk.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 48 UpperCamelCase_ = 1_024 UpperCamelCase_ = 128 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 32 UpperCamelCase_ = 0 UpperCamelCase_ = 0 UpperCamelCase_ = False UpperCamelCase_ = 4 UpperCamelCase_ = 128 UpperCamelCase_ = None def __A ( self : Any ): '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE : Optional[int] = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) SCREAMING_SNAKE_CASE : Dict = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE : Tuple = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = asdict(self ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.structure_module.to_dict() return output @dataclass class lowercase__ : UpperCamelCase_ = 384 UpperCamelCase_ = 128 UpperCamelCase_ = 16 UpperCamelCase_ = 128 UpperCamelCase_ = 12 UpperCamelCase_ = 4 UpperCamelCase_ = 8 UpperCamelCase_ = 0.1 UpperCamelCase_ = 8 UpperCamelCase_ = 1 UpperCamelCase_ = 2 UpperCamelCase_ = 7 UpperCamelCase_ = 10 UpperCamelCase_ = 1E-8 UpperCamelCase_ = 1E5 def __A ( self : Dict ): '''simple docstring''' return asdict(self ) def A ( ): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
34
0
import unittest from transformers import DebertaVaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DebertaVaForMaskedLM, DebertaVaForMultipleChoice, DebertaVaForQuestionAnswering, DebertaVaForSequenceClassification, DebertaVaForTokenClassification, DebertaVaModel, ) from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST class lowercase__ ( _snake_case): def __init__( self : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[int]=13 , UpperCamelCase__ : Optional[Any]=7 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : int=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Optional[int]=99 , UpperCamelCase__ : Tuple=32 , UpperCamelCase__ : List[str]=5 , UpperCamelCase__ : int=4 , UpperCamelCase__ : Optional[Any]=37 , UpperCamelCase__ : List[Any]="gelu" , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : int=0.1 , UpperCamelCase__ : Optional[Any]=512 , UpperCamelCase__ : Optional[int]=16 , UpperCamelCase__ : str=2 , UpperCamelCase__ : Union[str, Any]=0.02 , UpperCamelCase__ : int=False , UpperCamelCase__ : Dict=True , UpperCamelCase__ : Any="None" , UpperCamelCase__ : Optional[int]=3 , UpperCamelCase__ : List[str]=4 , UpperCamelCase__ : Union[str, Any]=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = parent SCREAMING_SNAKE_CASE : List[Any] = batch_size SCREAMING_SNAKE_CASE : Tuple = seq_length SCREAMING_SNAKE_CASE : List[Any] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : int = use_token_type_ids SCREAMING_SNAKE_CASE : Any = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : int = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = num_labels SCREAMING_SNAKE_CASE : List[str] = num_choices SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention SCREAMING_SNAKE_CASE : Optional[Any] = position_biased_input SCREAMING_SNAKE_CASE : Dict = pos_att_type SCREAMING_SNAKE_CASE : List[Any] = scope def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE : List[str] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : Dict = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self : Optional[int] ): '''simple docstring''' return DebertaVaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def __A ( self : List[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' self.parent.assertListEqual(list(result.loss.size() ) , [] ) def __A ( self : str , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = DebertaVaModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : int = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ )[0] SCREAMING_SNAKE_CASE : Optional[Any] = model(UpperCamelCase__ )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def __A ( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = DebertaVaForMaskedLM(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = DebertaVaForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(UpperCamelCase__ ) def __A ( self : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.num_labels SCREAMING_SNAKE_CASE : Tuple = DebertaVaForTokenClassification(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Any = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = DebertaVaForQuestionAnswering(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = DebertaVaForMultipleChoice(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : Dict = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[Any] = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Any = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase__ ( _snake_case , _snake_case , unittest.TestCase): UpperCamelCase_ = ( ( DebertaVaModel, DebertaVaForMaskedLM, DebertaVaForSequenceClassification, DebertaVaForTokenClassification, DebertaVaForQuestionAnswering, DebertaVaForMultipleChoice, ) if is_torch_available() else () ) UpperCamelCase_ = ( { 'feature-extraction': DebertaVaModel, 'fill-mask': DebertaVaForMaskedLM, 'question-answering': DebertaVaForQuestionAnswering, 'text-classification': DebertaVaForSequenceClassification, 'token-classification': DebertaVaForTokenClassification, 'zero-shot': DebertaVaForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase_ = True UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False UpperCamelCase_ = False def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = DebertaVaModelTester(self ) SCREAMING_SNAKE_CASE : Optional[Any] = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=37 ) def __A ( self : Dict ): '''simple docstring''' self.config_tester.run_common_tests() def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*UpperCamelCase__ ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase__ ) def __A ( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase__ ) def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase__ ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCamelCase__ ) @slow def __A ( self : Tuple ): '''simple docstring''' for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Any = DebertaVaModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_torch @require_sentencepiece @require_tokenizers class lowercase__ ( unittest.TestCase): @unittest.skip(reason='''Model not available yet''' ) def __A ( self : Dict ): '''simple docstring''' pass @slow def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge''' ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ )[0] # compare the actual values for a slice. SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor( [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , UpperCamelCase__ , atol=1E-4 ) , f"""{output[:, 1:4, 1:4]}""" )
713
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowercase__ ( unittest.TestCase): def __A ( self : Any ): '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=UpperCamelCase__ , ) assert hasattr(self , '''env''' ) def __A ( self : str , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings SCREAMING_SNAKE_CASE : Any = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=UpperCamelCase__ , instance_count=UpperCamelCase__ , instance_type=self.instance_type , debugger_hook_config=UpperCamelCase__ , hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=UpperCamelCase__ , py_version='''py36''' , ) def __A ( self : Optional[Any] , UpperCamelCase__ : List[str] ): '''simple docstring''' TrainingJobAnalytics(UpperCamelCase__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def __A ( self : Tuple , UpperCamelCase__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.create_estimator(UpperCamelCase__ ) # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[Any] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : List[str] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : List[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_9999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , UpperCamelCase__ )
34
0
import argparse import re import numpy as np import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SamConfig, SamImageProcessor, SamModel, SamProcessor, SamVisionConfig, ) __UpperCamelCase : Union[str, Any] = { 'iou_prediction_head.layers.0': 'iou_prediction_head.proj_in', 'iou_prediction_head.layers.1': 'iou_prediction_head.layers.0', 'iou_prediction_head.layers.2': 'iou_prediction_head.proj_out', 'mask_decoder.output_upscaling.0': 'mask_decoder.upscale_conv1', 'mask_decoder.output_upscaling.1': 'mask_decoder.upscale_layer_norm', 'mask_decoder.output_upscaling.3': 'mask_decoder.upscale_conv2', 'mask_downscaling.0': 'mask_embed.conv1', 'mask_downscaling.1': 'mask_embed.layer_norm1', 'mask_downscaling.3': 'mask_embed.conv2', 'mask_downscaling.4': 'mask_embed.layer_norm2', 'mask_downscaling.6': 'mask_embed.conv3', 'point_embeddings': 'point_embed', 'pe_layer.positional_encoding_gaussian_matrix': 'shared_embedding.positional_embedding', 'image_encoder': 'vision_encoder', 'neck.0': 'neck.conv1', 'neck.1': 'neck.layer_norm1', 'neck.2': 'neck.conv2', 'neck.3': 'neck.layer_norm2', 'patch_embed.proj': 'patch_embed.projection', '.norm': '.layer_norm', 'blocks': 'layers', } def A ( _lowercase ): SCREAMING_SNAKE_CASE : List[Any] = {} state_dict.pop('''pixel_mean''' , lowerCAmelCase__ ) state_dict.pop('''pixel_std''' , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = R'''.*.output_hypernetworks_mlps.(\d+).layers.(\d+).*''' for key, value in state_dict.items(): for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE : Any = key.replace(lowerCAmelCase__ , lowerCAmelCase__ ) if re.match(lowerCAmelCase__ , lowerCAmelCase__ ): SCREAMING_SNAKE_CASE : Dict = int(re.match(lowerCAmelCase__ , lowerCAmelCase__ ).group(2 ) ) if layer_nb == 0: SCREAMING_SNAKE_CASE : Tuple = key.replace('''layers.0''' , '''proj_in''' ) elif layer_nb == 1: SCREAMING_SNAKE_CASE : int = key.replace('''layers.1''' , '''layers.0''' ) elif layer_nb == 2: SCREAMING_SNAKE_CASE : List[Any] = key.replace('''layers.2''' , '''proj_out''' ) SCREAMING_SNAKE_CASE : Any = value SCREAMING_SNAKE_CASE : int = model_state_dict[ '''prompt_encoder.shared_embedding.positional_embedding''' ] return model_state_dict def A ( _lowercase , _lowercase , _lowercase , _lowercase="ybelkada/segment-anything" ): SCREAMING_SNAKE_CASE : Optional[int] = hf_hub_download(lowerCAmelCase__ , f"""checkpoints/{model_name}.pth""" ) if "sam_vit_b" in model_name: SCREAMING_SNAKE_CASE : Optional[int] = SamConfig() elif "sam_vit_l" in model_name: SCREAMING_SNAKE_CASE : Optional[int] = SamVisionConfig( hidden_size=1_024 , num_hidden_layers=24 , num_attention_heads=16 , global_attn_indexes=[5, 11, 17, 23] , ) SCREAMING_SNAKE_CASE : Any = SamConfig( vision_config=lowerCAmelCase__ , ) elif "sam_vit_h" in model_name: SCREAMING_SNAKE_CASE : Any = SamVisionConfig( hidden_size=1_280 , num_hidden_layers=32 , num_attention_heads=16 , global_attn_indexes=[7, 15, 23, 31] , ) SCREAMING_SNAKE_CASE : Tuple = SamConfig( vision_config=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE : List[Any] = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) SCREAMING_SNAKE_CASE : Optional[int] = replace_keys(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = SamImageProcessor() SCREAMING_SNAKE_CASE : Optional[int] = SamProcessor(image_processor=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = SamModel(lowerCAmelCase__ ) hf_model.load_state_dict(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = hf_model.to('''cuda''' ) SCREAMING_SNAKE_CASE : Tuple = '''https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png''' SCREAMING_SNAKE_CASE : Union[str, Any] = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ).convert('''RGB''' ) SCREAMING_SNAKE_CASE : Optional[int] = [[[400, 650]]] SCREAMING_SNAKE_CASE : int = [[1]] SCREAMING_SNAKE_CASE : List[str] = processor(images=np.array(lowerCAmelCase__ ) , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = hf_model(**lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = output.iou_scores.squeeze() if model_name == "sam_vit_h_4b8939": assert scores[-1].item() == 0.579_8902_5115_9668 SCREAMING_SNAKE_CASE : str = processor( images=np.array(lowerCAmelCase__ ) , input_points=lowerCAmelCase__ , input_labels=lowerCAmelCase__ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE : List[str] = hf_model(**lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : str = output.iou_scores.squeeze() assert scores[-1].item() == 0.9712_6030_9219_3604 SCREAMING_SNAKE_CASE : Tuple = ((75, 275, 1_725, 850),) SCREAMING_SNAKE_CASE : List[str] = processor(images=np.array(lowerCAmelCase__ ) , input_boxes=lowerCAmelCase__ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[Any] = hf_model(**lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.iou_scores.squeeze() assert scores[-1].item() == 0.8686_0156_0592_6514 # Test with 2 points and 1 image. SCREAMING_SNAKE_CASE : Optional[int] = [[[400, 650], [800, 650]]] SCREAMING_SNAKE_CASE : str = [[1, 1]] SCREAMING_SNAKE_CASE : str = processor( images=np.array(lowerCAmelCase__ ) , input_points=lowerCAmelCase__ , input_labels=lowerCAmelCase__ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = hf_model(**lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : str = output.iou_scores.squeeze() assert scores[-1].item() == 0.9936_0477_9243_4692 if __name__ == "__main__": __UpperCamelCase : Any = argparse.ArgumentParser() __UpperCamelCase : Tuple = ['sam_vit_b_01ec64', 'sam_vit_h_4b8939', 'sam_vit_l_0b3195'] parser.add_argument( '--model_name', default='sam_vit_h_4b8939', choices=choices, type=str, help='Path to hf config.json of model to convert', ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub after converting', ) parser.add_argument( '--model_hub_id', default='ybelkada/segment-anything', choices=choices, type=str, help='Path to hf config.json of model to convert', ) __UpperCamelCase : Dict = parser.parse_args() convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)
714
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore __UpperCamelCase : Dict = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" __UpperCamelCase : Tuple = [file for file in filepaths if file != file.lower()] if upper_files: print(f"""{len(upper_files)} files contain uppercase characters:""") print('\n'.join(upper_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if ' ' in file] if space_files: print(f"""{len(space_files)} files contain space characters:""") print('\n'.join(space_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if '-' in file] if hyphen_files: print(f"""{len(hyphen_files)} files contain hyphen characters:""") print('\n'.join(hyphen_files) + '\n') __UpperCamelCase : List[Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(f"""{len(nodir_files)} files are not in a directory:""") print('\n'.join(nodir_files) + '\n') __UpperCamelCase : Optional[Any] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
34
0
import doctest from collections import deque import numpy as np class lowercase__ : def __init__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [2, 1, 2, -1] SCREAMING_SNAKE_CASE : Union[str, Any] = [1, 2, 3, 4] def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = len(self.first_signal ) SCREAMING_SNAKE_CASE : Tuple = len(self.second_signal ) SCREAMING_SNAKE_CASE : Dict = max(_UpperCamelCase , _UpperCamelCase ) # create a zero matrix of max_length x max_length SCREAMING_SNAKE_CASE : Tuple = [[0] * max_length for i in range(_UpperCamelCase )] # fills the smaller signal with zeros to make both signals of same length if length_first_signal < length_second_signal: self.first_signal += [0] * (max_length - length_first_signal) elif length_first_signal > length_second_signal: self.second_signal += [0] * (max_length - length_second_signal) for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE : Tuple = deque(self.second_signal ) rotated_signal.rotate(_UpperCamelCase ) for j, item in enumerate(_UpperCamelCase ): matrix[i][j] += item # multiply the matrix with the first signal SCREAMING_SNAKE_CASE : Dict = np.matmul(np.transpose(_UpperCamelCase ) , np.transpose(self.first_signal ) ) # rounding-off to two decimal places return [round(_UpperCamelCase , 2 ) for i in final_signal] if __name__ == "__main__": doctest.testmod()
715
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: __UpperCamelCase : Dict = None __UpperCamelCase : Tuple = logging.get_logger(__name__) __UpperCamelCase : Optional[int] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} __UpperCamelCase : Optional[int] = { '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 __UpperCamelCase : Union[str, Any] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = TaTokenizer UpperCamelCase_ = [] def __init__( self : str , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Optional[int]="<pad>" , UpperCamelCase__ : Optional[Any]=100 , UpperCamelCase__ : List[Any]=None , **UpperCamelCase__ : str , ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[str] = [f"""<extra_id_{i}>""" for i in range(UpperCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE : int = len(set(filter(lambda UpperCamelCase__ : bool('''extra_id_''' in str(UpperCamelCase__ ) ) , UpperCamelCase__ ) ) ) 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__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , extra_ids=UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , **UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : str = vocab_file SCREAMING_SNAKE_CASE : int = False if not self.vocab_file else True SCREAMING_SNAKE_CASE : str = extra_ids @staticmethod def __A ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[Any] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE : List[str] = 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.''' , UpperCamelCase__ , ) return max_model_length def __A ( self : Tuple , UpperCamelCase__ : str , UpperCamelCase__ : 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(UpperCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE : Any = 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__ ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def __A ( self : Optional[Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE : Tuple = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __A ( self : Any , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, 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 __A ( self : Dict ): '''simple docstring''' return list( set(filter(lambda UpperCamelCase__ : bool(re.search(r'''<extra_id_\d+>''' , UpperCamelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def __A ( self : List[Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(UpperCamelCase__ ) for token in self.get_sentinel_tokens()]
34
0
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def A ( _lowercase , _lowercase=10 ): SCREAMING_SNAKE_CASE : List[Any] = [] for _ in range(_lowerCamelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def A ( _lowercase , _lowercase=10 ): SCREAMING_SNAKE_CASE : Union[str, Any] = [] for step in range(_lowerCamelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE : Tuple = os.path.join(_lowerCamelCase , '''schedule.bin''' ) torch.save(scheduler.state_dict() , _lowerCamelCase ) SCREAMING_SNAKE_CASE : str = torch.load(_lowerCamelCase ) scheduler.load_state_dict(_lowerCamelCase ) return lrs @require_torch class lowercase__ ( unittest.TestCase): def __A ( self : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : List[Any] ): '''simple docstring''' self.assertEqual(len(_A ) , len(_A ) ) for a, b in zip(_A , _A ): self.assertAlmostEqual(_A , _A , delta=_A ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = torch.tensor([0.1, -0.2, -0.1] , requires_grad=_A ) SCREAMING_SNAKE_CASE : Tuple = torch.tensor([0.4, 0.2, -0.5] ) SCREAMING_SNAKE_CASE : List[str] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE : List[Any] = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0 ) for _ in range(100 ): SCREAMING_SNAKE_CASE : int = criterion(_A , _A ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=_A ) SCREAMING_SNAKE_CASE : str = torch.tensor([0.4, 0.2, -0.5] ) SCREAMING_SNAKE_CASE : List[str] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE : int = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=_A , weight_decay=0.0 , relative_step=_A , scale_parameter=_A , warmup_init=_A , ) for _ in range(1000 ): SCREAMING_SNAKE_CASE : Optional[int] = criterion(_A , _A ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) @require_torch class lowercase__ ( unittest.TestCase): UpperCamelCase_ = nn.Linear(50 , 50) if is_torch_available() else None UpperCamelCase_ = AdamW(m.parameters() , lr=10.0) if is_torch_available() else None UpperCamelCase_ = 10 def __A ( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[Any]=None ): '''simple docstring''' self.assertEqual(len(_A ) , len(_A ) ) for a, b in zip(_A , _A ): self.assertAlmostEqual(_A , _A , delta=_A , msg=_A ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {'num_warmup_steps': 2, 'num_training_steps': 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE : Optional[Any] = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {'num_warmup_steps': 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, 'num_cycles': 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, 'power': 2.0, 'lr_end': 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {'num_warmup_steps': 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE : Optional[Any] = data SCREAMING_SNAKE_CASE : Dict = scheduler_func(self.optimizer , **_A ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) SCREAMING_SNAKE_CASE : int = unwrap_schedule(_A , self.num_steps ) self.assertListAlmostEqual( _A , _A , tol=1E-2 , msg=f"""failed for {scheduler_func} in normal scheduler""" , ) SCREAMING_SNAKE_CASE : Dict = scheduler_func(self.optimizer , **_A ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(_A ) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE : int = unwrap_and_save_reload_schedule(_A , self.num_steps ) self.assertListEqual(_A , _A , msg=f"""failed for {scheduler_func} in save and reload""" ) class lowercase__ : def __init__( self : Any , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = fn def __call__( self : List[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : List[Any] ): '''simple docstring''' return self.fn(*_A , **_A ) @classmethod def __A ( self : Union[str, Any] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = list(map(self , scheduler.lr_lambdas ) )
716
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __UpperCamelCase : str = False class lowercase__ ( unittest.TestCase): pass @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase): def __A ( self : Union[str, Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = VersatileDiffusionPipeline.from_pretrained(UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = pipe.dual_guided( prompt='''first prompt''' , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = '''cyberpunk 2077''' SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe.dual_guided( prompt=UpperCamelCase__ , image=UpperCamelCase__ , text_to_image_strength=0.75 , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images SCREAMING_SNAKE_CASE : Tuple = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : int = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = '''A painting of a squirrel eating a burger ''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe.text_to_image( prompt=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : List[str] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Any = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 SCREAMING_SNAKE_CASE : Optional[Any] = pipe.image_variation(UpperCamelCase__ , generator=UpperCamelCase__ , output_type='''numpy''' ).images SCREAMING_SNAKE_CASE : Optional[int] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE : Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
34
0
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 __UpperCamelCase : List[str] = { # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 2048-bit 14: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 3072-bit 15: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 4096-bit 16: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 6144-bit 17: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 8192-bit 18: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, } class lowercase__ : def __init__( self : Optional[Any] , UpperCamelCase__ : int = 14 ): '''simple docstring''' if group not in primes: raise ValueError('''Unsupported Group''' ) SCREAMING_SNAKE_CASE : Any = primes[group]["prime"] SCREAMING_SNAKE_CASE : str = primes[group]["generator"] SCREAMING_SNAKE_CASE : List[str] = int(hexlify(urandom(32 ) ) , base=16 ) def __A ( self : str ): '''simple docstring''' return hex(self.__private_key )[2:] def __A ( self : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = pow(self.generator , self.__private_key , self.prime ) return hex(__lowerCamelCase )[2:] def __A ( self : Dict , UpperCamelCase__ : int ): '''simple docstring''' return ( 2 <= key <= self.prime - 2 and pow(__lowerCamelCase , (self.prime - 1) // 2 , self.prime ) == 1 ) def __A ( self : Any , UpperCamelCase__ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = int(__lowerCamelCase , base=16 ) if not self.is_valid_public_key(__lowerCamelCase ): raise ValueError('''Invalid public key''' ) SCREAMING_SNAKE_CASE : str = pow(__lowerCamelCase , self.__private_key , self.prime ) return shaaaa(str(__lowerCamelCase ).encode() ).hexdigest() @staticmethod def __A ( UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' return ( 2 <= remote_public_key_str <= prime - 2 and pow(__lowerCamelCase , (prime - 1) // 2 , __lowerCamelCase ) == 1 ) @staticmethod def __A ( UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int = 14 ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(__lowerCamelCase , base=16 ) SCREAMING_SNAKE_CASE : Tuple = int(__lowerCamelCase , base=16 ) SCREAMING_SNAKE_CASE : Optional[Any] = primes[group]["prime"] if not DiffieHellman.is_valid_public_key_static(__lowerCamelCase , __lowerCamelCase ): raise ValueError('''Invalid public key''' ) SCREAMING_SNAKE_CASE : Dict = pow(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) return shaaaa(str(__lowerCamelCase ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
717
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def A ( _lowercase ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = analyze_text(_lowercase ) SCREAMING_SNAKE_CASE : Any = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Tuple = sum(single_char_strings.values() ) # one length string SCREAMING_SNAKE_CASE : Tuple = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: SCREAMING_SNAKE_CASE : Tuple = single_char_strings[ch] SCREAMING_SNAKE_CASE : List[str] = my_str / all_sum my_fir_sum += prob * math.loga(_lowercase ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string SCREAMING_SNAKE_CASE : Optional[Any] = sum(two_char_strings.values() ) SCREAMING_SNAKE_CASE : List[str] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : Any = two_char_strings[sequence] SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) / all_sum my_sec_sum += prob * math.loga(_lowercase ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = Counter() # type: ignore SCREAMING_SNAKE_CASE : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_lowercase ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def A ( ): import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
34
0
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def A ( _lowercase ): SCREAMING_SNAKE_CASE : str = SwinConfig() SCREAMING_SNAKE_CASE : Optional[Any] = swin_name.split('''_''' ) SCREAMING_SNAKE_CASE : Optional[Any] = name_split[1] SCREAMING_SNAKE_CASE : str = int(name_split[4] ) SCREAMING_SNAKE_CASE : Optional[int] = int(name_split[3][-1] ) if model_size == "tiny": SCREAMING_SNAKE_CASE : List[Any] = 96 SCREAMING_SNAKE_CASE : Dict = (2, 2, 6, 2) SCREAMING_SNAKE_CASE : List[Any] = (3, 6, 12, 24) elif model_size == "small": SCREAMING_SNAKE_CASE : Tuple = 96 SCREAMING_SNAKE_CASE : Union[str, Any] = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : Union[str, Any] = (3, 6, 12, 24) elif model_size == "base": SCREAMING_SNAKE_CASE : List[Any] = 128 SCREAMING_SNAKE_CASE : Optional[int] = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : Any = (4, 8, 16, 32) else: SCREAMING_SNAKE_CASE : List[str] = 192 SCREAMING_SNAKE_CASE : Dict = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : int = (6, 12, 24, 48) if "in22k" in swin_name: SCREAMING_SNAKE_CASE : Optional[int] = 21_841 else: SCREAMING_SNAKE_CASE : Optional[int] = 1_000 SCREAMING_SNAKE_CASE : List[str] = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(a_ , a_ , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(a_ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Union[str, Any] = idalabel SCREAMING_SNAKE_CASE : Optional[Any] = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Optional[Any] = img_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_classes SCREAMING_SNAKE_CASE : Tuple = embed_dim SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : int = num_heads SCREAMING_SNAKE_CASE : Optional[int] = window_size return config def A ( _lowercase ): if "patch_embed.proj" in name: SCREAMING_SNAKE_CASE : int = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: SCREAMING_SNAKE_CASE : Optional[Any] = name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if "layers" in name: SCREAMING_SNAKE_CASE : str = '''encoder.''' + name if "attn.proj" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: SCREAMING_SNAKE_CASE : str = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: SCREAMING_SNAKE_CASE : int = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: SCREAMING_SNAKE_CASE : List[str] = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "norm.weight": SCREAMING_SNAKE_CASE : Optional[Any] = '''layernorm.weight''' if name == "norm.bias": SCREAMING_SNAKE_CASE : Optional[int] = '''layernorm.bias''' if "head" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''head''' , '''classifier''' ) else: SCREAMING_SNAKE_CASE : Tuple = '''swin.''' + name return name def A ( _lowercase , _lowercase ): for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE : List[Any] = orig_state_dict.pop(a_ ) if "mask" in key: continue elif "qkv" in key: SCREAMING_SNAKE_CASE : List[str] = key.split('''.''' ) SCREAMING_SNAKE_CASE : Optional[Any] = int(key_split[1] ) SCREAMING_SNAKE_CASE : Any = int(key_split[3] ) SCREAMING_SNAKE_CASE : int = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: SCREAMING_SNAKE_CASE : Any = val[:dim, :] SCREAMING_SNAKE_CASE : Optional[int] = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE : Any = val[-dim:, :] else: SCREAMING_SNAKE_CASE : List[Any] = val[ :dim ] SCREAMING_SNAKE_CASE : List[Any] = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE : str = val[ -dim: ] else: SCREAMING_SNAKE_CASE : List[Any] = val return orig_state_dict def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = timm.create_model(a_ , pretrained=a_ ) timm_model.eval() SCREAMING_SNAKE_CASE : Any = get_swin_config(a_ ) SCREAMING_SNAKE_CASE : Any = SwinForImageClassification(a_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = convert_state_dict(timm_model.state_dict() , a_ ) model.load_state_dict(a_ ) SCREAMING_SNAKE_CASE : List[str] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : Tuple = AutoImageProcessor.from_pretrained('''microsoft/{}'''.format(swin_name.replace('''_''' , '''-''' ) ) ) SCREAMING_SNAKE_CASE : Optional[Any] = Image.open(requests.get(a_ , stream=a_ ).raw ) SCREAMING_SNAKE_CASE : int = image_processor(images=a_ , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : str = timm_model(inputs['''pixel_values'''] ) SCREAMING_SNAKE_CASE : List[str] = model(**a_ ).logits assert torch.allclose(a_ , a_ , atol=1e-3 ) print(f"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(a_ ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(a_ ) if __name__ == "__main__": __UpperCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swin_name', default='swin_tiny_patch4_window7_224', type=str, help='Name of the Swin timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) __UpperCamelCase : List[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
718
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCamelCase : Tuple = { 'configuration_ctrl': ['CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CTRLConfig'], 'tokenization_ctrl': ['CTRLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Dict = [ 'CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'CTRLForSequenceClassification', 'CTRLLMHeadModel', 'CTRLModel', 'CTRLPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Any = [ 'TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFCTRLForSequenceClassification', 'TFCTRLLMHeadModel', 'TFCTRLModel', 'TFCTRLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
0
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase__ ( _UpperCAmelCase): UpperCamelCase_ = ['image_processor', 'tokenizer'] UpperCamelCase_ = 'AutoImageProcessor' UpperCamelCase_ = 'AutoTokenizer' def __init__( self : Optional[int] , UpperCamelCase__ : Any , UpperCamelCase__ : Any ): '''simple docstring''' super().__init__(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE : Tuple = self.image_processor def __call__( self : int , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : str=None , UpperCamelCase__ : Tuple=None , **UpperCamelCase__ : str ): '''simple docstring''' if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) if images is not None: SCREAMING_SNAKE_CASE : Tuple = self.image_processor(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) if text is not None and images is not None: SCREAMING_SNAKE_CASE : Optional[int] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCamelCase ) , tensor_type=__UpperCamelCase ) def __A ( self : Optional[Any] , *UpperCamelCase__ : Optional[Any] , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' return self.tokenizer.batch_decode(*__UpperCamelCase , **__UpperCamelCase ) def __A ( self : Optional[int] , *UpperCamelCase__ : Dict , **UpperCamelCase__ : Optional[Any] ): '''simple docstring''' return self.tokenizer.decode(*__UpperCamelCase , **__UpperCamelCase ) @property def __A ( self : Any ): '''simple docstring''' return ["input_ids", "attention_mask", "pixel_values"]
719
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Tuple = { 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[Any] = ['MaskFormerFeatureExtractor'] __UpperCamelCase : List[Any] = ['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Optional[int] = [ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] __UpperCamelCase : Union[str, Any] = [ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
34
0
import unittest import numpy as np import requests from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: __UpperCamelCase : Union[str, Any] = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class lowercase__ ( unittest.TestCase): def __init__( self : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any]=7 , UpperCamelCase__ : Union[str, Any]=3 , UpperCamelCase__ : List[str]=18 , UpperCamelCase__ : List[str]=30 , UpperCamelCase__ : List[str]=400 , UpperCamelCase__ : Any=None , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Optional[int]=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = size if size is not None else {'''height''': 20, '''width''': 20} SCREAMING_SNAKE_CASE : List[str] = parent SCREAMING_SNAKE_CASE : Union[str, Any] = batch_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Any = min_resolution SCREAMING_SNAKE_CASE : int = max_resolution SCREAMING_SNAKE_CASE : Any = size SCREAMING_SNAKE_CASE : int = do_normalize SCREAMING_SNAKE_CASE : str = do_convert_rgb SCREAMING_SNAKE_CASE : List[Any] = [512, 1024, 2048, 4096] SCREAMING_SNAKE_CASE : Optional[int] = patch_size if patch_size is not None else {'''height''': 16, '''width''': 16} def __A ( self : Optional[Any] ): '''simple docstring''' return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg''' SCREAMING_SNAKE_CASE : List[Any] = Image.open(requests.get(snake_case_ , stream=snake_case_ ).raw ).convert('''RGB''' ) return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`.""" , ) @require_torch @require_vision class lowercase__ ( _a , unittest.TestCase): UpperCamelCase_ = PixaStructImageProcessor if is_vision_available() else None def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = PixaStructImageProcessingTester(self ) @property def __A ( self : int ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case_ , '''do_normalize''' ) ) self.assertTrue(hasattr(snake_case_ , '''do_convert_rgb''' ) ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.image_processor_tester.prepare_dummy_image() SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) SCREAMING_SNAKE_CASE : Union[str, Any] = 2048 SCREAMING_SNAKE_CASE : Tuple = image_processor(snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ ) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() , torch.tensor(0.0606 ) , atol=1E-3 , rtol=1E-3 ) ) def __A ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case_ ) for image in image_inputs: self.assertIsInstance(snake_case_ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE : List[str] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE : Tuple = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE : str = image_processor( snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __A ( self : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case_ ) for image in image_inputs: self.assertIsInstance(snake_case_ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE : int = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 SCREAMING_SNAKE_CASE : Dict = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(snake_case_ ): SCREAMING_SNAKE_CASE : List[str] = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches SCREAMING_SNAKE_CASE : int = '''Hello''' SCREAMING_SNAKE_CASE : str = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ , header_text=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE : str = image_processor( snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ , header_text=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __A ( self : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case_ , numpify=snake_case_ ) for image in image_inputs: self.assertIsInstance(snake_case_ , np.ndarray ) SCREAMING_SNAKE_CASE : Optional[Any] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE : Dict = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE : List[str] = image_processor( snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __A ( self : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case_ , torchify=snake_case_ ) for image in image_inputs: self.assertIsInstance(snake_case_ , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE : Optional[Any] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE : List[str] = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE : str = image_processor( snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`.""" , ) @require_torch @require_vision class lowercase__ ( _a , unittest.TestCase): UpperCamelCase_ = PixaStructImageProcessor if is_vision_available() else None def __A ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = PixaStructImageProcessingTester(self , num_channels=4 ) SCREAMING_SNAKE_CASE : Union[str, Any] = 3 @property def __A ( self : int ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __A ( self : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case_ , '''do_normalize''' ) ) self.assertTrue(hasattr(snake_case_ , '''do_convert_rgb''' ) ) def __A ( self : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE : int = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case_ ) for image in image_inputs: self.assertIsInstance(snake_case_ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE : Any = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE : Any = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE : Any = image_processor( snake_case_ , return_tensors='''pt''' , max_patches=snake_case_ ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , )
720
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __UpperCamelCase : Dict = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def A ( _lowercase , _lowercase=None , _lowercase=None , _lowercase=None ): SCREAMING_SNAKE_CASE : Union[str, Any] = True while ask_again: SCREAMING_SNAKE_CASE : Optional[Any] = input(_lowercase ) try: if default is not None and len(_lowercase ) == 0: return default return convert_value(_lowercase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowercase ) def A ( _lowercase , _lowercase=[] , _lowercase=None , _lowercase=0 ): SCREAMING_SNAKE_CASE : Dict = BulletMenu(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : str = menu.run(default_choice=_lowercase ) return convert_value(_lowercase ) if convert_value is not None else result def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Any = int(_lowercase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A ( _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = int(_lowercase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Dict = int(_lowercase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A ( _lowercase ): return {"yes": True, "no": False}[value.lower()] class lowercase__ ( argparse.RawDescriptionHelpFormatter): def __A ( self : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = super()._format_usage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
34
0