code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class _snake_case (unittest.TestCase): @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AutoImageProcessor.from_pretrained("microsoft/dit-base-finetuned-rvlcdip" ) UpperCAmelCase_ : Tuple = AutoModelForImageClassification.from_pretrained("microsoft/dit-base-finetuned-rvlcdip" ) model.to(_snake_case ) from datasets import load_dataset UpperCAmelCase_ : Tuple = load_dataset("nielsr/rvlcdip-demo" ) UpperCAmelCase_ : Dict = dataset["train"][0]["image"].convert("RGB" ) UpperCAmelCase_ : Optional[Any] = image_processor(_snake_case ,return_tensors="pt" ).to(_snake_case ) # forward pass with torch.no_grad(): UpperCAmelCase_ : Optional[int] = model(**_snake_case ) UpperCAmelCase_ : Any = outputs.logits UpperCAmelCase_ : List[Any] = torch.Size((1, 16) ) self.assertEqual(logits.shape ,_snake_case ) UpperCAmelCase_ : Dict = torch.tensor( [-0.4158, -0.4092, -0.4347] ,device=_snake_case ,dtype=torch.float ,) self.assertTrue(torch.allclose(logits[0, :3] ,_snake_case ,atol=1E-4 ) )
71
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(_SCREAMING_SNAKE_CASE ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =["pixel_values"] def __init__( self ,_snake_case = True ,_snake_case = None ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = True ,_snake_case = None ,_snake_case = True ,_snake_case = 1 / 2_55 ,_snake_case = True ,_snake_case = True ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Optional[Any] = size if size is not None else {"shortest_edge": 2_56} UpperCAmelCase_ : List[str] = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : str = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} UpperCAmelCase_ : Optional[Any] = get_size_dict(_snake_case ,param_name="crop_size" ) UpperCAmelCase_ : int = do_resize UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Dict = do_center_crop UpperCAmelCase_ : Optional[Any] = crop_size UpperCAmelCase_ : Optional[Any] = resample UpperCAmelCase_ : int = do_rescale UpperCAmelCase_ : Optional[int] = rescale_factor UpperCAmelCase_ : Dict = offset UpperCAmelCase_ : Optional[Any] = do_normalize UpperCAmelCase_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Any = get_size_dict(_snake_case ,default_to_square=_snake_case ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(_snake_case ,size["shortest_edge"] ,default_to_square=_snake_case ) elif "height" in size and "width" in size: UpperCAmelCase_ : Optional[Any] = (size["height"], size["width"]) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(_snake_case ,size=_snake_case ,resample=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Dict = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_snake_case ,size=(size["height"], size["width"]) ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = True ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = image.astype(np.floataa ) if offset: UpperCAmelCase_ : Any = image - (scale / 2) return rescale(_snake_case ,scale=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): return normalize(_snake_case ,mean=_snake_case ,std=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. UpperCAmelCase_ : Optional[int] = to_numpy_array(_snake_case ) if do_resize: UpperCAmelCase_ : Dict = self.resize(image=_snake_case ,size=_snake_case ,resample=_snake_case ) if do_center_crop: UpperCAmelCase_ : Optional[Any] = self.center_crop(_snake_case ,size=_snake_case ) if do_rescale: UpperCAmelCase_ : Union[str, Any] = self.rescale(image=_snake_case ,scale=_snake_case ,offset=_snake_case ) if do_normalize: UpperCAmelCase_ : Any = self.normalize(image=_snake_case ,mean=_snake_case ,std=_snake_case ) UpperCAmelCase_ : Any = to_channel_dimension_format(_snake_case ,_snake_case ) return image def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,**_snake_case ,): UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : str = resample if resample is not None else self.resample UpperCAmelCase_ : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : Tuple = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : List[Any] = offset if offset is not None else self.offset UpperCAmelCase_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : int = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : int = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : int = get_size_dict(_snake_case ,param_name="crop_size" ) if not valid_images(_snake_case ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) UpperCAmelCase_ : Any = make_batched(_snake_case ) UpperCAmelCase_ : Dict = [ [ self._preprocess_image( image=_snake_case ,do_resize=_snake_case ,size=_snake_case ,resample=_snake_case ,do_center_crop=_snake_case ,crop_size=_snake_case ,do_rescale=_snake_case ,rescale_factor=_snake_case ,offset=_snake_case ,do_normalize=_snake_case ,image_mean=_snake_case ,image_std=_snake_case ,data_format=_snake_case ,) for img in video ] for video in videos ] UpperCAmelCase_ : List[str] = {"pixel_values": videos} return BatchFeature(data=_snake_case ,tensor_type=_snake_case )
71
1
'''simple docstring''' from __future__ import annotations import math def a__ ( _SCREAMING_SNAKE_CASE : int ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(_SCREAMING_SNAKE_CASE ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Optional[int] = str(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = [n] for i in range(1 , len(_SCREAMING_SNAKE_CASE ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def a__ ( _SCREAMING_SNAKE_CASE : int ) -> bool: """simple docstring""" if len(str(_SCREAMING_SNAKE_CASE ) ) > 3: if not is_prime(int(str(_SCREAMING_SNAKE_CASE )[-3:] ) ) or not is_prime(int(str(_SCREAMING_SNAKE_CASE )[:3] ) ): return False return True def a__ ( _SCREAMING_SNAKE_CASE : int = 11 ) -> list[int]: """simple docstring""" UpperCAmelCase_ : list[int] = [] UpperCAmelCase_ : int = 13 while len(_SCREAMING_SNAKE_CASE ) != count: if validate(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = list_truncated_nums(_SCREAMING_SNAKE_CASE ) if all(is_prime(_SCREAMING_SNAKE_CASE ) for i in list_nums ): list_truncated_primes.append(_SCREAMING_SNAKE_CASE ) num += 2 return list_truncated_primes def a__ ( ) -> int: """simple docstring""" return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(f"""{sum(compute_truncated_primes(11)) = }""")
71
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__( _snake_case ,split=_snake_case ,features=_snake_case ,cache_dir=_snake_case ,keep_in_memory=_snake_case ,streaming=_snake_case ,num_proc=_snake_case ,**_snake_case ,) UpperCAmelCase_ : Tuple = field UpperCAmelCase_ : List[Any] = path_or_paths if isinstance(_snake_case ,_snake_case ) else {self.split: path_or_paths} UpperCAmelCase_ : Optional[int] = Json( cache_dir=_snake_case ,data_files=_snake_case ,features=_snake_case ,field=_snake_case ,**_snake_case ,) def UpperCamelCase__ ( self ): # Build iterable dataset if self.streaming: UpperCAmelCase_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase_ : Union[str, Any] = None UpperCAmelCase_ : int = None UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : int = None self.builder.download_and_prepare( download_config=_snake_case ,download_mode=_snake_case ,verification_mode=_snake_case ,base_path=_snake_case ,num_proc=self.num_proc ,) UpperCAmelCase_ : Dict = self.builder.as_dataset( split=self.split ,verification_mode=_snake_case ,in_memory=self.keep_in_memory ) return dataset class _snake_case : def __init__( self ,_snake_case ,_snake_case ,_snake_case = None ,_snake_case = None ,**_snake_case ,): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Union[str, Any] = path_or_buf UpperCAmelCase_ : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase_ : Dict = num_proc UpperCAmelCase_ : Optional[Any] = "utf-8" UpperCAmelCase_ : Optional[int] = to_json_kwargs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.to_json_kwargs.pop("path_or_buf" ,_snake_case ) UpperCAmelCase_ : Tuple = self.to_json_kwargs.pop("orient" ,"records" ) UpperCAmelCase_ : Any = self.to_json_kwargs.pop("lines" ,True if orient == "records" else False ) UpperCAmelCase_ : Optional[int] = self.to_json_kwargs.pop("index" ,False if orient in ["split", "table"] else True ) UpperCAmelCase_ : int = self.to_json_kwargs.pop("compression" ,_snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"wb" ,compression=_snake_case ) as buffer: UpperCAmelCase_ : List[str] = self._write(file_obj=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' " was passed. Please provide a local path instead." ) UpperCAmelCase_ : Union[str, Any] = self._write( file_obj=self.path_or_buf ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) return written def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = args UpperCAmelCase_ : List[str] = query_table( table=self.dataset.data ,key=slice(_snake_case ,offset + self.batch_size ) ,indices=self.dataset._indices ,) UpperCAmelCase_ : Optional[Any] = batch.to_pandas().to_json( path_or_buf=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**_snake_case ) if not json_str.endswith("\n" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,**_snake_case ,): UpperCAmelCase_ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): UpperCAmelCase_ : Any = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: UpperCAmelCase_ , UpperCAmelCase_ : int = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,_snake_case ,_snake_case )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): written += file_obj.write(_snake_case ) return written
71
1
'''simple docstring''' from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class _snake_case (__SCREAMING_SNAKE_CASE): __A : List[Any] ="new-model" if is_tf_available(): class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =NewModelConfig @require_tf class _snake_case (unittest.TestCase): @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = "bert-base-cased" UpperCAmelCase_ : Optional[Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = TFAutoModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "bert-base-cased" UpperCAmelCase_ : Dict = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = TFAutoModelForPreTraining.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = TFAutoModelForCausalLM.from_pretrained(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : str = TFAutoModelForCausalLM.from_pretrained(_snake_case ,output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Union[str, Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : List[str] = TFAutoModelWithLMHead.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Dict = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = TFAutoModelForMaskedLM.from_pretrained(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = TFAutoModelForMaskedLM.from_pretrained(_snake_case ,output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : str = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : int = TFAutoModelForSeqaSeqLM.from_pretrained(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = TFAutoModelForSeqaSeqLM.from_pretrained(_snake_case ,output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: UpperCAmelCase_ : Dict = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = TFAutoModelForSequenceClassification.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCamelCase__ ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: UpperCAmelCase_ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = TFAutoModelForQuestionAnswering.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow @require_tensorflow_probability def UpperCamelCase__ ( self ): for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: UpperCAmelCase_ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : str = TFAutoModelForTableQuestionAnswering.from_pretrained(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : Tuple = TFAutoModelForTableQuestionAnswering.from_pretrained( _snake_case ,output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = TFAutoModelWithLMHead.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,1_44_10 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = TFAutoModelWithLMHead.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,1_44_10 ) def UpperCamelCase__ ( self ): # For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel UpperCAmelCase_ : Optional[int] = TFAutoModel.from_pretrained("sgugger/funnel-random-tiny" ) self.assertIsInstance(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = copy.deepcopy(model.config ) UpperCAmelCase_ : Any = ["FunnelBaseModel"] UpperCAmelCase_ : str = TFAutoModel.from_config(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_snake_case ) UpperCAmelCase_ : Optional[Any] = TFAutoModel.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): try: AutoConfig.register("new-model" ,_snake_case ) UpperCAmelCase_ : List[str] = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(_snake_case ): auto_class.register(_snake_case ,_snake_case ) auto_class.register(_snake_case ,_snake_case ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_snake_case ): auto_class.register(_snake_case ,_snake_case ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCAmelCase_ : Any = BertModelTester(self ).get_config() UpperCAmelCase_ : Dict = NewModelConfig(**tiny_config.to_dict() ) UpperCAmelCase_ : Optional[int] = auto_class.from_config(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_snake_case ) UpperCAmelCase_ : Union[str, Any] = auto_class.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,"bert-base is not a local folder and is not a valid model identifier" ): UpperCAmelCase_ : Dict = TFAutoModel.from_pretrained("bert-base" ) def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): UpperCAmelCase_ : str = TFAutoModel.from_pretrained(_snake_case ,revision="aaaaaa" ) def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,"hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin" ,): UpperCAmelCase_ : Tuple = TFAutoModel.from_pretrained("hf-internal-testing/config-no-model" ) def UpperCamelCase__ ( self ): with self.assertRaisesRegex(_snake_case ,"Use `from_pt=True` to load this model" ): UpperCAmelCase_ : List[str] = TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only" ) def UpperCamelCase__ ( self ): # Make sure we have cached the model. UpperCAmelCase_ : Optional[Any] = TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) with RequestCounter() as counter: UpperCAmelCase_ : Optional[Any] = TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 ) # With a sharded checkpoint UpperCAmelCase_ : List[Any] = TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) with RequestCounter() as counter: UpperCAmelCase_ : int = TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 )
71
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Tuple =["transformers", "torch", "note_seq"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["transformers", "torch", "note_seq"] ) @classmethod def UpperCamelCase__ ( cls ,*_snake_case ,**_snake_case ): requires_backends(cls ,["transformers", "torch", "note_seq"] ) @classmethod def UpperCamelCase__ ( cls ,*_snake_case ,**_snake_case ): requires_backends(cls ,["transformers", "torch", "note_seq"] )
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
1
'''simple docstring''' import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup _lowerCamelCase = { """User-Agent""": """Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36""" """ (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582""" } def a__ ( _SCREAMING_SNAKE_CASE : str = "dhaka" , _SCREAMING_SNAKE_CASE : int = 5 ) -> int: """simple docstring""" UpperCAmelCase_ : Any = min(_SCREAMING_SNAKE_CASE , 50 ) # Prevent abuse! UpperCAmelCase_ : Optional[int] = { "q": query, "tbm": "isch", "hl": "en", "ijn": "0", } UpperCAmelCase_ : int = requests.get("https://www.google.com/search" , params=_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = BeautifulSoup(html.text , "html.parser" ) UpperCAmelCase_ : Optional[int] = "".join( re.findall(r"AF_initDataCallback\(([^<]+)\);" , str(soup.select("script" ) ) ) ) UpperCAmelCase_ : List[str] = json.dumps(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = json.loads(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = re.findall( r"\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\"," , _SCREAMING_SNAKE_CASE , ) if not matched_google_image_data: return 0 UpperCAmelCase_ : Optional[Any] = re.sub( r"\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]" , "" , str(_SCREAMING_SNAKE_CASE ) , ) UpperCAmelCase_ : Dict = re.findall( r"(?:'|,),\[\"(https:|http.*?)\",\d+,\d+\]" , _SCREAMING_SNAKE_CASE , ) for index, fixed_full_res_image in enumerate(_SCREAMING_SNAKE_CASE ): if index >= max_images: return index UpperCAmelCase_ : str = bytes(_SCREAMING_SNAKE_CASE , "ascii" ).decode( "unicode-escape" ) UpperCAmelCase_ : Any = bytes(_SCREAMING_SNAKE_CASE , "ascii" ).decode( "unicode-escape" ) UpperCAmelCase_ : int = urllib.request.build_opener() UpperCAmelCase_ : Dict = [ ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" " (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582", ) ] urllib.request.install_opener(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = F'''query_{query.replace(" " , "_" )}''' if not os.path.exists(_SCREAMING_SNAKE_CASE ): os.makedirs(_SCREAMING_SNAKE_CASE ) urllib.request.urlretrieve( # noqa: S310 _SCREAMING_SNAKE_CASE , F'''{path_name}/original_size_img_{index}.jpg''' ) return index if __name__ == "__main__": try: _lowerCamelCase = download_images_from_google_query(sys.argv[1]) print(f"""{image_count} images were downloaded to disk.""") except IndexError: print("""Please provide a search term.""") raise
71
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
1
'''simple docstring''' # 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 _lowerCamelCase = [ """EAGER""", """AOT_EAGER""", """INDUCTOR""", """NVFUSER""", """AOT_NVFUSER""", """AOT_CUDAGRAPHS""", """OFI""", """FX2TRT""", """ONNXRT""", """IPEX""", ] def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Optional[int]=None , _SCREAMING_SNAKE_CASE : Dict=None , _SCREAMING_SNAKE_CASE : Any=None ) -> int: """simple docstring""" UpperCAmelCase_ : Optional[int] = True while ask_again: UpperCAmelCase_ : Any = input(_SCREAMING_SNAKE_CASE ) try: if default is not None and len(_SCREAMING_SNAKE_CASE ) == 0: return default return convert_value(_SCREAMING_SNAKE_CASE ) if convert_value is not None else result except Exception: if error_message is not None: print(_SCREAMING_SNAKE_CASE ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : str=[] , _SCREAMING_SNAKE_CASE : int=None , _SCREAMING_SNAKE_CASE : Union[str, Any]=0 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Any = BulletMenu(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = menu.run(default_choice=_SCREAMING_SNAKE_CASE ) return convert_value(_SCREAMING_SNAKE_CASE ) if convert_value is not None else result def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Tuple: """simple docstring""" UpperCAmelCase_ : List[str] = int(_SCREAMING_SNAKE_CASE ) return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value] ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] ) -> Dict: """simple docstring""" UpperCAmelCase_ : Dict = int(_SCREAMING_SNAKE_CASE ) return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value] ) def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Dict: """simple docstring""" UpperCAmelCase_ : Dict = int(_SCREAMING_SNAKE_CASE ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def a__ ( _SCREAMING_SNAKE_CASE : Any ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Any = int(_SCREAMING_SNAKE_CASE ) return PrecisionType(["no", "fp16", "bf16", "fp8"][value] ) def a__ ( _SCREAMING_SNAKE_CASE : int ) -> Tuple: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = int(_SCREAMING_SNAKE_CASE ) return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value] ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] ) -> Union[str, Any]: """simple docstring""" return {"yes": True, "no": False}[value.lower()] class _snake_case (argparse.RawDescriptionHelpFormatter): def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : Optional[Any] = super()._format_usage(_snake_case ,_snake_case ,_snake_case ,_snake_case ) UpperCAmelCase_ : int = usage.replace("<command> [<args>] " ,"" ) return usage
71
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
1
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase = logging.get_logger(__name__) _lowerCamelCase = { """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""", # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class _snake_case (__SCREAMING_SNAKE_CASE): __A : List[Any] ="altclip_text_model" def __init__( self ,_snake_case=25_00_02 ,_snake_case=10_24 ,_snake_case=24 ,_snake_case=16 ,_snake_case=40_96 ,_snake_case="gelu" ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=5_14 ,_snake_case=1 ,_snake_case=0.02 ,_snake_case=0.02 ,_snake_case=1E-05 ,_snake_case=1 ,_snake_case=0 ,_snake_case=2 ,_snake_case="absolute" ,_snake_case=True ,_snake_case=7_68 ,**_snake_case ,): super().__init__(pad_token_id=_snake_case ,bos_token_id=_snake_case ,eos_token_id=_snake_case ,**_snake_case ) UpperCAmelCase_ : int = vocab_size UpperCAmelCase_ : List[str] = hidden_size UpperCAmelCase_ : List[str] = num_hidden_layers UpperCAmelCase_ : List[Any] = num_attention_heads UpperCAmelCase_ : int = hidden_act UpperCAmelCase_ : List[Any] = intermediate_size UpperCAmelCase_ : Optional[Any] = hidden_dropout_prob UpperCAmelCase_ : Tuple = attention_probs_dropout_prob UpperCAmelCase_ : Optional[int] = max_position_embeddings UpperCAmelCase_ : List[str] = type_vocab_size UpperCAmelCase_ : str = initializer_range UpperCAmelCase_ : Union[str, Any] = initializer_factor UpperCAmelCase_ : List[Any] = layer_norm_eps UpperCAmelCase_ : Union[str, Any] = position_embedding_type UpperCAmelCase_ : Union[str, Any] = use_cache UpperCAmelCase_ : Any = project_dim class _snake_case (__SCREAMING_SNAKE_CASE): __A : str ="altclip_vision_model" def __init__( self ,_snake_case=7_68 ,_snake_case=30_72 ,_snake_case=5_12 ,_snake_case=12 ,_snake_case=12 ,_snake_case=3 ,_snake_case=2_24 ,_snake_case=32 ,_snake_case="quick_gelu" ,_snake_case=1E-5 ,_snake_case=0.0 ,_snake_case=0.02 ,_snake_case=1.0 ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Any = hidden_size UpperCAmelCase_ : Dict = intermediate_size UpperCAmelCase_ : str = projection_dim UpperCAmelCase_ : str = num_hidden_layers UpperCAmelCase_ : Tuple = num_attention_heads UpperCAmelCase_ : int = num_channels UpperCAmelCase_ : Union[str, Any] = patch_size UpperCAmelCase_ : List[Any] = image_size UpperCAmelCase_ : int = initializer_range UpperCAmelCase_ : str = initializer_factor UpperCAmelCase_ : Union[str, Any] = attention_dropout UpperCAmelCase_ : str = layer_norm_eps UpperCAmelCase_ : str = hidden_act @classmethod def UpperCamelCase__ ( cls ,_snake_case ,**_snake_case ): cls._set_token_in_kwargs(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : str = cls.get_config_dict(_snake_case ,**_snake_case ) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get("model_type" ) == "altclip": UpperCAmelCase_ : Dict = config_dict["vision_config"] if "model_type" in config_dict and hasattr(cls ,"model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_snake_case ,**_snake_case ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : str ="altclip" __A : Dict =True def __init__( self ,_snake_case=None ,_snake_case=None ,_snake_case=7_68 ,_snake_case=2.6592 ,**_snake_case ): # If `_config_dict` exist, we use them for the backward compatibility. # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot # of confusion!). UpperCAmelCase_ : Optional[Any] = kwargs.pop("text_config_dict" ,_snake_case ) UpperCAmelCase_ : str = kwargs.pop("vision_config_dict" ,_snake_case ) super().__init__(**_snake_case ) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: UpperCAmelCase_ : int = {} # This is the complete result when using `text_config_dict`. UpperCAmelCase_ : List[str] = AltCLIPTextConfig(**_snake_case ).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: UpperCAmelCase_ : Union[str, Any] = ( f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. ''' f'''The value `text_config_dict["{key}"]` will be used instead.''' ) # If inferred from default argument values (just to be super careful) else: UpperCAmelCase_ : Tuple = ( f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The ''' f'''value `text_config["{key}"]` will be overriden.''' ) logger.warning(_snake_case ) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict ) if vision_config_dict is not None: if vision_config is None: UpperCAmelCase_ : Dict = {} # This is the complete result when using `vision_config_dict`. UpperCAmelCase_ : Dict = AltCLIPVisionConfig(**_snake_case ).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: UpperCAmelCase_ : Optional[Any] = { str(_snake_case ): value for key, value in _vision_config_dict["id2label"].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: UpperCAmelCase_ : Dict = ( f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different ''' f'''values. The value `vision_config_dict["{key}"]` will be used instead.''' ) # If inferred from default argument values (just to be super careful) else: UpperCAmelCase_ : List[str] = ( f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. ''' f'''The value `vision_config["{key}"]` will be overriden.''' ) logger.warning(_snake_case ) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict ) if text_config is None: UpperCAmelCase_ : Optional[int] = {} logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values." ) if vision_config is None: UpperCAmelCase_ : Tuple = {} logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values." ) UpperCAmelCase_ : Any = AltCLIPTextConfig(**_snake_case ) UpperCAmelCase_ : str = AltCLIPVisionConfig(**_snake_case ) UpperCAmelCase_ : Tuple = projection_dim UpperCAmelCase_ : Any = logit_scale_init_value UpperCAmelCase_ : List[Any] = 1.0 @classmethod def UpperCamelCase__ ( cls ,_snake_case ,_snake_case ,**_snake_case ): return cls(text_config=text_config.to_dict() ,vision_config=vision_config.to_dict() ,**_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = copy.deepcopy(self.__dict__ ) UpperCAmelCase_ : Any = self.text_config.to_dict() UpperCAmelCase_ : int = self.vision_config.to_dict() UpperCAmelCase_ : Union[str, Any] = self.__class__.model_type return output
71
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxCrossAttnUpBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, FlaxUpBlockaD, ) @flax.struct.dataclass class _snake_case (__SCREAMING_SNAKE_CASE): __A : jnp.ndarray @flax_register_to_config class _snake_case (nn.Module , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): __A : int =32 __A : int =4 __A : int =4 __A : Tuple[str] =( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) __A : Tuple[str] =("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D") __A : Union[bool, Tuple[bool]] =False __A : Tuple[int] =(3_20, 6_40, 12_80, 12_80) __A : int =2 __A : Union[int, Tuple[int]] =8 __A : Optional[Union[int, Tuple[int]]] =None __A : int =12_80 __A : float =0.0 __A : bool =False __A : jnp.dtype =jnp.floataa __A : bool =True __A : int =0 __A : bool =False def UpperCamelCase__ ( self ,_snake_case ): # init input tensors UpperCAmelCase_ : List[str] = (1, self.in_channels, self.sample_size, self.sample_size) UpperCAmelCase_ : Tuple = jnp.zeros(_snake_case ,dtype=jnp.floataa ) UpperCAmelCase_ : Optional[Any] = jnp.ones((1,) ,dtype=jnp.intaa ) UpperCAmelCase_ : Optional[Any] = jnp.zeros((1, 1, self.cross_attention_dim) ,dtype=jnp.floataa ) UpperCAmelCase_ , UpperCAmelCase_ : Tuple = jax.random.split(_snake_case ) UpperCAmelCase_ : int = {"params": params_rng, "dropout": dropout_rng} return self.init(_snake_case ,_snake_case ,_snake_case ,_snake_case )["params"] def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.block_out_channels UpperCAmelCase_ : Tuple = block_out_channels[0] * 4 if self.num_attention_heads is not None: raise ValueError( "At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19." ) # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. UpperCAmelCase_ : Union[str, Any] = self.num_attention_heads or self.attention_head_dim # input UpperCAmelCase_ : List[Any] = nn.Conv( block_out_channels[0] ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) # time UpperCAmelCase_ : Tuple = FlaxTimesteps( block_out_channels[0] ,flip_sin_to_cos=self.flip_sin_to_cos ,freq_shift=self.config.freq_shift ) UpperCAmelCase_ : int = FlaxTimestepEmbedding(_snake_case ,dtype=self.dtype ) UpperCAmelCase_ : str = self.only_cross_attention if isinstance(_snake_case ,_snake_case ): UpperCAmelCase_ : Dict = (only_cross_attention,) * len(self.down_block_types ) if isinstance(_snake_case ,_snake_case ): UpperCAmelCase_ : List[str] = (num_attention_heads,) * len(self.down_block_types ) # down UpperCAmelCase_ : int = [] UpperCAmelCase_ : Optional[Any] = block_out_channels[0] for i, down_block_type in enumerate(self.down_block_types ): UpperCAmelCase_ : Any = output_channel UpperCAmelCase_ : List[Any] = block_out_channels[i] UpperCAmelCase_ : str = i == len(_snake_case ) - 1 if down_block_type == "CrossAttnDownBlock2D": UpperCAmelCase_ : Optional[Any] = FlaxCrossAttnDownBlockaD( in_channels=_snake_case ,out_channels=_snake_case ,dropout=self.dropout ,num_layers=self.layers_per_block ,num_attention_heads=num_attention_heads[i] ,add_downsample=not is_final_block ,use_linear_projection=self.use_linear_projection ,only_cross_attention=only_cross_attention[i] ,use_memory_efficient_attention=self.use_memory_efficient_attention ,dtype=self.dtype ,) else: UpperCAmelCase_ : Optional[int] = FlaxDownBlockaD( in_channels=_snake_case ,out_channels=_snake_case ,dropout=self.dropout ,num_layers=self.layers_per_block ,add_downsample=not is_final_block ,dtype=self.dtype ,) down_blocks.append(_snake_case ) UpperCAmelCase_ : Any = down_blocks # mid UpperCAmelCase_ : Union[str, Any] = FlaxUNetMidBlockaDCrossAttn( in_channels=block_out_channels[-1] ,dropout=self.dropout ,num_attention_heads=num_attention_heads[-1] ,use_linear_projection=self.use_linear_projection ,use_memory_efficient_attention=self.use_memory_efficient_attention ,dtype=self.dtype ,) # up UpperCAmelCase_ : str = [] UpperCAmelCase_ : Union[str, Any] = list(reversed(_snake_case ) ) UpperCAmelCase_ : Union[str, Any] = list(reversed(_snake_case ) ) UpperCAmelCase_ : str = list(reversed(_snake_case ) ) UpperCAmelCase_ : Any = reversed_block_out_channels[0] for i, up_block_type in enumerate(self.up_block_types ): UpperCAmelCase_ : Tuple = output_channel UpperCAmelCase_ : Any = reversed_block_out_channels[i] UpperCAmelCase_ : List[Any] = reversed_block_out_channels[min(i + 1 ,len(_snake_case ) - 1 )] UpperCAmelCase_ : Optional[Any] = i == len(_snake_case ) - 1 if up_block_type == "CrossAttnUpBlock2D": UpperCAmelCase_ : Dict = FlaxCrossAttnUpBlockaD( in_channels=_snake_case ,out_channels=_snake_case ,prev_output_channel=_snake_case ,num_layers=self.layers_per_block + 1 ,num_attention_heads=reversed_num_attention_heads[i] ,add_upsample=not is_final_block ,dropout=self.dropout ,use_linear_projection=self.use_linear_projection ,only_cross_attention=only_cross_attention[i] ,use_memory_efficient_attention=self.use_memory_efficient_attention ,dtype=self.dtype ,) else: UpperCAmelCase_ : str = FlaxUpBlockaD( in_channels=_snake_case ,out_channels=_snake_case ,prev_output_channel=_snake_case ,num_layers=self.layers_per_block + 1 ,add_upsample=not is_final_block ,dropout=self.dropout ,dtype=self.dtype ,) up_blocks.append(_snake_case ) UpperCAmelCase_ : List[Any] = output_channel UpperCAmelCase_ : List[str] = up_blocks # out UpperCAmelCase_ : Optional[int] = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) UpperCAmelCase_ : str = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case = True ,_snake_case = False ,): # 1. time if not isinstance(_snake_case ,jnp.ndarray ): UpperCAmelCase_ : Optional[int] = jnp.array([timesteps] ,dtype=jnp.intaa ) elif isinstance(_snake_case ,jnp.ndarray ) and len(timesteps.shape ) == 0: UpperCAmelCase_ : List[str] = timesteps.astype(dtype=jnp.floataa ) UpperCAmelCase_ : Optional[int] = jnp.expand_dims(_snake_case ,0 ) UpperCAmelCase_ : Union[str, Any] = self.time_proj(_snake_case ) UpperCAmelCase_ : Optional[Any] = self.time_embedding(_snake_case ) # 2. pre-process UpperCAmelCase_ : Optional[int] = jnp.transpose(_snake_case ,(0, 2, 3, 1) ) UpperCAmelCase_ : Union[str, Any] = self.conv_in(_snake_case ) # 3. down UpperCAmelCase_ : int = (sample,) for down_block in self.down_blocks: if isinstance(_snake_case ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = down_block(_snake_case ,_snake_case ,_snake_case ,deterministic=not train ) else: UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = down_block(_snake_case ,_snake_case ,deterministic=not train ) down_block_res_samples += res_samples if down_block_additional_residuals is not None: UpperCAmelCase_ : int = () for down_block_res_sample, down_block_additional_residual in zip( _snake_case ,_snake_case ): down_block_res_sample += down_block_additional_residual new_down_block_res_samples += (down_block_res_sample,) UpperCAmelCase_ : Optional[Any] = new_down_block_res_samples # 4. mid UpperCAmelCase_ : Dict = self.mid_block(_snake_case ,_snake_case ,_snake_case ,deterministic=not train ) if mid_block_additional_residual is not None: sample += mid_block_additional_residual # 5. up for up_block in self.up_blocks: UpperCAmelCase_ : List[str] = down_block_res_samples[-(self.layers_per_block + 1) :] UpperCAmelCase_ : Tuple = down_block_res_samples[: -(self.layers_per_block + 1)] if isinstance(_snake_case ,_snake_case ): UpperCAmelCase_ : Dict = up_block( _snake_case ,temb=_snake_case ,encoder_hidden_states=_snake_case ,res_hidden_states_tuple=_snake_case ,deterministic=not train ,) else: UpperCAmelCase_ : Tuple = up_block(_snake_case ,temb=_snake_case ,res_hidden_states_tuple=_snake_case ,deterministic=not train ) # 6. post-process UpperCAmelCase_ : Optional[int] = self.conv_norm_out(_snake_case ) UpperCAmelCase_ : List[Any] = nn.silu(_snake_case ) UpperCAmelCase_ : str = self.conv_out(_snake_case ) UpperCAmelCase_ : str = jnp.transpose(_snake_case ,(0, 3, 1, 2) ) if not return_dict: return (sample,) return FlaxUNetaDConditionOutput(sample=_snake_case )
71
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCamelCase = """\ @misc{wu2016googles, title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _lowerCamelCase = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the 'GLEU score'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score's range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ _lowerCamelCase = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: 'google_bleu': google_bleu score Examples: Example 1: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _snake_case (datasets.Metric): def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ) ,id="references" ), } ) ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = 1 ,_snake_case = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case ,hypotheses=_snake_case ,min_len=_snake_case ,max_len=_snake_case ) }
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : float ) -> float: """simple docstring""" return price * (1 + tax_rate) if __name__ == "__main__": print(f"""{price_plus_tax(100, 0.25) = }""") print(f"""{price_plus_tax(1_25.50, 0.05) = }""")
71
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import _LazyModule _lowerCamelCase = {"""tokenization_byt5""": ["""ByT5Tokenizer"""]} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class _snake_case : __A : Dict =BlenderbotConfig __A : Union[str, Any] ={} __A : Any ="gelu" def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=20 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Dict = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Optional[int] = attention_probs_dropout_prob UpperCAmelCase_ : List[Any] = max_position_embeddings UpperCAmelCase_ : str = eos_token_id UpperCAmelCase_ : List[Any] = pad_token_id UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) UpperCAmelCase_ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) UpperCAmelCase_ : Optional[Any] = tf.concat([input_ids, eos_tensor] ,axis=1 ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) UpperCAmelCase_ : List[str] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlenderbotModel(config=_snake_case ).get_decoder() UpperCAmelCase_ : int = inputs_dict["input_ids"] UpperCAmelCase_ : Dict = input_ids[:1, :] UpperCAmelCase_ : Any = inputs_dict["attention_mask"][:1, :] UpperCAmelCase_ : int = inputs_dict["head_mask"] UpperCAmelCase_ : Optional[int] = 1 # first forward pass UpperCAmelCase_ : List[str] = model(_snake_case ,attention_mask=_snake_case ,head_mask=_snake_case ,use_cache=_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) ,config.vocab_size ) UpperCAmelCase_ : Any = tf.cast(ids_tensor((self.batch_size, 3) ,2 ) ,tf.inta ) # append to next input_ids and UpperCAmelCase_ : Union[str, Any] = tf.concat([input_ids, next_tokens] ,axis=-1 ) UpperCAmelCase_ : Any = tf.concat([attention_mask, next_attn_mask] ,axis=-1 ) UpperCAmelCase_ : Any = model(_snake_case ,attention_mask=_snake_case )[0] UpperCAmelCase_ : List[Any] = model(_snake_case ,attention_mask=_snake_case ,past_key_values=_snake_case )[0] self.parent.assertEqual(next_tokens.shape[1] ,output_from_past.shape[1] ) # select random slice UpperCAmelCase_ : str = int(ids_tensor((1,) ,output_from_past.shape[-1] ) ) UpperCAmelCase_ : List[str] = output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase_ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_snake_case ,_snake_case ,rtol=1E-3 ) def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : List[str]=None , _SCREAMING_SNAKE_CASE : Dict=None , ) -> Union[str, Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase_ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase_ : List[Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __A : List[str] =(TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __A : Dict =( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __A : Any =True __A : Dict =False __A : Dict =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = TFBlenderbotModelTester(self ) UpperCAmelCase_ : int = ConfigTester(self ,config_class=_snake_case ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_snake_case ) @require_tokenizers @require_tf class _snake_case (unittest.TestCase): __A : Optional[int] =["My friends are cool but they eat too many carbs."] __A : Optional[Any] ="facebook/blenderbot-400M-distill" @cached_property def UpperCamelCase__ ( self ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.tokenizer(self.src_text ,return_tensors="tf" ) UpperCAmelCase_ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,) UpperCAmelCase_ : str = self.tokenizer.batch_decode(generated_ids.numpy() ,skip_special_tokens=_snake_case )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
71
1
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : int ) -> List[Any]: """simple docstring""" assert isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Optional[int] ) -> Tuple: """simple docstring""" UpperCAmelCase_ : List[Any] = tmp_path / "cache" UpperCAmelCase_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCAmelCase_ : int = ParquetDatasetReader(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE , keep_in_memory=_SCREAMING_SNAKE_CASE ).read() _check_parquet_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Optional[int] ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Optional[Any] = tmp_path / "cache" UpperCAmelCase_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} UpperCAmelCase_ : List[str] = features.copy() if features else default_expected_features UpperCAmelCase_ : Optional[Any] = ( Features({feature: Value(_SCREAMING_SNAKE_CASE ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCAmelCase_ : Optional[int] = ParquetDatasetReader(_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ).read() _check_parquet_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Any ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ : str = tmp_path / "cache" UpperCAmelCase_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} UpperCAmelCase_ : Optional[Any] = ParquetDatasetReader(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE ).read() _check_parquet_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Dict ) -> List[Any]: """simple docstring""" if issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[int] = parquet_path elif issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = [parquet_path] UpperCAmelCase_ : str = tmp_path / "cache" UpperCAmelCase_ : List[Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} UpperCAmelCase_ : Union[str, Any] = ParquetDatasetReader(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ).read() _check_parquet_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[Any]=("train",) ) -> Optional[Any]: """simple docstring""" assert isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for split in splits: UpperCAmelCase_ : str = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def a__ ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ : List[Any] = tmp_path / "cache" UpperCAmelCase_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCAmelCase_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_SCREAMING_SNAKE_CASE , keep_in_memory=_SCREAMING_SNAKE_CASE ).read() _check_parquet_datasetdict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Optional[Any] ) -> Dict: """simple docstring""" UpperCAmelCase_ : Any = tmp_path / "cache" UpperCAmelCase_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} UpperCAmelCase_ : List[str] = features.copy() if features else default_expected_features UpperCAmelCase_ : str = ( Features({feature: Value(_SCREAMING_SNAKE_CASE ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCAmelCase_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ).read() _check_parquet_datasetdict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Any ) -> int: """simple docstring""" if split: UpperCAmelCase_ : Tuple = {split: parquet_path} else: UpperCAmelCase_ : int = "train" UpperCAmelCase_ : int = {"train": parquet_path, "test": parquet_path} UpperCAmelCase_ : List[Any] = tmp_path / "cache" UpperCAmelCase_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} UpperCAmelCase_ : Any = ParquetDatasetReader(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ).read() _check_parquet_datasetdict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def a__ ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Union[str, Any] ) -> List[str]: """simple docstring""" UpperCAmelCase_ : str = ParquetDatasetWriter(_SCREAMING_SNAKE_CASE , tmp_path / "foo.parquet" ) assert writer.write() > 0 UpperCAmelCase_ : Optional[int] = pq.ParquetFile(tmp_path / "foo.parquet" ) UpperCAmelCase_ : Optional[Any] = pf.read() assert dataset.data.table == output_table def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Optional[int] ) -> int: """simple docstring""" UpperCAmelCase_ : Optional[Any] = str(shared_datadir / "test_image_rgb.jpg" ) UpperCAmelCase_ : Optional[int] = {"image": [image_path]} UpperCAmelCase_ : List[str] = Features({"image": Image()} ) UpperCAmelCase_ : Any = Dataset.from_dict(_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = ParquetDatasetWriter(_SCREAMING_SNAKE_CASE , tmp_path / "foo.parquet" ) assert writer.write() > 0 UpperCAmelCase_ : Tuple = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features UpperCAmelCase_ : str = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_SCREAMING_SNAKE_CASE ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Any ) -> Tuple: """simple docstring""" assert get_writer_batch_size(_SCREAMING_SNAKE_CASE ) == expected
71
'''simple docstring''' from numpy import exp, pi, sqrt def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _snake_case (nn.Module): def __init__( self ,_snake_case = 16 ,_snake_case = 88 ,_snake_case = None ,_snake_case = 1 ,_snake_case = 0.0 ,_snake_case = 32 ,_snake_case = None ,_snake_case = False ,_snake_case = None ,_snake_case = None ,_snake_case = "geglu" ,_snake_case = None ,): super().__init__() UpperCAmelCase_ : Optional[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case ,attention_head_dim=_snake_case ,in_channels=_snake_case ,num_layers=_snake_case ,dropout=_snake_case ,norm_num_groups=_snake_case ,cross_attention_dim=_snake_case ,attention_bias=_snake_case ,sample_size=_snake_case ,num_vector_embeds=_snake_case ,activation_fn=_snake_case ,num_embeds_ada_norm=_snake_case ,) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference UpperCAmelCase_ : List[str] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` UpperCAmelCase_ : int = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` UpperCAmelCase_ : List[Any] = [1, 0] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = True ,): UpperCAmelCase_ : List[str] = hidden_states UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[int] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens UpperCAmelCase_ : Any = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] UpperCAmelCase_ : Any = self.transformer_index_for_condition[i] UpperCAmelCase_ : int = self.transformers[transformer_index]( _snake_case ,encoder_hidden_states=_snake_case ,timestep=_snake_case ,cross_attention_kwargs=_snake_case ,return_dict=_snake_case ,)[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] UpperCAmelCase_ : Dict = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) UpperCAmelCase_ : List[Any] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
71
1
'''simple docstring''' _lowerCamelCase = [ """DownloadConfig""", """DownloadManager""", """DownloadMode""", """StreamingDownloadManager""", ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
71
'''simple docstring''' import json import sys def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : int ) -> Tuple: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: UpperCAmelCase_ : Dict = json.load(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = ["<details>", "<summary>Show updated benchmarks!</summary>", " "] for benchmark_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = results[benchmark_name] UpperCAmelCase_ : Any = benchmark_name.split("/" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) UpperCAmelCase_ : Any = "| metric |" UpperCAmelCase_ : Any = "|--------|" UpperCAmelCase_ : Union[str, Any] = "| new / old (diff) |" for metric_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = benchmark_res[metric_name] UpperCAmelCase_ : Union[str, Any] = metric_vals["new"] UpperCAmelCase_ : Optional[Any] = metric_vals.get("old" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = metric_vals.get("diff" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = F''' {new_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("</details>" ) with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.writelines("\n".join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": _lowerCamelCase = sys.argv[1] _lowerCamelCase = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
71
1
'''simple docstring''' class _snake_case : def __init__( self ,_snake_case = "" ,_snake_case = False ): # Mapping from the first character of the prefix of the node UpperCAmelCase_ : dict[str, RadixNode] = {} # A node will be a leaf if the tree contains its word UpperCAmelCase_ : List[Any] = is_leaf UpperCAmelCase_ : Union[str, Any] = prefix def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Optional[Any] = 0 for q, w in zip(self.prefix ,_snake_case ): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def UpperCamelCase__ ( self ,_snake_case ): for word in words: self.insert(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): # Case 1: If the word is the prefix of the node # Solution: We set the current node as leaf if self.prefix == word: UpperCAmelCase_ : Optional[int] = True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: UpperCAmelCase_ : List[Any] = RadixNode(prefix=_snake_case ,is_leaf=_snake_case ) else: UpperCAmelCase_ : str = self.nodes[word[0]] UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = incoming_node.match( _snake_case ) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(_snake_case ) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: UpperCAmelCase_ : str = remaining_prefix UpperCAmelCase_ : Optional[int] = self.nodes[matching_string[0]] UpperCAmelCase_ : int = RadixNode(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = aux_node if remaining_word == "": UpperCAmelCase_ : Union[str, Any] = True else: self.nodes[matching_string[0]].insert(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Optional[Any] = self.nodes.get(word[0] ,_snake_case ) if not incoming_node: return False else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = incoming_node.match( _snake_case ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : str = self.nodes.get(word[0] ,_snake_case ) if not incoming_node: return False else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = incoming_node.match( _snake_case ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(_snake_case ) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes ) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes ) == 1 and not self.is_leaf: UpperCAmelCase_ : Dict = list(self.nodes.values() )[0] UpperCAmelCase_ : str = merging_node.is_leaf self.prefix += merging_node.prefix UpperCAmelCase_ : List[Any] = merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes ) > 1: UpperCAmelCase_ : Tuple = False # If there is 1 edge, we merge it with its child else: UpperCAmelCase_ : Any = list(incoming_node.nodes.values() )[0] UpperCAmelCase_ : Dict = merging_node.is_leaf incoming_node.prefix += merging_node.prefix UpperCAmelCase_ : Union[str, Any] = merging_node.nodes return True def UpperCamelCase__ ( self ,_snake_case = 0 ): if self.prefix != "": print("-" * height ,self.prefix ," (leaf)" if self.is_leaf else "" ) for value in self.nodes.values(): value.print_tree(height + 1 ) def a__ ( ) -> bool: """simple docstring""" UpperCAmelCase_ : List[Any] = "banana bananas bandana band apple all beast".split() UpperCAmelCase_ : Union[str, Any] = RadixNode() root.insert_many(_SCREAMING_SNAKE_CASE ) assert all(root.find(_SCREAMING_SNAKE_CASE ) for word in words ) assert not root.find("bandanas" ) assert not root.find("apps" ) root.delete("all" ) assert not root.find("all" ) root.delete("banana" ) assert not root.find("banana" ) assert root.find("bananas" ) return True def a__ ( ) -> None: """simple docstring""" assert test_trie() def a__ ( ) -> None: """simple docstring""" UpperCAmelCase_ : Any = RadixNode() UpperCAmelCase_ : str = "banana bananas bandanas bandana band apple all beast".split() root.insert_many(_SCREAMING_SNAKE_CASE ) print("Words:" , _SCREAMING_SNAKE_CASE ) print("Tree:" ) root.print_tree() if __name__ == "__main__": main()
71
'''simple docstring''' import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase = get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[int] =DebertaVaTokenizer __A : Union[str, Any] =DebertaVaTokenizerFast __A : str =True __A : List[str] =True def UpperCamelCase__ ( self ): super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase_ : Optional[int] = DebertaVaTokenizer(_snake_case ,unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[Any] = "this is a test" UpperCAmelCase_ : Optional[Any] = "this is a test" return input_text, output_text def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = "<pad>" UpperCAmelCase_ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case ) ,_snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case ) ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"<pad>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"[PAD]" ) self.assertEqual(len(_snake_case ) ,3_00_01 ) def UpperCamelCase__ ( self ): self.assertEqual(self.get_tokenizer().vocab_size ,3_00_00 ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : str = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : Union[str, Any] = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on UpperCAmelCase_ : Tuple = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Dict = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[str] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : List[Any] = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_tokenizer() UpperCAmelCase_ : Union[str, Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Dict = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) UpperCAmelCase_ : Optional[Any] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) UpperCAmelCase_ : int = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Union[str, Any] = tokenizer.encode(_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = "This is a test" UpperCAmelCase_ : Optional[int] = [13, 1, 43_98, 25, 21, 12_89] UpperCAmelCase_ : Optional[Any] = ["▁", "T", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : str = DebertaVaTokenizer(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[str] = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) # fmt: off UpperCAmelCase_ : List[str] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = [13, 1, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] UpperCAmelCase_ : str = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[str] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode("sequence builders" ) UpperCAmelCase_ : Dict = tokenizer.encode("multi-sequence build" ) UpperCAmelCase_ : Tuple = tokenizer.build_inputs_with_special_tokens(_snake_case ) UpperCAmelCase_ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(_snake_case ,_snake_case ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] ,_snake_case ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] ,_snake_case ,) @slow def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Union[str, Any] = {"input_ids": [[1, 3_98_67, 36, 1_93_90, 4_86, 27, 3_50_52, 8_14_36, 18, 6_06_85, 12_25, 7, 3_50_52, 8_14_36, 18, 93_67, 1_68_99, 18, 1_59_37, 53, 5_94, 7_73, 18, 1_62_87, 3_04_65, 36, 1_59_37, 6, 4_11_39, 38, 3_69_79, 6_07_63, 1_91, 6, 3_41_32, 99, 6, 5_05_38, 3_90, 4_32_30, 6, 3_41_32, 27_79, 2_08_50, 14, 6_99, 10_72, 11_94, 36, 3_82, 1_09_01, 53, 7, 6_99, 10_72, 20_84, 36, 2_04_22, 6_30, 53, 19, 1_05, 30_49, 18_96, 10_53, 1_68_99, 15_06, 11, 3_79_78, 42_43, 7, 12_37, 3_18_69, 2_00, 1_65_66, 6_54, 6, 3_50_52, 8_14_36, 7, 5_56_30, 1_35_93, 4, 2], [1, 26, 1_50_11, 13, 6_67, 8, 10_53, 18, 2_36_11, 12_37, 7_23_56, 1_28_20, 34, 10_41_34, 12_09, 35, 1_33_13, 66_27, 21, 2_02, 3_47, 7, 1_64, 23_99, 11, 46, 44_85, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 12_32, 28_64, 1_57_85, 1_49_51, 1_05, 5, 85_81, 12_50, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_snake_case ,model_name="microsoft/deberta-v2-xlarge" ,revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" ,)
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list ) -> list: """simple docstring""" if len(_SCREAMING_SNAKE_CASE ) <= 1: return [tuple(_SCREAMING_SNAKE_CASE )] UpperCAmelCase_ : List[str] = [] def generate(_SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , _SCREAMING_SNAKE_CASE ) for i in range(k - 1 ): if k % 2 == 0: # k is even UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = arr[k - 1], arr[i] else: # k is odd UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = arr[k - 1], arr[0] generate(k - 1 , _SCREAMING_SNAKE_CASE ) generate(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) return res if __name__ == "__main__": _lowerCamelCase = input("""Enter numbers separated by a comma:\n""").strip() _lowerCamelCase = [int(item) for item in user_input.split(""",""")] print(heaps(arr))
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise TypeError("Input value must be an 'int' type" ) UpperCAmelCase_ : Union[str, Any] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
'''simple docstring''' from math import factorial def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k" ) return factorial(_SCREAMING_SNAKE_CASE ) // (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) if __name__ == "__main__": print( """The number of five-card hands possible from a standard""", f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( """If a class of 40 students must be arranged into groups of""", f"""4 for group projects, there are {combinations(40, 4)} ways""", """to arrange them.\n""", ) print( """If 10 teams are competing in a Formula One race, there""", f"""are {combinations(10, 3)} ways that first, second and""", """third place can be awarded.""", )
71
1
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _snake_case : def __init__( self ,_snake_case ,_snake_case=12 ,_snake_case=7 ,_snake_case=True ,_snake_case=True ,_snake_case=True ,_snake_case=99 ,_snake_case=32 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=5_12 ,_snake_case=0.02 ,_snake_case=0 ,_snake_case=None ,): UpperCAmelCase_ : int = parent UpperCAmelCase_ : Optional[Any] = batch_size UpperCAmelCase_ : Tuple = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Dict = use_input_mask UpperCAmelCase_ : List[str] = use_labels UpperCAmelCase_ : int = vocab_size UpperCAmelCase_ : int = hidden_size UpperCAmelCase_ : Any = projection_dim UpperCAmelCase_ : Dict = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Optional[Any] = intermediate_size UpperCAmelCase_ : Optional[Any] = dropout UpperCAmelCase_ : Any = attention_dropout UpperCAmelCase_ : List[str] = max_position_embeddings UpperCAmelCase_ : Dict = initializer_range UpperCAmelCase_ : Tuple = scope UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = None if self.use_input_mask: UpperCAmelCase_ : str = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase_ : Tuple = input_mask.numpy() UpperCAmelCase_ , UpperCAmelCase_ : str = input_mask.shape UpperCAmelCase_ : Union[str, Any] = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): UpperCAmelCase_ : Union[str, Any] = 1 UpperCAmelCase_ : Optional[Any] = 0 UpperCAmelCase_ : int = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def UpperCamelCase__ ( self ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlipTextModel(config=_snake_case ) UpperCAmelCase_ : Tuple = model(_snake_case ,attention_mask=_snake_case ,training=_snake_case ) UpperCAmelCase_ : int = model(_snake_case ,training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Tuple = config_and_inputs UpperCAmelCase_ : List[str] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[Any] =(TFBlipTextModel,) if is_tf_available() else () __A : Dict =False __A : Optional[Any] =False __A : Union[str, Any] =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = BlipTextModelTester(self ) UpperCAmelCase_ : Dict = ConfigTester(self ,config_class=_snake_case ,hidden_size=37 ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Blip does not use inputs_embeds" ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING" ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING" ) def UpperCamelCase__ ( self ): pass @slow def UpperCamelCase__ ( self ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Any = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def UpperCamelCase__ ( self ,_snake_case=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
71
'''simple docstring''' import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =VideoToVideoSDPipeline __A : Tuple =TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"} __A : Union[str, Any] =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"} __A : str =PipelineTesterMixin.required_optional_params - {"latents"} __A : Dict =False # No `output_type`. __A : Optional[int] =frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") ,cross_attention_dim=32 ,attention_head_dim=4 ,) UpperCAmelCase_ : int = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=1_28 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=10_00 ,hidden_act="gelu" ,projection_dim=5_12 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): # 3 frames UpperCAmelCase_ : Dict = floats_tensor((1, 3, 3, 32, 32) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Tuple = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : Tuple = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : str = VideoToVideoSDPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = "np" UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ).frames UpperCAmelCase_ : Tuple = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) UpperCAmelCase_ : Dict = np.array([1_06, 1_17, 1_13, 1_74, 1_37, 1_12, 1_48, 1_51, 1_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ,expected_max_diff=5E-3 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): return super().test_progress_bar() @slow @skip_mps class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" ,torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames UpperCAmelCase_ : str = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ : int = torch.randn((1, 10, 3, 10_24, 5_76) ,generator=_snake_case ) UpperCAmelCase_ : List[Any] = video.to("cuda" ) UpperCAmelCase_ : List[Any] = "Spiderman is surfing" UpperCAmelCase_ : Optional[Any] = pipe(_snake_case ,video=_snake_case ,generator=_snake_case ,num_inference_steps=3 ,output_type="pt" ).frames UpperCAmelCase_ : Any = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
71
1
'''simple docstring''' import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html _lowerCamelCase = """platform""" import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : int=None , _SCREAMING_SNAKE_CASE : List[Any]=None , _SCREAMING_SNAKE_CASE : List[Any]=None , _SCREAMING_SNAKE_CASE : Optional[Any]=None , _SCREAMING_SNAKE_CASE : Union[str, Any]=None , _SCREAMING_SNAKE_CASE : List[str]=None , ) -> Optional[Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: UpperCAmelCase_ : Dict = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: UpperCAmelCase_ : Union[str, Any] = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : List[Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : Optional[Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class _snake_case : def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=16 ,_snake_case=2 ,_snake_case=4 ,_snake_case=4 ,_snake_case="gelu" ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=32 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,_snake_case=0.02 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : Union[str, Any] = batch_size UpperCAmelCase_ : Union[str, Any] = seq_length UpperCAmelCase_ : Union[str, Any] = is_training UpperCAmelCase_ : Optional[int] = use_labels UpperCAmelCase_ : Dict = vocab_size UpperCAmelCase_ : Union[str, Any] = hidden_size UpperCAmelCase_ : Dict = num_hidden_layers UpperCAmelCase_ : Optional[int] = num_attention_heads UpperCAmelCase_ : List[str] = intermediate_size UpperCAmelCase_ : str = hidden_act UpperCAmelCase_ : Dict = hidden_dropout_prob UpperCAmelCase_ : Union[str, Any] = attention_probs_dropout_prob UpperCAmelCase_ : List[str] = max_position_embeddings UpperCAmelCase_ : int = eos_token_id UpperCAmelCase_ : Dict = pad_token_id UpperCAmelCase_ : int = bos_token_id UpperCAmelCase_ : List[str] = initializer_range def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) ,3 ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) ,dtype=np.intaa )) ,-1 ) UpperCAmelCase_ : str = shift_tokens_right(_snake_case ,1 ,2 ) UpperCAmelCase_ : int = BlenderbotSmallConfig( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_id=self.eos_token_id ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,initializer_range=self.initializer_range ,use_cache=_snake_case ,) UpperCAmelCase_ : List[Any] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ : Tuple = self.prepare_config_and_inputs() return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : Optional[int] = 20 UpperCAmelCase_ : Tuple = model_class_name(_snake_case ) UpperCAmelCase_ : str = model.encode(inputs_dict["input_ids"] ) UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) UpperCAmelCase_ : Tuple = model.init_cache(decoder_input_ids.shape[0] ,_snake_case ,_snake_case ) UpperCAmelCase_ : str = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) ,dtype="i4" ) UpperCAmelCase_ : int = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) UpperCAmelCase_ : Tuple = model.decode( decoder_input_ids[:, :-1] ,_snake_case ,decoder_attention_mask=_snake_case ,past_key_values=_snake_case ,decoder_position_ids=_snake_case ,) UpperCAmelCase_ : str = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype="i4" ) UpperCAmelCase_ : List[str] = model.decode( decoder_input_ids[:, -1:] ,_snake_case ,decoder_attention_mask=_snake_case ,past_key_values=outputs_cache.past_key_values ,decoder_position_ids=_snake_case ,) UpperCAmelCase_ : List[Any] = model.decode(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=f'''Max diff is {diff}''' ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = 20 UpperCAmelCase_ : int = model_class_name(_snake_case ) UpperCAmelCase_ : Dict = model.encode(inputs_dict["input_ids"] ) UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) UpperCAmelCase_ : Any = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] ,axis=-1 ,) UpperCAmelCase_ : List[str] = model.init_cache(decoder_input_ids.shape[0] ,_snake_case ,_snake_case ) UpperCAmelCase_ : Any = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) UpperCAmelCase_ : Dict = model.decode( decoder_input_ids[:, :-1] ,_snake_case ,decoder_attention_mask=_snake_case ,past_key_values=_snake_case ,decoder_position_ids=_snake_case ,) UpperCAmelCase_ : str = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype="i4" ) UpperCAmelCase_ : Optional[int] = model.decode( decoder_input_ids[:, -1:] ,_snake_case ,past_key_values=outputs_cache.past_key_values ,decoder_attention_mask=_snake_case ,decoder_position_ids=_snake_case ,) UpperCAmelCase_ : str = model.decode(_snake_case ,_snake_case ,decoder_attention_mask=_snake_case ) UpperCAmelCase_ : Tuple = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=f'''Max diff is {diff}''' ) @require_flax class _snake_case (unittest.TestCase): __A : Optional[int] =99 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] ,dtype=np.intaa ,) UpperCAmelCase_ : List[Any] = input_ids.shape[0] UpperCAmelCase_ : Dict = BlenderbotSmallConfig( vocab_size=self.vocab_size ,d_model=24 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=32 ,decoder_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 def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self._get_config_and_data() UpperCAmelCase_ : List[Any] = FlaxBlenderbotSmallForConditionalGeneration(_snake_case ) UpperCAmelCase_ : int = lm_model(input_ids=_snake_case ) UpperCAmelCase_ : List[str] = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["logits"].shape ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size ,d_model=14 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=8 ,decoder_ffn_dim=8 ,max_position_embeddings=48 ,) UpperCAmelCase_ : List[Any] = FlaxBlenderbotSmallForConditionalGeneration(_snake_case ) UpperCAmelCase_ : str = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] ,dtype=np.intaa ) UpperCAmelCase_ : Union[str, Any] = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] ,dtype=np.intaa ) UpperCAmelCase_ : List[str] = lm_model(input_ids=_snake_case ,decoder_input_ids=_snake_case ) UpperCAmelCase_ : Optional[Any] = (*summary.shape, config.vocab_size) self.assertEqual(outputs["logits"].shape ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] ,dtype=np.intaa ) UpperCAmelCase_ : Optional[Any] = shift_tokens_right(_snake_case ,1 ,2 ) UpperCAmelCase_ : Optional[Any] = np.equal(_snake_case ,1 ).astype(np.floataa ).sum() UpperCAmelCase_ : Tuple = np.equal(_snake_case ,1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape ,input_ids.shape ) self.assertEqual(_snake_case ,n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] ,2 ).all() ) @require_flax class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase , __SCREAMING_SNAKE_CASE): __A : int =True __A : Union[str, Any] =( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) __A : Tuple =(FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def UpperCamelCase__ ( self ): UpperCAmelCase_ : Union[str, Any] = FlaxBlenderbotSmallModelTester(self ) def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(_snake_case ,_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(_snake_case ,_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase_ : Tuple = self._prepare_for_class(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = model_class(_snake_case ) @jax.jit def encode_jitted(_snake_case ,_snake_case=None ,**_snake_case ): return model.encode(input_ids=_snake_case ,attention_mask=_snake_case ) with self.subTest("JIT Enabled" ): UpperCAmelCase_ : Optional[int] = encode_jitted(**_snake_case ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCAmelCase_ : List[str] = encode_jitted(**_snake_case ).to_tuple() self.assertEqual(len(_snake_case ) ,len(_snake_case ) ) for jitted_output, output in zip(_snake_case ,_snake_case ): self.assertEqual(jitted_output.shape ,output.shape ) def UpperCamelCase__ ( self ): UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase_ : Optional[int] = model_class(_snake_case ) UpperCAmelCase_ : Tuple = model.encode(inputs_dict["input_ids"] ,inputs_dict["attention_mask"] ) UpperCAmelCase_ : Tuple = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(_snake_case ,_snake_case ,_snake_case ): return model.decode( decoder_input_ids=_snake_case ,decoder_attention_mask=_snake_case ,encoder_outputs=_snake_case ,) with self.subTest("JIT Enabled" ): UpperCAmelCase_ : Union[str, Any] = decode_jitted(**_snake_case ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCAmelCase_ : int = decode_jitted(**_snake_case ).to_tuple() self.assertEqual(len(_snake_case ) ,len(_snake_case ) ) for jitted_output, output in zip(_snake_case ,_snake_case ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCamelCase__ ( self ): for model_class_name in self.all_model_classes: UpperCAmelCase_ : Dict = model_class_name.from_pretrained("facebook/blenderbot_small-90M" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids UpperCAmelCase_ : Any = np.ones((1, 1) ) * model.config.eos_token_id UpperCAmelCase_ : Optional[int] = model(_snake_case ) self.assertIsNotNone(_snake_case )
71
'''simple docstring''' import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) _lowerCamelCase = pytest.mark.integration @pytest.mark.parametrize("path" , ["paws", "csv"] ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : str ) -> Tuple: """simple docstring""" inspect_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.parametrize("path" , ["accuracy"] ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: """simple docstring""" inspect_metric(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, config_name, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict ) -> str: """simple docstring""" UpperCAmelCase_ : int = get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : List[str] ) -> str: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, expected" , [ ("squad", "plain_text"), ("acronym_identification", "default"), ("lhoestq/squad", "plain_text"), ("lhoestq/test", "default"), ("lhoestq/demo1", "lhoestq--demo1"), ("dalle-mini/wit", "dalle-mini--wit"), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_config_names(_SCREAMING_SNAKE_CASE ) assert expected in config_names @pytest.mark.parametrize( "path, expected_configs, expected_splits_in_first_config" , [ ("squad", ["plain_text"], ["train", "validation"]), ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]), ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str] ) -> Any: """simple docstring""" UpperCAmelCase_ : Any = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert list(infos.keys() ) == expected_configs UpperCAmelCase_ : Optional[Any] = expected_configs[0] assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( "path, expected_config, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ) -> Any: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : str ) -> Any: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_split_names(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE )
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if index == number_of_items: return 0 UpperCAmelCase_ : Optional[int] = 0 UpperCAmelCase_ : Optional[Any] = 0 UpperCAmelCase_ : List[str] = knapsack(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index + 1 ) if weights[index] <= max_weight: UpperCAmelCase_ : Tuple = values[index] + knapsack( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , max_weight - weights[index] , index + 1 ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Any =AudioLDMPipeline __A : Dict =TEXT_TO_AUDIO_PARAMS __A : Any =TEXT_TO_AUDIO_BATCH_PARAMS __A : Tuple =frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = 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, 64) ,class_embed_type="simple_projection" ,projection_class_embeddings_input_dim=32 ,class_embeddings_concat=_snake_case ,) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=1 ,out_channels=1 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = ClapTextConfig( 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=10_00 ,projection_dim=32 ,) UpperCAmelCase_ : Optional[Any] = ClapTextModelWithProjection(_snake_case ) UpperCAmelCase_ : List[Any] = RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,model_max_length=77 ) UpperCAmelCase_ : Optional[int] = SpeechTaHifiGanConfig( model_in_dim=8 ,sampling_rate=1_60_00 ,upsample_initial_channel=16 ,upsample_rates=[2, 2] ,upsample_kernel_sizes=[4, 4] ,resblock_kernel_sizes=[3, 7] ,resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] ,normalize_before=_snake_case ,) UpperCAmelCase_ : Union[str, Any] = SpeechTaHifiGan(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "vocoder": vocoder, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Optional[int] = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[str] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Any = { "prompt": "A hammer hitting a wooden surface", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : str = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : Any = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : int = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Dict = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Tuple = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[str] = output.audios[0] UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : str = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : Dict = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : Optional[Any] = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Tuple = F.normalize(_snake_case ,dim=-1 ) UpperCAmelCase_ : int = prompt_embeds # forward UpperCAmelCase_ : int = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = 3 * ["this is a negative prompt"] UpperCAmelCase_ : Any = negative_prompt UpperCAmelCase_ : Union[str, Any] = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Dict = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : List[Any] = [] for p in [prompt, negative_prompt]: UpperCAmelCase_ : Any = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : List[Any] = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : List[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Any = F.normalize(_snake_case ,dim=-1 ) embeds.append(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = embeds # forward UpperCAmelCase_ : Tuple = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Any = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : int = "egg cracking" UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : int = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : List[Any] = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[str] = self.get_dummy_components() UpperCAmelCase_ : Dict = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Any = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Dict = "A hammer hitting a wooden surface" # test num_waveforms_per_prompt=1 (default) UpperCAmelCase_ : Any = audioldm_pipe(_snake_case ,num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : Dict = audioldm_pipe([prompt] * batch_size ,num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : List[Any] = audioldm_pipe(_snake_case ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = audioldm_pipe( [prompt] * batch_size ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Union[str, Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe.vocoder.config.sampling_rate UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = audioldm_pipe(audio_length_in_s=0.016 ,**_snake_case ) UpperCAmelCase_ : str = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.016 UpperCAmelCase_ : List[Any] = audioldm_pipe(audio_length_in_s=0.032 ,**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.032 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : str = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : int = ["hey"] UpperCAmelCase_ : Dict = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : Any = output.audios.shape assert audio_shape == (1, 2_56) UpperCAmelCase_ : Tuple = audioldm_pipe.vocoder.config config.model_in_dim *= 2 UpperCAmelCase_ : List[Any] = SpeechTaHifiGan(_snake_case ).to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : int = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def UpperCamelCase__ ( self ): self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case ) def UpperCamelCase__ ( self ): self._test_inference_batch_single_identical(test_mean_pixel_difference=_snake_case ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ) @slow class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case ,_snake_case="cpu" ,_snake_case=torch.floataa ,_snake_case=0 ): UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : str = np.random.RandomState(_snake_case ).standard_normal((1, 8, 1_28, 16) ) UpperCAmelCase_ : Optional[Any] = torch.from_numpy(_snake_case ).to(device=_snake_case ,dtype=_snake_case ) UpperCAmelCase_ : List[str] = { "prompt": "A hammer hitting a wooden surface", "latents": latents, "generator": generator, "num_inference_steps": 3, "guidance_scale": 2.5, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : Optional[int] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[Any] = self.get_inputs(_snake_case ) UpperCAmelCase_ : List[Any] = 25 UpperCAmelCase_ : Union[str, Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Union[str, Any] = audio[7_72_30:7_72_40] UpperCAmelCase_ : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) UpperCAmelCase_ : Dict = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : List[Any] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Any = audio[2_77_80:2_77_90] UpperCAmelCase_ : List[str] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) UpperCAmelCase_ : Union[str, Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
71
1
'''simple docstring''' import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoImageProcessor, ViTImageProcessor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / """utils""")) from test_module.custom_image_processing import CustomImageProcessor # noqa E402 _lowerCamelCase = get_tests_dir("""fixtures""") class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): # A mock response for an HTTP head request to emulate server down UpperCAmelCase_ : List[Any] = mock.Mock() UpperCAmelCase_ : List[str] = 5_00 UpperCAmelCase_ : List[str] = {} UpperCAmelCase_ : Any = HTTPError UpperCAmelCase_ : Optional[Any] = {} # Download this model to make sure it's in the cache. UpperCAmelCase_ : Tuple = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" ,return_value=_snake_case ) as mock_head: UpperCAmelCase_ : Optional[int] = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit" ) # This check we did call the fake head request mock_head.assert_called() def UpperCamelCase__ ( self ): # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase_ : Tuple = ViTImageProcessor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json" ) def UpperCamelCase__ ( self ): with self.assertRaises(_snake_case ): # config is in subfolder, the following should not work without specifying the subfolder UpperCAmelCase_ : Optional[int] = AutoImageProcessor.from_pretrained("hf-internal-testing/stable-diffusion-all-variants" ) UpperCAmelCase_ : Dict = AutoImageProcessor.from_pretrained( "hf-internal-testing/stable-diffusion-all-variants" ,subfolder="feature_extractor" ) self.assertIsNotNone(_snake_case ) @is_staging_test class _snake_case (unittest.TestCase): @classmethod def UpperCamelCase__ ( cls ): UpperCAmelCase_ : Union[str, Any] = TOKEN HfFolder.save_token(_snake_case ) @classmethod def UpperCamelCase__ ( cls ): try: delete_repo(token=cls._token ,repo_id="test-image-processor" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="valid_org/test-image-processor-org" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="test-dynamic-image-processor" ) except HTTPError: pass def UpperCamelCase__ ( self ): UpperCAmelCase_ : Union[str, Any] = ViTImageProcessor.from_pretrained(_snake_case ) image_processor.push_to_hub("test-image-processor" ,use_auth_token=self._token ) UpperCAmelCase_ : Tuple = ViTImageProcessor.from_pretrained(f'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(_snake_case ,getattr(_snake_case ,_snake_case ) ) # Reset repo delete_repo(token=self._token ,repo_id="test-image-processor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( _snake_case ,repo_id="test-image-processor" ,push_to_hub=_snake_case ,use_auth_token=self._token ) UpperCAmelCase_ : Any = ViTImageProcessor.from_pretrained(f'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(_snake_case ,getattr(_snake_case ,_snake_case ) ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = ViTImageProcessor.from_pretrained(_snake_case ) image_processor.push_to_hub("valid_org/test-image-processor" ,use_auth_token=self._token ) UpperCAmelCase_ : Dict = ViTImageProcessor.from_pretrained("valid_org/test-image-processor" ) for k, v in image_processor.__dict__.items(): self.assertEqual(_snake_case ,getattr(_snake_case ,_snake_case ) ) # Reset repo delete_repo(token=self._token ,repo_id="valid_org/test-image-processor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( _snake_case ,repo_id="valid_org/test-image-processor-org" ,push_to_hub=_snake_case ,use_auth_token=self._token ) UpperCAmelCase_ : List[Any] = ViTImageProcessor.from_pretrained("valid_org/test-image-processor-org" ) for k, v in image_processor.__dict__.items(): self.assertEqual(_snake_case ,getattr(_snake_case ,_snake_case ) ) def UpperCamelCase__ ( self ): CustomImageProcessor.register_for_auto_class() UpperCAmelCase_ : Tuple = CustomImageProcessor.from_pretrained(_snake_case ) image_processor.push_to_hub("test-dynamic-image-processor" ,use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( image_processor.auto_map ,{"AutoImageProcessor": "custom_image_processing.CustomImageProcessor"} ,) UpperCAmelCase_ : int = AutoImageProcessor.from_pretrained( f'''{USER}/test-dynamic-image-processor''' ,trust_remote_code=_snake_case ) # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module self.assertEqual(new_image_processor.__class__.__name__ ,"CustomImageProcessor" )
71
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable _lowerCamelCase = { """configuration_gpt_neox_japanese""": ["""GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXJapaneseConfig"""], """tokenization_gpt_neox_japanese""": ["""GPTNeoXJapaneseTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXJapaneseForCausalLM""", """GPTNeoXJapaneseLayer""", """GPTNeoXJapaneseModel""", """GPTNeoXJapanesePreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
1
'''simple docstring''' import heapq def a__ ( _SCREAMING_SNAKE_CASE : dict ) -> set[int]: """simple docstring""" UpperCAmelCase_ : list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(_SCREAMING_SNAKE_CASE , [-1 * len(_SCREAMING_SNAKE_CASE ), (key, value)] ) # chosen_vertices = set of chosen vertices UpperCAmelCase_ : Optional[int] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices UpperCAmelCase_ : Tuple = heapq.heappop(_SCREAMING_SNAKE_CASE )[1][0] chosen_vertices.add(_SCREAMING_SNAKE_CASE ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: UpperCAmelCase_ : Any = elem[1][1].index(_SCREAMING_SNAKE_CASE ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(_SCREAMING_SNAKE_CASE ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(f"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
71
'''simple docstring''' import heapq def a__ ( _SCREAMING_SNAKE_CASE : dict ) -> set[int]: """simple docstring""" UpperCAmelCase_ : list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(_SCREAMING_SNAKE_CASE , [-1 * len(_SCREAMING_SNAKE_CASE ), (key, value)] ) # chosen_vertices = set of chosen vertices UpperCAmelCase_ : Optional[int] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices UpperCAmelCase_ : Tuple = heapq.heappop(_SCREAMING_SNAKE_CASE )[1][0] chosen_vertices.add(_SCREAMING_SNAKE_CASE ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: UpperCAmelCase_ : Any = elem[1][1].index(_SCREAMING_SNAKE_CASE ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(_SCREAMING_SNAKE_CASE ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(f"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
71
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) _lowerCamelCase = { """configuration_perceiver""": ["""PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PerceiverConfig""", """PerceiverOnnxConfig"""], """tokenization_perceiver""": ["""PerceiverTokenizer"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = ["""PerceiverFeatureExtractor"""] _lowerCamelCase = ["""PerceiverImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST""", """PerceiverForImageClassificationConvProcessing""", """PerceiverForImageClassificationFourier""", """PerceiverForImageClassificationLearned""", """PerceiverForMaskedLM""", """PerceiverForMultimodalAutoencoding""", """PerceiverForOpticalFlow""", """PerceiverForSequenceClassification""", """PerceiverLayer""", """PerceiverModel""", """PerceiverPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(_SCREAMING_SNAKE_CASE ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =["pixel_values"] def __init__( self ,_snake_case = True ,_snake_case = None ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = True ,_snake_case = None ,_snake_case = True ,_snake_case = 1 / 2_55 ,_snake_case = True ,_snake_case = True ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Optional[Any] = size if size is not None else {"shortest_edge": 2_56} UpperCAmelCase_ : List[str] = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : str = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} UpperCAmelCase_ : Optional[Any] = get_size_dict(_snake_case ,param_name="crop_size" ) UpperCAmelCase_ : int = do_resize UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Dict = do_center_crop UpperCAmelCase_ : Optional[Any] = crop_size UpperCAmelCase_ : Optional[Any] = resample UpperCAmelCase_ : int = do_rescale UpperCAmelCase_ : Optional[int] = rescale_factor UpperCAmelCase_ : Dict = offset UpperCAmelCase_ : Optional[Any] = do_normalize UpperCAmelCase_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Any = get_size_dict(_snake_case ,default_to_square=_snake_case ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(_snake_case ,size["shortest_edge"] ,default_to_square=_snake_case ) elif "height" in size and "width" in size: UpperCAmelCase_ : Optional[Any] = (size["height"], size["width"]) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(_snake_case ,size=_snake_case ,resample=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Dict = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_snake_case ,size=(size["height"], size["width"]) ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = True ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = image.astype(np.floataa ) if offset: UpperCAmelCase_ : Any = image - (scale / 2) return rescale(_snake_case ,scale=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): return normalize(_snake_case ,mean=_snake_case ,std=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. UpperCAmelCase_ : Optional[int] = to_numpy_array(_snake_case ) if do_resize: UpperCAmelCase_ : Dict = self.resize(image=_snake_case ,size=_snake_case ,resample=_snake_case ) if do_center_crop: UpperCAmelCase_ : Optional[Any] = self.center_crop(_snake_case ,size=_snake_case ) if do_rescale: UpperCAmelCase_ : Union[str, Any] = self.rescale(image=_snake_case ,scale=_snake_case ,offset=_snake_case ) if do_normalize: UpperCAmelCase_ : Any = self.normalize(image=_snake_case ,mean=_snake_case ,std=_snake_case ) UpperCAmelCase_ : Any = to_channel_dimension_format(_snake_case ,_snake_case ) return image def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,**_snake_case ,): UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : str = resample if resample is not None else self.resample UpperCAmelCase_ : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : Tuple = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : List[Any] = offset if offset is not None else self.offset UpperCAmelCase_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : int = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : int = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : int = get_size_dict(_snake_case ,param_name="crop_size" ) if not valid_images(_snake_case ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) UpperCAmelCase_ : Any = make_batched(_snake_case ) UpperCAmelCase_ : Dict = [ [ self._preprocess_image( image=_snake_case ,do_resize=_snake_case ,size=_snake_case ,resample=_snake_case ,do_center_crop=_snake_case ,crop_size=_snake_case ,do_rescale=_snake_case ,rescale_factor=_snake_case ,offset=_snake_case ,do_normalize=_snake_case ,image_mean=_snake_case ,image_std=_snake_case ,data_format=_snake_case ,) for img in video ] for video in videos ] UpperCAmelCase_ : List[str] = {"pixel_values": videos} return BatchFeature(data=_snake_case ,tensor_type=_snake_case )
71
1
'''simple docstring''' from torch import nn def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> str: """simple docstring""" if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(F'''Unsupported activation function: {act_fn}''' )
71
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__( _snake_case ,split=_snake_case ,features=_snake_case ,cache_dir=_snake_case ,keep_in_memory=_snake_case ,streaming=_snake_case ,num_proc=_snake_case ,**_snake_case ,) UpperCAmelCase_ : Tuple = field UpperCAmelCase_ : List[Any] = path_or_paths if isinstance(_snake_case ,_snake_case ) else {self.split: path_or_paths} UpperCAmelCase_ : Optional[int] = Json( cache_dir=_snake_case ,data_files=_snake_case ,features=_snake_case ,field=_snake_case ,**_snake_case ,) def UpperCamelCase__ ( self ): # Build iterable dataset if self.streaming: UpperCAmelCase_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase_ : Union[str, Any] = None UpperCAmelCase_ : int = None UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : int = None self.builder.download_and_prepare( download_config=_snake_case ,download_mode=_snake_case ,verification_mode=_snake_case ,base_path=_snake_case ,num_proc=self.num_proc ,) UpperCAmelCase_ : Dict = self.builder.as_dataset( split=self.split ,verification_mode=_snake_case ,in_memory=self.keep_in_memory ) return dataset class _snake_case : def __init__( self ,_snake_case ,_snake_case ,_snake_case = None ,_snake_case = None ,**_snake_case ,): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Union[str, Any] = path_or_buf UpperCAmelCase_ : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase_ : Dict = num_proc UpperCAmelCase_ : Optional[Any] = "utf-8" UpperCAmelCase_ : Optional[int] = to_json_kwargs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.to_json_kwargs.pop("path_or_buf" ,_snake_case ) UpperCAmelCase_ : Tuple = self.to_json_kwargs.pop("orient" ,"records" ) UpperCAmelCase_ : Any = self.to_json_kwargs.pop("lines" ,True if orient == "records" else False ) UpperCAmelCase_ : Optional[int] = self.to_json_kwargs.pop("index" ,False if orient in ["split", "table"] else True ) UpperCAmelCase_ : int = self.to_json_kwargs.pop("compression" ,_snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"wb" ,compression=_snake_case ) as buffer: UpperCAmelCase_ : List[str] = self._write(file_obj=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' " was passed. Please provide a local path instead." ) UpperCAmelCase_ : Union[str, Any] = self._write( file_obj=self.path_or_buf ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) return written def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = args UpperCAmelCase_ : List[str] = query_table( table=self.dataset.data ,key=slice(_snake_case ,offset + self.batch_size ) ,indices=self.dataset._indices ,) UpperCAmelCase_ : Optional[Any] = batch.to_pandas().to_json( path_or_buf=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**_snake_case ) if not json_str.endswith("\n" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,**_snake_case ,): UpperCAmelCase_ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): UpperCAmelCase_ : Any = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: UpperCAmelCase_ , UpperCAmelCase_ : int = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,_snake_case ,_snake_case )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): written += file_obj.write(_snake_case ) return written
71
1
'''simple docstring''' import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: # picklable for multiprocessing """simple docstring""" return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def a__ ( ) -> int: """simple docstring""" with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" UpperCAmelCase_ : List[str] = [1, 2, 3] with pytest.raises(_SCREAMING_SNAKE_CASE ): with parallel_backend("unsupported backend" ): map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=2 ) with pytest.raises(_SCREAMING_SNAKE_CASE ): with parallel_backend("unsupported backend" ): map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def a__ ( _SCREAMING_SNAKE_CASE : str ) -> str: """simple docstring""" UpperCAmelCase_ : Optional[int] = [1, 2] UpperCAmelCase_ : str = {"a": 1, "b": 2} UpperCAmelCase_ : List[Any] = {"a": [1, 2], "b": [3, 4]} UpperCAmelCase_ : Optional[Any] = {"a": {"1": 1}, "b": 2} UpperCAmelCase_ : Optional[int] = {"a": 1, "b": 2, "c": 3, "d": 4} UpperCAmelCase_ : str = [2, 3] UpperCAmelCase_ : Union[str, Any] = {"a": 2, "b": 3} UpperCAmelCase_ : int = {"a": [2, 3], "b": [4, 5]} UpperCAmelCase_ : Dict = {"a": {"1": 2}, "b": 3} UpperCAmelCase_ : Any = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE ) == expected_map_nested_sa assert map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE ) == expected_map_nested_sa assert map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE ) == expected_map_nested_sa assert map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE ) == expected_map_nested_sa assert map_nested(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE ) == expected_map_nested_sa
71
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Dict =StableDiffusionPanoramaPipeline __A : Tuple =TEXT_TO_IMAGE_PARAMS __A : Dict =TEXT_TO_IMAGE_BATCH_PARAMS __A : Union[str, Any] =TEXT_TO_IMAGE_IMAGE_PARAMS __A : List[Any] =TEXT_TO_IMAGE_IMAGE_PARAMS def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Any = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=1 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") ,up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") ,cross_attention_dim=32 ,) UpperCAmelCase_ : Union[str, Any] = DDIMScheduler() torch.manual_seed(0 ) UpperCAmelCase_ : List[Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : List[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=10_00 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : Optional[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Dict = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): UpperCAmelCase_ : int = torch.manual_seed(_snake_case ) UpperCAmelCase_ : Optional[Any] = { "prompt": "a photo of the dolomites", "generator": generator, # Setting height and width to None to prevent OOMs on CPU. "height": None, "width": None, "num_inference_steps": 1, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = StableDiffusionPanoramaPipeline(**_snake_case ) UpperCAmelCase_ : Dict = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = sd_pipe(**_snake_case ).images UpperCAmelCase_ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : List[Any] = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def UpperCamelCase__ ( self ): super().test_inference_batch_single_identical(batch_size=2 ,expected_max_diff=3.25E-3 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Union[str, Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = StableDiffusionPanoramaPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = "french fries" UpperCAmelCase_ : Tuple = sd_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : Optional[int] = output.images UpperCAmelCase_ : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : Dict = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Any = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = StableDiffusionPanoramaPipeline(**_snake_case ) UpperCAmelCase_ : Optional[Any] = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ,view_batch_size=2 ) UpperCAmelCase_ : Optional[int] = output.images UpperCAmelCase_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : Any = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : List[Any] = EulerAncestralDiscreteScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ) UpperCAmelCase_ : str = StableDiffusionPanoramaPipeline(**_snake_case ) UpperCAmelCase_ : Dict = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Union[str, Any] = sd_pipe(**_snake_case ).images UpperCAmelCase_ : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : Union[str, Any] = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = PNDMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,skip_prk_steps=_snake_case ) UpperCAmelCase_ : Tuple = StableDiffusionPanoramaPipeline(**_snake_case ) UpperCAmelCase_ : Optional[int] = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : str = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = sd_pipe(**_snake_case ).images UpperCAmelCase_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : Optional[Any] = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case=0 ): UpperCAmelCase_ : Any = torch.manual_seed(_snake_case ) UpperCAmelCase_ : List[Any] = { "prompt": "a photo of the dolomites", "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "stabilityai/stable-diffusion-2-base" UpperCAmelCase_ : int = DDIMScheduler.from_pretrained(_snake_case ,subfolder="scheduler" ) UpperCAmelCase_ : Optional[Any] = StableDiffusionPanoramaPipeline.from_pretrained(_snake_case ,scheduler=_snake_case ,safety_checker=_snake_case ) pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) pipe.enable_attention_slicing() UpperCAmelCase_ : Any = self.get_inputs() UpperCAmelCase_ : List[str] = pipe(**_snake_case ).images UpperCAmelCase_ : Optional[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) UpperCAmelCase_ : Optional[int] = np.array( [ 0.36968392, 0.27025372, 0.32446766, 0.28379387, 0.36363274, 0.30733347, 0.27100027, 0.27054125, 0.25536096, ] ) assert np.abs(expected_slice - image_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = StableDiffusionPanoramaPipeline.from_pretrained( "stabilityai/stable-diffusion-2-base" ,safety_checker=_snake_case ) UpperCAmelCase_ : List[str] = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) pipe.enable_attention_slicing() UpperCAmelCase_ : str = self.get_inputs() UpperCAmelCase_ : Optional[Any] = pipe(**_snake_case ).images UpperCAmelCase_ : int = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) UpperCAmelCase_ : str = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = 0 def callback_fn(_snake_case ,_snake_case ,_snake_case ) -> None: UpperCAmelCase_ : Tuple = True nonlocal number_of_steps number_of_steps += 1 if step == 1: UpperCAmelCase_ : List[str] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) UpperCAmelCase_ : Tuple = latents[0, -3:, -3:, -1] UpperCAmelCase_ : Optional[int] = np.array( [ 0.18681869, 0.33907816, 0.5361276, 0.14432865, -0.02856611, -0.73941123, 0.23397987, 0.47322682, -0.37823164, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: UpperCAmelCase_ : List[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) UpperCAmelCase_ : Optional[int] = latents[0, -3:, -3:, -1] UpperCAmelCase_ : Tuple = np.array( [ 0.18539645, 0.33987248, 0.5378559, 0.14437142, -0.02455261, -0.7338317, 0.23990755, 0.47356272, -0.3786505, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 UpperCAmelCase_ : int = False UpperCAmelCase_ : List[Any] = "stabilityai/stable-diffusion-2-base" UpperCAmelCase_ : Optional[Any] = DDIMScheduler.from_pretrained(_snake_case ,subfolder="scheduler" ) UpperCAmelCase_ : Any = StableDiffusionPanoramaPipeline.from_pretrained(_snake_case ,scheduler=_snake_case ,safety_checker=_snake_case ) UpperCAmelCase_ : int = pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) pipe.enable_attention_slicing() UpperCAmelCase_ : Optional[int] = self.get_inputs() pipe(**_snake_case ,callback=_snake_case ,callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def UpperCamelCase__ ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() UpperCAmelCase_ : Dict = "stabilityai/stable-diffusion-2-base" UpperCAmelCase_ : Union[str, Any] = DDIMScheduler.from_pretrained(_snake_case ,subfolder="scheduler" ) UpperCAmelCase_ : Dict = StableDiffusionPanoramaPipeline.from_pretrained(_snake_case ,scheduler=_snake_case ,safety_checker=_snake_case ) UpperCAmelCase_ : Tuple = pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() UpperCAmelCase_ : Union[str, Any] = self.get_inputs() UpperCAmelCase_ : Dict = pipe(**_snake_case ) UpperCAmelCase_ : Tuple = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
1
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput _lowerCamelCase = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,*_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,**_snake_case ): super().__init__(*_snake_case ,**_snake_case ) UpperCAmelCase_ : int = eval_examples UpperCAmelCase_ : Optional[int] = post_process_function UpperCAmelCase_ : int = quant_trainer_args UpperCAmelCase_ : Any = 1_28 # default number of calibration samples def UpperCamelCase__ ( self ,_snake_case=None ): if calib_dataset is None and self.calib_dataset is None: raise ValueError("Trainer: calibration requires an calib_dataset." ) UpperCAmelCase_ : Union[str, Any] = calib_dataset if calib_dataset is not None else self.calib_dataset UpperCAmelCase_ : Dict = self._remove_unused_columns(_snake_case ,description="Calibration" ) return DataLoader( _snake_case ,batch_size=self.args.eval_batch_size ,collate_fn=self.data_collator ,drop_last=self.args.dataloader_drop_last ,num_workers=self.args.dataloader_num_workers ,pin_memory=self.args.dataloader_pin_memory ,shuffle=_snake_case ,) def UpperCamelCase__ ( self ,_snake_case=None ): UpperCAmelCase_ : Any = self.train_dataset if calib_dataset is None else calib_dataset UpperCAmelCase_ : Any = self.get_calib_dataloader(_snake_case ) UpperCAmelCase_ : int = self.model quant_trainer.configure_model(_snake_case ,self.quant_trainer_args ,calib=_snake_case ) model.eval() quant_trainer.enable_calibration(_snake_case ) logger.info("***** Running calibration *****" ) logger.info(f''' Num examples = {self.calib_num}''' ) logger.info(f''' Batch size = {calib_dataloader.batch_size}''' ) for step, inputs in enumerate(_snake_case ): # Prediction step UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Dict = self.prediction_step(_snake_case ,_snake_case ,prediction_loss_only=_snake_case ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(_snake_case ,self.quant_trainer_args ) UpperCAmelCase_ : Dict = model def UpperCamelCase__ ( self ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = "eval" ): UpperCAmelCase_ : str = self.eval_dataset if eval_dataset is None else eval_dataset UpperCAmelCase_ : Any = self.get_eval_dataloader(_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. UpperCAmelCase_ : List[Any] = self.compute_metrics UpperCAmelCase_ : Dict = None UpperCAmelCase_ : int = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: UpperCAmelCase_ : List[str] = eval_loop( _snake_case ,description="Evaluation" ,prediction_loss_only=True if compute_metrics is None else None ,ignore_keys=_snake_case ,) finally: UpperCAmelCase_ : Tuple = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: UpperCAmelCase_ : List[str] = self.post_process_function(_snake_case ,_snake_case ,output.predictions ) UpperCAmelCase_ : int = self.compute_metrics(_snake_case ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): UpperCAmelCase_ : Tuple = metrics.pop(_snake_case ) self.log(_snake_case ) else: UpperCAmelCase_ : Tuple = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) UpperCAmelCase_ : List[Any] = self.callback_handler.on_evaluate(self.args ,self.state ,self.control ,_snake_case ) return metrics def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case = "test" ): UpperCAmelCase_ : int = self.get_test_dataloader(_snake_case ) # Temporarily disable metric computation, we will do it in the loop here. UpperCAmelCase_ : Any = self.compute_metrics UpperCAmelCase_ : Tuple = None UpperCAmelCase_ : Dict = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: UpperCAmelCase_ : Dict = eval_loop( _snake_case ,description="Prediction" ,prediction_loss_only=True if compute_metrics is None else None ,ignore_keys=_snake_case ,) finally: UpperCAmelCase_ : Optional[Any] = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output UpperCAmelCase_ : Union[str, Any] = self.post_process_function(_snake_case ,_snake_case ,output.predictions ,"predict" ) UpperCAmelCase_ : str = self.compute_metrics(_snake_case ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): UpperCAmelCase_ : Optional[int] = metrics.pop(_snake_case ) return PredictionOutput(predictions=predictions.predictions ,label_ids=predictions.label_ids ,metrics=_snake_case ) def UpperCamelCase__ ( self ,_snake_case="./" ): UpperCAmelCase_ : int = self.eval_dataset UpperCAmelCase_ : Union[str, Any] = self.get_eval_dataloader(_snake_case ) UpperCAmelCase_ : str = next(iter(_snake_case ) ) # saving device - to make it consistent UpperCAmelCase_ : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) # convert to tuple UpperCAmelCase_ : Any = tuple(v.to(_snake_case ) for k, v in batch.items() ) logger.info("Converting model to be onnx compatible" ) from pytorch_quantization.nn import TensorQuantizer UpperCAmelCase_ : Optional[int] = True UpperCAmelCase_ : int = self.model.to(_snake_case ) model.eval() model.float() UpperCAmelCase_ : Optional[Any] = model.module if hasattr(_snake_case ,"module" ) else model quant_trainer.configure_model(_snake_case ,self.quant_trainer_args ) UpperCAmelCase_ : List[str] = os.path.join(_snake_case ,"model.onnx" ) logger.info(f'''exporting model to {output_model_file}''' ) UpperCAmelCase_ : List[Any] = {0: "batch_size", 1: "seq_len"} torch.onnx.export( _snake_case ,_snake_case ,_snake_case ,export_params=_snake_case ,opset_version=13 ,do_constant_folding=_snake_case ,input_names=["input_ids", "attention_mask", "token_type_ids"] ,output_names=["output_start_logits", "output_end_logits"] ,dynamic_axes={ "input_ids": axes, "attention_mask": axes, "token_type_ids": axes, "output_start_logits": axes, "output_end_logits": axes, } ,verbose=_snake_case ,) logger.info("onnx export finished" )
71
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
1
'''simple docstring''' # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase = { """configuration_xmod""": [ """XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XmodConfig""", """XmodOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """XMOD_PRETRAINED_MODEL_ARCHIVE_LIST""", """XmodForCausalLM""", """XmodForMaskedLM""", """XmodForMultipleChoice""", """XmodForQuestionAnswering""", """XmodForSequenceClassification""", """XmodForTokenClassification""", """XmodModel""", """XmodPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
1
'''simple docstring''' 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() _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Dict ) -> Optional[int]: """simple docstring""" UpperCAmelCase_ : Optional[int] = "huggingface/label-files" UpperCAmelCase_ : List[Any] = "imagenet-1k-id2label.json" UpperCAmelCase_ : Optional[int] = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type="dataset" ) , "r" ) ) UpperCAmelCase_ : Any = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} UpperCAmelCase_ : List[Any] = {v: k for k, v in idalabel.items()} UpperCAmelCase_ : Optional[Any] = "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" UpperCAmelCase_ : int = BitConfig( conv_layer=_SCREAMING_SNAKE_CASE , num_labels=10_00 , idalabel=_SCREAMING_SNAKE_CASE , labelaid=_SCREAMING_SNAKE_CASE , ) return config def a__ ( _SCREAMING_SNAKE_CASE : Any ) -> int: """simple docstring""" if "stem.conv" in name: UpperCAmelCase_ : Optional[int] = name.replace("stem.conv" , "bit.embedder.convolution" ) if "blocks" in name: UpperCAmelCase_ : List[Any] = name.replace("blocks" , "layers" ) if "head.fc" in name: UpperCAmelCase_ : str = name.replace("head.fc" , "classifier.1" ) if name.startswith("norm" ): UpperCAmelCase_ : Optional[int] = "bit." + name if "bit" not in name and "classifier" not in name: UpperCAmelCase_ : List[Any] = "bit.encoder." + name return name def a__ ( ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Tuple = "http://images.cocodataset.org/val2017/000000039769.jpg" UpperCAmelCase_ : Dict = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Optional[int]=False ) -> Tuple: """simple docstring""" UpperCAmelCase_ : List[str] = get_config(_SCREAMING_SNAKE_CASE ) # load original model from timm UpperCAmelCase_ : str = create_model(_SCREAMING_SNAKE_CASE , pretrained=_SCREAMING_SNAKE_CASE ) timm_model.eval() # load state_dict of original model UpperCAmelCase_ : Dict = timm_model.state_dict() for key in state_dict.copy().keys(): UpperCAmelCase_ : Dict = state_dict.pop(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = val.squeeze() if "head" in key else val # load HuggingFace model UpperCAmelCase_ : List[str] = BitForImageClassification(_SCREAMING_SNAKE_CASE ) model.eval() model.load_state_dict(_SCREAMING_SNAKE_CASE ) # create image processor UpperCAmelCase_ : str = create_transform(**resolve_data_config({} , model=_SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : int = transform.transforms UpperCAmelCase_ : Any = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } UpperCAmelCase_ : Any = BitImageProcessor( do_resize=_SCREAMING_SNAKE_CASE , size={"shortest_edge": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_SCREAMING_SNAKE_CASE , crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} , do_normalize=_SCREAMING_SNAKE_CASE , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) UpperCAmelCase_ : List[Any] = prepare_img() UpperCAmelCase_ : Tuple = transform(_SCREAMING_SNAKE_CASE ).unsqueeze(0 ) UpperCAmelCase_ : List[str] = processor(_SCREAMING_SNAKE_CASE , return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # verify logits with torch.no_grad(): UpperCAmelCase_ : Dict = model(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = outputs.logits print("Logits:" , logits[0, :3] ) print("Predicted class:" , model.config.idalabel[logits.argmax(-1 ).item()] ) UpperCAmelCase_ : List[Any] = timm_model(_SCREAMING_SNAKE_CASE ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_SCREAMING_SNAKE_CASE , outputs.logits , atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(F'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) 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__": _lowerCamelCase = 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.""", ) _lowerCamelCase = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
71
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> float: """simple docstring""" UpperCAmelCase_ : Optional[int] = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def a__ ( ) -> Optional[int]: """simple docstring""" print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCamelCase = """\ @misc{wu2016googles, title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _lowerCamelCase = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the 'GLEU score'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score's range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ _lowerCamelCase = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: 'google_bleu': google_bleu score Examples: Example 1: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _snake_case (datasets.Metric): def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ) ,id="references" ), } ) ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = 1 ,_snake_case = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case ,hypotheses=_snake_case ,min_len=_snake_case ,max_len=_snake_case ) }
71
1
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
1
'''simple docstring''' import re def a__ ( _SCREAMING_SNAKE_CASE : str ) -> bool: """simple docstring""" UpperCAmelCase_ : Optional[Any] = re.compile(r"^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$" ) if match := re.search(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): return match.string == phone return False if __name__ == "__main__": print(indian_phone_validator("""+918827897895"""))
71
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class _snake_case : __A : Dict =BlenderbotConfig __A : Union[str, Any] ={} __A : Any ="gelu" def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=20 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Dict = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Optional[int] = attention_probs_dropout_prob UpperCAmelCase_ : List[Any] = max_position_embeddings UpperCAmelCase_ : str = eos_token_id UpperCAmelCase_ : List[Any] = pad_token_id UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) UpperCAmelCase_ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) UpperCAmelCase_ : Optional[Any] = tf.concat([input_ids, eos_tensor] ,axis=1 ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) UpperCAmelCase_ : List[str] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlenderbotModel(config=_snake_case ).get_decoder() UpperCAmelCase_ : int = inputs_dict["input_ids"] UpperCAmelCase_ : Dict = input_ids[:1, :] UpperCAmelCase_ : Any = inputs_dict["attention_mask"][:1, :] UpperCAmelCase_ : int = inputs_dict["head_mask"] UpperCAmelCase_ : Optional[int] = 1 # first forward pass UpperCAmelCase_ : List[str] = model(_snake_case ,attention_mask=_snake_case ,head_mask=_snake_case ,use_cache=_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) ,config.vocab_size ) UpperCAmelCase_ : Any = tf.cast(ids_tensor((self.batch_size, 3) ,2 ) ,tf.inta ) # append to next input_ids and UpperCAmelCase_ : Union[str, Any] = tf.concat([input_ids, next_tokens] ,axis=-1 ) UpperCAmelCase_ : Any = tf.concat([attention_mask, next_attn_mask] ,axis=-1 ) UpperCAmelCase_ : Any = model(_snake_case ,attention_mask=_snake_case )[0] UpperCAmelCase_ : List[Any] = model(_snake_case ,attention_mask=_snake_case ,past_key_values=_snake_case )[0] self.parent.assertEqual(next_tokens.shape[1] ,output_from_past.shape[1] ) # select random slice UpperCAmelCase_ : str = int(ids_tensor((1,) ,output_from_past.shape[-1] ) ) UpperCAmelCase_ : List[str] = output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase_ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_snake_case ,_snake_case ,rtol=1E-3 ) def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : List[str]=None , _SCREAMING_SNAKE_CASE : Dict=None , ) -> Union[str, Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase_ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase_ : List[Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __A : List[str] =(TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __A : Dict =( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __A : Any =True __A : Dict =False __A : Dict =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = TFBlenderbotModelTester(self ) UpperCAmelCase_ : int = ConfigTester(self ,config_class=_snake_case ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_snake_case ) @require_tokenizers @require_tf class _snake_case (unittest.TestCase): __A : Optional[int] =["My friends are cool but they eat too many carbs."] __A : Optional[Any] ="facebook/blenderbot-400M-distill" @cached_property def UpperCamelCase__ ( self ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.tokenizer(self.src_text ,return_tensors="tf" ) UpperCAmelCase_ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,) UpperCAmelCase_ : str = self.tokenizer.batch_decode(generated_ids.numpy() ,skip_special_tokens=_snake_case )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
71
1
'''simple docstring''' import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline _lowerCamelCase = argparse.ArgumentParser("""Stable Diffusion script with intel optimization""", add_help=False) parser.add_argument("""--dpm""", action="""store_true""", help="""Enable DPMSolver or not""") parser.add_argument("""--steps""", default=None, type=int, help="""Num inference steps""") _lowerCamelCase = parser.parse_args() _lowerCamelCase = """cpu""" _lowerCamelCase = """a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings""" _lowerCamelCase = """path-to-your-trained-model""" _lowerCamelCase = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: _lowerCamelCase = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) _lowerCamelCase = pipe.to(device) # to channels last _lowerCamelCase = pipe.unet.to(memory_format=torch.channels_last) _lowerCamelCase = pipe.vae.to(memory_format=torch.channels_last) _lowerCamelCase = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: _lowerCamelCase = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex _lowerCamelCase = torch.randn(2, 4, 64, 64) _lowerCamelCase = torch.rand(1) * 999 _lowerCamelCase = torch.randn(2, 77, 768) _lowerCamelCase = (sample, timestep, encoder_hidden_status) try: _lowerCamelCase = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: _lowerCamelCase = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) _lowerCamelCase = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) _lowerCamelCase = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: _lowerCamelCase = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute _lowerCamelCase = 666 _lowerCamelCase = torch.Generator(device).manual_seed(seed) _lowerCamelCase = {"""generator""": generator} if args.steps is not None: _lowerCamelCase = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): _lowerCamelCase = pipe(prompt, **generate_kwargs).images[0] # save image image.save("""generated.png""")
71
'''simple docstring''' from numpy import exp, pi, sqrt def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =VideoToVideoSDPipeline __A : Tuple =TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"} __A : Union[str, Any] =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"} __A : str =PipelineTesterMixin.required_optional_params - {"latents"} __A : Dict =False # No `output_type`. __A : Optional[int] =frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") ,cross_attention_dim=32 ,attention_head_dim=4 ,) UpperCAmelCase_ : int = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=1_28 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=10_00 ,hidden_act="gelu" ,projection_dim=5_12 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): # 3 frames UpperCAmelCase_ : Dict = floats_tensor((1, 3, 3, 32, 32) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Tuple = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : Tuple = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : str = VideoToVideoSDPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = "np" UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ).frames UpperCAmelCase_ : Tuple = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) UpperCAmelCase_ : Dict = np.array([1_06, 1_17, 1_13, 1_74, 1_37, 1_12, 1_48, 1_51, 1_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ,expected_max_diff=5E-3 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): return super().test_progress_bar() @slow @skip_mps class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" ,torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames UpperCAmelCase_ : str = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ : int = torch.randn((1, 10, 3, 10_24, 5_76) ,generator=_snake_case ) UpperCAmelCase_ : List[Any] = video.to("cuda" ) UpperCAmelCase_ : List[Any] = "Spiderman is surfing" UpperCAmelCase_ : Optional[Any] = pipe(_snake_case ,video=_snake_case ,generator=_snake_case ,num_inference_steps=3 ,output_type="pt" ).frames UpperCAmelCase_ : Any = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
71
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _snake_case (nn.Module): def __init__( self ,_snake_case = 16 ,_snake_case = 88 ,_snake_case = None ,_snake_case = 1 ,_snake_case = 0.0 ,_snake_case = 32 ,_snake_case = None ,_snake_case = False ,_snake_case = None ,_snake_case = None ,_snake_case = "geglu" ,_snake_case = None ,): super().__init__() UpperCAmelCase_ : Optional[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case ,attention_head_dim=_snake_case ,in_channels=_snake_case ,num_layers=_snake_case ,dropout=_snake_case ,norm_num_groups=_snake_case ,cross_attention_dim=_snake_case ,attention_bias=_snake_case ,sample_size=_snake_case ,num_vector_embeds=_snake_case ,activation_fn=_snake_case ,num_embeds_ada_norm=_snake_case ,) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference UpperCAmelCase_ : List[str] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` UpperCAmelCase_ : int = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` UpperCAmelCase_ : List[Any] = [1, 0] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = True ,): UpperCAmelCase_ : List[str] = hidden_states UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[int] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens UpperCAmelCase_ : Any = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] UpperCAmelCase_ : Any = self.transformer_index_for_condition[i] UpperCAmelCase_ : int = self.transformers[transformer_index]( _snake_case ,encoder_hidden_states=_snake_case ,timestep=_snake_case ,cross_attention_kwargs=_snake_case ,return_dict=_snake_case ,)[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] UpperCAmelCase_ : Dict = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) UpperCAmelCase_ : List[Any] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
71
1
'''simple docstring''' import baseaa def a__ ( _SCREAMING_SNAKE_CASE : str ) -> bytes: """simple docstring""" return baseaa.aaaencode(string.encode("utf-8" ) ) def a__ ( _SCREAMING_SNAKE_CASE : bytes ) -> str: """simple docstring""" return baseaa.aaadecode(_SCREAMING_SNAKE_CASE ).decode("utf-8" ) if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' import json import sys def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : int ) -> Tuple: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: UpperCAmelCase_ : Dict = json.load(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = ["<details>", "<summary>Show updated benchmarks!</summary>", " "] for benchmark_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = results[benchmark_name] UpperCAmelCase_ : Any = benchmark_name.split("/" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) UpperCAmelCase_ : Any = "| metric |" UpperCAmelCase_ : Any = "|--------|" UpperCAmelCase_ : Union[str, Any] = "| new / old (diff) |" for metric_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = benchmark_res[metric_name] UpperCAmelCase_ : Union[str, Any] = metric_vals["new"] UpperCAmelCase_ : Optional[Any] = metric_vals.get("old" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = metric_vals.get("diff" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = F''' {new_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("</details>" ) with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.writelines("\n".join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": _lowerCamelCase = sys.argv[1] _lowerCamelCase = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
71
1
'''simple docstring''' from __future__ import annotations from typing import Generic, TypeVar _lowerCamelCase = TypeVar("""T""") class _snake_case (Generic[T]): def __init__( self ,_snake_case ): UpperCAmelCase_ : List[str] = data UpperCAmelCase_ : Dict = self UpperCAmelCase_ : int = 0 class _snake_case (Generic[T]): def __init__( self ): # map from node name to the node object UpperCAmelCase_ : dict[T, DisjointSetTreeNode[T]] = {} def UpperCamelCase__ ( self ,_snake_case ): # create a new set with x as its member UpperCAmelCase_ : Dict = DisjointSetTreeNode(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): # find the set x belongs to (with path-compression) UpperCAmelCase_ : Tuple = self.map[data] if elem_ref != elem_ref.parent: UpperCAmelCase_ : int = self.find_set(elem_ref.parent.data ) return elem_ref.parent def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): # helper function for union operation if nodea.rank > nodea.rank: UpperCAmelCase_ : List[Any] = nodea else: UpperCAmelCase_ : List[Any] = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): # merge 2 disjoint sets self.link(self.find_set(_snake_case ) ,self.find_set(_snake_case ) ) class _snake_case (Generic[T]): def __init__( self ): # connections: map from the node to the neighbouring nodes (with weights) UpperCAmelCase_ : dict[T, dict[T, int]] = {} def UpperCamelCase__ ( self ,_snake_case ): # add a node ONLY if its not present in the graph if node not in self.connections: UpperCAmelCase_ : Union[str, Any] = {} def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): # add an edge with the given weight self.add_node(_snake_case ) self.add_node(_snake_case ) UpperCAmelCase_ : Dict = weight UpperCAmelCase_ : Union[str, Any] = weight def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = [] UpperCAmelCase_ : str = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda _snake_case : x[2] ) # creating the disjoint set UpperCAmelCase_ : str = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(_snake_case ) # MST generation UpperCAmelCase_ : int = 0 UpperCAmelCase_ : int = 0 UpperCAmelCase_ : str = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = edges[index] index += 1 UpperCAmelCase_ : Dict = disjoint_set.find_set(_snake_case ) UpperCAmelCase_ : str = disjoint_set.find_set(_snake_case ) if parent_u != parent_v: num_edges += 1 graph.add_edge(_snake_case ,_snake_case ,_snake_case ) disjoint_set.union(_snake_case ,_snake_case ) return graph
71
'''simple docstring''' import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase = get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[int] =DebertaVaTokenizer __A : Union[str, Any] =DebertaVaTokenizerFast __A : str =True __A : List[str] =True def UpperCamelCase__ ( self ): super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase_ : Optional[int] = DebertaVaTokenizer(_snake_case ,unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[Any] = "this is a test" UpperCAmelCase_ : Optional[Any] = "this is a test" return input_text, output_text def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = "<pad>" UpperCAmelCase_ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case ) ,_snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case ) ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"<pad>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"[PAD]" ) self.assertEqual(len(_snake_case ) ,3_00_01 ) def UpperCamelCase__ ( self ): self.assertEqual(self.get_tokenizer().vocab_size ,3_00_00 ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : str = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : Union[str, Any] = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on UpperCAmelCase_ : Tuple = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Dict = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[str] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : List[Any] = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_tokenizer() UpperCAmelCase_ : Union[str, Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Dict = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) UpperCAmelCase_ : Optional[Any] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) UpperCAmelCase_ : int = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Union[str, Any] = tokenizer.encode(_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = "This is a test" UpperCAmelCase_ : Optional[int] = [13, 1, 43_98, 25, 21, 12_89] UpperCAmelCase_ : Optional[Any] = ["▁", "T", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : str = DebertaVaTokenizer(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[str] = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) # fmt: off UpperCAmelCase_ : List[str] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = [13, 1, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] UpperCAmelCase_ : str = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[str] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode("sequence builders" ) UpperCAmelCase_ : Dict = tokenizer.encode("multi-sequence build" ) UpperCAmelCase_ : Tuple = tokenizer.build_inputs_with_special_tokens(_snake_case ) UpperCAmelCase_ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(_snake_case ,_snake_case ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] ,_snake_case ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] ,_snake_case ,) @slow def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Union[str, Any] = {"input_ids": [[1, 3_98_67, 36, 1_93_90, 4_86, 27, 3_50_52, 8_14_36, 18, 6_06_85, 12_25, 7, 3_50_52, 8_14_36, 18, 93_67, 1_68_99, 18, 1_59_37, 53, 5_94, 7_73, 18, 1_62_87, 3_04_65, 36, 1_59_37, 6, 4_11_39, 38, 3_69_79, 6_07_63, 1_91, 6, 3_41_32, 99, 6, 5_05_38, 3_90, 4_32_30, 6, 3_41_32, 27_79, 2_08_50, 14, 6_99, 10_72, 11_94, 36, 3_82, 1_09_01, 53, 7, 6_99, 10_72, 20_84, 36, 2_04_22, 6_30, 53, 19, 1_05, 30_49, 18_96, 10_53, 1_68_99, 15_06, 11, 3_79_78, 42_43, 7, 12_37, 3_18_69, 2_00, 1_65_66, 6_54, 6, 3_50_52, 8_14_36, 7, 5_56_30, 1_35_93, 4, 2], [1, 26, 1_50_11, 13, 6_67, 8, 10_53, 18, 2_36_11, 12_37, 7_23_56, 1_28_20, 34, 10_41_34, 12_09, 35, 1_33_13, 66_27, 21, 2_02, 3_47, 7, 1_64, 23_99, 11, 46, 44_85, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 12_32, 28_64, 1_57_85, 1_49_51, 1_05, 5, 85_81, 12_50, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_snake_case ,model_name="microsoft/deberta-v2-xlarge" ,revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" ,)
71
1
'''simple docstring''' import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class _snake_case (unittest.TestCase): def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=True ,_snake_case=True ,_snake_case=True ,_snake_case=99 ,_snake_case=32 ,_snake_case=5 ,_snake_case=4 ,_snake_case=37 ,_snake_case="gelu" ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=5_12 ,_snake_case=16 ,_snake_case=2 ,_snake_case=0.02 ,_snake_case=4 ,): UpperCAmelCase_ : List[str] = parent UpperCAmelCase_ : Optional[int] = batch_size UpperCAmelCase_ : int = seq_length UpperCAmelCase_ : Optional[int] = is_training UpperCAmelCase_ : Any = use_attention_mask UpperCAmelCase_ : str = use_token_type_ids UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : List[str] = hidden_size UpperCAmelCase_ : int = num_hidden_layers UpperCAmelCase_ : List[str] = num_attention_heads UpperCAmelCase_ : str = intermediate_size UpperCAmelCase_ : Tuple = hidden_act UpperCAmelCase_ : Optional[int] = hidden_dropout_prob UpperCAmelCase_ : int = attention_probs_dropout_prob UpperCAmelCase_ : Any = max_position_embeddings UpperCAmelCase_ : List[str] = type_vocab_size UpperCAmelCase_ : Optional[int] = type_sequence_label_size UpperCAmelCase_ : Union[str, Any] = initializer_range UpperCAmelCase_ : Optional[int] = num_choices def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : int = None if self.use_attention_mask: UpperCAmelCase_ : Dict = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_ : str = None if self.use_token_type_ids: UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) UpperCAmelCase_ : str = 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=_snake_case ,initializer_range=self.initializer_range ,) return config, input_ids, token_type_ids, attention_mask def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = config_and_inputs UpperCAmelCase_ : Union[str, Any] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Dict = config_and_inputs UpperCAmelCase_ : Any = True UpperCAmelCase_ : Dict = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Tuple =True __A : int =( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = FlaxRobertaModelTester(self ) @slow def UpperCamelCase__ ( self ): for model_class_name in self.all_model_classes: UpperCAmelCase_ : List[str] = model_class_name.from_pretrained("roberta-base" ,from_pt=_snake_case ) UpperCAmelCase_ : Any = model(np.ones((1, 1) ) ) self.assertIsNotNone(_snake_case )
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise TypeError("Input value must be an 'int' type" ) UpperCAmelCase_ : Union[str, Any] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int = 4_00_00_00 ) -> int: """simple docstring""" UpperCAmelCase_ : Optional[Any] = [0, 1] UpperCAmelCase_ : Optional[Any] = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 UpperCAmelCase_ : Dict = 0 for j in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
71
'''simple docstring''' from math import factorial def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k" ) return factorial(_SCREAMING_SNAKE_CASE ) // (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) if __name__ == "__main__": print( """The number of five-card hands possible from a standard""", f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( """If a class of 40 students must be arranged into groups of""", f"""4 for group projects, there are {combinations(40, 4)} ways""", """to arrange them.\n""", ) print( """If 10 teams are competing in a Formula One race, there""", f"""are {combinations(10, 3)} ways that first, second and""", """third place can be awarded.""", )
71
1
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput _lowerCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name def a__ ( _SCREAMING_SNAKE_CASE : Union[List, PIL.Image.Image, torch.Tensor] ) -> List[str]: """simple docstring""" warnings.warn( "The preprocess method is deprecated and will be removed in a future version. Please" " use VaeImageProcessor.preprocess instead" , _SCREAMING_SNAKE_CASE , ) if isinstance(_SCREAMING_SNAKE_CASE , torch.Tensor ): return image elif isinstance(_SCREAMING_SNAKE_CASE , PIL.Image.Image ): UpperCAmelCase_ : List[Any] = [image] if isinstance(image[0] , PIL.Image.Image ): UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = image[0].size UpperCAmelCase_ , UpperCAmelCase_ : List[str] = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 UpperCAmelCase_ : int = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["lanczos"] ) )[None, :] for i in image] UpperCAmelCase_ : Any = np.concatenate(_SCREAMING_SNAKE_CASE , axis=0 ) UpperCAmelCase_ : Dict = np.array(_SCREAMING_SNAKE_CASE ).astype(np.floataa ) / 255.0 UpperCAmelCase_ : Any = image.transpose(0 , 3 , 1 , 2 ) UpperCAmelCase_ : List[str] = 2.0 * image - 1.0 UpperCAmelCase_ : List[str] = torch.from_numpy(_SCREAMING_SNAKE_CASE ) elif isinstance(image[0] , torch.Tensor ): UpperCAmelCase_ : List[str] = torch.cat(_SCREAMING_SNAKE_CASE , dim=0 ) return image def a__ ( _SCREAMING_SNAKE_CASE : Union[List, PIL.Image.Image, torch.Tensor] ) -> Union[str, Any]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , torch.Tensor ): return mask elif isinstance(_SCREAMING_SNAKE_CASE , PIL.Image.Image ): UpperCAmelCase_ : Optional[int] = [mask] if isinstance(mask[0] , PIL.Image.Image ): UpperCAmelCase_ , UpperCAmelCase_ : List[str] = mask[0].size UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 UpperCAmelCase_ : Tuple = [np.array(m.convert("L" ).resize((w, h) , resample=PIL_INTERPOLATION["nearest"] ) )[None, :] for m in mask] UpperCAmelCase_ : Union[str, Any] = np.concatenate(_SCREAMING_SNAKE_CASE , axis=0 ) UpperCAmelCase_ : List[str] = mask.astype(np.floataa ) / 255.0 UpperCAmelCase_ : Any = 0 UpperCAmelCase_ : Optional[Any] = 1 UpperCAmelCase_ : Any = torch.from_numpy(_SCREAMING_SNAKE_CASE ) elif isinstance(mask[0] , torch.Tensor ): UpperCAmelCase_ : Tuple = torch.cat(_SCREAMING_SNAKE_CASE , dim=0 ) return mask class _snake_case (__SCREAMING_SNAKE_CASE): __A : UNetaDModel __A : RePaintScheduler def __init__( self ,_snake_case ,_snake_case ): super().__init__() self.register_modules(unet=_snake_case ,scheduler=_snake_case ) @torch.no_grad() def __call__( self ,_snake_case ,_snake_case ,_snake_case = 2_50 ,_snake_case = 0.0 ,_snake_case = 10 ,_snake_case = 10 ,_snake_case = None ,_snake_case = "pil" ,_snake_case = True ,): UpperCAmelCase_ : List[str] = image UpperCAmelCase_ : Tuple = _preprocess_image(_snake_case ) UpperCAmelCase_ : Union[str, Any] = original_image.to(device=self.device ,dtype=self.unet.dtype ) UpperCAmelCase_ : List[str] = _preprocess_mask(_snake_case ) UpperCAmelCase_ : Dict = mask_image.to(device=self.device ,dtype=self.unet.dtype ) UpperCAmelCase_ : Dict = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(_snake_case ,_snake_case ) and len(_snake_case ) != batch_size: raise ValueError( f'''You have passed a list of generators of length {len(_snake_case )}, but requested an effective batch''' f''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) UpperCAmelCase_ : List[Any] = original_image.shape UpperCAmelCase_ : Optional[Any] = randn_tensor(_snake_case ,generator=_snake_case ,device=self.device ,dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(_snake_case ,_snake_case ,_snake_case ,self.device ) UpperCAmelCase_ : List[str] = eta UpperCAmelCase_ : Optional[int] = self.scheduler.timesteps[0] + 1 UpperCAmelCase_ : str = generator[0] if isinstance(_snake_case ,_snake_case ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual UpperCAmelCase_ : int = self.unet(_snake_case ,_snake_case ).sample # compute previous image: x_t -> x_t-1 UpperCAmelCase_ : int = self.scheduler.step(_snake_case ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,_snake_case ).prev_sample else: # compute the reverse: x_t-1 -> x_t UpperCAmelCase_ : Optional[int] = self.scheduler.undo_step(_snake_case ,_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = t UpperCAmelCase_ : Union[str, Any] = (image / 2 + 0.5).clamp(0 ,1 ) UpperCAmelCase_ : Optional[int] = image.cpu().permute(0 ,2 ,3 ,1 ).numpy() if output_type == "pil": UpperCAmelCase_ : Optional[int] = self.numpy_to_pil(_snake_case ) if not return_dict: return (image,) return ImagePipelineOutput(images=_snake_case )
71
'''simple docstring''' import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =VideoToVideoSDPipeline __A : Tuple =TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"} __A : Union[str, Any] =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"} __A : str =PipelineTesterMixin.required_optional_params - {"latents"} __A : Dict =False # No `output_type`. __A : Optional[int] =frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") ,cross_attention_dim=32 ,attention_head_dim=4 ,) UpperCAmelCase_ : int = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=1_28 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=10_00 ,hidden_act="gelu" ,projection_dim=5_12 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): # 3 frames UpperCAmelCase_ : Dict = floats_tensor((1, 3, 3, 32, 32) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Tuple = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : Tuple = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : str = VideoToVideoSDPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = "np" UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ).frames UpperCAmelCase_ : Tuple = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) UpperCAmelCase_ : Dict = np.array([1_06, 1_17, 1_13, 1_74, 1_37, 1_12, 1_48, 1_51, 1_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ,expected_max_diff=5E-3 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): return super().test_progress_bar() @slow @skip_mps class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" ,torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames UpperCAmelCase_ : str = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ : int = torch.randn((1, 10, 3, 10_24, 5_76) ,generator=_snake_case ) UpperCAmelCase_ : List[Any] = video.to("cuda" ) UpperCAmelCase_ : List[Any] = "Spiderman is surfing" UpperCAmelCase_ : Optional[Any] = pipe(_snake_case ,video=_snake_case ,generator=_snake_case ,num_inference_steps=3 ,output_type="pt" ).frames UpperCAmelCase_ : Any = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
71
1
'''simple docstring''' from __future__ import annotations import typing from collections import Counter def a__ ( _SCREAMING_SNAKE_CASE : int ) -> typing.Counter[int]: """simple docstring""" UpperCAmelCase_ : typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(_SCREAMING_SNAKE_CASE , max_perimeter + 1 ): UpperCAmelCase_ : List[str] = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : str = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def a__ ( _SCREAMING_SNAKE_CASE : int = 10_00 ) -> int: """simple docstring""" UpperCAmelCase_ : Dict = pythagorean_triple(_SCREAMING_SNAKE_CASE ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
71
'''simple docstring''' import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) _lowerCamelCase = pytest.mark.integration @pytest.mark.parametrize("path" , ["paws", "csv"] ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : str ) -> Tuple: """simple docstring""" inspect_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.parametrize("path" , ["accuracy"] ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: """simple docstring""" inspect_metric(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, config_name, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict ) -> str: """simple docstring""" UpperCAmelCase_ : int = get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : List[str] ) -> str: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, expected" , [ ("squad", "plain_text"), ("acronym_identification", "default"), ("lhoestq/squad", "plain_text"), ("lhoestq/test", "default"), ("lhoestq/demo1", "lhoestq--demo1"), ("dalle-mini/wit", "dalle-mini--wit"), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_config_names(_SCREAMING_SNAKE_CASE ) assert expected in config_names @pytest.mark.parametrize( "path, expected_configs, expected_splits_in_first_config" , [ ("squad", ["plain_text"], ["train", "validation"]), ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]), ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str] ) -> Any: """simple docstring""" UpperCAmelCase_ : Any = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert list(infos.keys() ) == expected_configs UpperCAmelCase_ : Optional[Any] = expected_configs[0] assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( "path, expected_config, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ) -> Any: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : str ) -> Any: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_split_names(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE )
71
1
'''simple docstring''' from collections.abc import Callable class _snake_case : def __init__( self ,_snake_case = None ): # Stores actual heap items. UpperCAmelCase_ : list = [] # Stores indexes of each item for supporting updates and deletion. UpperCAmelCase_ : dict = {} # Stores current size of heap. UpperCAmelCase_ : List[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. UpperCAmelCase_ : str = key or (lambda _snake_case : x) def UpperCamelCase__ ( self ,_snake_case ): return int((i - 1) / 2 ) if i > 0 else None def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Optional[int] = int(2 * i + 1 ) return left if 0 < left < self.size else None def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Optional[Any] = int(2 * i + 2 ) return right if 0 < right < self.size else None def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = self.arr[j], self.arr[i] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): return self.arr[i][1] < self.arr[j][1] def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Dict = self._left(_snake_case ) UpperCAmelCase_ : Dict = self._right(_snake_case ) UpperCAmelCase_ : Tuple = i if left is not None and not self._cmp(_snake_case ,_snake_case ): UpperCAmelCase_ : str = left if right is not None and not self._cmp(_snake_case ,_snake_case ): UpperCAmelCase_ : Union[str, Any] = right return valid_parent def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[str] = self._parent(_snake_case ) while parent is not None and not self._cmp(_snake_case ,_snake_case ): self._swap(_snake_case ,_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : Tuple = parent, self._parent(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[Any] = self._get_valid_parent(_snake_case ) while valid_parent != index: self._swap(_snake_case ,_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : Any = valid_parent, self._get_valid_parent(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): if item not in self.pos_map: return UpperCAmelCase_ : int = self.pos_map[item] UpperCAmelCase_ : Union[str, Any] = [item, self.key(_snake_case )] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(_snake_case ) self._heapify_down(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ): if item not in self.pos_map: return UpperCAmelCase_ : List[str] = self.pos_map[item] del self.pos_map[item] UpperCAmelCase_ : Dict = self.arr[self.size - 1] UpperCAmelCase_ : Any = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(_snake_case ) self._heapify_down(_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Optional[Any] = len(self.arr ) if arr_len == self.size: self.arr.append([item, self.key(_snake_case )] ) else: UpperCAmelCase_ : Optional[Any] = [item, self.key(_snake_case )] UpperCAmelCase_ : str = self.size self.size += 1 self._heapify_up(self.size - 1 ) def UpperCamelCase__ ( self ): return self.arr[0] if self.size else None def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0] ) return top_item_tuple def a__ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
1
'''simple docstring''' import random from .binary_exp_mod import bin_exp_mod def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Optional[int]=10_00 ) -> List[str]: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd UpperCAmelCase_ : Union[str, Any] = n - 1 UpperCAmelCase_ : Optional[Any] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) UpperCAmelCase_ : Dict = 0 while count < prec: UpperCAmelCase_ : Any = random.randint(2 , n - 1 ) UpperCAmelCase_ : int = bin_exp_mod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if b != 1: UpperCAmelCase_ : Any = True for _ in range(_SCREAMING_SNAKE_CASE ): if b == n - 1: UpperCAmelCase_ : Union[str, Any] = False break UpperCAmelCase_ : List[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _lowerCamelCase = abs(int(input("""Enter bound : """).strip())) print("""Here's the list of primes:""") print(""", """.join(str(i) for i in range(n + 1) if is_prime_big(i)))
71
'''simple docstring''' import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Any =AudioLDMPipeline __A : Dict =TEXT_TO_AUDIO_PARAMS __A : Any =TEXT_TO_AUDIO_BATCH_PARAMS __A : Tuple =frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = 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, 64) ,class_embed_type="simple_projection" ,projection_class_embeddings_input_dim=32 ,class_embeddings_concat=_snake_case ,) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=1 ,out_channels=1 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = ClapTextConfig( 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=10_00 ,projection_dim=32 ,) UpperCAmelCase_ : Optional[Any] = ClapTextModelWithProjection(_snake_case ) UpperCAmelCase_ : List[Any] = RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,model_max_length=77 ) UpperCAmelCase_ : Optional[int] = SpeechTaHifiGanConfig( model_in_dim=8 ,sampling_rate=1_60_00 ,upsample_initial_channel=16 ,upsample_rates=[2, 2] ,upsample_kernel_sizes=[4, 4] ,resblock_kernel_sizes=[3, 7] ,resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] ,normalize_before=_snake_case ,) UpperCAmelCase_ : Union[str, Any] = SpeechTaHifiGan(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "vocoder": vocoder, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Optional[int] = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[str] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Any = { "prompt": "A hammer hitting a wooden surface", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : str = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : Any = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : int = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Dict = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Tuple = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[str] = output.audios[0] UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : str = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : Dict = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : Optional[Any] = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Tuple = F.normalize(_snake_case ,dim=-1 ) UpperCAmelCase_ : int = prompt_embeds # forward UpperCAmelCase_ : int = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = 3 * ["this is a negative prompt"] UpperCAmelCase_ : Any = negative_prompt UpperCAmelCase_ : Union[str, Any] = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Dict = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : List[Any] = [] for p in [prompt, negative_prompt]: UpperCAmelCase_ : Any = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : List[Any] = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : List[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Any = F.normalize(_snake_case ,dim=-1 ) embeds.append(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = embeds # forward UpperCAmelCase_ : Tuple = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Any = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : int = "egg cracking" UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : int = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : List[Any] = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[str] = self.get_dummy_components() UpperCAmelCase_ : Dict = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Any = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Dict = "A hammer hitting a wooden surface" # test num_waveforms_per_prompt=1 (default) UpperCAmelCase_ : Any = audioldm_pipe(_snake_case ,num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : Dict = audioldm_pipe([prompt] * batch_size ,num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : List[Any] = audioldm_pipe(_snake_case ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = audioldm_pipe( [prompt] * batch_size ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Union[str, Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe.vocoder.config.sampling_rate UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = audioldm_pipe(audio_length_in_s=0.016 ,**_snake_case ) UpperCAmelCase_ : str = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.016 UpperCAmelCase_ : List[Any] = audioldm_pipe(audio_length_in_s=0.032 ,**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.032 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : str = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : int = ["hey"] UpperCAmelCase_ : Dict = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : Any = output.audios.shape assert audio_shape == (1, 2_56) UpperCAmelCase_ : Tuple = audioldm_pipe.vocoder.config config.model_in_dim *= 2 UpperCAmelCase_ : List[Any] = SpeechTaHifiGan(_snake_case ).to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : int = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def UpperCamelCase__ ( self ): self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case ) def UpperCamelCase__ ( self ): self._test_inference_batch_single_identical(test_mean_pixel_difference=_snake_case ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ) @slow class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case ,_snake_case="cpu" ,_snake_case=torch.floataa ,_snake_case=0 ): UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : str = np.random.RandomState(_snake_case ).standard_normal((1, 8, 1_28, 16) ) UpperCAmelCase_ : Optional[Any] = torch.from_numpy(_snake_case ).to(device=_snake_case ,dtype=_snake_case ) UpperCAmelCase_ : List[str] = { "prompt": "A hammer hitting a wooden surface", "latents": latents, "generator": generator, "num_inference_steps": 3, "guidance_scale": 2.5, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : Optional[int] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[Any] = self.get_inputs(_snake_case ) UpperCAmelCase_ : List[Any] = 25 UpperCAmelCase_ : Union[str, Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Union[str, Any] = audio[7_72_30:7_72_40] UpperCAmelCase_ : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) UpperCAmelCase_ : Dict = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : List[Any] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Any = audio[2_77_80:2_77_90] UpperCAmelCase_ : List[str] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) UpperCAmelCase_ : Union[str, Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
71
1
'''simple docstring''' import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Any =AudioLDMPipeline __A : Dict =TEXT_TO_AUDIO_PARAMS __A : Any =TEXT_TO_AUDIO_BATCH_PARAMS __A : Tuple =frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = 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, 64) ,class_embed_type="simple_projection" ,projection_class_embeddings_input_dim=32 ,class_embeddings_concat=_snake_case ,) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=1 ,out_channels=1 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = ClapTextConfig( 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=10_00 ,projection_dim=32 ,) UpperCAmelCase_ : Optional[Any] = ClapTextModelWithProjection(_snake_case ) UpperCAmelCase_ : List[Any] = RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,model_max_length=77 ) UpperCAmelCase_ : Optional[int] = SpeechTaHifiGanConfig( model_in_dim=8 ,sampling_rate=1_60_00 ,upsample_initial_channel=16 ,upsample_rates=[2, 2] ,upsample_kernel_sizes=[4, 4] ,resblock_kernel_sizes=[3, 7] ,resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] ,normalize_before=_snake_case ,) UpperCAmelCase_ : Union[str, Any] = SpeechTaHifiGan(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "vocoder": vocoder, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Optional[int] = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[str] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Any = { "prompt": "A hammer hitting a wooden surface", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : str = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : Any = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : int = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Dict = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Tuple = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[str] = output.audios[0] UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : str = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : Dict = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : Optional[Any] = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Tuple = F.normalize(_snake_case ,dim=-1 ) UpperCAmelCase_ : int = prompt_embeds # forward UpperCAmelCase_ : int = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = 3 * ["this is a negative prompt"] UpperCAmelCase_ : Any = negative_prompt UpperCAmelCase_ : Union[str, Any] = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Dict = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : List[Any] = [] for p in [prompt, negative_prompt]: UpperCAmelCase_ : Any = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : List[Any] = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : List[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Any = F.normalize(_snake_case ,dim=-1 ) embeds.append(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = embeds # forward UpperCAmelCase_ : Tuple = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Any = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : int = "egg cracking" UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : int = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : List[Any] = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[str] = self.get_dummy_components() UpperCAmelCase_ : Dict = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Any = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Dict = "A hammer hitting a wooden surface" # test num_waveforms_per_prompt=1 (default) UpperCAmelCase_ : Any = audioldm_pipe(_snake_case ,num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : Dict = audioldm_pipe([prompt] * batch_size ,num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : List[Any] = audioldm_pipe(_snake_case ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = audioldm_pipe( [prompt] * batch_size ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Union[str, Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe.vocoder.config.sampling_rate UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = audioldm_pipe(audio_length_in_s=0.016 ,**_snake_case ) UpperCAmelCase_ : str = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.016 UpperCAmelCase_ : List[Any] = audioldm_pipe(audio_length_in_s=0.032 ,**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.032 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : str = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : int = ["hey"] UpperCAmelCase_ : Dict = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : Any = output.audios.shape assert audio_shape == (1, 2_56) UpperCAmelCase_ : Tuple = audioldm_pipe.vocoder.config config.model_in_dim *= 2 UpperCAmelCase_ : List[Any] = SpeechTaHifiGan(_snake_case ).to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : int = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def UpperCamelCase__ ( self ): self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case ) def UpperCamelCase__ ( self ): self._test_inference_batch_single_identical(test_mean_pixel_difference=_snake_case ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ) @slow class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case ,_snake_case="cpu" ,_snake_case=torch.floataa ,_snake_case=0 ): UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : str = np.random.RandomState(_snake_case ).standard_normal((1, 8, 1_28, 16) ) UpperCAmelCase_ : Optional[Any] = torch.from_numpy(_snake_case ).to(device=_snake_case ,dtype=_snake_case ) UpperCAmelCase_ : List[str] = { "prompt": "A hammer hitting a wooden surface", "latents": latents, "generator": generator, "num_inference_steps": 3, "guidance_scale": 2.5, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : Optional[int] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[Any] = self.get_inputs(_snake_case ) UpperCAmelCase_ : List[Any] = 25 UpperCAmelCase_ : Union[str, Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Union[str, Any] = audio[7_72_30:7_72_40] UpperCAmelCase_ : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) UpperCAmelCase_ : Dict = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : List[Any] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Any = audio[2_77_80:2_77_90] UpperCAmelCase_ : List[str] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) UpperCAmelCase_ : Union[str, Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
71
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable _lowerCamelCase = { """configuration_gpt_neox_japanese""": ["""GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXJapaneseConfig"""], """tokenization_gpt_neox_japanese""": ["""GPTNeoXJapaneseTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXJapaneseForCausalLM""", """GPTNeoXJapaneseLayer""", """GPTNeoXJapaneseModel""", """GPTNeoXJapanesePreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
1
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Optional[int] = 0 UpperCAmelCase_ : Any = len(_SCREAMING_SNAKE_CASE ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: UpperCAmelCase_ : int = i + 1 else: UpperCAmelCase_ : Any = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f"""{two_pointer([2, 7, 11, 15], 9) = }""")
71
'''simple docstring''' import heapq def a__ ( _SCREAMING_SNAKE_CASE : dict ) -> set[int]: """simple docstring""" UpperCAmelCase_ : list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(_SCREAMING_SNAKE_CASE , [-1 * len(_SCREAMING_SNAKE_CASE ), (key, value)] ) # chosen_vertices = set of chosen vertices UpperCAmelCase_ : Optional[int] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices UpperCAmelCase_ : Tuple = heapq.heappop(_SCREAMING_SNAKE_CASE )[1][0] chosen_vertices.add(_SCREAMING_SNAKE_CASE ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: UpperCAmelCase_ : Any = elem[1][1].index(_SCREAMING_SNAKE_CASE ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(_SCREAMING_SNAKE_CASE ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(f"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
71
1
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable _lowerCamelCase = { """configuration_gpt_neox_japanese""": ["""GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXJapaneseConfig"""], """tokenization_gpt_neox_japanese""": ["""GPTNeoXJapaneseTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXJapaneseForCausalLM""", """GPTNeoXJapaneseLayer""", """GPTNeoXJapaneseModel""", """GPTNeoXJapanesePreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(_SCREAMING_SNAKE_CASE ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =["pixel_values"] def __init__( self ,_snake_case = True ,_snake_case = None ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = True ,_snake_case = None ,_snake_case = True ,_snake_case = 1 / 2_55 ,_snake_case = True ,_snake_case = True ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Optional[Any] = size if size is not None else {"shortest_edge": 2_56} UpperCAmelCase_ : List[str] = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : str = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} UpperCAmelCase_ : Optional[Any] = get_size_dict(_snake_case ,param_name="crop_size" ) UpperCAmelCase_ : int = do_resize UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Dict = do_center_crop UpperCAmelCase_ : Optional[Any] = crop_size UpperCAmelCase_ : Optional[Any] = resample UpperCAmelCase_ : int = do_rescale UpperCAmelCase_ : Optional[int] = rescale_factor UpperCAmelCase_ : Dict = offset UpperCAmelCase_ : Optional[Any] = do_normalize UpperCAmelCase_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Any = get_size_dict(_snake_case ,default_to_square=_snake_case ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(_snake_case ,size["shortest_edge"] ,default_to_square=_snake_case ) elif "height" in size and "width" in size: UpperCAmelCase_ : Optional[Any] = (size["height"], size["width"]) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(_snake_case ,size=_snake_case ,resample=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Dict = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_snake_case ,size=(size["height"], size["width"]) ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = True ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = image.astype(np.floataa ) if offset: UpperCAmelCase_ : Any = image - (scale / 2) return rescale(_snake_case ,scale=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): return normalize(_snake_case ,mean=_snake_case ,std=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. UpperCAmelCase_ : Optional[int] = to_numpy_array(_snake_case ) if do_resize: UpperCAmelCase_ : Dict = self.resize(image=_snake_case ,size=_snake_case ,resample=_snake_case ) if do_center_crop: UpperCAmelCase_ : Optional[Any] = self.center_crop(_snake_case ,size=_snake_case ) if do_rescale: UpperCAmelCase_ : Union[str, Any] = self.rescale(image=_snake_case ,scale=_snake_case ,offset=_snake_case ) if do_normalize: UpperCAmelCase_ : Any = self.normalize(image=_snake_case ,mean=_snake_case ,std=_snake_case ) UpperCAmelCase_ : Any = to_channel_dimension_format(_snake_case ,_snake_case ) return image def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,**_snake_case ,): UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : str = resample if resample is not None else self.resample UpperCAmelCase_ : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : Tuple = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : List[Any] = offset if offset is not None else self.offset UpperCAmelCase_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : int = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : int = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : int = get_size_dict(_snake_case ,param_name="crop_size" ) if not valid_images(_snake_case ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) UpperCAmelCase_ : Any = make_batched(_snake_case ) UpperCAmelCase_ : Dict = [ [ self._preprocess_image( image=_snake_case ,do_resize=_snake_case ,size=_snake_case ,resample=_snake_case ,do_center_crop=_snake_case ,crop_size=_snake_case ,do_rescale=_snake_case ,rescale_factor=_snake_case ,offset=_snake_case ,do_normalize=_snake_case ,image_mean=_snake_case ,image_std=_snake_case ,data_format=_snake_case ,) for img in video ] for video in videos ] UpperCAmelCase_ : List[str] = {"pixel_values": videos} return BatchFeature(data=_snake_case ,tensor_type=_snake_case )
71
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__( _snake_case ,split=_snake_case ,features=_snake_case ,cache_dir=_snake_case ,keep_in_memory=_snake_case ,streaming=_snake_case ,num_proc=_snake_case ,**_snake_case ,) UpperCAmelCase_ : Tuple = field UpperCAmelCase_ : List[Any] = path_or_paths if isinstance(_snake_case ,_snake_case ) else {self.split: path_or_paths} UpperCAmelCase_ : Optional[int] = Json( cache_dir=_snake_case ,data_files=_snake_case ,features=_snake_case ,field=_snake_case ,**_snake_case ,) def UpperCamelCase__ ( self ): # Build iterable dataset if self.streaming: UpperCAmelCase_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase_ : Union[str, Any] = None UpperCAmelCase_ : int = None UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : int = None self.builder.download_and_prepare( download_config=_snake_case ,download_mode=_snake_case ,verification_mode=_snake_case ,base_path=_snake_case ,num_proc=self.num_proc ,) UpperCAmelCase_ : Dict = self.builder.as_dataset( split=self.split ,verification_mode=_snake_case ,in_memory=self.keep_in_memory ) return dataset class _snake_case : def __init__( self ,_snake_case ,_snake_case ,_snake_case = None ,_snake_case = None ,**_snake_case ,): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Union[str, Any] = path_or_buf UpperCAmelCase_ : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase_ : Dict = num_proc UpperCAmelCase_ : Optional[Any] = "utf-8" UpperCAmelCase_ : Optional[int] = to_json_kwargs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.to_json_kwargs.pop("path_or_buf" ,_snake_case ) UpperCAmelCase_ : Tuple = self.to_json_kwargs.pop("orient" ,"records" ) UpperCAmelCase_ : Any = self.to_json_kwargs.pop("lines" ,True if orient == "records" else False ) UpperCAmelCase_ : Optional[int] = self.to_json_kwargs.pop("index" ,False if orient in ["split", "table"] else True ) UpperCAmelCase_ : int = self.to_json_kwargs.pop("compression" ,_snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"wb" ,compression=_snake_case ) as buffer: UpperCAmelCase_ : List[str] = self._write(file_obj=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' " was passed. Please provide a local path instead." ) UpperCAmelCase_ : Union[str, Any] = self._write( file_obj=self.path_or_buf ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) return written def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = args UpperCAmelCase_ : List[str] = query_table( table=self.dataset.data ,key=slice(_snake_case ,offset + self.batch_size ) ,indices=self.dataset._indices ,) UpperCAmelCase_ : Optional[Any] = batch.to_pandas().to_json( path_or_buf=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**_snake_case ) if not json_str.endswith("\n" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,**_snake_case ,): UpperCAmelCase_ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): UpperCAmelCase_ : Any = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: UpperCAmelCase_ , UpperCAmelCase_ : int = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,_snake_case ,_snake_case )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): written += file_obj.write(_snake_case ) return written
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[int] ) -> List[str]: """simple docstring""" UpperCAmelCase_ : int = "" for i in table: res += inp[i - 1] return res def a__ ( _SCREAMING_SNAKE_CASE : Dict ) -> Optional[Any]: """simple docstring""" return data[1:] + data[0] def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Any ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ : int = "" for i in range(len(_SCREAMING_SNAKE_CASE ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: """simple docstring""" UpperCAmelCase_ : Optional[Any] = int("0b" + data[0] + data[-1] , 2 ) UpperCAmelCase_ : List[str] = int("0b" + data[1:3] , 2 ) return bin(s[row][col] )[2:] def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : List[Any] = message[:4] UpperCAmelCase_ : Union[str, Any] = message[4:] UpperCAmelCase_ : str = apply_table(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = xor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = apply_sbox(_SCREAMING_SNAKE_CASE , temp[:4] ) # noqa: E741 UpperCAmelCase_ : str = apply_sbox(_SCREAMING_SNAKE_CASE , temp[4:] ) UpperCAmelCase_ : int = "0" * (2 - len(_SCREAMING_SNAKE_CASE )) + l # noqa: E741 UpperCAmelCase_ : Optional[Any] = "0" * (2 - len(_SCREAMING_SNAKE_CASE )) + r UpperCAmelCase_ : List[str] = apply_table(l + r , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = xor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return temp + right if __name__ == "__main__": _lowerCamelCase = input("""Enter 10 bit key: """) _lowerCamelCase = input("""Enter 8 bit message: """) _lowerCamelCase = [6, 3, 7, 4, 8, 5, 10, 9] _lowerCamelCase = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _lowerCamelCase = [2, 4, 3, 1] _lowerCamelCase = [2, 6, 3, 1, 4, 8, 5, 7] _lowerCamelCase = [4, 1, 3, 5, 7, 2, 8, 6] _lowerCamelCase = [4, 1, 2, 3, 2, 3, 4, 1] _lowerCamelCase = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _lowerCamelCase = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _lowerCamelCase = apply_table(key, paa_table) _lowerCamelCase = temp[:5] _lowerCamelCase = temp[5:] _lowerCamelCase = left_shift(left) _lowerCamelCase = left_shift(right) _lowerCamelCase = apply_table(left + right, pa_table) _lowerCamelCase = left_shift(left) _lowerCamelCase = left_shift(right) _lowerCamelCase = left_shift(left) _lowerCamelCase = left_shift(right) _lowerCamelCase = apply_table(left + right, pa_table) # encryption _lowerCamelCase = apply_table(message, IP) _lowerCamelCase = function(expansion, sa, sa, keya, temp) _lowerCamelCase = temp[4:] + temp[:4] _lowerCamelCase = function(expansion, sa, sa, keya, temp) _lowerCamelCase = apply_table(temp, IP_inv) print("""Cipher text is:""", CT) # decryption _lowerCamelCase = apply_table(CT, IP) _lowerCamelCase = function(expansion, sa, sa, keya, temp) _lowerCamelCase = temp[4:] + temp[:4] _lowerCamelCase = function(expansion, sa, sa, keya, temp) _lowerCamelCase = apply_table(temp, IP_inv) print("""Plain text after decypting is:""", PT)
71
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
1
'''simple docstring''' from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = path_or_paths UpperCAmelCase_ : Optional[int] = split if split or isinstance(_snake_case ,_snake_case ) else "train" UpperCAmelCase_ : Tuple = features UpperCAmelCase_ : int = cache_dir UpperCAmelCase_ : Optional[Any] = keep_in_memory UpperCAmelCase_ : Any = streaming UpperCAmelCase_ : List[str] = num_proc UpperCAmelCase_ : int = kwargs @abstractmethod def UpperCamelCase__ ( self ): pass class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Union[str, Any] = features UpperCAmelCase_ : Dict = cache_dir UpperCAmelCase_ : int = keep_in_memory UpperCAmelCase_ : int = streaming UpperCAmelCase_ : Tuple = num_proc UpperCAmelCase_ : Optional[Any] = kwargs @abstractmethod def UpperCamelCase__ ( self ): pass
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
1
'''simple docstring''' import importlib import json import os import sys import tempfile import unittest from pathlib import Path import transformers import transformers.models.auto from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.bert.configuration_bert import BertConfig from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 _lowerCamelCase = get_tests_dir("""fixtures/dummy-config.json""") class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = 0 def UpperCamelCase__ ( self ): self.assertIsNotNone(transformers.models.auto.__spec__ ) self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto" ) ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AutoConfig.from_pretrained("bert-base-uncased" ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = AutoConfig.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AutoConfig.for_model("roberta" ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): with tempfile.TemporaryDirectory() as tmp_dir: # This model name contains bert and roberta, but roberta ends up being picked. UpperCAmelCase_ : List[str] = os.path.join(_snake_case ,"fake-roberta" ) os.makedirs(_snake_case ,exist_ok=_snake_case ) with open(os.path.join(_snake_case ,"config.json" ) ,"w" ) as f: f.write(json.dumps({} ) ) UpperCAmelCase_ : Union[str, Any] = AutoConfig.from_pretrained(_snake_case ) self.assertEqual(type(_snake_case ) ,_snake_case ) def UpperCamelCase__ ( self ): try: AutoConfig.register("custom" ,_snake_case ) # Wrong model type will raise an error with self.assertRaises(_snake_case ): AutoConfig.register("model" ,_snake_case ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_snake_case ): AutoConfig.register("bert" ,_snake_case ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCAmelCase_ : Union[str, Any] = CustomConfig() with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_snake_case ) UpperCAmelCase_ : Union[str, Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,"bert-base is not a local folder and is not a valid model identifier" ): UpperCAmelCase_ : int = AutoConfig.from_pretrained("bert-base" ) def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): UpperCAmelCase_ : Optional[int] = AutoConfig.from_pretrained(_snake_case ,revision="aaaaaa" ) def UpperCamelCase__ ( self ): with self.assertRaisesRegex( _snake_case ,"hf-internal-testing/no-config-test-repo does not appear to have a file named config.json." ,): UpperCAmelCase_ : Any = AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo" ) def UpperCamelCase__ ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_snake_case ): UpperCAmelCase_ : Optional[Any] = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ) # If remote code is disabled, we can't load this config. with self.assertRaises(_snake_case ): UpperCAmelCase_ : List[Any] = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ,trust_remote_code=_snake_case ) UpperCAmelCase_ : Optional[int] = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ,trust_remote_code=_snake_case ) self.assertEqual(config.__class__.__name__ ,"NewModelConfig" ) # Test config can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_snake_case ) UpperCAmelCase_ : Any = AutoConfig.from_pretrained(_snake_case ,trust_remote_code=_snake_case ) self.assertEqual(reloaded_config.__class__.__name__ ,"NewModelConfig" ) def UpperCamelCase__ ( self ): class _snake_case (__SCREAMING_SNAKE_CASE): __A : Optional[int] ="new-model" try: AutoConfig.register("new-model" ,_snake_case ) # If remote code is not set, the default is to use local UpperCAmelCase_ : Optional[int] = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ) self.assertEqual(config.__class__.__name__ ,"NewModelConfigLocal" ) # If remote code is disabled, we load the local one. UpperCAmelCase_ : Optional[int] = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ,trust_remote_code=_snake_case ) self.assertEqual(config.__class__.__name__ ,"NewModelConfigLocal" ) # If remote is enabled, we load from the Hub UpperCAmelCase_ : Any = AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" ,trust_remote_code=_snake_case ) self.assertEqual(config.__class__.__name__ ,"NewModelConfig" ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"]
71
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
1
'''simple docstring''' from __future__ import annotations from collections import Counter from random import random class _snake_case : def __init__( self ): UpperCAmelCase_ : Union[str, Any] = {} def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : Tuple = {} def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): if nodea not in self.connections: self.add_node(_snake_case ) if nodea not in self.connections: self.add_node(_snake_case ) UpperCAmelCase_ : Dict = probability def UpperCamelCase__ ( self ): return list(self.connections ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : str = 0 UpperCAmelCase_ : int = random() for dest in self.connections[node]: current_probability += self.connections[node][dest] if current_probability > random_value: return dest return "" def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : list[tuple[str, str, float]] , _SCREAMING_SNAKE_CASE : int ) -> dict[str, int]: """simple docstring""" UpperCAmelCase_ : List[Any] = MarkovChainGraphUndirectedUnweighted() for nodea, nodea, probability in transitions: graph.add_transition_probability(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = Counter(graph.get_nodes() ) UpperCAmelCase_ : Any = start for _ in range(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : str = graph.transition(_SCREAMING_SNAKE_CASE ) visited[node] += 1 return visited if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
1
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version _lowerCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.31.0""") require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt""") @dataclass class _snake_case : __A : Optional[str] =field( default="cifar10" , metadata={"help": "Name of a dataset from the datasets package"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "The column name of the images in the files."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "A folder containing the training data."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "A folder containing the validation data."}) __A : Optional[float] =field( default=0.1_5 , metadata={"help": "Percent to split off of train for validation."}) __A : Optional[int] =field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) __A : Optional[int] =field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = {} if self.train_dir is not None: UpperCAmelCase_ : List[str] = self.train_dir if self.validation_dir is not None: UpperCAmelCase_ : str = self.validation_dir UpperCAmelCase_ : Tuple = data_files if data_files else None @dataclass class _snake_case : __A : str =field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "The model checkpoint for weights initialization.Don't set if you want to train a model from scratch." ) } , ) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name_or_path"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "Override some existing default config settings when a model is trained from scratch. Example: " "n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index" ) } , ) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from s3"}) __A : str =field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) __A : str =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Name or path of preprocessor config."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) __A : float =field( default=0.7_5 , metadata={"help": "The ratio of the number of masked tokens in the input sequence."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether or not to train with normalized pixel values as target."}) @dataclass class _snake_case (__SCREAMING_SNAKE_CASE): __A : float =field( default=1e-3 , metadata={"help": "Base learning rate: absolute_lr = base_lr * total_batch_size / 256."}) def a__ ( _SCREAMING_SNAKE_CASE : Dict ) -> Any: """simple docstring""" UpperCAmelCase_ : Dict = torch.stack([example["pixel_values"] for example in examples] ) return {"pixel_values": pixel_values} def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Any = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_mae" , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCAmelCase_ : List[Any] = training_args.get_process_log_level() logger.setLevel(_SCREAMING_SNAKE_CASE ) transformers.utils.logging.set_verbosity(_SCREAMING_SNAKE_CASE ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. UpperCAmelCase_ : Dict = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCAmelCase_ : str = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset. UpperCAmelCase_ : int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCAmelCase_ : List[str] = None if "validation" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , _SCREAMING_SNAKE_CASE ) and data_args.train_val_split > 0.0: UpperCAmelCase_ : str = ds["train"].train_test_split(data_args.train_val_split ) UpperCAmelCase_ : int = split["train"] UpperCAmelCase_ : List[Any] = split["test"] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : Any = { "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "use_auth_token": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCAmelCase_ : int = ViTMAEConfig.from_pretrained(model_args.config_name , **_SCREAMING_SNAKE_CASE ) elif model_args.model_name_or_path: UpperCAmelCase_ : List[Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **_SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : List[str] = ViTMAEConfig() logger.warning("You are instantiating a new config instance from scratch." ) if model_args.config_overrides is not None: logger.info(F'''Overriding config: {model_args.config_overrides}''' ) config.update_from_string(model_args.config_overrides ) logger.info(F'''New config: {config}''' ) # adapt config config.update( { "mask_ratio": model_args.mask_ratio, "norm_pix_loss": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCAmelCase_ : Tuple = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **_SCREAMING_SNAKE_CASE ) elif model_args.model_name_or_path: UpperCAmelCase_ : List[Any] = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **_SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : Optional[Any] = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCAmelCase_ : Optional[int] = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("Training new model from scratch" ) UpperCAmelCase_ : Optional[int] = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) if training_args.do_train: UpperCAmelCase_ : Dict = ds["train"].column_names else: UpperCAmelCase_ : Dict = ds["validation"].column_names if data_args.image_column_name is not None: UpperCAmelCase_ : Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCAmelCase_ : Tuple = "image" elif "img" in column_names: UpperCAmelCase_ : int = "img" else: UpperCAmelCase_ : int = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCAmelCase_ : Optional[Any] = image_processor.size["shortest_edge"] else: UpperCAmelCase_ : str = (image_processor.size["height"], image_processor.size["width"]) UpperCAmelCase_ : Union[str, Any] = Compose( [ Lambda(lambda _SCREAMING_SNAKE_CASE : img.convert("RGB" ) if img.mode != "RGB" else img ), RandomResizedCrop(_SCREAMING_SNAKE_CASE , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(_SCREAMING_SNAKE_CASE : str ): UpperCAmelCase_ : List[Any] = [transforms(_SCREAMING_SNAKE_CASE ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("--do_train requires a train dataset" ) if data_args.max_train_samples is not None: UpperCAmelCase_ : Dict = ds["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(_SCREAMING_SNAKE_CASE ) if training_args.do_eval: if "validation" not in ds: raise ValueError("--do_eval requires a validation dataset" ) if data_args.max_eval_samples is not None: UpperCAmelCase_ : Optional[int] = ( ds["validation"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(_SCREAMING_SNAKE_CASE ) # Compute absolute learning rate UpperCAmelCase_ : Union[str, Any] = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCAmelCase_ : Dict = training_args.base_learning_rate * total_train_batch_size / 2_56 # Initialize our trainer UpperCAmelCase_ : List[str] = Trainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , train_dataset=ds["train"] if training_args.do_train else None , eval_dataset=ds["validation"] if training_args.do_eval else None , tokenizer=_SCREAMING_SNAKE_CASE , data_collator=_SCREAMING_SNAKE_CASE , ) # Training if training_args.do_train: UpperCAmelCase_ : str = None if training_args.resume_from_checkpoint is not None: UpperCAmelCase_ : List[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCAmelCase_ : List[Any] = last_checkpoint UpperCAmelCase_ : List[Any] = trainer.train(resume_from_checkpoint=_SCREAMING_SNAKE_CASE ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCAmelCase_ : Dict = trainer.evaluate() trainer.log_metrics("eval" , _SCREAMING_SNAKE_CASE ) trainer.save_metrics("eval" , _SCREAMING_SNAKE_CASE ) # Write model card and (optionally) push to hub UpperCAmelCase_ : List[str] = { "tasks": "masked-auto-encoding", "dataset": data_args.dataset_name, "tags": ["masked-auto-encoding"], } if training_args.push_to_hub: trainer.push_to_hub(**_SCREAMING_SNAKE_CASE ) else: trainer.create_model_card(**_SCREAMING_SNAKE_CASE ) def a__ ( _SCREAMING_SNAKE_CASE : Any ) -> Union[str, Any]: """simple docstring""" main() if __name__ == "__main__": main()
71
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int = 10_00 ) -> int: """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : List[str] = 1, 1 UpperCAmelCase_ : Dict = 2 while True: UpperCAmelCase_ : str = 0 UpperCAmelCase_ : List[str] = fa + fa UpperCAmelCase_ , UpperCAmelCase_ : str = fa, f index += 1 for _ in str(_SCREAMING_SNAKE_CASE ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
71
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCamelCase = """\ @misc{wu2016googles, title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _lowerCamelCase = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the 'GLEU score'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score's range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ _lowerCamelCase = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: 'google_bleu': google_bleu score Examples: Example 1: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _snake_case (datasets.Metric): def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ) ,id="references" ), } ) ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = 1 ,_snake_case = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case ,hypotheses=_snake_case ,min_len=_snake_case ,max_len=_snake_case ) }
71
1
'''simple docstring''' from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def a__ ( _SCREAMING_SNAKE_CASE : int ) -> Dict: """simple docstring""" def is_in_circle(_SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : float ) -> bool: UpperCAmelCase_ : Dict = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle UpperCAmelCase_ : List[Any] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(_SCREAMING_SNAKE_CASE ) ) # The ratio of the area for circle to square is pi/4. UpperCAmelCase_ : Optional[int] = proportion * 4 print(F'''The estimated value of pi is {pi_estimate}''' ) print(F'''The numpy value of pi is {pi}''' ) print(F'''The total error is {abs(pi - pi_estimate )}''' ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Callable[[float], float] , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) for _ in range(_SCREAMING_SNAKE_CASE ) ) * (max_value - min_value) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> None: """simple docstring""" def identity_function(_SCREAMING_SNAKE_CASE : float ) -> float: return x UpperCAmelCase_ : Optional[Any] = area_under_curve_estimator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = (max_value * max_value - min_value * min_value) / 2 print("******************" ) print(F'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(F'''Estimated value is {estimated_value}''' ) print(F'''Expected value is {expected_value}''' ) print(F'''Total error is {abs(estimated_value - expected_value )}''' ) print("******************" ) def a__ ( _SCREAMING_SNAKE_CASE : int ) -> None: """simple docstring""" def function_to_integrate(_SCREAMING_SNAKE_CASE : float ) -> float: return sqrt(4.0 - x * x ) UpperCAmelCase_ : List[Any] = area_under_curve_estimator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , 0.0 , 2.0 ) print("******************" ) print("Estimating pi using area_under_curve_estimator" ) print(F'''Estimated value is {estimated_value}''' ) print(F'''Expected value is {pi}''' ) print(F'''Total error is {abs(estimated_value - pi )}''' ) print("******************" ) if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
1
'''simple docstring''' from maths.prime_check import is_prime def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Any = F'''Input value of [number={number}] must be an integer''' raise TypeError(_SCREAMING_SNAKE_CASE ) if is_prime(_SCREAMING_SNAKE_CASE ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class _snake_case : __A : Dict =BlenderbotConfig __A : Union[str, Any] ={} __A : Any ="gelu" def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=20 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Dict = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Optional[int] = attention_probs_dropout_prob UpperCAmelCase_ : List[Any] = max_position_embeddings UpperCAmelCase_ : str = eos_token_id UpperCAmelCase_ : List[Any] = pad_token_id UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) UpperCAmelCase_ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) UpperCAmelCase_ : Optional[Any] = tf.concat([input_ids, eos_tensor] ,axis=1 ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) UpperCAmelCase_ : List[str] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlenderbotModel(config=_snake_case ).get_decoder() UpperCAmelCase_ : int = inputs_dict["input_ids"] UpperCAmelCase_ : Dict = input_ids[:1, :] UpperCAmelCase_ : Any = inputs_dict["attention_mask"][:1, :] UpperCAmelCase_ : int = inputs_dict["head_mask"] UpperCAmelCase_ : Optional[int] = 1 # first forward pass UpperCAmelCase_ : List[str] = model(_snake_case ,attention_mask=_snake_case ,head_mask=_snake_case ,use_cache=_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) ,config.vocab_size ) UpperCAmelCase_ : Any = tf.cast(ids_tensor((self.batch_size, 3) ,2 ) ,tf.inta ) # append to next input_ids and UpperCAmelCase_ : Union[str, Any] = tf.concat([input_ids, next_tokens] ,axis=-1 ) UpperCAmelCase_ : Any = tf.concat([attention_mask, next_attn_mask] ,axis=-1 ) UpperCAmelCase_ : Any = model(_snake_case ,attention_mask=_snake_case )[0] UpperCAmelCase_ : List[Any] = model(_snake_case ,attention_mask=_snake_case ,past_key_values=_snake_case )[0] self.parent.assertEqual(next_tokens.shape[1] ,output_from_past.shape[1] ) # select random slice UpperCAmelCase_ : str = int(ids_tensor((1,) ,output_from_past.shape[-1] ) ) UpperCAmelCase_ : List[str] = output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase_ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_snake_case ,_snake_case ,rtol=1E-3 ) def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : List[str]=None , _SCREAMING_SNAKE_CASE : Dict=None , ) -> Union[str, Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase_ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase_ : List[Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __A : List[str] =(TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __A : Dict =( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __A : Any =True __A : Dict =False __A : Dict =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = TFBlenderbotModelTester(self ) UpperCAmelCase_ : int = ConfigTester(self ,config_class=_snake_case ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_snake_case ) @require_tokenizers @require_tf class _snake_case (unittest.TestCase): __A : Optional[int] =["My friends are cool but they eat too many carbs."] __A : Optional[Any] ="facebook/blenderbot-400M-distill" @cached_property def UpperCamelCase__ ( self ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.tokenizer(self.src_text ,return_tensors="tf" ) UpperCAmelCase_ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,) UpperCAmelCase_ : str = self.tokenizer.batch_decode(generated_ids.numpy() ,skip_special_tokens=_snake_case )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
71
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise TypeError("Input value must be an 'int' type" ) UpperCAmelCase_ : Union[str, Any] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' from numpy import exp, pi, sqrt def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaPriorEmbaEmbPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[Any] =KandinskyVaaControlnetImgaImgPipeline __A : Union[str, Any] =["image_embeds", "negative_image_embeds", "image", "hint"] __A : Tuple =["image_embeds", "negative_image_embeds", "image", "hint"] __A : Any =[ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] __A : Tuple =False @property def UpperCamelCase__ ( self ): return 32 @property def UpperCamelCase__ ( self ): return 32 @property def UpperCamelCase__ ( self ): return self.time_input_dim @property def UpperCamelCase__ ( self ): return self.time_input_dim * 4 @property def UpperCamelCase__ ( self ): return 1_00 @property def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : List[Any] = { "in_channels": 8, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "image_hint", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCAmelCase_ : Union[str, Any] = UNetaDConditionModel(**_snake_case ) return model @property def UpperCamelCase__ ( self ): return { "block_out_channels": [32, 32, 64, 64], "down_block_types": [ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "AttnDownEncoderBlock2D", ], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"], "vq_embed_dim": 4, } @property def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : str = VQModel(**self.dummy_movq_kwargs ) return model def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = self.dummy_unet UpperCAmelCase_ : Dict = self.dummy_movq UpperCAmelCase_ : Dict = { "num_train_timesteps": 10_00, "beta_schedule": "linear", "beta_start": 0.00085, "beta_end": 0.012, "clip_sample": False, "set_alpha_to_one": False, "steps_offset": 0, "prediction_type": "epsilon", "thresholding": False, } UpperCAmelCase_ : Any = DDIMScheduler(**_snake_case ) UpperCAmelCase_ : Any = { "unet": unet, "scheduler": scheduler, "movq": movq, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): UpperCAmelCase_ : Optional[int] = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(_snake_case ) ).to(_snake_case ) UpperCAmelCase_ : int = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(seed + 1 ) ).to( _snake_case ) # create init_image UpperCAmelCase_ : int = floats_tensor((1, 3, 64, 64) ,rng=random.Random(_snake_case ) ).to(_snake_case ) UpperCAmelCase_ : str = image.cpu().permute(0 ,2 ,3 ,1 )[0] UpperCAmelCase_ : Optional[Any] = Image.fromarray(np.uinta(_snake_case ) ).convert("RGB" ).resize((2_56, 2_56) ) # create hint UpperCAmelCase_ : Optional[Any] = floats_tensor((1, 3, 64, 64) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : int = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : str = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Optional[Any] = { "image": init_image, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "hint": hint, "generator": generator, "height": 64, "width": 64, "num_inference_steps": 10, "guidance_scale": 7.0, "strength": 0.2, "output_type": "np", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" UpperCAmelCase_ : Tuple = self.get_dummy_components() UpperCAmelCase_ : List[Any] = self.pipeline_class(**_snake_case ) UpperCAmelCase_ : List[Any] = pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = pipe(**self.get_dummy_inputs(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = output.images UpperCAmelCase_ : Dict = pipe( **self.get_dummy_inputs(_snake_case ) ,return_dict=_snake_case ,)[0] UpperCAmelCase_ : Optional[Any] = image[0, -3:, -3:, -1] UpperCAmelCase_ : int = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase_ : Union[str, Any] = np.array( [0.54985034, 0.55509365, 0.52561504, 0.5570494, 0.5593818, 0.5263979, 0.50285643, 0.5069846, 0.51196736] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/kandinskyv22_controlnet_img2img_robotcat_fp16.npy" ) UpperCAmelCase_ : Tuple = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase_ : List[Any] = init_image.resize((5_12, 5_12) ) UpperCAmelCase_ : Union[str, Any] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/hint_image_cat.png" ) UpperCAmelCase_ : int = torch.from_numpy(np.array(_snake_case ) ).float() / 255.0 UpperCAmelCase_ : Tuple = hint.permute(2 ,0 ,1 ).unsqueeze(0 ) UpperCAmelCase_ : Optional[int] = "A robot, 4k photo" UpperCAmelCase_ : str = KandinskyVaaPriorEmbaEmbPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-prior" ,torch_dtype=torch.floataa ) pipe_prior.to(_snake_case ) UpperCAmelCase_ : int = KandinskyVaaControlnetImgaImgPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-controlnet-depth" ,torch_dtype=torch.floataa ) UpperCAmelCase_ : Dict = pipeline.to(_snake_case ) pipeline.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ , UpperCAmelCase_ : Tuple = pipe_prior( _snake_case ,image=_snake_case ,strength=0.85 ,generator=_snake_case ,negative_prompt="" ,).to_tuple() UpperCAmelCase_ : Optional[Any] = pipeline( image=_snake_case ,image_embeds=_snake_case ,negative_image_embeds=_snake_case ,hint=_snake_case ,generator=_snake_case ,num_inference_steps=1_00 ,height=5_12 ,width=5_12 ,strength=0.5 ,output_type="np" ,) UpperCAmelCase_ : Optional[Any] = output.images[0] assert image.shape == (5_12, 5_12, 3) assert_mean_pixel_difference(_snake_case ,_snake_case )
71
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _snake_case (nn.Module): def __init__( self ,_snake_case = 16 ,_snake_case = 88 ,_snake_case = None ,_snake_case = 1 ,_snake_case = 0.0 ,_snake_case = 32 ,_snake_case = None ,_snake_case = False ,_snake_case = None ,_snake_case = None ,_snake_case = "geglu" ,_snake_case = None ,): super().__init__() UpperCAmelCase_ : Optional[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case ,attention_head_dim=_snake_case ,in_channels=_snake_case ,num_layers=_snake_case ,dropout=_snake_case ,norm_num_groups=_snake_case ,cross_attention_dim=_snake_case ,attention_bias=_snake_case ,sample_size=_snake_case ,num_vector_embeds=_snake_case ,activation_fn=_snake_case ,num_embeds_ada_norm=_snake_case ,) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference UpperCAmelCase_ : List[str] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` UpperCAmelCase_ : int = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` UpperCAmelCase_ : List[Any] = [1, 0] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = True ,): UpperCAmelCase_ : List[str] = hidden_states UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[int] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens UpperCAmelCase_ : Any = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] UpperCAmelCase_ : Any = self.transformer_index_for_condition[i] UpperCAmelCase_ : int = self.transformers[transformer_index]( _snake_case ,encoder_hidden_states=_snake_case ,timestep=_snake_case ,cross_attention_kwargs=_snake_case ,return_dict=_snake_case ,)[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] UpperCAmelCase_ : Dict = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) UpperCAmelCase_ : List[Any] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
71
1
'''simple docstring''' import argparse import os import re _lowerCamelCase = """src/diffusers""" # Pattern that looks at the indentation in a line. _lowerCamelCase = re.compile(R"""^(\s*)\S""") # Pattern that matches `"key":" and puts `key` in group 0. _lowerCamelCase = re.compile(R"""^\s*\"([^\"]+)\":""") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. _lowerCamelCase = re.compile(R"""^\s*_import_structure\[\"([^\"]+)\"\]""") # Pattern that matches `"key",` and puts `key` in group 0. _lowerCamelCase = re.compile(R"""^\s*\"([^\"]+)\",\s*$""") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. _lowerCamelCase = re.compile(R"""\[([^\]]+)\]""") def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : int = _re_indent.search(_SCREAMING_SNAKE_CASE ) return "" if search is None else search.groups()[0] def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : List[str]="" , _SCREAMING_SNAKE_CASE : Optional[Any]=None , _SCREAMING_SNAKE_CASE : Tuple=None ) -> Dict: """simple docstring""" UpperCAmelCase_ : str = 0 UpperCAmelCase_ : List[Any] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(_SCREAMING_SNAKE_CASE ): index += 1 UpperCAmelCase_ : Optional[Any] = ["\n".join(lines[:index] )] else: UpperCAmelCase_ : Optional[int] = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). UpperCAmelCase_ : Tuple = [lines[index]] index += 1 while index < len(_SCREAMING_SNAKE_CASE ) and (end_prompt is None or not lines[index].startswith(_SCREAMING_SNAKE_CASE )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(_SCREAMING_SNAKE_CASE ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(_SCREAMING_SNAKE_CASE ) ) if index < len(_SCREAMING_SNAKE_CASE ) - 1: UpperCAmelCase_ : int = [lines[index + 1]] index += 1 else: UpperCAmelCase_ : Any = [] else: blocks.append("\n".join(_SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : str = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(_SCREAMING_SNAKE_CASE ) > 0: blocks.append("\n".join(_SCREAMING_SNAKE_CASE ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(_SCREAMING_SNAKE_CASE ): blocks.append("\n".join(lines[index:] ) ) return blocks def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" def _inner(_SCREAMING_SNAKE_CASE : List[Any] ): return key(_SCREAMING_SNAKE_CASE ).lower().replace("_" , "" ) return _inner def a__ ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : List[Any]=None ) -> Optional[Any]: """simple docstring""" def noop(_SCREAMING_SNAKE_CASE : str ): return x if key is None: UpperCAmelCase_ : Optional[Any] = noop # Constants are all uppercase, they go first. UpperCAmelCase_ : str = [obj for obj in objects if key(_SCREAMING_SNAKE_CASE ).isupper()] # Classes are not all uppercase but start with a capital, they go second. UpperCAmelCase_ : List[Any] = [obj for obj in objects if key(_SCREAMING_SNAKE_CASE )[0].isupper() and not key(_SCREAMING_SNAKE_CASE ).isupper()] # Functions begin with a lowercase, they go last. UpperCAmelCase_ : Any = [obj for obj in objects if not key(_SCREAMING_SNAKE_CASE )[0].isupper()] UpperCAmelCase_ : Tuple = ignore_underscore(_SCREAMING_SNAKE_CASE ) return sorted(_SCREAMING_SNAKE_CASE , key=_SCREAMING_SNAKE_CASE ) + sorted(_SCREAMING_SNAKE_CASE , key=_SCREAMING_SNAKE_CASE ) + sorted(_SCREAMING_SNAKE_CASE , key=_SCREAMING_SNAKE_CASE ) def a__ ( _SCREAMING_SNAKE_CASE : int ) -> str: """simple docstring""" def _replace(_SCREAMING_SNAKE_CASE : int ): UpperCAmelCase_ : Dict = match.groups()[0] if "," not in imports: return F'''[{imports}]''' UpperCAmelCase_ : Union[str, Any] = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase_ : Dict = keys[:-1] return "[" + ", ".join([F'''"{k}"''' for k in sort_objects(_SCREAMING_SNAKE_CASE )] ) + "]" UpperCAmelCase_ : str = import_statement.split("\n" ) if len(_SCREAMING_SNAKE_CASE ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. UpperCAmelCase_ : Tuple = 2 if lines[1].strip() == "[" else 1 UpperCAmelCase_ : Optional[int] = [(i, _re_strip_line.search(_SCREAMING_SNAKE_CASE ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] UpperCAmelCase_ : str = sort_objects(_SCREAMING_SNAKE_CASE , key=lambda _SCREAMING_SNAKE_CASE : x[1] ) UpperCAmelCase_ : Optional[int] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(_SCREAMING_SNAKE_CASE ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: UpperCAmelCase_ : Optional[Any] = _re_bracket_content.sub(_replace , lines[1] ) else: UpperCAmelCase_ : Tuple = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase_ : Union[str, Any] = keys[:-1] UpperCAmelCase_ : Union[str, Any] = get_indent(lines[1] ) + ", ".join([F'''"{k}"''' for k in sort_objects(_SCREAMING_SNAKE_CASE )] ) return "\n".join(_SCREAMING_SNAKE_CASE ) else: # Finally we have to deal with imports fitting on one line UpperCAmelCase_ : int = _re_bracket_content.sub(_replace , _SCREAMING_SNAKE_CASE ) return import_statement def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : List[Any]=True ) -> List[str]: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , "r" ) as f: UpperCAmelCase_ : Union[str, Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 UpperCAmelCase_ : Any = split_code_in_indented_blocks( _SCREAMING_SNAKE_CASE , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(_SCREAMING_SNAKE_CASE ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. UpperCAmelCase_ : Optional[Any] = main_blocks[block_idx] UpperCAmelCase_ : List[Any] = block.split("\n" ) # Get to the start of the imports. UpperCAmelCase_ : List[Any] = 0 while line_idx < len(_SCREAMING_SNAKE_CASE ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: UpperCAmelCase_ : List[str] = len(_SCREAMING_SNAKE_CASE ) else: line_idx += 1 if line_idx >= len(_SCREAMING_SNAKE_CASE ): continue # Ignore beginning and last line: they don't contain anything. UpperCAmelCase_ : Union[str, Any] = "\n".join(block_lines[line_idx:-1] ) UpperCAmelCase_ : Optional[Any] = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. UpperCAmelCase_ : Tuple = split_code_in_indented_blocks(_SCREAMING_SNAKE_CASE , indent_level=_SCREAMING_SNAKE_CASE ) # We have two categories of import key: list or _import_structure[key].append/extend UpperCAmelCase_ : Optional[int] = _re_direct_key if "_import_structure" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. UpperCAmelCase_ : int = [(pattern.search(_SCREAMING_SNAKE_CASE ).groups()[0] if pattern.search(_SCREAMING_SNAKE_CASE ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. UpperCAmelCase_ : Optional[int] = [(i, key) for i, key in enumerate(_SCREAMING_SNAKE_CASE ) if key is not None] UpperCAmelCase_ : str = [x[0] for x in sorted(_SCREAMING_SNAKE_CASE , key=lambda _SCREAMING_SNAKE_CASE : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. UpperCAmelCase_ : Tuple = 0 UpperCAmelCase_ : Any = [] for i in range(len(_SCREAMING_SNAKE_CASE ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: UpperCAmelCase_ : Union[str, Any] = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(_SCREAMING_SNAKE_CASE ) count += 1 # And we put our main block back together with its first and last line. UpperCAmelCase_ : Dict = "\n".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(_SCREAMING_SNAKE_CASE ): if check_only: return True else: print(F'''Overwriting {file}.''' ) with open(_SCREAMING_SNAKE_CASE , "w" ) as f: f.write("\n".join(_SCREAMING_SNAKE_CASE ) ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any]=True ) -> Dict: """simple docstring""" UpperCAmelCase_ : Tuple = [] for root, _, files in os.walk(_SCREAMING_SNAKE_CASE ): if "__init__.py" in files: UpperCAmelCase_ : str = sort_imports(os.path.join(_SCREAMING_SNAKE_CASE , "__init__.py" ) , check_only=_SCREAMING_SNAKE_CASE ) if result: UpperCAmelCase_ : List[Any] = [os.path.join(_SCREAMING_SNAKE_CASE , "__init__.py" )] if len(_SCREAMING_SNAKE_CASE ) > 0: raise ValueError(F'''Would overwrite {len(_SCREAMING_SNAKE_CASE )} files, run `make style`.''' ) if __name__ == "__main__": _lowerCamelCase = argparse.ArgumentParser() parser.add_argument("""--check_only""", action="""store_true""", help="""Whether to only check or fix style.""") _lowerCamelCase = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
71
'''simple docstring''' import json import sys def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : int ) -> Tuple: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: UpperCAmelCase_ : Dict = json.load(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = ["<details>", "<summary>Show updated benchmarks!</summary>", " "] for benchmark_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = results[benchmark_name] UpperCAmelCase_ : Any = benchmark_name.split("/" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) UpperCAmelCase_ : Any = "| metric |" UpperCAmelCase_ : Any = "|--------|" UpperCAmelCase_ : Union[str, Any] = "| new / old (diff) |" for metric_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = benchmark_res[metric_name] UpperCAmelCase_ : Union[str, Any] = metric_vals["new"] UpperCAmelCase_ : Optional[Any] = metric_vals.get("old" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = metric_vals.get("diff" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = F''' {new_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("</details>" ) with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.writelines("\n".join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": _lowerCamelCase = sys.argv[1] _lowerCamelCase = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
71
1
'''simple docstring''' import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCamelCase = logging.get_logger(__name__) _lowerCamelCase = { """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""", """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""", """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""", """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""", """self_attn_layer_norm""": """encoder.layers.*.layer_norm""", """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""", """fc2""": """encoder.layers.*.feed_forward.output_dense""", """final_layer_norm""": """encoder.layers.*.final_layer_norm""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } _lowerCamelCase = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Dict ) -> str: """simple docstring""" for attribute in key.split("." ): UpperCAmelCase_ : List[Any] = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if weight_type is not None: UpperCAmelCase_ : List[str] = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).shape else: UpperCAmelCase_ : Optional[Any] = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": UpperCAmelCase_ : Dict = value elif weight_type == "weight_g": UpperCAmelCase_ : Optional[int] = value elif weight_type == "weight_v": UpperCAmelCase_ : Union[str, Any] = value elif weight_type == "bias": UpperCAmelCase_ : Union[str, Any] = value else: UpperCAmelCase_ : int = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Tuple ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : int = [] UpperCAmelCase_ : Tuple = fairseq_model.state_dict() UpperCAmelCase_ : Any = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight UpperCAmelCase_ : Optional[int] = None for name, value in fairseq_dict.items(): UpperCAmelCase_ : Optional[Any] = False if "conv_layers" in name: load_conv_layer( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , hf_model.config.feat_extract_norm == "group" , ) UpperCAmelCase_ : List[Any] = True elif name.split("." )[0] == "proj": UpperCAmelCase_ : Dict = fairseq_model.proj UpperCAmelCase_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: UpperCAmelCase_ : Dict = True if "*" in mapped_key: UpperCAmelCase_ : Optional[Any] = name.split(_SCREAMING_SNAKE_CASE )[0].split("." )[-2] UpperCAmelCase_ : Union[str, Any] = mapped_key.replace("*" , _SCREAMING_SNAKE_CASE ) if "weight_g" in name: UpperCAmelCase_ : Any = "weight_g" elif "weight_v" in name: UpperCAmelCase_ : Any = "weight_v" elif "bias" in name: UpperCAmelCase_ : Dict = "bias" elif "weight" in name: UpperCAmelCase_ : str = "weight" else: UpperCAmelCase_ : List[str] = None set_recursively(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) continue if not is_used: unused_weights.append(_SCREAMING_SNAKE_CASE ) logger.warning(F'''Unused weights: {unused_weights}''' ) return proj_weight def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Dict ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = full_name.split("conv_layers." )[-1] UpperCAmelCase_ : Dict = name.split("." ) UpperCAmelCase_ : Optional[Any] = int(items[0] ) UpperCAmelCase_ : Any = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) UpperCAmelCase_ : Tuple = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) UpperCAmelCase_ : Optional[Any] = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) UpperCAmelCase_ : Union[str, Any] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) UpperCAmelCase_ : List[str] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(_SCREAMING_SNAKE_CASE ) def a__ ( _SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Tuple = emb.weight.shape UpperCAmelCase_ : int = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = emb.weight.data return lin_layer def a__ ( _SCREAMING_SNAKE_CASE : List[str] ) -> int: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , "r" , encoding="utf-8" ) as f: UpperCAmelCase_ : Optional[Any] = f.readlines() UpperCAmelCase_ : Any = [line.split(" " )[0] for line in lines] UpperCAmelCase_ : int = len(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_SCREAMING_SNAKE_CASE , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def a__ ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : List[Any] , ) -> int: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = WavaVecaConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = SpeechaTextaConfig.from_pretrained( _SCREAMING_SNAKE_CASE , vocab_size=_SCREAMING_SNAKE_CASE , decoder_layers=_SCREAMING_SNAKE_CASE , do_stable_layer_norm=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) UpperCAmelCase_ : Any = model[0].eval() # set weights for wav2vec2 encoder UpperCAmelCase_ : Optional[int] = WavaVecaModel(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = recursively_load_weights_wavaveca(model.encoder , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = SpeechaTextaForCausalLM(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_SCREAMING_SNAKE_CASE ) # set output linear layer unexpected_keys.remove("embed_out" ) UpperCAmelCase_ : str = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F'''The following keys are missing when loading the decoder weights: {missing_keys}''' ) logger.warning(F'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}''' ) UpperCAmelCase_ : Optional[int] = SpeechEncoderDecoderModel(encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = False # add projection layer UpperCAmelCase_ : Any = nn.Parameter(projection_layer.weight ) UpperCAmelCase_ : Tuple = nn.Parameter(projection_layer.bias ) UpperCAmelCase_ : int = create_vocab_dict(_SCREAMING_SNAKE_CASE ) with open(os.path.join(_SCREAMING_SNAKE_CASE , "vocab.json" ) , "w" ) as fp: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = SpeechaTextaTokenizer(os.path.join(_SCREAMING_SNAKE_CASE , "vocab.json" ) ) tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = hf_wavavec.config.to_dict() UpperCAmelCase_ : Tuple = tokenizer.pad_token_id UpperCAmelCase_ : Dict = tokenizer.bos_token_id UpperCAmelCase_ : Dict = tokenizer.eos_token_id UpperCAmelCase_ : int = "speech_to_text_2" UpperCAmelCase_ : Union[str, Any] = "wav2vec2" UpperCAmelCase_ : int = SpeechEncoderDecoderConfig.from_dict(_SCREAMING_SNAKE_CASE ) hf_wavavec.save_pretrained(_SCREAMING_SNAKE_CASE ) feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": _lowerCamelCase = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument( """--encoder_config_path""", default="""facebook/wav2vec2-large-lv60""", type=str, help="""Path to hf encoder wav2vec2 checkpoint config""", ) parser.add_argument( """--decoder_config_path""", default="""facebook/s2t-small-mustc-en-fr-st""", type=str, help="""Path to hf decoder s2t checkpoint config""", ) parser.add_argument("""--vocab_size""", default=1_0224, type=int, help="""Vocab size of decoder""") parser.add_argument("""--num_decoder_layers""", default=7, type=int, help="""Number of decoder layers""") _lowerCamelCase = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
71
'''simple docstring''' import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase = get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[int] =DebertaVaTokenizer __A : Union[str, Any] =DebertaVaTokenizerFast __A : str =True __A : List[str] =True def UpperCamelCase__ ( self ): super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase_ : Optional[int] = DebertaVaTokenizer(_snake_case ,unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[Any] = "this is a test" UpperCAmelCase_ : Optional[Any] = "this is a test" return input_text, output_text def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = "<pad>" UpperCAmelCase_ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case ) ,_snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case ) ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"<pad>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"[PAD]" ) self.assertEqual(len(_snake_case ) ,3_00_01 ) def UpperCamelCase__ ( self ): self.assertEqual(self.get_tokenizer().vocab_size ,3_00_00 ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : str = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : Union[str, Any] = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on UpperCAmelCase_ : Tuple = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Dict = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[str] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : List[Any] = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_tokenizer() UpperCAmelCase_ : Union[str, Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Dict = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) UpperCAmelCase_ : Optional[Any] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) UpperCAmelCase_ : int = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Union[str, Any] = tokenizer.encode(_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = "This is a test" UpperCAmelCase_ : Optional[int] = [13, 1, 43_98, 25, 21, 12_89] UpperCAmelCase_ : Optional[Any] = ["▁", "T", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : str = DebertaVaTokenizer(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[str] = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) # fmt: off UpperCAmelCase_ : List[str] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = [13, 1, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] UpperCAmelCase_ : str = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[str] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode("sequence builders" ) UpperCAmelCase_ : Dict = tokenizer.encode("multi-sequence build" ) UpperCAmelCase_ : Tuple = tokenizer.build_inputs_with_special_tokens(_snake_case ) UpperCAmelCase_ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(_snake_case ,_snake_case ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] ,_snake_case ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] ,_snake_case ,) @slow def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Union[str, Any] = {"input_ids": [[1, 3_98_67, 36, 1_93_90, 4_86, 27, 3_50_52, 8_14_36, 18, 6_06_85, 12_25, 7, 3_50_52, 8_14_36, 18, 93_67, 1_68_99, 18, 1_59_37, 53, 5_94, 7_73, 18, 1_62_87, 3_04_65, 36, 1_59_37, 6, 4_11_39, 38, 3_69_79, 6_07_63, 1_91, 6, 3_41_32, 99, 6, 5_05_38, 3_90, 4_32_30, 6, 3_41_32, 27_79, 2_08_50, 14, 6_99, 10_72, 11_94, 36, 3_82, 1_09_01, 53, 7, 6_99, 10_72, 20_84, 36, 2_04_22, 6_30, 53, 19, 1_05, 30_49, 18_96, 10_53, 1_68_99, 15_06, 11, 3_79_78, 42_43, 7, 12_37, 3_18_69, 2_00, 1_65_66, 6_54, 6, 3_50_52, 8_14_36, 7, 5_56_30, 1_35_93, 4, 2], [1, 26, 1_50_11, 13, 6_67, 8, 10_53, 18, 2_36_11, 12_37, 7_23_56, 1_28_20, 34, 10_41_34, 12_09, 35, 1_33_13, 66_27, 21, 2_02, 3_47, 7, 1_64, 23_99, 11, 46, 44_85, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 12_32, 28_64, 1_57_85, 1_49_51, 1_05, 5, 85_81, 12_50, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_snake_case ,model_name="microsoft/deberta-v2-xlarge" ,revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" ,)
71
1
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise TypeError("Input value must be an 'int' type" ) UpperCAmelCase_ : Union[str, Any] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
71
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase = logging.get_logger(__name__) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Optional[int] ="timm_backbone" def __init__( self ,_snake_case=None ,_snake_case=3 ,_snake_case=True ,_snake_case=True ,_snake_case=None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Tuple = backbone UpperCAmelCase_ : List[str] = num_channels UpperCAmelCase_ : str = features_only UpperCAmelCase_ : int = use_pretrained_backbone UpperCAmelCase_ : Tuple = True UpperCAmelCase_ : str = out_indices if out_indices is not None else (-1,)
71
'''simple docstring''' from math import factorial def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k" ) return factorial(_SCREAMING_SNAKE_CASE ) // (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) if __name__ == "__main__": print( """The number of five-card hands possible from a standard""", f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( """If a class of 40 students must be arranged into groups of""", f"""4 for group projects, there are {combinations(40, 4)} ways""", """to arrange them.\n""", ) print( """If 10 teams are competing in a Formula One race, there""", f"""are {combinations(10, 3)} ways that first, second and""", """third place can be awarded.""", )
71
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE__ : Optional[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ : int = ["""MLukeTokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys SCREAMING_SNAKE_CASE__ : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
0
'''simple docstring''' import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =VideoToVideoSDPipeline __A : Tuple =TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"} __A : Union[str, Any] =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"} __A : str =PipelineTesterMixin.required_optional_params - {"latents"} __A : Dict =False # No `output_type`. __A : Optional[int] =frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") ,cross_attention_dim=32 ,attention_head_dim=4 ,) UpperCAmelCase_ : int = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=1_28 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=10_00 ,hidden_act="gelu" ,projection_dim=5_12 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): # 3 frames UpperCAmelCase_ : Dict = floats_tensor((1, 3, 3, 32, 32) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Tuple = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : Tuple = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : str = VideoToVideoSDPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = "np" UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ).frames UpperCAmelCase_ : Tuple = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) UpperCAmelCase_ : Dict = np.array([1_06, 1_17, 1_13, 1_74, 1_37, 1_12, 1_48, 1_51, 1_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ,expected_max_diff=5E-3 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): return super().test_progress_bar() @slow @skip_mps class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" ,torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames UpperCAmelCase_ : str = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ : int = torch.randn((1, 10, 3, 10_24, 5_76) ,generator=_snake_case ) UpperCAmelCase_ : List[Any] = video.to("cuda" ) UpperCAmelCase_ : List[Any] = "Spiderman is surfing" UpperCAmelCase_ : Optional[Any] = pipe(_snake_case ,video=_snake_case ,generator=_snake_case ,num_inference_steps=3 ,output_type="pt" ).frames UpperCAmelCase_ : Any = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
71
0
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class __lowerCamelCase (unittest.TestCase ): def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = 10 def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4] __UpperCamelCase = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(A_,self.block_size,0 ),A_ ) def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(A_,self.block_size,0 ),A_ ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(A_,self.block_size,0 ),A_ ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' __UpperCamelCase, __UpperCamelCase = process_story(A_ ) self.assertEqual(A_,[] ) def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = '' __UpperCamelCase, __UpperCamelCase = process_story(A_ ) self.assertEqual(A_,[] ) self.assertEqual(A_,[] ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) __UpperCamelCase, __UpperCamelCase = process_story(A_ ) __UpperCamelCase = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(A_,A_ ) __UpperCamelCase = ['It was the best of times.'] self.assertEqual(A_,A_ ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = torch.tensor([1, 2, 3, 4] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(A_,0 ).numpy(),expected.numpy() ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(A_,23 ).numpy(),expected.numpy() ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(A_,1 ).numpy(),expected.numpy() ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = 101 __UpperCamelCase = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) __UpperCamelCase = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) __UpperCamelCase = compute_token_type_ids(A_,A_ ) np.testing.assert_array_equal(A_,A_ )
1
'''simple docstring''' import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) _lowerCamelCase = pytest.mark.integration @pytest.mark.parametrize("path" , ["paws", "csv"] ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : str ) -> Tuple: """simple docstring""" inspect_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.parametrize("path" , ["accuracy"] ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: """simple docstring""" inspect_metric(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, config_name, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict ) -> str: """simple docstring""" UpperCAmelCase_ : int = get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : List[str] ) -> str: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, expected" , [ ("squad", "plain_text"), ("acronym_identification", "default"), ("lhoestq/squad", "plain_text"), ("lhoestq/test", "default"), ("lhoestq/demo1", "lhoestq--demo1"), ("dalle-mini/wit", "dalle-mini--wit"), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_config_names(_SCREAMING_SNAKE_CASE ) assert expected in config_names @pytest.mark.parametrize( "path, expected_configs, expected_splits_in_first_config" , [ ("squad", ["plain_text"], ["train", "validation"]), ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]), ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str] ) -> Any: """simple docstring""" UpperCAmelCase_ : Any = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert list(infos.keys() ) == expected_configs UpperCAmelCase_ : Optional[Any] = expected_configs[0] assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( "path, expected_config, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ) -> Any: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : str ) -> Any: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_split_names(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE )
71
0
import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { """facebook/encodec_24khz""": """https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json""", """facebook/encodec_48khz""": """https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json""", } class lowerCamelCase__ ( _A): """simple docstring""" a__ : List[Any] = "encodec" def __init__( self : Union[str, Any] , __lowerCAmelCase : List[str]=[1.5, 3.0, 6.0, 12.0, 24.0] , __lowerCAmelCase : Optional[Any]=2_40_00 , __lowerCAmelCase : int=1 , __lowerCAmelCase : Union[str, Any]=False , __lowerCAmelCase : List[Any]=None , __lowerCAmelCase : Optional[Any]=None , __lowerCAmelCase : Dict=1_28 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : int=1 , __lowerCAmelCase : List[str]=[8, 5, 4, 2] , __lowerCAmelCase : Optional[int]="weight_norm" , __lowerCAmelCase : Optional[Any]=7 , __lowerCAmelCase : Union[str, Any]=7 , __lowerCAmelCase : Optional[int]=3 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Dict="reflect" , __lowerCAmelCase : List[Any]=2 , __lowerCAmelCase : Union[str, Any]=2 , __lowerCAmelCase : Optional[Any]=1.0 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : str=True , **__lowerCAmelCase : str , ) -> str: _A = target_bandwidths _A = sampling_rate _A = audio_channels _A = normalize _A = chunk_length_s _A = overlap _A = hidden_size _A = num_filters _A = num_residual_layers _A = upsampling_ratios _A = norm_type _A = kernel_size _A = last_kernel_size _A = residual_kernel_size _A = dilation_growth_rate _A = use_causal_conv _A = pad_mode _A = compress _A = num_lstm_layers _A = trim_right_ratio _A = codebook_size _A = codebook_dim if codebook_dim is not None else hidden_size _A = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f'''self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}''' ) super().__init__(**__lowerCAmelCase ) @property def snake_case_ ( self : Dict ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def snake_case_ ( self : int ) -> Optional[int]: if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def snake_case_ ( self : Optional[Any] ) -> int: _A = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def snake_case_ ( self : int ) -> int: return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
2
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
0
'''simple docstring''' from __future__ import annotations import bisect def A_( A : list[int] , A : int , A : int = 0 , A : int = -1): if hi < 0: UpperCamelCase = len(A) while lo < hi: UpperCamelCase = lo + (hi - lo) // 2 if sorted_collection[mid] < item: UpperCamelCase = mid + 1 else: UpperCamelCase = mid return lo def A_( A : list[int] , A : int , A : int = 0 , A : int = -1): if hi < 0: UpperCamelCase = len(A) while lo < hi: UpperCamelCase = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: UpperCamelCase = mid + 1 else: UpperCamelCase = mid return lo def A_( A : list[int] , A : int , A : int = 0 , A : int = -1): sorted_collection.insert(bisect_left(A , A , A , A) , A) def A_( A : list[int] , A : int , A : int = 0 , A : int = -1): sorted_collection.insert(bisect_right(A , A , A , A) , A) def A_( A : list[int] , A : int): UpperCamelCase = 0 UpperCamelCase = len(A) - 1 while left <= right: UpperCamelCase = left + (right - left) // 2 UpperCamelCase = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: UpperCamelCase = midpoint - 1 else: UpperCamelCase = midpoint + 1 return None def A_( A : list[int] , A : int): UpperCamelCase = bisect.bisect_left(A , A) if index != len(A) and sorted_collection[index] == item: return index return None def A_( A : list[int] , A : int , A : int , A : int): if right < left: return None UpperCamelCase = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(A , A , A , midpoint - 1) else: return binary_search_by_recursion(A , A , midpoint + 1 , A) if __name__ == "__main__": lowerCAmelCase : Tuple = input('Enter numbers separated by comma:\n').strip() lowerCAmelCase : int = sorted(int(item) for item in user_input.split(',')) lowerCAmelCase : Optional[Any] = int(input('Enter a single number to be found in the list:\n')) lowerCAmelCase : Any = binary_search(collection, target) if result is None: print(f"""{target} was not found in {collection}.""") else: print(f"""{target} was found at position {result} in {collection}.""")
3
'''simple docstring''' import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Any =AudioLDMPipeline __A : Dict =TEXT_TO_AUDIO_PARAMS __A : Any =TEXT_TO_AUDIO_BATCH_PARAMS __A : Tuple =frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = 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, 64) ,class_embed_type="simple_projection" ,projection_class_embeddings_input_dim=32 ,class_embeddings_concat=_snake_case ,) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=1 ,out_channels=1 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = ClapTextConfig( 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=10_00 ,projection_dim=32 ,) UpperCAmelCase_ : Optional[Any] = ClapTextModelWithProjection(_snake_case ) UpperCAmelCase_ : List[Any] = RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,model_max_length=77 ) UpperCAmelCase_ : Optional[int] = SpeechTaHifiGanConfig( model_in_dim=8 ,sampling_rate=1_60_00 ,upsample_initial_channel=16 ,upsample_rates=[2, 2] ,upsample_kernel_sizes=[4, 4] ,resblock_kernel_sizes=[3, 7] ,resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] ,normalize_before=_snake_case ,) UpperCAmelCase_ : Union[str, Any] = SpeechTaHifiGan(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "vocoder": vocoder, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Optional[int] = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[str] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Any = { "prompt": "A hammer hitting a wooden surface", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : str = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : Any = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : int = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Dict = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Tuple = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[str] = output.audios[0] UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : str = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : Dict = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : Optional[Any] = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Tuple = F.normalize(_snake_case ,dim=-1 ) UpperCAmelCase_ : int = prompt_embeds # forward UpperCAmelCase_ : int = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = 3 * ["this is a negative prompt"] UpperCAmelCase_ : Any = negative_prompt UpperCAmelCase_ : Union[str, Any] = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Dict = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : List[Any] = [] for p in [prompt, negative_prompt]: UpperCAmelCase_ : Any = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : List[Any] = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : List[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Any = F.normalize(_snake_case ,dim=-1 ) embeds.append(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = embeds # forward UpperCAmelCase_ : Tuple = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Any = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : int = "egg cracking" UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : int = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : List[Any] = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[str] = self.get_dummy_components() UpperCAmelCase_ : Dict = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Any = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Dict = "A hammer hitting a wooden surface" # test num_waveforms_per_prompt=1 (default) UpperCAmelCase_ : Any = audioldm_pipe(_snake_case ,num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : Dict = audioldm_pipe([prompt] * batch_size ,num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : List[Any] = audioldm_pipe(_snake_case ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = audioldm_pipe( [prompt] * batch_size ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Union[str, Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe.vocoder.config.sampling_rate UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = audioldm_pipe(audio_length_in_s=0.016 ,**_snake_case ) UpperCAmelCase_ : str = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.016 UpperCAmelCase_ : List[Any] = audioldm_pipe(audio_length_in_s=0.032 ,**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.032 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : str = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : int = ["hey"] UpperCAmelCase_ : Dict = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : Any = output.audios.shape assert audio_shape == (1, 2_56) UpperCAmelCase_ : Tuple = audioldm_pipe.vocoder.config config.model_in_dim *= 2 UpperCAmelCase_ : List[Any] = SpeechTaHifiGan(_snake_case ).to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : int = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def UpperCamelCase__ ( self ): self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case ) def UpperCamelCase__ ( self ): self._test_inference_batch_single_identical(test_mean_pixel_difference=_snake_case ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ) @slow class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case ,_snake_case="cpu" ,_snake_case=torch.floataa ,_snake_case=0 ): UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : str = np.random.RandomState(_snake_case ).standard_normal((1, 8, 1_28, 16) ) UpperCAmelCase_ : Optional[Any] = torch.from_numpy(_snake_case ).to(device=_snake_case ,dtype=_snake_case ) UpperCAmelCase_ : List[str] = { "prompt": "A hammer hitting a wooden surface", "latents": latents, "generator": generator, "num_inference_steps": 3, "guidance_scale": 2.5, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : Optional[int] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[Any] = self.get_inputs(_snake_case ) UpperCAmelCase_ : List[Any] = 25 UpperCAmelCase_ : Union[str, Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Union[str, Any] = audio[7_72_30:7_72_40] UpperCAmelCase_ : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) UpperCAmelCase_ : Dict = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : List[Any] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Any = audio[2_77_80:2_77_90] UpperCAmelCase_ : List[str] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) UpperCAmelCase_ : Union[str, Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
71
0
"""simple docstring""" import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class a : def __init__( self , _snake_case , _snake_case=2 , _snake_case=8 , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=99 , _snake_case=16 , _snake_case=5 , _snake_case=2 , _snake_case=36 , _snake_case="gelu" , _snake_case=0.0 , _snake_case=0.0 , _snake_case=5_12 , _snake_case=16 , _snake_case=2 , _snake_case=0.02 , _snake_case=3 , _snake_case=4 , _snake_case=None , ): """simple docstring""" lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = seq_length lowerCAmelCase = is_training lowerCAmelCase = use_input_mask lowerCAmelCase = use_token_type_ids lowerCAmelCase = use_labels lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_act lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = max_position_embeddings lowerCAmelCase = type_vocab_size lowerCAmelCase = type_sequence_label_size lowerCAmelCase = initializer_range lowerCAmelCase = num_labels lowerCAmelCase = num_choices lowerCAmelCase = scope def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase = None if self.use_input_mask: lowerCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase = None if self.use_token_type_ids: lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = None if self.use_labels: lowerCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ ( self ): """simple docstring""" return MraConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_snake_case , initializer_range=self.initializer_range , ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.get_config() lowerCAmelCase = 3_00 return config def UpperCamelCase__ ( self ): """simple docstring""" ( ( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) , ) = self.prepare_config_and_inputs() lowerCAmelCase = True lowerCAmelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = MraModel(config=_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model(_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case ) lowerCAmelCase = model(_snake_case , token_type_ids=_snake_case ) lowerCAmelCase = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , ): """simple docstring""" lowerCAmelCase = True lowerCAmelCase = MraModel(_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model( _snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , encoder_hidden_states=_snake_case , encoder_attention_mask=_snake_case , ) lowerCAmelCase = model( _snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , encoder_hidden_states=_snake_case , ) lowerCAmelCase = model(_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = MraForMaskedLM(config=_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model(_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = MraForQuestionAnswering(config=_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model( _snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , start_positions=_snake_case , end_positions=_snake_case , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = self.num_labels lowerCAmelCase = MraForSequenceClassification(_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model(_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = self.num_labels lowerCAmelCase = MraForTokenClassification(config=_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = model(_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = self.num_choices lowerCAmelCase = MraForMultipleChoice(config=_snake_case ) model.to(_snake_case ) model.eval() lowerCAmelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase = model( _snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) ,( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class a ( a__ , unittest.TestCase ): snake_case__ = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) snake_case__ = False snake_case__ = False snake_case__ = False snake_case__ = False snake_case__ = () def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = MraModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def UpperCamelCase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase = type self.model_tester.create_and_check_model(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_snake_case ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase = MraModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) @unittest.skip(reason='MRA does not output attentions' ) def UpperCamelCase__ ( self ): """simple docstring""" return @require_torch class a ( unittest.TestCase ): @slow def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = MraModel.from_pretrained('uw-madison/mra-base-512-4' ) lowerCAmelCase = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): lowerCAmelCase = model(_snake_case )[0] lowerCAmelCase = torch.Size((1, 2_56, 7_68) ) self.assertEqual(output.shape , _snake_case ) lowerCAmelCase = torch.tensor( [[[-0.0_140, 0.0_830, -0.0_381], [0.1_546, 0.1_402, 0.0_220], [0.1_162, 0.0_851, 0.0_165]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4' ) lowerCAmelCase = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): lowerCAmelCase = model(_snake_case )[0] lowerCAmelCase = 5_02_65 lowerCAmelCase = torch.Size((1, 2_56, vocab_size) ) self.assertEqual(output.shape , _snake_case ) lowerCAmelCase = torch.tensor( [[[9.2_595, -3.6_038, 11.8_819], [9.3_869, -3.2_693, 11.0_956], [11.8_524, -3.4_938, 13.1_210]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3' ) lowerCAmelCase = torch.arange(40_96 ).unsqueeze(0 ) with torch.no_grad(): lowerCAmelCase = model(_snake_case )[0] lowerCAmelCase = 5_02_65 lowerCAmelCase = torch.Size((1, 40_96, vocab_size) ) self.assertEqual(output.shape , _snake_case ) lowerCAmelCase = torch.tensor( [[[5.4_789, -2.3_564, 7.5_064], [7.9_067, -1.3_369, 9.9_668], [9.0_712, -1.8_106, 7.0_380]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _snake_case , atol=1E-4 ) )
4
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable _lowerCamelCase = { """configuration_gpt_neox_japanese""": ["""GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXJapaneseConfig"""], """tokenization_gpt_neox_japanese""": ["""GPTNeoXJapaneseTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXJapaneseForCausalLM""", """GPTNeoXJapaneseLayer""", """GPTNeoXJapaneseModel""", """GPTNeoXJapanesePreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCAmelCase_ ( metaclass=_SCREAMING_SNAKE_CASE ): '''simple docstring''' _lowercase : str = ['''note_seq'''] def __init__( self , *_lowercase , **_lowercase ): """simple docstring""" requires_backends(self , ["""note_seq"""] ) @classmethod def _lowercase ( cls , *_lowercase , **_lowercase ): """simple docstring""" requires_backends(cls , ["""note_seq"""] ) @classmethod def _lowercase ( cls , *_lowercase , **_lowercase ): """simple docstring""" requires_backends(cls , ["""note_seq"""] )
5
'''simple docstring''' import heapq def a__ ( _SCREAMING_SNAKE_CASE : dict ) -> set[int]: """simple docstring""" UpperCAmelCase_ : list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(_SCREAMING_SNAKE_CASE , [-1 * len(_SCREAMING_SNAKE_CASE ), (key, value)] ) # chosen_vertices = set of chosen vertices UpperCAmelCase_ : Optional[int] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices UpperCAmelCase_ : Tuple = heapq.heappop(_SCREAMING_SNAKE_CASE )[1][0] chosen_vertices.add(_SCREAMING_SNAKE_CASE ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: UpperCAmelCase_ : Any = elem[1][1].index(_SCREAMING_SNAKE_CASE ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(_SCREAMING_SNAKE_CASE ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(f"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
71
0
import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Tuple , UpperCamelCase__: Optional[Any] , UpperCamelCase__: List[Any] ): SCREAMING_SNAKE_CASE__ = AutoConfig.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = FlaxAutoModelForSeqaSeqLM.from_config(config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = checkpoints.load_tax_checkpoint(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = """wi_0""" in tax_model["""target"""]["""encoder"""]["""layers_0"""]["""mlp"""] if config.model_type == "t5": SCREAMING_SNAKE_CASE__ = """SelfAttention""" if config.model_type == "longt5" and config.encoder_attention_type == "local": SCREAMING_SNAKE_CASE__ = """LocalSelfAttention""" elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE__ = """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__ = f'''layers_{str(UpperCamelCase__ )}''' # Self-Attention SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""key"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""out"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""query"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = 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__ = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""T5LayerNorm_0"""]["""scale"""] # Layer Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""pre_attention_layer_norm"""]["""scale"""] if split_mlp_wi: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi_0"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi_1"""]["""kernel"""] else: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wo"""]["""kernel"""] # Layer Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""][layer_name]["""pre_mlp_layer_norm"""]["""scale"""] # Assigning SCREAMING_SNAKE_CASE__ = flax_model.params["""encoder"""]["""block"""][str(UpperCamelCase__ )]["""layer"""] SCREAMING_SNAKE_CASE__ = tax_attention_key SCREAMING_SNAKE_CASE__ = tax_attention_out SCREAMING_SNAKE_CASE__ = tax_attention_query SCREAMING_SNAKE_CASE__ = tax_attention_value SCREAMING_SNAKE_CASE__ = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE__ = tax_global_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE__ = tax_mlp_wi_a SCREAMING_SNAKE_CASE__ = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE__ = tax_mlp_wi SCREAMING_SNAKE_CASE__ = tax_mlp_wo SCREAMING_SNAKE_CASE__ = tax_mlp_layer_norm SCREAMING_SNAKE_CASE__ = flax_model_encoder_layer_block # Only for layer 0: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""]["""relpos_bias"""]["""rel_embedding"""].T SCREAMING_SNAKE_CASE__ = 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__ = tax_model["""target"""]["""encoder"""]["""side_relpos_bias"""]["""rel_embedding"""].T SCREAMING_SNAKE_CASE__ = tax_encoder_global_rel_embedding # Assigning SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""encoder"""]["""encoder_norm"""]["""scale"""] SCREAMING_SNAKE_CASE__ = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): SCREAMING_SNAKE_CASE__ = f'''layers_{str(UpperCamelCase__ )}''' # Self-Attention SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""key"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""out"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""query"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""value"""]["""kernel"""] # Layer Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""pre_self_attention_layer_norm"""][ """scale""" ] # Encoder-Decoder-Attention SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""encoder_decoder_attention"""] SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_module["""key"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_module["""out"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_module["""query"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_module["""value"""]["""kernel"""] # Layer Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""pre_cross_attention_layer_norm"""]["""scale"""] # MLP if split_mlp_wi: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi_0"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi_1"""]["""kernel"""] else: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi"""]["""kernel"""] SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wo"""]["""kernel"""] # Layer Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""][layer_name]["""pre_mlp_layer_norm"""]["""scale"""] # Assigning SCREAMING_SNAKE_CASE__ = flax_model.params["""decoder"""]["""block"""][str(UpperCamelCase__ )]["""layer"""] SCREAMING_SNAKE_CASE__ = tax_attention_key SCREAMING_SNAKE_CASE__ = tax_attention_out SCREAMING_SNAKE_CASE__ = tax_attention_query SCREAMING_SNAKE_CASE__ = tax_attention_value SCREAMING_SNAKE_CASE__ = tax_pre_attention_layer_norm SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_key SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_out SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_query SCREAMING_SNAKE_CASE__ = tax_enc_dec_attention_value SCREAMING_SNAKE_CASE__ = tax_cross_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE__ = tax_mlp_wi_a SCREAMING_SNAKE_CASE__ = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE__ = tax_mlp_wi SCREAMING_SNAKE_CASE__ = tax_mlp_wo SCREAMING_SNAKE_CASE__ = txa_mlp_layer_norm SCREAMING_SNAKE_CASE__ = flax_model_decoder_layer_block # Decoder Normalization SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""]["""decoder_norm"""]["""scale"""] SCREAMING_SNAKE_CASE__ = txa_decoder_norm # Only for layer 0: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""]["""relpos_bias"""]["""rel_embedding"""].T SCREAMING_SNAKE_CASE__ = tax_decoder_rel_embedding # Token Embeddings SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""token_embedder"""]["""embedding"""] SCREAMING_SNAKE_CASE__ = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: SCREAMING_SNAKE_CASE__ = tax_model["""target"""]["""decoder"""]["""logits_dense"""]["""kernel"""] flax_model.save_pretrained(UpperCamelCase__ ) print("""T5X Model was sucessfully converted!""" ) if __name__ == "__main__": _lowerCamelCase = 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.' ) _lowerCamelCase = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
6
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(_SCREAMING_SNAKE_CASE ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =["pixel_values"] def __init__( self ,_snake_case = True ,_snake_case = None ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = True ,_snake_case = None ,_snake_case = True ,_snake_case = 1 / 2_55 ,_snake_case = True ,_snake_case = True ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Optional[Any] = size if size is not None else {"shortest_edge": 2_56} UpperCAmelCase_ : List[str] = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : str = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} UpperCAmelCase_ : Optional[Any] = get_size_dict(_snake_case ,param_name="crop_size" ) UpperCAmelCase_ : int = do_resize UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Dict = do_center_crop UpperCAmelCase_ : Optional[Any] = crop_size UpperCAmelCase_ : Optional[Any] = resample UpperCAmelCase_ : int = do_rescale UpperCAmelCase_ : Optional[int] = rescale_factor UpperCAmelCase_ : Dict = offset UpperCAmelCase_ : Optional[Any] = do_normalize UpperCAmelCase_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Any = get_size_dict(_snake_case ,default_to_square=_snake_case ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(_snake_case ,size["shortest_edge"] ,default_to_square=_snake_case ) elif "height" in size and "width" in size: UpperCAmelCase_ : Optional[Any] = (size["height"], size["width"]) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(_snake_case ,size=_snake_case ,resample=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Dict = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_snake_case ,size=(size["height"], size["width"]) ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = True ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = image.astype(np.floataa ) if offset: UpperCAmelCase_ : Any = image - (scale / 2) return rescale(_snake_case ,scale=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): return normalize(_snake_case ,mean=_snake_case ,std=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. UpperCAmelCase_ : Optional[int] = to_numpy_array(_snake_case ) if do_resize: UpperCAmelCase_ : Dict = self.resize(image=_snake_case ,size=_snake_case ,resample=_snake_case ) if do_center_crop: UpperCAmelCase_ : Optional[Any] = self.center_crop(_snake_case ,size=_snake_case ) if do_rescale: UpperCAmelCase_ : Union[str, Any] = self.rescale(image=_snake_case ,scale=_snake_case ,offset=_snake_case ) if do_normalize: UpperCAmelCase_ : Any = self.normalize(image=_snake_case ,mean=_snake_case ,std=_snake_case ) UpperCAmelCase_ : Any = to_channel_dimension_format(_snake_case ,_snake_case ) return image def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,**_snake_case ,): UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : str = resample if resample is not None else self.resample UpperCAmelCase_ : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : Tuple = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : List[Any] = offset if offset is not None else self.offset UpperCAmelCase_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : int = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : int = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : int = get_size_dict(_snake_case ,param_name="crop_size" ) if not valid_images(_snake_case ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) UpperCAmelCase_ : Any = make_batched(_snake_case ) UpperCAmelCase_ : Dict = [ [ self._preprocess_image( image=_snake_case ,do_resize=_snake_case ,size=_snake_case ,resample=_snake_case ,do_center_crop=_snake_case ,crop_size=_snake_case ,do_rescale=_snake_case ,rescale_factor=_snake_case ,offset=_snake_case ,do_normalize=_snake_case ,image_mean=_snake_case ,image_std=_snake_case ,data_format=_snake_case ,) for img in video ] for video in videos ] UpperCAmelCase_ : List[str] = {"pixel_values": videos} return BatchFeature(data=_snake_case ,tensor_type=_snake_case )
71
0
"""simple docstring""" # 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 a = [ '''EAGER''', '''AOT_EAGER''', '''INDUCTOR''', '''NVFUSER''', '''AOT_NVFUSER''', '''AOT_CUDAGRAPHS''', '''OFI''', '''FX2TRT''', '''ONNXRT''', '''IPEX''', ] def _snake_case ( _snake_case : Union[str, Any] , _snake_case : Any=None , _snake_case : Any=None , _snake_case : List[str]=None ) -> Optional[int]: '''simple docstring''' _A = True while ask_again: _A = input(_snake_case ) try: if default is not None and len(_snake_case ) == 0: return default return convert_value(_snake_case ) if convert_value is not None else result except Exception: if error_message is not None: print(_snake_case ) def _snake_case ( _snake_case : List[str] , _snake_case : Optional[Any]=[] , _snake_case : str=None , _snake_case : Tuple=0 ) -> Union[str, Any]: '''simple docstring''' _A = BulletMenu(_snake_case , _snake_case ) _A = menu.run(default_choice=_snake_case ) return convert_value(_snake_case ) if convert_value is not None else result def _snake_case ( _snake_case : str ) -> Optional[Any]: '''simple docstring''' _A = int(_snake_case ) return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value] ) def _snake_case ( _snake_case : Any ) -> Tuple: '''simple docstring''' _A = int(_snake_case ) return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value] ) def _snake_case ( _snake_case : List[Any] ) -> Optional[int]: '''simple docstring''' _A = int(_snake_case ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def _snake_case ( _snake_case : Tuple ) -> Union[str, Any]: '''simple docstring''' _A = int(_snake_case ) return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value] ) def _snake_case ( _snake_case : Optional[int] ) -> List[str]: '''simple docstring''' _A = int(_snake_case ) return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value] ) def _snake_case ( _snake_case : int ) -> List[Any]: '''simple docstring''' return {"yes": True, "no": False}[value.lower()] class lowercase_ ( argparse.RawDescriptionHelpFormatter ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] , _UpperCAmelCase : Optional[int] , _UpperCAmelCase : Optional[int] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : str ): _A = super()._format_usage(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) _A = usage.replace('<command> [<args>] ' , '' ) return usage
7
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__( _snake_case ,split=_snake_case ,features=_snake_case ,cache_dir=_snake_case ,keep_in_memory=_snake_case ,streaming=_snake_case ,num_proc=_snake_case ,**_snake_case ,) UpperCAmelCase_ : Tuple = field UpperCAmelCase_ : List[Any] = path_or_paths if isinstance(_snake_case ,_snake_case ) else {self.split: path_or_paths} UpperCAmelCase_ : Optional[int] = Json( cache_dir=_snake_case ,data_files=_snake_case ,features=_snake_case ,field=_snake_case ,**_snake_case ,) def UpperCamelCase__ ( self ): # Build iterable dataset if self.streaming: UpperCAmelCase_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase_ : Union[str, Any] = None UpperCAmelCase_ : int = None UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : int = None self.builder.download_and_prepare( download_config=_snake_case ,download_mode=_snake_case ,verification_mode=_snake_case ,base_path=_snake_case ,num_proc=self.num_proc ,) UpperCAmelCase_ : Dict = self.builder.as_dataset( split=self.split ,verification_mode=_snake_case ,in_memory=self.keep_in_memory ) return dataset class _snake_case : def __init__( self ,_snake_case ,_snake_case ,_snake_case = None ,_snake_case = None ,**_snake_case ,): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Union[str, Any] = path_or_buf UpperCAmelCase_ : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase_ : Dict = num_proc UpperCAmelCase_ : Optional[Any] = "utf-8" UpperCAmelCase_ : Optional[int] = to_json_kwargs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.to_json_kwargs.pop("path_or_buf" ,_snake_case ) UpperCAmelCase_ : Tuple = self.to_json_kwargs.pop("orient" ,"records" ) UpperCAmelCase_ : Any = self.to_json_kwargs.pop("lines" ,True if orient == "records" else False ) UpperCAmelCase_ : Optional[int] = self.to_json_kwargs.pop("index" ,False if orient in ["split", "table"] else True ) UpperCAmelCase_ : int = self.to_json_kwargs.pop("compression" ,_snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"wb" ,compression=_snake_case ) as buffer: UpperCAmelCase_ : List[str] = self._write(file_obj=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' " was passed. Please provide a local path instead." ) UpperCAmelCase_ : Union[str, Any] = self._write( file_obj=self.path_or_buf ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) return written def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = args UpperCAmelCase_ : List[str] = query_table( table=self.dataset.data ,key=slice(_snake_case ,offset + self.batch_size ) ,indices=self.dataset._indices ,) UpperCAmelCase_ : Optional[Any] = batch.to_pandas().to_json( path_or_buf=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**_snake_case ) if not json_str.endswith("\n" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,**_snake_case ,): UpperCAmelCase_ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): UpperCAmelCase_ : Any = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: UpperCAmelCase_ , UpperCAmelCase_ : int = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,_snake_case ,_snake_case )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): written += file_obj.write(_snake_case ) return written
71
0
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = None class SCREAMING_SNAKE_CASE (a__ ): def __init__( self , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , _UpperCAmelCase=512 , _UpperCAmelCase="cls" , _UpperCAmelCase=False , _UpperCAmelCase=True , **_UpperCAmelCase , ): '''simple docstring''' super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase) __A : Any = project_dim __A : List[str] = pooler_fn __A : Tuple = learn_encoder __A : Optional[int] = use_attention_mask class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = [r'''pooler''', r'''logit_scale'''] lowerCAmelCase = [r'''position_ids''', r'''predictions.decoder.bias'''] lowerCAmelCase = '''roberta''' lowerCAmelCase = RobertaSeriesConfig def __init__( self , _UpperCAmelCase): '''simple docstring''' super().__init__(_UpperCAmelCase) __A : List[str] = XLMRobertaModel(_UpperCAmelCase) __A : List[str] = nn.Linear(config.hidden_size , config.project_dim) __A : List[Any] = getattr(_UpperCAmelCase , 'has_pre_transformation' , _UpperCAmelCase) if self.has_pre_transformation: __A : Tuple = nn.Linear(config.hidden_size , config.project_dim) __A : Optional[int] = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps) self.post_init() def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , ): '''simple docstring''' __A : Optional[Any] = return_dict if return_dict is not None else self.config.use_return_dict __A : Any = self.base_model( input_ids=_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , position_ids=_UpperCAmelCase , head_mask=_UpperCAmelCase , inputs_embeds=_UpperCAmelCase , encoder_hidden_states=_UpperCAmelCase , encoder_attention_mask=_UpperCAmelCase , output_attentions=_UpperCAmelCase , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=_UpperCAmelCase , ) if self.has_pre_transformation: __A : Optional[int] = outputs['hidden_states'][-2] __A : Dict = self.pre_LN(_UpperCAmelCase) __A : Optional[Any] = self.transformation_pre(_UpperCAmelCase) return TransformationModelOutput( projection_state=_UpperCAmelCase , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: __A : Optional[Any] = self.transformation(outputs.last_hidden_state) return TransformationModelOutput( projection_state=_UpperCAmelCase , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
8
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
0
import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Optional[Any] ): """simple docstring""" super().tearDown() gc.collect() def _a ( self : str ): """simple docstring""" A__ , A__ = FlaxStableDiffusionPipeline.from_pretrained( 'stabilityai/stable-diffusion-2' , revision='bf16' , dtype=jnp.bfloataa , ) A__ = 'A painting of a squirrel eating a burger' A__ = jax.device_count() A__ = num_samples * [prompt] A__ = sd_pipe.prepare_inputs(_snake_case ) A__ = replicate(_snake_case ) A__ = shard(_snake_case ) A__ = jax.random.PRNGKey(0 ) A__ = jax.random.split(_snake_case , jax.device_count() ) A__ = sd_pipe(_snake_case , _snake_case , _snake_case , num_inference_steps=25 , jit=_snake_case )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) A__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) A__ = images[0, 2_53:2_56, 2_53:2_56, -1] A__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ = jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.4_5508, 0.4512] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def _a ( self : Optional[int] ): """simple docstring""" A__ = 'stabilityai/stable-diffusion-2' A__ , A__ = FlaxDPMSolverMultistepScheduler.from_pretrained(_snake_case , subfolder='scheduler' ) A__ , A__ = FlaxStableDiffusionPipeline.from_pretrained( _snake_case , scheduler=_snake_case , revision='bf16' , dtype=jnp.bfloataa , ) A__ = scheduler_params A__ = 'A painting of a squirrel eating a burger' A__ = jax.device_count() A__ = num_samples * [prompt] A__ = sd_pipe.prepare_inputs(_snake_case ) A__ = replicate(_snake_case ) A__ = shard(_snake_case ) A__ = jax.random.PRNGKey(0 ) A__ = jax.random.split(_snake_case , jax.device_count() ) A__ = sd_pipe(_snake_case , _snake_case , _snake_case , num_inference_steps=25 , jit=_snake_case )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) A__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) A__ = images[0, 2_53:2_56, 2_53:2_56, -1] A__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ = jnp.array([0.4336, 0.4_2969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
9
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
0
# 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. _lowerCAmelCase = abspath(join(dirname(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 _snake_case ( __snake_case ): from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__snake_case ) def _snake_case ( __snake_case ): from transformers.testing_utils import pytest_terminal_summary_main _UpperCamelCase = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__snake_case , id=__snake_case )
10
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
0
'''simple docstring''' from __future__ import annotations import math import numpy as np from numpy.linalg import norm def lowerCAmelCase (__A , __A): """simple docstring""" return math.sqrt(sum(pow(a - b , 2) for a, b in zip(__A , __A))) def lowerCAmelCase (__A , __A): """simple docstring""" if dataset.ndim != value_array.ndim: _a = ( '''Wrong input data\'s dimensions... ''' F'''dataset : {dataset.ndim}, value_array : {value_array.ndim}''' ) raise ValueError(__A) try: if dataset.shape[1] != value_array.shape[1]: _a = ( '''Wrong input data\'s shape... ''' F'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}''' ) raise ValueError(__A) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('''Wrong shape''') if dataset.dtype != value_array.dtype: _a = ( '''Input data have different datatype... ''' F'''dataset : {dataset.dtype}, value_array : {value_array.dtype}''' ) raise TypeError(__A) _a = [] for value in value_array: _a = euclidean(__A , dataset[0]) _a = dataset[0].tolist() for dataset_value in dataset[1:]: _a = euclidean(__A , __A) if dist > temp_dist: _a = temp_dist _a = dataset_value.tolist() answer.append([vector, dist]) return answer def lowerCAmelCase (__A , __A): """simple docstring""" return np.dot(__A , __A) / (norm(__A) * norm(__A)) if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
0
def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ ) -> float: '''simple docstring''' return round(float(moles / volume ) * nfactor ) def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ ) -> float: '''simple docstring''' return round(float((moles * 0.0821 * temperature) / (volume) ) ) def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ ) -> float: '''simple docstring''' return round(float((moles * 0.0821 * temperature) / (pressure) ) ) def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ ) -> float: '''simple docstring''' return round(float((pressure * volume) / (0.0821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
12
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
71
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" lowerCamelCase : Optional[int] = 'microsoft/speecht5_tts' lowerCamelCase : List[str] = ( 'This is a tool that reads an English text out loud. It takes an input named `text` which should contain the ' 'text to read (in English) and returns a waveform object containing the sound.' ) lowerCamelCase : Tuple = 'text_reader' lowerCamelCase : Tuple = SpeechTaProcessor lowerCamelCase : Any = SpeechTaForTextToSpeech lowerCamelCase : List[str] = SpeechTaHifiGan lowerCamelCase : Union[str, Any] = ['text'] lowerCamelCase : List[Any] = ['audio'] def lowercase_ ( self ) -> Dict: if self.post_processor is None: __lowerCamelCase : str = 'microsoft/speecht5_hifigan' super().setup() def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> int: __lowerCamelCase : str = self.pre_processor(text=SCREAMING_SNAKE_CASE_ , return_tensors='pt' , truncation=SCREAMING_SNAKE_CASE_ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('Datasets needs to be installed if not passing speaker embeddings.' ) __lowerCamelCase : List[str] = load_dataset('Matthijs/cmu-arctic-xvectors' , split='validation' ) __lowerCamelCase : List[str] = torch.tensor(embeddings_dataset[73_05]['xvector'] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def lowercase_ ( self , SCREAMING_SNAKE_CASE_ ) -> str: with torch.no_grad(): return self.model.generate_speech(**SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ ) -> str: with torch.no_grad(): return self.post_processor(SCREAMING_SNAKE_CASE_ ).cpu().detach()
13
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCamelCase = """\ @misc{wu2016googles, title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _lowerCamelCase = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the 'GLEU score'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score's range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ _lowerCamelCase = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: 'google_bleu': google_bleu score Examples: Example 1: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _snake_case (datasets.Metric): def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ) ,id="references" ), } ) ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = 1 ,_snake_case = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case ,hypotheses=_snake_case ,min_len=_snake_case ,max_len=_snake_case ) }
71
0
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING a__ = logging.get_logger(__name__) @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , **_a ) -> List[str]: super().__init__(**_a ) if self.framework == "tf": raise ValueError(F"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type(_a ) def __call__( self , _a , _a = None , **_a , ) -> List[str]: if "text_queries" in kwargs: _a : Optional[Any] = kwargs.pop('''text_queries''' ) if isinstance(_a , (str, Image.Image) ): _a : Tuple = {'''image''': image, '''candidate_labels''': candidate_labels} else: _a : str = image _a : int = super().__call__(_a , **_a ) return results def __lowercase ( self , **_a ) -> Union[str, Any]: _a : Union[str, Any] = {} if "threshold" in kwargs: _a : Dict = kwargs['''threshold'''] if "top_k" in kwargs: _a : Tuple = kwargs['''top_k'''] return {}, {}, postprocess_params def __lowercase ( self , _a ) -> List[str]: _a : int = load_image(inputs['''image'''] ) _a : Union[str, Any] = inputs['''candidate_labels'''] if isinstance(_a , _a ): _a : Tuple = candidate_labels.split(''',''' ) _a : Dict = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(_a ): _a : Union[str, Any] = self.tokenizer(_a , return_tensors=self.framework ) _a : Optional[Any] = self.image_processor(_a , return_tensors=self.framework ) yield { "is_last": i == len(_a ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def __lowercase ( self , _a ) -> str: _a : Optional[int] = model_inputs.pop('''target_size''' ) _a : int = model_inputs.pop('''candidate_label''' ) _a : str = model_inputs.pop('''is_last''' ) _a : Union[str, Any] = self.model(**_a ) _a : List[str] = {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs} return model_outputs def __lowercase ( self , _a , _a=0.1 , _a=None ) -> Any: _a : str = [] for model_output in model_outputs: _a : str = model_output['''candidate_label'''] _a : int = BaseModelOutput(_a ) _a : Tuple = self.image_processor.post_process_object_detection( outputs=_a , threshold=_a , target_sizes=model_output['''target_size'''] )[0] for index in outputs["scores"].nonzero(): _a : Optional[Any] = outputs['''scores'''][index].item() _a : List[str] = self._get_bounding_box(outputs['''boxes'''][index][0] ) _a : Dict = {'''score''': score, '''label''': label, '''box''': box} results.append(_a ) _a : str = sorted(_a , key=lambda _a : x["score"] , reverse=_a ) if top_k: _a : Any = results[:top_k] return results def __lowercase ( self , _a ) -> Dict[str, int]: if self.framework != "pt": raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.''' ) _a , _a , _a , _a : Dict = box.int().tolist() _a : int = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
14
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
0
import argparse import fairseq import torch from transformers import UniSpeechSatConfig, UniSpeechSatForCTC, UniSpeechSatForPreTraining, logging logging.set_verbosity_info() A : str = logging.get_logger(__name__) A : str = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'encoder.layer_norm_for_extract': 'layer_norm_for_extract', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'label_embs_concat': 'label_embeddings_concat', 'mask_emb': 'masked_spec_embed', 'spk_proj': 'speaker_proj', } A : List[str] = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'label_embeddings_concat', 'speaker_proj', 'layer_norm_for_extract', ] def UpperCamelCase ( __magic_name__ : Union[str, Any] , __magic_name__ : int , __magic_name__ : Optional[int] , __magic_name__ : str , __magic_name__ : Any ) -> int: """simple docstring""" for attribute in key.split(""".""" ): lowercase__ = getattr(__magic_name__ , __magic_name__ ) if weight_type is not None: lowercase__ = getattr(__magic_name__ , __magic_name__ ).shape else: lowercase__ = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": lowercase__ = value elif weight_type == "weight_g": lowercase__ = value elif weight_type == "weight_v": lowercase__ = value elif weight_type == "bias": lowercase__ = value else: lowercase__ = value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def UpperCamelCase ( __magic_name__ : str , __magic_name__ : str ) -> Optional[int]: """simple docstring""" lowercase__ = [] lowercase__ = fairseq_model.state_dict() lowercase__ = hf_model.unispeech_sat.feature_extractor for name, value in fairseq_dict.items(): lowercase__ = False if "conv_layers" in name: load_conv_layer( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , hf_model.config.feat_extract_norm == """group""" , ) lowercase__ = True else: for key, mapped_key in MAPPING.items(): lowercase__ = """unispeech_sat.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: if "layer_norm_for_extract" in name and (".".join(name.split(""".""" )[:-1] ) != key): # special case since naming is very similar continue lowercase__ = True if "*" in mapped_key: lowercase__ = name.split(__magic_name__ )[0].split(""".""" )[-2] lowercase__ = mapped_key.replace("""*""" , __magic_name__ ) if "weight_g" in name: lowercase__ = """weight_g""" elif "weight_v" in name: lowercase__ = """weight_v""" elif "bias" in name: lowercase__ = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj lowercase__ = """weight""" else: lowercase__ = None set_recursively(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) continue if not is_used: unused_weights.append(__magic_name__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) def UpperCamelCase ( __magic_name__ : List[str] , __magic_name__ : Any , __magic_name__ : str , __magic_name__ : Any , __magic_name__ : List[str] ) -> List[Any]: """simple docstring""" lowercase__ = full_name.split("""conv_layers.""" )[-1] lowercase__ = name.split(""".""" ) lowercase__ = int(items[0] ) lowercase__ = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) lowercase__ = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) lowercase__ = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.bias.data.shape} was found.''' ) lowercase__ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) lowercase__ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__magic_name__ ) @torch.no_grad() def UpperCamelCase ( __magic_name__ : List[str] , __magic_name__ : Tuple , __magic_name__ : Any=None , __magic_name__ : Optional[int]=None , __magic_name__ : Optional[Any]=True ) -> List[Any]: """simple docstring""" if config_path is not None: lowercase__ = UniSpeechSatConfig.from_pretrained(__magic_name__ ) else: lowercase__ = UniSpeechSatConfig() lowercase__ = """""" if is_finetuned: lowercase__ = UniSpeechSatForCTC(__magic_name__ ) else: lowercase__ = UniSpeechSatForPreTraining(__magic_name__ ) lowercase__ , lowercase__ , lowercase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) lowercase__ = model[0].eval() recursively_load_weights(__magic_name__ , __magic_name__ ) hf_wavavec.save_pretrained(__magic_name__ ) if __name__ == "__main__": A : List[str] = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) A : Optional[int] = parser.parse_args() convert_unispeech_sat_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
15
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class _snake_case : __A : Dict =BlenderbotConfig __A : Union[str, Any] ={} __A : Any ="gelu" def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=20 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Dict = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Optional[int] = attention_probs_dropout_prob UpperCAmelCase_ : List[Any] = max_position_embeddings UpperCAmelCase_ : str = eos_token_id UpperCAmelCase_ : List[Any] = pad_token_id UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) UpperCAmelCase_ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) UpperCAmelCase_ : Optional[Any] = tf.concat([input_ids, eos_tensor] ,axis=1 ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) UpperCAmelCase_ : List[str] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlenderbotModel(config=_snake_case ).get_decoder() UpperCAmelCase_ : int = inputs_dict["input_ids"] UpperCAmelCase_ : Dict = input_ids[:1, :] UpperCAmelCase_ : Any = inputs_dict["attention_mask"][:1, :] UpperCAmelCase_ : int = inputs_dict["head_mask"] UpperCAmelCase_ : Optional[int] = 1 # first forward pass UpperCAmelCase_ : List[str] = model(_snake_case ,attention_mask=_snake_case ,head_mask=_snake_case ,use_cache=_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) ,config.vocab_size ) UpperCAmelCase_ : Any = tf.cast(ids_tensor((self.batch_size, 3) ,2 ) ,tf.inta ) # append to next input_ids and UpperCAmelCase_ : Union[str, Any] = tf.concat([input_ids, next_tokens] ,axis=-1 ) UpperCAmelCase_ : Any = tf.concat([attention_mask, next_attn_mask] ,axis=-1 ) UpperCAmelCase_ : Any = model(_snake_case ,attention_mask=_snake_case )[0] UpperCAmelCase_ : List[Any] = model(_snake_case ,attention_mask=_snake_case ,past_key_values=_snake_case )[0] self.parent.assertEqual(next_tokens.shape[1] ,output_from_past.shape[1] ) # select random slice UpperCAmelCase_ : str = int(ids_tensor((1,) ,output_from_past.shape[-1] ) ) UpperCAmelCase_ : List[str] = output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase_ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_snake_case ,_snake_case ,rtol=1E-3 ) def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : List[str]=None , _SCREAMING_SNAKE_CASE : Dict=None , ) -> Union[str, Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase_ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase_ : List[Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __A : List[str] =(TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __A : Dict =( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __A : Any =True __A : Dict =False __A : Dict =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = TFBlenderbotModelTester(self ) UpperCAmelCase_ : int = ConfigTester(self ,config_class=_snake_case ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_snake_case ) @require_tokenizers @require_tf class _snake_case (unittest.TestCase): __A : Optional[int] =["My friends are cool but they eat too many carbs."] __A : Optional[Any] ="facebook/blenderbot-400M-distill" @cached_property def UpperCamelCase__ ( self ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.tokenizer(self.src_text ,return_tensors="tf" ) UpperCAmelCase_ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,) UpperCAmelCase_ : str = self.tokenizer.batch_decode(generated_ids.numpy() ,skip_special_tokens=_snake_case )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
71
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __A : List[str] = logging.get_logger(__name__) __A : str = { 'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ): '''simple docstring''' lowerCamelCase__ = "resnet" lowerCamelCase__ = ["basic", "bottleneck"] def __init__( self : List[Any] , __lowerCamelCase : List[Any]=3 , __lowerCamelCase : Optional[int]=64 , __lowerCamelCase : int=[256, 512, 1024, 2048] , __lowerCamelCase : Dict=[3, 4, 6, 3] , __lowerCamelCase : List[str]="bottleneck" , __lowerCamelCase : Optional[int]="relu" , __lowerCamelCase : List[Any]=False , __lowerCamelCase : Optional[Any]=None , __lowerCamelCase : List[str]=None , **__lowerCamelCase : Union[str, Any] , ): super().__init__(**__lowerCamelCase ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embedding_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = layer_type SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = downsample_in_first_stage SCREAMING_SNAKE_CASE = ["stem"] + [f"stage{idx}" for idx in range(1 , len(__lowerCamelCase ) + 1 )] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = get_aligned_output_features_output_indices( out_features=__lowerCamelCase , out_indices=__lowerCamelCase , stage_names=self.stage_names ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = version.parse("1.11" ) @property def _snake_case ( self : List[Any] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def _snake_case ( self : Union[str, Any] ): return 1e-3
16
'''simple docstring''' from numpy import exp, pi, sqrt def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
71
0
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def __SCREAMING_SNAKE_CASE ( ) -> Tuple: __A : List[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=a__ ,default=1 ,help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" ,type=a__ ,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=a__ ) return parser.parse_args() def __SCREAMING_SNAKE_CASE ( ) -> str: __A : Union[str, Any] = parse_args() # Import training_script as a module. __A : List[Any] = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __A : str = script_fpath.stem __A : int = importlib.import_module(a__ ) # Patch sys.argv __A : List[str] = [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()
17
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _snake_case (nn.Module): def __init__( self ,_snake_case = 16 ,_snake_case = 88 ,_snake_case = None ,_snake_case = 1 ,_snake_case = 0.0 ,_snake_case = 32 ,_snake_case = None ,_snake_case = False ,_snake_case = None ,_snake_case = None ,_snake_case = "geglu" ,_snake_case = None ,): super().__init__() UpperCAmelCase_ : Optional[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case ,attention_head_dim=_snake_case ,in_channels=_snake_case ,num_layers=_snake_case ,dropout=_snake_case ,norm_num_groups=_snake_case ,cross_attention_dim=_snake_case ,attention_bias=_snake_case ,sample_size=_snake_case ,num_vector_embeds=_snake_case ,activation_fn=_snake_case ,num_embeds_ada_norm=_snake_case ,) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference UpperCAmelCase_ : List[str] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` UpperCAmelCase_ : int = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` UpperCAmelCase_ : List[Any] = [1, 0] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = True ,): UpperCAmelCase_ : List[str] = hidden_states UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[int] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens UpperCAmelCase_ : Any = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] UpperCAmelCase_ : Any = self.transformer_index_for_condition[i] UpperCAmelCase_ : int = self.transformers[transformer_index]( _snake_case ,encoder_hidden_states=_snake_case ,timestep=_snake_case ,cross_attention_kwargs=_snake_case ,return_dict=_snake_case ,)[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] UpperCAmelCase_ : Dict = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) UpperCAmelCase_ : List[Any] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
71
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(SCREAMING_SNAKE_CASE_ : Dict ): '''simple docstring''' _lowerCAmelCase = SwinConfig() _lowerCAmelCase = swin_name.split("_" ) _lowerCAmelCase = name_split[1] _lowerCAmelCase = int(name_split[4] ) _lowerCAmelCase = int(name_split[3][-1] ) if model_size == "tiny": _lowerCAmelCase = 96 _lowerCAmelCase = (2, 2, 6, 2) _lowerCAmelCase = (3, 6, 12, 24) elif model_size == "small": _lowerCAmelCase = 96 _lowerCAmelCase = (2, 2, 18, 2) _lowerCAmelCase = (3, 6, 12, 24) elif model_size == "base": _lowerCAmelCase = 128 _lowerCAmelCase = (2, 2, 18, 2) _lowerCAmelCase = (4, 8, 16, 32) else: _lowerCAmelCase = 192 _lowerCAmelCase = (2, 2, 18, 2) _lowerCAmelCase = (6, 12, 24, 48) if "in22k" in swin_name: _lowerCAmelCase = 21841 else: _lowerCAmelCase = 1000 _lowerCAmelCase = "huggingface/label-files" _lowerCAmelCase = "imagenet-1k-id2label.json" _lowerCAmelCase = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , repo_type="dataset" ) , "r" ) ) _lowerCAmelCase = {int(SCREAMING_SNAKE_CASE_ ): v for k, v in idalabel.items()} _lowerCAmelCase = idalabel _lowerCAmelCase = {v: k for k, v in idalabel.items()} _lowerCAmelCase = img_size _lowerCAmelCase = num_classes _lowerCAmelCase = embed_dim _lowerCAmelCase = depths _lowerCAmelCase = num_heads _lowerCAmelCase = window_size return config def __a(SCREAMING_SNAKE_CASE_ : List[Any] ): '''simple docstring''' if "patch_embed.proj" in name: _lowerCAmelCase = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: _lowerCAmelCase = name.replace("patch_embed.norm" , "embeddings.norm" ) if "layers" in name: _lowerCAmelCase = "encoder." + name if "attn.proj" in name: _lowerCAmelCase = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: _lowerCAmelCase = name.replace("attn" , "attention.self" ) if "norm1" in name: _lowerCAmelCase = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: _lowerCAmelCase = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: _lowerCAmelCase = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: _lowerCAmelCase = name.replace("mlp.fc2" , "output.dense" ) if name == "norm.weight": _lowerCAmelCase = "layernorm.weight" if name == "norm.bias": _lowerCAmelCase = "layernorm.bias" if "head" in name: _lowerCAmelCase = name.replace("head" , "classifier" ) else: _lowerCAmelCase = "swin." + name return name def __a(SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] ): '''simple docstring''' for key in orig_state_dict.copy().keys(): _lowerCAmelCase = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "mask" in key: continue elif "qkv" in key: _lowerCAmelCase = key.split("." ) _lowerCAmelCase = int(key_split[1] ) _lowerCAmelCase = int(key_split[3] ) _lowerCAmelCase = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _lowerCAmelCase = val[:dim, :] _lowerCAmelCase = val[ dim : dim * 2, : ] _lowerCAmelCase = val[-dim:, :] else: _lowerCAmelCase = val[ :dim ] _lowerCAmelCase = val[ dim : dim * 2 ] _lowerCAmelCase = val[ -dim: ] else: _lowerCAmelCase = val return orig_state_dict def __a(SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] ): '''simple docstring''' _lowerCAmelCase = timm.create_model(SCREAMING_SNAKE_CASE_ , pretrained=SCREAMING_SNAKE_CASE_ ) timm_model.eval() _lowerCAmelCase = get_swin_config(SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = SwinForImageClassification(SCREAMING_SNAKE_CASE_ ) model.eval() _lowerCAmelCase = convert_state_dict(timm_model.state_dict() , SCREAMING_SNAKE_CASE_ ) model.load_state_dict(SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = "http://images.cocodataset.org/val2017/000000039769.jpg" _lowerCAmelCase = AutoImageProcessor.from_pretrained("microsoft/{}".format(swin_name.replace("_" , "-" ) ) ) _lowerCAmelCase = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ) _lowerCAmelCase = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors="pt" ) _lowerCAmelCase = timm_model(inputs["pixel_values"] ) _lowerCAmelCase = model(**SCREAMING_SNAKE_CASE_ ).logits assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print(F'''Saving model {swin_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = 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." ) _SCREAMING_SNAKE_CASE = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
18
'''simple docstring''' import json import sys def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : int ) -> Tuple: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: UpperCAmelCase_ : Dict = json.load(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = ["<details>", "<summary>Show updated benchmarks!</summary>", " "] for benchmark_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = results[benchmark_name] UpperCAmelCase_ : Any = benchmark_name.split("/" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) UpperCAmelCase_ : Any = "| metric |" UpperCAmelCase_ : Any = "|--------|" UpperCAmelCase_ : Union[str, Any] = "| new / old (diff) |" for metric_name in sorted(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = benchmark_res[metric_name] UpperCAmelCase_ : Union[str, Any] = metric_vals["new"] UpperCAmelCase_ : Optional[Any] = metric_vals.get("old" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = metric_vals.get("diff" , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = F''' {new_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("</details>" ) with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.writelines("\n".join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": _lowerCamelCase = sys.argv[1] _lowerCamelCase = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
71
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _a = logging.get_logger(__name__) _a = { """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""", # See all Nat models at https://huggingface.co/models?filter=nat } class _UpperCAmelCase( lowerCamelCase , lowerCamelCase ): lowercase__ = 'nat' lowercase__ = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , __a=4 , __a=3 , __a=64 , __a=[3, 4, 6, 5] , __a=[2, 4, 8, 16] , __a=7 , __a=3.0 , __a=True , __a=0.0 , __a=0.0 , __a=0.1 , __a="gelu" , __a=0.02 , __a=1e-5 , __a=0.0 , __a=None , __a=None , **__a , ) -> Tuple: '''simple docstring''' super().__init__(**__a) _UpperCamelCase = patch_size _UpperCamelCase = num_channels _UpperCamelCase = embed_dim _UpperCamelCase = depths _UpperCamelCase = len(__a) _UpperCamelCase = num_heads _UpperCamelCase = kernel_size _UpperCamelCase = mlp_ratio _UpperCamelCase = qkv_bias _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = drop_path_rate _UpperCamelCase = hidden_act _UpperCamelCase = layer_norm_eps _UpperCamelCase = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _UpperCamelCase = int(embed_dim * 2 ** (len(__a) - 1)) _UpperCamelCase = layer_scale_init_value _UpperCamelCase = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__a) + 1)] _UpperCamelCase , _UpperCamelCase = get_aligned_output_features_output_indices( out_features=__a , out_indices=__a , stage_names=self.stage_names)
19
'''simple docstring''' import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase = get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Optional[int] =DebertaVaTokenizer __A : Union[str, Any] =DebertaVaTokenizerFast __A : str =True __A : List[str] =True def UpperCamelCase__ ( self ): super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase_ : Optional[int] = DebertaVaTokenizer(_snake_case ,unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ : List[Any] = "this is a test" UpperCAmelCase_ : Optional[Any] = "this is a test" return input_text, output_text def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = "<pad>" UpperCAmelCase_ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case ) ,_snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case ) ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"<pad>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"[PAD]" ) self.assertEqual(len(_snake_case ) ,3_00_01 ) def UpperCamelCase__ ( self ): self.assertEqual(self.get_tokenizer().vocab_size ,3_00_00 ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : str = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : Union[str, Any] = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on UpperCAmelCase_ : Tuple = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Dict = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[Any] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : str = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Optional[int] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on UpperCAmelCase_ : List[str] = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Tuple = " \tHeLLo!how \n Are yoU? " UpperCAmelCase_ : List[Any] = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = DebertaVaTokenizerFast(_snake_case ,do_lower_case=_snake_case ,split_by_punct=_snake_case ) UpperCAmelCase_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_tokenizer() UpperCAmelCase_ : Union[str, Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Dict = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) UpperCAmelCase_ : Optional[Any] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Tuple = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) UpperCAmelCase_ : int = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[Any] = self.get_rust_tokenizer() UpperCAmelCase_ : Union[str, Any] = tokenizer.encode(_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = "This is a test" UpperCAmelCase_ : Optional[int] = [13, 1, 43_98, 25, 21, 12_89] UpperCAmelCase_ : Optional[Any] = ["▁", "T", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] UpperCAmelCase_ : str = DebertaVaTokenizer(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : List[Any] = DebertaVaTokenizerFast(_snake_case ,keep_accents=_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[Any] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : List[str] = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) # fmt: off UpperCAmelCase_ : List[str] = "I was born in 92000, and this is falsé." UpperCAmelCase_ : Optional[int] = [13, 1, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] UpperCAmelCase_ : str = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] UpperCAmelCase_ : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on UpperCAmelCase_ : List[str] = tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : int = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.encode(_snake_case ,add_special_tokens=_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Optional[int] = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) UpperCAmelCase_ : Any = rust_tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = DebertaVaTokenizer(_snake_case ) UpperCAmelCase_ : Optional[int] = tokenizer.encode("sequence builders" ) UpperCAmelCase_ : Dict = tokenizer.encode("multi-sequence build" ) UpperCAmelCase_ : Tuple = tokenizer.build_inputs_with_special_tokens(_snake_case ) UpperCAmelCase_ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(_snake_case ,_snake_case ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] ,_snake_case ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] ,_snake_case ,) @slow def UpperCamelCase__ ( self ): # fmt: off UpperCAmelCase_ : Union[str, Any] = {"input_ids": [[1, 3_98_67, 36, 1_93_90, 4_86, 27, 3_50_52, 8_14_36, 18, 6_06_85, 12_25, 7, 3_50_52, 8_14_36, 18, 93_67, 1_68_99, 18, 1_59_37, 53, 5_94, 7_73, 18, 1_62_87, 3_04_65, 36, 1_59_37, 6, 4_11_39, 38, 3_69_79, 6_07_63, 1_91, 6, 3_41_32, 99, 6, 5_05_38, 3_90, 4_32_30, 6, 3_41_32, 27_79, 2_08_50, 14, 6_99, 10_72, 11_94, 36, 3_82, 1_09_01, 53, 7, 6_99, 10_72, 20_84, 36, 2_04_22, 6_30, 53, 19, 1_05, 30_49, 18_96, 10_53, 1_68_99, 15_06, 11, 3_79_78, 42_43, 7, 12_37, 3_18_69, 2_00, 1_65_66, 6_54, 6, 3_50_52, 8_14_36, 7, 5_56_30, 1_35_93, 4, 2], [1, 26, 1_50_11, 13, 6_67, 8, 10_53, 18, 2_36_11, 12_37, 7_23_56, 1_28_20, 34, 10_41_34, 12_09, 35, 1_33_13, 66_27, 21, 2_02, 3_47, 7, 1_64, 23_99, 11, 46, 44_85, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 12_32, 28_64, 1_57_85, 1_49_51, 1_05, 5, 85_81, 12_50, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_snake_case ,model_name="microsoft/deberta-v2-xlarge" ,revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" ,)
71
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _lowerCAmelCase: Optional[int] = logging.get_logger(__name__) class lowercase_ (lowercase__ , lowercase__ ): snake_case ='maskformer-swin' snake_case ={ 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , lowercase_=224 , lowercase_=4 , lowercase_=3 , lowercase_=96 , lowercase_=[2, 2, 6, 2] , lowercase_=[3, 6, 12, 24] , lowercase_=7 , lowercase_=4.0 , lowercase_=True , lowercase_=0.0 , lowercase_=0.0 , lowercase_=0.1 , lowercase_="gelu" , lowercase_=False , lowercase_=0.02 , lowercase_=1e-5 , lowercase_=None , lowercase_=None , **lowercase_ , ) -> Union[str, Any]: super().__init__(**lowercase_) a__ =image_size a__ =patch_size a__ =num_channels a__ =embed_dim a__ =depths a__ =len(lowercase_) a__ =num_heads a__ =window_size a__ =mlp_ratio a__ =qkv_bias a__ =hidden_dropout_prob a__ =attention_probs_dropout_prob a__ =drop_path_rate a__ =hidden_act a__ =use_absolute_embeddings a__ =layer_norm_eps a__ =initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model a__ =int(embed_dim * 2 ** (len(lowercase_) - 1)) a__ =['stem'] + [F"""stage{idx}""" for idx in range(1 , len(lowercase_) + 1)] a__ , a__ =get_aligned_output_features_output_indices( out_features=lowercase_ , out_indices=lowercase_ , stage_names=self.stage_names)
20
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise TypeError("Input value must be an 'int' type" ) UpperCAmelCase_ : Union[str, Any] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
71
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase_ : Optional[Any] = logging.get_logger(__name__) def lowerCAmelCase_ ( lowerCamelCase ): __magic_name__ : Tuple =SwinConfig( embed_dim=192 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=["""stage2""", """stage3""", """stage4"""] , ) __magic_name__ : Tuple =DetaConfig( backbone_config=lowerCamelCase , num_queries=900 , encoder_ffn_dim=2048 , decoder_ffn_dim=2048 , num_feature_levels=5 , assign_first_stage=lowerCamelCase , with_box_refine=lowerCamelCase , two_stage=lowerCamelCase , ) # set labels __magic_name__ : Optional[Any] ="""huggingface/label-files""" if "o365" in model_name: __magic_name__ : int =366 __magic_name__ : Any ="""object365-id2label.json""" else: __magic_name__ : Optional[int] =91 __magic_name__ : Any ="""coco-detection-id2label.json""" __magic_name__ : Union[str, Any] =num_labels __magic_name__ : Any =json.load(open(cached_download(hf_hub_url(lowerCamelCase , lowerCamelCase , repo_type="""dataset""" ) ) , """r""" ) ) __magic_name__ : Union[str, Any] ={int(lowerCamelCase ): v for k, v in idalabel.items()} __magic_name__ : Union[str, Any] =idalabel __magic_name__ : Tuple ={v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( lowerCamelCase ): __magic_name__ : str =[] # stem # fmt: off rename_keys.append(("""backbone.0.body.patch_embed.proj.weight""", """model.backbone.model.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""backbone.0.body.patch_embed.proj.bias""", """model.backbone.model.embeddings.patch_embeddings.projection.bias""") ) rename_keys.append(("""backbone.0.body.patch_embed.norm.weight""", """model.backbone.model.embeddings.norm.weight""") ) rename_keys.append(("""backbone.0.body.patch_embed.norm.bias""", """model.backbone.model.embeddings.norm.bias""") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.norm1.weight", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.norm1.bias", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.norm2.weight", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.norm2.bias", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias", F"model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias") ) if i < 3: rename_keys.append((F"backbone.0.body.layers.{i}.downsample.reduction.weight", F"model.backbone.model.encoder.layers.{i}.downsample.reduction.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.downsample.norm.weight", F"model.backbone.model.encoder.layers.{i}.downsample.norm.weight") ) rename_keys.append((F"backbone.0.body.layers.{i}.downsample.norm.bias", F"model.backbone.model.encoder.layers.{i}.downsample.norm.bias") ) rename_keys.append(("""backbone.0.body.norm1.weight""", """model.backbone.model.hidden_states_norms.stage2.weight""") ) rename_keys.append(("""backbone.0.body.norm1.bias""", """model.backbone.model.hidden_states_norms.stage2.bias""") ) rename_keys.append(("""backbone.0.body.norm2.weight""", """model.backbone.model.hidden_states_norms.stage3.weight""") ) rename_keys.append(("""backbone.0.body.norm2.bias""", """model.backbone.model.hidden_states_norms.stage3.bias""") ) rename_keys.append(("""backbone.0.body.norm3.weight""", """model.backbone.model.hidden_states_norms.stage4.weight""") ) rename_keys.append(("""backbone.0.body.norm3.bias""", """model.backbone.model.hidden_states_norms.stage4.bias""") ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight", F"model.encoder.layers.{i}.self_attn.sampling_offsets.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias", F"model.encoder.layers.{i}.self_attn.sampling_offsets.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.attention_weights.weight", F"model.encoder.layers.{i}.self_attn.attention_weights.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.attention_weights.bias", F"model.encoder.layers.{i}.self_attn.attention_weights.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.value_proj.weight", F"model.encoder.layers.{i}.self_attn.value_proj.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.value_proj.bias", F"model.encoder.layers.{i}.self_attn.value_proj.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.output_proj.weight", F"model.encoder.layers.{i}.self_attn.output_proj.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.self_attn.output_proj.bias", F"model.encoder.layers.{i}.self_attn.output_proj.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.norm1.weight", F"model.encoder.layers.{i}.self_attn_layer_norm.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.norm1.bias", F"model.encoder.layers.{i}.self_attn_layer_norm.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.linear1.weight", F"model.encoder.layers.{i}.fc1.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.linear1.bias", F"model.encoder.layers.{i}.fc1.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.linear2.weight", F"model.encoder.layers.{i}.fc2.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.linear2.bias", F"model.encoder.layers.{i}.fc2.bias") ) rename_keys.append((F"transformer.encoder.layers.{i}.norm2.weight", F"model.encoder.layers.{i}.final_layer_norm.weight") ) rename_keys.append((F"transformer.encoder.layers.{i}.norm2.bias", F"model.encoder.layers.{i}.final_layer_norm.bias") ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight", F"model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias", F"model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.attention_weights.weight", F"model.decoder.layers.{i}.encoder_attn.attention_weights.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.attention_weights.bias", F"model.decoder.layers.{i}.encoder_attn.attention_weights.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.value_proj.weight", F"model.decoder.layers.{i}.encoder_attn.value_proj.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.value_proj.bias", F"model.decoder.layers.{i}.encoder_attn.value_proj.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.output_proj.weight", F"model.decoder.layers.{i}.encoder_attn.output_proj.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.cross_attn.output_proj.bias", F"model.decoder.layers.{i}.encoder_attn.output_proj.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm1.weight", F"model.decoder.layers.{i}.encoder_attn_layer_norm.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm1.bias", F"model.decoder.layers.{i}.encoder_attn_layer_norm.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.self_attn.out_proj.weight", F"model.decoder.layers.{i}.self_attn.out_proj.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.self_attn.out_proj.bias", F"model.decoder.layers.{i}.self_attn.out_proj.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm2.weight", F"model.decoder.layers.{i}.self_attn_layer_norm.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm2.bias", F"model.decoder.layers.{i}.self_attn_layer_norm.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.linear1.weight", F"model.decoder.layers.{i}.fc1.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.linear1.bias", F"model.decoder.layers.{i}.fc1.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.linear2.weight", F"model.decoder.layers.{i}.fc2.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.linear2.bias", F"model.decoder.layers.{i}.fc2.bias") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm3.weight", F"model.decoder.layers.{i}.final_layer_norm.weight") ) rename_keys.append((F"transformer.decoder.layers.{i}.norm3.bias", F"model.decoder.layers.{i}.final_layer_norm.bias") ) # fmt: on return rename_keys def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase , lowerCamelCase ): __magic_name__ : str =dct.pop(lowerCamelCase ) __magic_name__ : List[str] =val def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase ): __magic_name__ : Tuple =[int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): __magic_name__ : Optional[int] =num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) __magic_name__ : List[str] =state_dict.pop(F"backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight" ) __magic_name__ : Any =state_dict.pop(F"backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict __magic_name__ : List[Any] =in_proj_weight[:dim, :] __magic_name__ : Tuple =in_proj_bias[: dim] __magic_name__ : str =in_proj_weight[ dim : dim * 2, : ] __magic_name__ : Optional[int] =in_proj_bias[ dim : dim * 2 ] __magic_name__ : Union[str, Any] =in_proj_weight[ -dim :, : ] __magic_name__ : Tuple =in_proj_bias[-dim :] # fmt: on def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase ): # transformer decoder self-attention layers __magic_name__ : Union[str, Any] =config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention __magic_name__ : Dict =state_dict.pop(F"transformer.decoder.layers.{i}.self_attn.in_proj_weight" ) __magic_name__ : int =state_dict.pop(F"transformer.decoder.layers.{i}.self_attn.in_proj_bias" ) # next, add query, keys and values (in that order) to the state dict __magic_name__ : int =in_proj_weight[:hidden_size, :] __magic_name__ : Tuple =in_proj_bias[:hidden_size] __magic_name__ : int =in_proj_weight[ hidden_size : hidden_size * 2, : ] __magic_name__ : List[Any] =in_proj_bias[hidden_size : hidden_size * 2] __magic_name__ : str =in_proj_weight[-hidden_size:, :] __magic_name__ : str =in_proj_bias[-hidden_size:] def lowerCAmelCase_ ( ): __magic_name__ : int ="""http://images.cocodataset.org/val2017/000000039769.jpg""" __magic_name__ : str =Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase , lowerCamelCase ): __magic_name__ : Dict =get_deta_config(lowerCamelCase ) # load original state dict if model_name == "deta-swin-large": __magic_name__ : List[str] =hf_hub_download(repo_id="""nielsr/deta-checkpoints""" , filename="""adet_swin_ft.pth""" ) elif model_name == "deta-swin-large-o365": __magic_name__ : Dict =hf_hub_download(repo_id="""jozhang97/deta-swin-l-o365""" , filename="""deta_swin_pt_o365.pth""" ) else: raise ValueError(F"Model name {model_name} not supported" ) __magic_name__ : int =torch.load(lowerCamelCase , map_location="""cpu""" )["""model"""] # original state dict for name, param in state_dict.items(): print(lowerCamelCase , param.shape ) # rename keys __magic_name__ : Any =create_rename_keys(lowerCamelCase ) for src, dest in rename_keys: rename_key(lowerCamelCase , lowerCamelCase , lowerCamelCase ) read_in_swin_q_k_v(lowerCamelCase , config.backbone_config ) read_in_decoder_q_k_v(lowerCamelCase , lowerCamelCase ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: __magic_name__ : Any =state_dict.pop(lowerCamelCase ) __magic_name__ : Any =val if "input_proj" in key: __magic_name__ : List[str] =state_dict.pop(lowerCamelCase ) __magic_name__ : Optional[Any] =val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: __magic_name__ : int =state_dict.pop(lowerCamelCase ) __magic_name__ : Any =val # finally, create HuggingFace model and load state dict __magic_name__ : str =DetaForObjectDetection(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() __magic_name__ : Optional[Any] ="""cuda""" if torch.cuda.is_available() else """cpu""" model.to(lowerCamelCase ) # load image processor __magic_name__ : Any =DetaImageProcessor(format="""coco_detection""" ) # verify our conversion on image __magic_name__ : Tuple =prepare_img() __magic_name__ : Optional[Any] =processor(images=lowerCamelCase , return_tensors="""pt""" ) __magic_name__ : int =encoding["""pixel_values"""] __magic_name__ : List[str] =model(pixel_values.to(lowerCamelCase ) ) # verify logits print("""Logits:""" , outputs.logits[0, :3, :3] ) print("""Boxes:""" , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": __magic_name__ : str =torch.tensor( [[-7.6_3_0_8, -2.8_4_8_5, -5.3_7_3_7], [-7.2_0_3_7, -4.5_5_0_5, -4.8_0_2_7], [-7.2_9_4_3, -4.2_6_1_1, -4.6_6_1_7]] ) __magic_name__ : Dict =torch.tensor([[0.4_9_8_7, 0.4_9_6_9, 0.9_9_9_9], [0.2_5_4_9, 0.5_4_9_8, 0.4_8_0_5], [0.5_4_9_8, 0.2_7_5_7, 0.0_5_6_9]] ) elif model_name == "deta-swin-large-o365": __magic_name__ : List[Any] =torch.tensor( [[-8.0_1_2_2, -3.5_7_2_0, -4.9_7_1_7], [-8.1_5_4_7, -3.6_8_8_6, -4.6_3_8_9], [-7.6_6_1_0, -3.6_1_9_4, -5.0_1_3_4]] ) __magic_name__ : Optional[Any] =torch.tensor([[0.2_5_2_3, 0.5_5_4_9, 0.4_8_8_1], [0.7_7_1_5, 0.4_1_4_9, 0.4_6_0_1], [0.5_5_0_3, 0.2_7_5_3, 0.0_5_7_5]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(lowerCamelCase ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(lowerCamelCase ) , atol=1E-4 ) print("""Everything ok!""" ) if pytorch_dump_folder_path: # Save model and processor logger.info(F"Saving PyTorch model and processor to {pytorch_dump_folder_path}..." ) Path(lowerCamelCase ).mkdir(exist_ok=lowerCamelCase ) model.save_pretrained(lowerCamelCase ) processor.save_pretrained(lowerCamelCase ) # Push to hub if push_to_hub: print("""Pushing model and processor to hub...""" ) model.push_to_hub(F"jozhang97/{model_name}" ) processor.push_to_hub(F"jozhang97/{model_name}" ) if __name__ == "__main__": UpperCAmelCase_ : Optional[Any] = argparse.ArgumentParser() parser.add_argument( "--model_name", type=str, default="deta-swin-large", choices=["deta-swin-large", "deta-swin-large-o365"], help="Name of the model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model.", ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCAmelCase_ : str = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
21
'''simple docstring''' from math import factorial def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k" ) return factorial(_SCREAMING_SNAKE_CASE ) // (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) if __name__ == "__main__": print( """The number of five-card hands possible from a standard""", f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( """If a class of 40 students must be arranged into groups of""", f"""4 for group projects, there are {combinations(40, 4)} ways""", """to arrange them.\n""", ) print( """If 10 teams are competing in a Formula One race, there""", f"""are {combinations(10, 3)} ways that first, second and""", """third place can be awarded.""", )
71
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer _snake_case : Tuple = logging.get_logger(__name__) _snake_case : Optional[int] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} _snake_case : List[Any] = { 'vocab_file': { 'squeezebert/squeezebert-uncased': ( 'https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt' ), 'squeezebert/squeezebert-mnli': 'https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt', 'squeezebert/squeezebert-mnli-headless': ( 'https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'squeezebert/squeezebert-uncased': ( 'https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json' ), 'squeezebert/squeezebert-mnli': ( 'https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json' ), 'squeezebert/squeezebert-mnli-headless': ( 'https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json' ), }, } _snake_case : Union[str, Any] = { 'squeezebert/squeezebert-uncased': 512, 'squeezebert/squeezebert-mnli': 512, 'squeezebert/squeezebert-mnli-headless': 512, } _snake_case : Tuple = { 'squeezebert/squeezebert-uncased': {'do_lower_case': True}, 'squeezebert/squeezebert-mnli': {'do_lower_case': True}, 'squeezebert/squeezebert-mnli-headless': {'do_lower_case': True}, } class A ( _a ): lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_INIT_CONFIGURATION lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = SqueezeBertTokenizer def __init__( self : str , lowerCAmelCase_ : str=None , lowerCAmelCase_ : List[str]=None , lowerCAmelCase_ : str=True , lowerCAmelCase_ : List[str]="[UNK]" , lowerCAmelCase_ : Union[str, Any]="[SEP]" , lowerCAmelCase_ : Optional[Any]="[PAD]" , lowerCAmelCase_ : Any="[CLS]" , lowerCAmelCase_ : List[str]="[MASK]" , lowerCAmelCase_ : int=True , lowerCAmelCase_ : List[Any]=None , **lowerCAmelCase_ : Optional[int] , ) -> int: """simple docstring""" super().__init__( lowerCAmelCase_ , tokenizer_file=lowerCAmelCase_ , do_lower_case=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , sep_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , cls_token=lowerCAmelCase_ , mask_token=lowerCAmelCase_ , tokenize_chinese_chars=lowerCAmelCase_ , strip_accents=lowerCAmelCase_ , **lowerCAmelCase_ , ) _a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , lowerCAmelCase_ ) != do_lower_case or normalizer_state.get('''strip_accents''' , lowerCAmelCase_ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , lowerCAmelCase_ ) != tokenize_chinese_chars ): _a = getattr(lowerCAmelCase_ , normalizer_state.pop('''type''' ) ) _a = do_lower_case _a = strip_accents _a = tokenize_chinese_chars _a = normalizer_class(**lowerCAmelCase_ ) _a = do_lower_case def __lowerCAmelCase ( self : Union[str, Any] , lowerCAmelCase_ : int , lowerCAmelCase_ : Optional[Any]=None ) -> List[str]: """simple docstring""" _a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : Any , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowerCAmelCase ( self : Optional[Any] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _a = self._tokenizer.model.save(lowerCAmelCase_ , name=lowerCAmelCase_ ) return tuple(lowerCAmelCase_ )
22
'''simple docstring''' import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =VideoToVideoSDPipeline __A : Tuple =TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"} __A : Union[str, Any] =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"} __A : str =PipelineTesterMixin.required_optional_params - {"latents"} __A : Dict =False # No `output_type`. __A : Optional[int] =frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") ,cross_attention_dim=32 ,attention_head_dim=4 ,) UpperCAmelCase_ : int = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=1_28 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Dict = 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=10_00 ,hidden_act="gelu" ,projection_dim=5_12 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): # 3 frames UpperCAmelCase_ : Dict = floats_tensor((1, 3, 3, 32, 32) ,rng=random.Random(_snake_case ) ).to(_snake_case ) if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Tuple = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : Tuple = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Dict = self.get_dummy_components() UpperCAmelCase_ : str = VideoToVideoSDPipeline(**_snake_case ) UpperCAmelCase_ : int = sd_pipe.to(_snake_case ) sd_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = "np" UpperCAmelCase_ : Dict = sd_pipe(**_snake_case ).frames UpperCAmelCase_ : Tuple = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) UpperCAmelCase_ : Dict = np.array([1_06, 1_17, 1_13, 1_74, 1_37, 1_12, 1_48, 1_51, 1_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ,expected_max_diff=5E-3 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): return super().test_progress_bar() @slow @skip_mps class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" ,torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames UpperCAmelCase_ : str = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase_ : int = torch.randn((1, 10, 3, 10_24, 5_76) ,generator=_snake_case ) UpperCAmelCase_ : List[Any] = video.to("cuda" ) UpperCAmelCase_ : List[Any] = "Spiderman is surfing" UpperCAmelCase_ : Optional[Any] = pipe(_snake_case ,video=_snake_case ,generator=_snake_case ,num_inference_steps=3 ,output_type="pt" ).frames UpperCAmelCase_ : Any = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
71
0
def _snake_case (__lowercase): UpperCamelCase_ = int(__lowercase) if decimal in (0, 1): # Exit cases for the recursion return str(__lowercase) UpperCamelCase_ , UpperCamelCase_ = divmod(__lowercase , 2) return binary_recursive(__lowercase) + str(__lowercase) def _snake_case (__lowercase): UpperCamelCase_ = str(__lowercase).strip() if not number: raise ValueError('No input value was provided') UpperCamelCase_ = '-' if number.startswith('-') else '' UpperCamelCase_ = number.lstrip('-') if not number.isnumeric(): raise ValueError('Input value is not an integer') return f"""{negative}0b{binary_recursive(int(__lowercase))}""" if __name__ == "__main__": from doctest import testmod testmod()
23
'''simple docstring''' import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) _lowerCamelCase = pytest.mark.integration @pytest.mark.parametrize("path" , ["paws", "csv"] ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : str ) -> Tuple: """simple docstring""" inspect_dataset(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.parametrize("path" , ["accuracy"] ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: """simple docstring""" inspect_metric(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = path + ".py" assert script_name in os.listdir(_SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, config_name, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict ) -> str: """simple docstring""" UpperCAmelCase_ : int = get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : List[str] ) -> str: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_config_info(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "path, expected" , [ ("squad", "plain_text"), ("acronym_identification", "default"), ("lhoestq/squad", "plain_text"), ("lhoestq/test", "default"), ("lhoestq/demo1", "lhoestq--demo1"), ("dalle-mini/wit", "dalle-mini--wit"), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_config_names(_SCREAMING_SNAKE_CASE ) assert expected in config_names @pytest.mark.parametrize( "path, expected_configs, expected_splits_in_first_config" , [ ("squad", ["plain_text"], ["train", "validation"]), ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]), ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str] ) -> Any: """simple docstring""" UpperCAmelCase_ : Any = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert list(infos.keys() ) == expected_configs UpperCAmelCase_ : Optional[Any] = expected_configs[0] assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( "path, expected_config, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ) -> Any: """simple docstring""" UpperCAmelCase_ : Optional[int] = get_dataset_infos(_SCREAMING_SNAKE_CASE ) assert expected_config in infos UpperCAmelCase_ : Dict = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : str ) -> Any: """simple docstring""" with pytest.raises(_SCREAMING_SNAKE_CASE ): get_dataset_split_names(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE )
71
0
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2 ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
0
import unittest from transformers import MPNetConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) class _UpperCamelCase : '''simple docstring''' def __init__( self : Union[str, Any] , a : Tuple , a : List[str]=13 , a : List[str]=7 , a : Union[str, Any]=True , a : List[Any]=True , a : Any=False , a : List[str]=True , a : str=99 , a : Union[str, Any]=64 , a : Any=5 , a : Dict=4 , a : List[Any]=64 , a : Optional[Any]="gelu" , a : Tuple=0.1 , a : Union[str, Any]=0.1 , a : Dict=512 , a : List[str]=16 , a : Tuple=2 , a : Optional[int]=0.02 , a : Tuple=3 , a : List[Any]=4 , a : Any=None , ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : List[Any] = batch_size SCREAMING_SNAKE_CASE : Any = seq_length SCREAMING_SNAKE_CASE : Tuple = is_training SCREAMING_SNAKE_CASE : List[Any] = use_input_mask SCREAMING_SNAKE_CASE : Union[str, Any] = use_token_type_ids SCREAMING_SNAKE_CASE : Union[str, Any] = use_labels SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : List[Any] = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : List[str] = type_vocab_size SCREAMING_SNAKE_CASE : str = type_sequence_label_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Optional[int] = num_choices SCREAMING_SNAKE_CASE : Optional[int] = scope def __UpperCamelCase ( self : int ) -> List[str]: """simple docstring""" return MPNetConfig.from_pretrained("microsoft/mpnet-base" ) def __UpperCamelCase ( self : List[Any] ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[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 : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = 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 : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCamelCase ( self : List[Any] ) -> Dict: """simple docstring""" return MPNetConfig( 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 , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self : Optional[int] , a : Union[str, Any] , a : str , a : int , a : List[str] , a : Union[str, Any] , a : List[str] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = MPNetModel(config=a ) model.to(a ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model(a , a ) SCREAMING_SNAKE_CASE : int = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __UpperCamelCase ( self : List[str] , a : Any , a : Union[str, Any] , a : Dict , a : int , a : List[Any] , a : Dict ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = MPNetForQuestionAnswering(config=a ) model.to(a ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model( a , attention_mask=a , start_positions=a , end_positions=a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __UpperCamelCase ( self : Tuple , a : Union[str, Any] , a : int , a : Optional[int] , a : Dict , a : Any , a : List[str] ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = MPNetForSequenceClassification(a ) model.to(a ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : List[str] , a : Any , a : Optional[Any] , a : Dict , a : Optional[Any] , a : int , a : Tuple ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[int] = MPNetForMultipleChoice(config=a ) model.to(a ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : Tuple = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[Any] = model( a , attention_mask=a , labels=a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __UpperCamelCase ( self : Optional[Any] , a : str , a : Tuple , a : Optional[int] , a : List[Any] , a : Tuple , a : int ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = self.num_labels SCREAMING_SNAKE_CASE : List[str] = MPNetForTokenClassification(config=a ) model.to(a ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCamelCase ( self : Dict ) -> Optional[Any]: """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)) : Optional[Any] = config_and_inputs SCREAMING_SNAKE_CASE : List[str] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _UpperCamelCase ( __A , __A , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ =( ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) if is_torch_available() else () ) lowerCamelCase__ =( { 'feature-extraction': MPNetModel, 'fill-mask': MPNetForMaskedLM, 'question-answering': MPNetForQuestionAnswering, 'text-classification': MPNetForSequenceClassification, 'token-classification': MPNetForTokenClassification, 'zero-shot': MPNetForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase__ =False lowerCamelCase__ =True def __UpperCamelCase ( self : Tuple ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = MPNetModelTester(self ) SCREAMING_SNAKE_CASE : str = ConfigTester(self , config_class=a , hidden_size=37 ) def __UpperCamelCase ( self : int ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def __UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_model(*a ) def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_sequence_classification(*a ) def __UpperCamelCase ( self : Dict ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_multiple_choice(*a ) def __UpperCamelCase ( self : Any ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_token_classification(*a ) def __UpperCamelCase ( self : List[Any] ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_question_answering(*a ) @require_torch class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCamelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = MPNetModel.from_pretrained("microsoft/mpnet-base" ) SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) SCREAMING_SNAKE_CASE : List[str] = model(a )[0] SCREAMING_SNAKE_CASE : Tuple = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , a ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]] ) # compare the actual values for a slice. self.assertTrue(torch.allclose(output[:, :3, :3] , a , atol=1e-4 ) )
25
'''simple docstring''' import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Any =AudioLDMPipeline __A : Dict =TEXT_TO_AUDIO_PARAMS __A : Any =TEXT_TO_AUDIO_BATCH_PARAMS __A : Tuple =frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ]) def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = 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, 64) ,class_embed_type="simple_projection" ,projection_class_embeddings_input_dim=32 ,class_embeddings_concat=_snake_case ,) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=1 ,out_channels=1 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = ClapTextConfig( 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=10_00 ,projection_dim=32 ,) UpperCAmelCase_ : Optional[Any] = ClapTextModelWithProjection(_snake_case ) UpperCAmelCase_ : List[Any] = RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,model_max_length=77 ) UpperCAmelCase_ : Optional[int] = SpeechTaHifiGanConfig( model_in_dim=8 ,sampling_rate=1_60_00 ,upsample_initial_channel=16 ,upsample_rates=[2, 2] ,upsample_kernel_sizes=[4, 4] ,resblock_kernel_sizes=[3, 7] ,resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] ,normalize_before=_snake_case ,) UpperCAmelCase_ : Union[str, Any] = SpeechTaHifiGan(_snake_case ) UpperCAmelCase_ : Union[str, Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "vocoder": vocoder, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Optional[int] = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[str] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Any = { "prompt": "A hammer hitting a wooden surface", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : str = self.get_dummy_components() UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[str] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : Any = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : int = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Dict = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Tuple = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Any = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[str] = output.audios[0] UpperCAmelCase_ : Optional[Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : str = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : str = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : Dict = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : Optional[Any] = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Tuple = F.normalize(_snake_case ,dim=-1 ) UpperCAmelCase_ : int = prompt_embeds # forward UpperCAmelCase_ : int = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : List[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.get_dummy_components() UpperCAmelCase_ : Tuple = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = 3 * ["this is a negative prompt"] UpperCAmelCase_ : Any = negative_prompt UpperCAmelCase_ : Union[str, Any] = 3 * [inputs["prompt"]] # forward UpperCAmelCase_ : Dict = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Dict = output.audios[0] UpperCAmelCase_ : Tuple = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = 3 * [inputs.pop("prompt" )] UpperCAmelCase_ : List[Any] = [] for p in [prompt, negative_prompt]: UpperCAmelCase_ : Any = audioldm_pipe.tokenizer( _snake_case ,padding="max_length" ,max_length=audioldm_pipe.tokenizer.model_max_length ,truncation=_snake_case ,return_tensors="pt" ,) UpperCAmelCase_ : List[Any] = text_inputs["input_ids"].to(_snake_case ) UpperCAmelCase_ : str = audioldm_pipe.text_encoder( _snake_case ,) UpperCAmelCase_ : List[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state UpperCAmelCase_ : Any = F.normalize(_snake_case ,dim=-1 ) embeds.append(_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = embeds # forward UpperCAmelCase_ : Tuple = audioldm_pipe(**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Any = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Optional[Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : int = "egg cracking" UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ,negative_prompt=_snake_case ) UpperCAmelCase_ : int = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 2_56 UpperCAmelCase_ : List[Any] = audio[:10] UpperCAmelCase_ : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : List[str] = self.get_dummy_components() UpperCAmelCase_ : Dict = PNDMScheduler(skip_prk_steps=_snake_case ) UpperCAmelCase_ : Any = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : Any = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Dict = "A hammer hitting a wooden surface" # test num_waveforms_per_prompt=1 (default) UpperCAmelCase_ : Any = audioldm_pipe(_snake_case ,num_inference_steps=2 ).audios assert audios.shape == (1, 2_56) # test num_waveforms_per_prompt=1 (default) for batch of prompts UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : Dict = audioldm_pipe([prompt] * batch_size ,num_inference_steps=2 ).audios assert audios.shape == (batch_size, 2_56) # test num_waveforms_per_prompt for single prompt UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : List[Any] = audioldm_pipe(_snake_case ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (num_waveforms_per_prompt, 2_56) # test num_waveforms_per_prompt for batch of prompts UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = audioldm_pipe( [prompt] * batch_size ,num_inference_steps=2 ,num_waveforms_per_prompt=_snake_case ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ : Optional[Any] = self.get_dummy_components() UpperCAmelCase_ : Union[str, Any] = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : List[Any] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe.vocoder.config.sampling_rate UpperCAmelCase_ : Any = self.get_dummy_inputs(_snake_case ) UpperCAmelCase_ : Optional[int] = audioldm_pipe(audio_length_in_s=0.016 ,**_snake_case ) UpperCAmelCase_ : str = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.016 UpperCAmelCase_ : List[Any] = audioldm_pipe(audio_length_in_s=0.032 ,**_snake_case ) UpperCAmelCase_ : Any = output.audios[0] assert audio.ndim == 1 assert len(_snake_case ) / vocoder_sampling_rate == 0.032 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.get_dummy_components() UpperCAmelCase_ : str = AudioLDMPipeline(**_snake_case ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : int = ["hey"] UpperCAmelCase_ : Dict = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : Any = output.audios.shape assert audio_shape == (1, 2_56) UpperCAmelCase_ : Tuple = audioldm_pipe.vocoder.config config.model_in_dim *= 2 UpperCAmelCase_ : List[Any] = SpeechTaHifiGan(_snake_case ).to(_snake_case ) UpperCAmelCase_ : Tuple = audioldm_pipe(_snake_case ,num_inference_steps=1 ) UpperCAmelCase_ : int = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 2_56) def UpperCamelCase__ ( self ): self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case ) def UpperCamelCase__ ( self ): self._test_inference_batch_single_identical(test_mean_pixel_difference=_snake_case ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() ,reason="XFormers attention is only available with CUDA and `xformers` installed" ,) def UpperCamelCase__ ( self ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case ) @slow class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ,_snake_case ,_snake_case="cpu" ,_snake_case=torch.floataa ,_snake_case=0 ): UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : str = np.random.RandomState(_snake_case ).standard_normal((1, 8, 1_28, 16) ) UpperCAmelCase_ : Optional[Any] = torch.from_numpy(_snake_case ).to(device=_snake_case ,dtype=_snake_case ) UpperCAmelCase_ : List[str] = { "prompt": "A hammer hitting a wooden surface", "latents": latents, "generator": generator, "num_inference_steps": 3, "guidance_scale": 2.5, } return inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : int = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : Optional[int] = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : List[Any] = self.get_inputs(_snake_case ) UpperCAmelCase_ : List[Any] = 25 UpperCAmelCase_ : Union[str, Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Union[str, Any] = audio[7_72_30:7_72_40] UpperCAmelCase_ : Any = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) UpperCAmelCase_ : Dict = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = AudioLDMPipeline.from_pretrained("cvssp/audioldm" ) UpperCAmelCase_ : List[Any] = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) UpperCAmelCase_ : int = audioldm_pipe.to(_snake_case ) audioldm_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Tuple = self.get_inputs(_snake_case ) UpperCAmelCase_ : Optional[Any] = audioldm_pipe(**_snake_case ).audios[0] assert audio.ndim == 1 assert len(_snake_case ) == 8_19_20 UpperCAmelCase_ : Any = audio[2_77_80:2_77_90] UpperCAmelCase_ : List[str] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) UpperCAmelCase_ : Union[str, Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3E-2
71
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = { "uclanlp/visualbert-vqa": "https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json", "uclanlp/visualbert-vqa-pre": "https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json", "uclanlp/visualbert-vqa-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-vcr": "https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json", "uclanlp/visualbert-vcr-pre": "https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json", "uclanlp/visualbert-vcr-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-nlvr2": "https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json", "uclanlp/visualbert-nlvr2-pre": "https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json", "uclanlp/visualbert-nlvr2-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class _A ( __lowercase ): lowercase__: Optional[Any] = '''visual_bert''' def __init__( self : Union[str, Any] , __magic_name__ : Any=3_05_22 , __magic_name__ : Union[str, Any]=7_68 , __magic_name__ : Any=5_12 , __magic_name__ : Dict=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : Optional[Any]=30_72 , __magic_name__ : Tuple="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[Any]=0.1 , __magic_name__ : List[str]=5_12 , __magic_name__ : int=2 , __magic_name__ : str=0.02 , __magic_name__ : Dict=1E-12 , __magic_name__ : Any=False , __magic_name__ : List[str]=True , __magic_name__ : Union[str, Any]=1 , __magic_name__ : Dict=0 , __magic_name__ : Union[str, Any]=2 , **__magic_name__ : Tuple , ) -> str: """simple docstring""" super().__init__(pad_token_id=__magic_name__ , bos_token_id=__magic_name__ , eos_token_id=__magic_name__ , **__magic_name__ ) __snake_case : Dict = vocab_size __snake_case : Tuple = max_position_embeddings __snake_case : int = hidden_size __snake_case : Dict = visual_embedding_dim __snake_case : Tuple = num_hidden_layers __snake_case : str = num_attention_heads __snake_case : str = intermediate_size __snake_case : int = hidden_act __snake_case : Tuple = hidden_dropout_prob __snake_case : int = attention_probs_dropout_prob __snake_case : List[str] = initializer_range __snake_case : Optional[Any] = type_vocab_size __snake_case : Union[str, Any] = layer_norm_eps __snake_case : Any = bypass_transformer __snake_case : Dict = special_visual_initialize
26
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable _lowerCamelCase = { """configuration_gpt_neox_japanese""": ["""GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXJapaneseConfig"""], """tokenization_gpt_neox_japanese""": ["""GPTNeoXJapaneseTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXJapaneseForCausalLM""", """GPTNeoXJapaneseLayer""", """GPTNeoXJapaneseModel""", """GPTNeoXJapanesePreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
71
0
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING __A : Optional[int] = logging.get_logger(__name__) __A : Dict = Dict[str, Any] __A : Optional[int] = List[Prediction] @add_end_docstrings(__snake_case ) class lowerCamelCase( __snake_case ): '''simple docstring''' def __init__( self , *snake_case_ , **snake_case_ ): super().__init__(*snake_case_ , **snake_case_ ) if self.framework == "tf": raise ValueError(F"The {self.__class__} is only available in PyTorch." ) requires_backends(self , 'vision' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def lowerCAmelCase__ ( self , **snake_case_ ): _A = {} if "threshold" in kwargs: _A = kwargs['threshold'] return {}, {}, postprocess_kwargs def __call__( self , *snake_case_ , **snake_case_ ): return super().__call__(*snake_case_ , **snake_case_ ) def lowerCAmelCase__ ( self , snake_case_ ): _A = load_image(snake_case_ ) _A = torch.IntTensor([[image.height, image.width]] ) _A = self.image_processor(images=[image] , return_tensors='pt' ) if self.tokenizer is not None: _A = self.tokenizer(text=inputs['words'] , boxes=inputs['boxes'] , return_tensors='pt' ) _A = target_size return inputs def lowerCAmelCase__ ( self , snake_case_ ): _A = model_inputs.pop('target_size' ) _A = self.model(**snake_case_ ) _A = outputs.__class__({'target_size': target_size, **outputs} ) if self.tokenizer is not None: _A = model_inputs['bbox'] return model_outputs def lowerCAmelCase__ ( self , snake_case_ , snake_case_=0.9 ): _A = model_outputs['target_size'] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. _A, _A = target_size[0].tolist() def unnormalize(snake_case_ ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 1000), (height * bbox[1] / 1000), (width * bbox[2] / 1000), (height * bbox[3] / 1000), ] ) ) _A, _A = model_outputs['logits'].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) _A = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] _A = [unnormalize(snake_case_ ) for bbox in model_outputs['bbox'].squeeze(0 )] _A = ['score', 'label', 'box'] _A = [dict(zip(snake_case_ , snake_case_ ) ) for vals in zip(scores.tolist() , snake_case_ , snake_case_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel _A = self.image_processor.post_process_object_detection(snake_case_ , snake_case_ , snake_case_ ) _A = raw_annotations[0] _A = raw_annotation['scores'] _A = raw_annotation['labels'] _A = raw_annotation['boxes'] _A = scores.tolist() _A = [self.model.config.idalabel[label.item()] for label in labels] _A = [self._get_bounding_box(snake_case_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] _A = ['score', 'label', 'box'] _A = [ dict(zip(snake_case_ , snake_case_ ) ) for vals in zip(raw_annotation['scores'] , raw_annotation['labels'] , raw_annotation['boxes'] ) ] return annotation def lowerCAmelCase__ ( self , snake_case_ ): if self.framework != "pt": raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.' ) _A, _A, _A, _A = box.int().tolist() _A = { 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax, } return bbox
27
'''simple docstring''' import heapq def a__ ( _SCREAMING_SNAKE_CASE : dict ) -> set[int]: """simple docstring""" UpperCAmelCase_ : list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(_SCREAMING_SNAKE_CASE , [-1 * len(_SCREAMING_SNAKE_CASE ), (key, value)] ) # chosen_vertices = set of chosen vertices UpperCAmelCase_ : Optional[int] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices UpperCAmelCase_ : Tuple = heapq.heappop(_SCREAMING_SNAKE_CASE )[1][0] chosen_vertices.add(_SCREAMING_SNAKE_CASE ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: UpperCAmelCase_ : Any = elem[1][1].index(_SCREAMING_SNAKE_CASE ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(_SCREAMING_SNAKE_CASE ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(f"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
71
0
'''simple docstring''' import math from collections.abc import Callable def lowercase__( __UpperCamelCase: Callable[[float], float] ,__UpperCamelCase: float ,__UpperCamelCase: float ): """simple docstring""" SCREAMING_SNAKE_CASE : float = xa SCREAMING_SNAKE_CASE : float = xa while True: if x_n == x_na or function(__UpperCamelCase ) == function(__UpperCamelCase ): raise ZeroDivisionError('float division by zero, could not find root' ) SCREAMING_SNAKE_CASE : float = x_na - ( function(__UpperCamelCase ) / ((function(__UpperCamelCase ) - function(__UpperCamelCase )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na SCREAMING_SNAKE_CASE : Dict = x_na SCREAMING_SNAKE_CASE : Tuple = x_na def lowercase__( __UpperCamelCase: float ): """simple docstring""" return math.pow(__UpperCamelCase ,3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
28
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase = logging.get_logger(__name__) def a__ ( _SCREAMING_SNAKE_CASE : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(_SCREAMING_SNAKE_CASE ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class _snake_case (__SCREAMING_SNAKE_CASE): __A : Tuple =["pixel_values"] def __init__( self ,_snake_case = True ,_snake_case = None ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = True ,_snake_case = None ,_snake_case = True ,_snake_case = 1 / 2_55 ,_snake_case = True ,_snake_case = True ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__(**_snake_case ) UpperCAmelCase_ : Optional[Any] = size if size is not None else {"shortest_edge": 2_56} UpperCAmelCase_ : List[str] = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : str = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} UpperCAmelCase_ : Optional[Any] = get_size_dict(_snake_case ,param_name="crop_size" ) UpperCAmelCase_ : int = do_resize UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Dict = do_center_crop UpperCAmelCase_ : Optional[Any] = crop_size UpperCAmelCase_ : Optional[Any] = resample UpperCAmelCase_ : int = do_rescale UpperCAmelCase_ : Optional[int] = rescale_factor UpperCAmelCase_ : Dict = offset UpperCAmelCase_ : Optional[Any] = do_normalize UpperCAmelCase_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = PILImageResampling.BILINEAR ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Any = get_size_dict(_snake_case ,default_to_square=_snake_case ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(_snake_case ,size["shortest_edge"] ,default_to_square=_snake_case ) elif "height" in size and "width" in size: UpperCAmelCase_ : Optional[Any] = (size["height"], size["width"]) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(_snake_case ,size=_snake_case ,resample=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : Dict = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_snake_case ,size=(size["height"], size["width"]) ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = True ,_snake_case = None ,**_snake_case ,): UpperCAmelCase_ : int = image.astype(np.floataa ) if offset: UpperCAmelCase_ : Any = image - (scale / 2) return rescale(_snake_case ,scale=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case = None ,**_snake_case ,): return normalize(_snake_case ,mean=_snake_case ,std=_snake_case ,data_format=_snake_case ,**_snake_case ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. UpperCAmelCase_ : Optional[int] = to_numpy_array(_snake_case ) if do_resize: UpperCAmelCase_ : Dict = self.resize(image=_snake_case ,size=_snake_case ,resample=_snake_case ) if do_center_crop: UpperCAmelCase_ : Optional[Any] = self.center_crop(_snake_case ,size=_snake_case ) if do_rescale: UpperCAmelCase_ : Union[str, Any] = self.rescale(image=_snake_case ,scale=_snake_case ,offset=_snake_case ) if do_normalize: UpperCAmelCase_ : Any = self.normalize(image=_snake_case ,mean=_snake_case ,std=_snake_case ) UpperCAmelCase_ : Any = to_channel_dimension_format(_snake_case ,_snake_case ) return image def UpperCamelCase__ ( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = ChannelDimension.FIRST ,**_snake_case ,): UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : str = resample if resample is not None else self.resample UpperCAmelCase_ : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : Tuple = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : List[Any] = offset if offset is not None else self.offset UpperCAmelCase_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : int = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : int = get_size_dict(_snake_case ,default_to_square=_snake_case ) UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : int = get_size_dict(_snake_case ,param_name="crop_size" ) if not valid_images(_snake_case ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) UpperCAmelCase_ : Any = make_batched(_snake_case ) UpperCAmelCase_ : Dict = [ [ self._preprocess_image( image=_snake_case ,do_resize=_snake_case ,size=_snake_case ,resample=_snake_case ,do_center_crop=_snake_case ,crop_size=_snake_case ,do_rescale=_snake_case ,rescale_factor=_snake_case ,offset=_snake_case ,do_normalize=_snake_case ,image_mean=_snake_case ,image_std=_snake_case ,data_format=_snake_case ,) for img in video ] for video in videos ] UpperCAmelCase_ : List[str] = {"pixel_values": videos} return BatchFeature(data=_snake_case ,tensor_type=_snake_case )
71
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor A_ = logging.get_logger(__name__) class __lowerCamelCase ( lowerCAmelCase ): def __init__( self , *UpperCAmelCase , **UpperCAmelCase ): warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , UpperCAmelCase , ) super().__init__(*UpperCAmelCase , **UpperCAmelCase )
29
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _snake_case (__SCREAMING_SNAKE_CASE): def __init__( self ,_snake_case ,_snake_case = None ,_snake_case = None ,_snake_case = None ,_snake_case = False ,_snake_case = False ,_snake_case = None ,_snake_case = None ,**_snake_case ,): super().__init__( _snake_case ,split=_snake_case ,features=_snake_case ,cache_dir=_snake_case ,keep_in_memory=_snake_case ,streaming=_snake_case ,num_proc=_snake_case ,**_snake_case ,) UpperCAmelCase_ : Tuple = field UpperCAmelCase_ : List[Any] = path_or_paths if isinstance(_snake_case ,_snake_case ) else {self.split: path_or_paths} UpperCAmelCase_ : Optional[int] = Json( cache_dir=_snake_case ,data_files=_snake_case ,features=_snake_case ,field=_snake_case ,**_snake_case ,) def UpperCamelCase__ ( self ): # Build iterable dataset if self.streaming: UpperCAmelCase_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase_ : Union[str, Any] = None UpperCAmelCase_ : int = None UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : int = None self.builder.download_and_prepare( download_config=_snake_case ,download_mode=_snake_case ,verification_mode=_snake_case ,base_path=_snake_case ,num_proc=self.num_proc ,) UpperCAmelCase_ : Dict = self.builder.as_dataset( split=self.split ,verification_mode=_snake_case ,in_memory=self.keep_in_memory ) return dataset class _snake_case : def __init__( self ,_snake_case ,_snake_case ,_snake_case = None ,_snake_case = None ,**_snake_case ,): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) UpperCAmelCase_ : int = dataset UpperCAmelCase_ : Union[str, Any] = path_or_buf UpperCAmelCase_ : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase_ : Dict = num_proc UpperCAmelCase_ : Optional[Any] = "utf-8" UpperCAmelCase_ : Optional[int] = to_json_kwargs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.to_json_kwargs.pop("path_or_buf" ,_snake_case ) UpperCAmelCase_ : Tuple = self.to_json_kwargs.pop("orient" ,"records" ) UpperCAmelCase_ : Any = self.to_json_kwargs.pop("lines" ,True if orient == "records" else False ) UpperCAmelCase_ : Optional[int] = self.to_json_kwargs.pop("index" ,False if orient in ["split", "table"] else True ) UpperCAmelCase_ : int = self.to_json_kwargs.pop("compression" ,_snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf ,"wb" ,compression=_snake_case ) as buffer: UpperCAmelCase_ : List[str] = self._write(file_obj=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' " was passed. Please provide a local path instead." ) UpperCAmelCase_ : Union[str, Any] = self._write( file_obj=self.path_or_buf ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**self.to_json_kwargs ) return written def UpperCamelCase__ ( self ,_snake_case ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = args UpperCAmelCase_ : List[str] = query_table( table=self.dataset.data ,key=slice(_snake_case ,offset + self.batch_size ) ,indices=self.dataset._indices ,) UpperCAmelCase_ : Optional[Any] = batch.to_pandas().to_json( path_or_buf=_snake_case ,orient=_snake_case ,lines=_snake_case ,index=_snake_case ,**_snake_case ) if not json_str.endswith("\n" ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ,_snake_case ,**_snake_case ,): UpperCAmelCase_ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset ) ,self.batch_size ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): UpperCAmelCase_ : Any = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: UpperCAmelCase_ , UpperCAmelCase_ : int = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,_snake_case ,_snake_case )] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating json from Arrow format" ,): written += file_obj.write(_snake_case ) return written
71
0
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class __a( unittest.TestCase ): """simple docstring""" def __init__( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=56 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=99 ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=7 ,_SCREAMING_SNAKE_CASE="gelu_new" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=16 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=0.02 ,_SCREAMING_SNAKE_CASE=4 ,_SCREAMING_SNAKE_CASE="block_sparse" ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=3 ,) -> Tuple: UpperCAmelCase_ : int = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Tuple = seq_length UpperCAmelCase_ : Optional[Any] = is_training UpperCAmelCase_ : Union[str, Any] = use_attention_mask UpperCAmelCase_ : Union[str, Any] = use_token_type_ids UpperCAmelCase_ : List[str] = use_labels UpperCAmelCase_ : List[str] = vocab_size UpperCAmelCase_ : Tuple = hidden_size UpperCAmelCase_ : Optional[Any] = num_hidden_layers UpperCAmelCase_ : List[Any] = num_attention_heads UpperCAmelCase_ : str = intermediate_size UpperCAmelCase_ : Dict = hidden_act UpperCAmelCase_ : Optional[int] = hidden_dropout_prob UpperCAmelCase_ : Tuple = attention_probs_dropout_prob UpperCAmelCase_ : str = max_position_embeddings UpperCAmelCase_ : List[str] = type_vocab_size UpperCAmelCase_ : Any = type_sequence_label_size UpperCAmelCase_ : List[Any] = initializer_range UpperCAmelCase_ : List[str] = num_choices UpperCAmelCase_ : int = rescale_embeddings UpperCAmelCase_ : List[Any] = attention_type UpperCAmelCase_ : Dict = use_bias UpperCAmelCase_ : Dict = block_size UpperCAmelCase_ : List[str] = num_random_blocks def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Tuple = None if self.use_attention_mask: UpperCAmelCase_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_ : Optional[Any] = None if self.use_token_type_ids: UpperCAmelCase_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) UpperCAmelCase_ : List[Any] = BigBirdConfig( 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=_SCREAMING_SNAKE_CASE ,initializer_range=self.initializer_range ,attention_type=self.attention_type ,block_size=self.block_size ,num_random_blocks=self.num_random_blocks ,use_bias=self.use_bias ,rescale_embeddings=self.rescale_embeddings ,) return config, input_ids, token_type_ids, attention_mask def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : Dict = self.prepare_config_and_inputs() UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Optional[Any] = config_and_inputs UpperCAmelCase_ : Optional[int] = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask, } return config, inputs_dict @require_flax class __a( _a , unittest.TestCase ): """simple docstring""" lowerCAmelCase = ( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) lowerCAmelCase = False lowerCAmelCase = False def a__ ( self ) -> int: UpperCAmelCase_ : int = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def a__ ( self ) -> Optional[int]: super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def a__ ( self ) -> Optional[Any]: super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def a__ ( self ) -> Tuple: super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def a__ ( self ) -> int: super().test_hidden_states_output() @slow def a__ ( self ) -> str: for model_class_name in self.all_model_classes: UpperCAmelCase_ : str = model_class_name.from_pretrained('''google/bigbird-roberta-base''' ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> Any: if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def a__ ( self ) -> List[str]: UpperCAmelCase_, UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase_ : Optional[int] = self._prepare_for_class(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = model_class(_SCREAMING_SNAKE_CASE ) @jax.jit def model_jitted(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ,**_SCREAMING_SNAKE_CASE ): return model(input_ids=_SCREAMING_SNAKE_CASE ,attention_mask=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) with self.subTest('''JIT Enabled''' ): UpperCAmelCase_ : Optional[int] = model_jitted(**_SCREAMING_SNAKE_CASE ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): UpperCAmelCase_ : Dict = model_jitted(**_SCREAMING_SNAKE_CASE ).to_tuple() self.assertEqual(len(_SCREAMING_SNAKE_CASE ) ,len(_SCREAMING_SNAKE_CASE ) ) for jitted_output, output in zip(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): self.assertEqual(jitted_output.shape ,output.shape ) def a__ ( self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE="outputs" ,_SCREAMING_SNAKE_CASE=None ) -> Any: # `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version, # an effort was done to return `attention_probs` (yet to be verified). if name.startswith('''outputs.attentions''' ): return else: super().check_pt_flax_outputs(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE )
30
'''simple docstring''' from ..utils import DummyObject, requires_backends class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Any =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] ) class _snake_case (metaclass=__SCREAMING_SNAKE_CASE): __A : Dict =["speech"] def __init__( self ,*_snake_case ,**_snake_case ): requires_backends(self ,["speech"] )
71
0
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list[int] , _SCREAMING_SNAKE_CASE : list[int] ) -> tuple[float, float]: """simple docstring""" if not len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) == 3: raise ValueError("Please enter a valid equation." ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("Both a & b of two equations can't be zero." ) # Extract the coefficients UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = equationa UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = equationa # Calculate the determinants of the matrices UpperCAmelCase_ : Optional[int] = aa * ba - aa * ba UpperCAmelCase_ : Optional[int] = ca * ba - ca * ba UpperCAmelCase_ : Any = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)" ) else: raise ValueError("No solution. (Inconsistent system)" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: UpperCAmelCase_ : Optional[int] = determinant_x / determinant UpperCAmelCase_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
71
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { "SCUT-DLVCLab/lilt-roberta-en-base": ( "https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json" ), } class __UpperCamelCase ( A__ ): __A : List[Any] = """lilt""" def __init__( self , _UpperCamelCase=30522 , _UpperCamelCase=768 , _UpperCamelCase=12 , _UpperCamelCase=12 , _UpperCamelCase=3072 , _UpperCamelCase="gelu" , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=512 , _UpperCamelCase=2 , _UpperCamelCase=0.02 , _UpperCamelCase=1e-12 , _UpperCamelCase=0 , _UpperCamelCase="absolute" , _UpperCamelCase=None , _UpperCamelCase=4 , _UpperCamelCase=1024 , **_UpperCamelCase , ): super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = hidden_act _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = position_embedding_type _UpperCAmelCase = classifier_dropout _UpperCAmelCase = channel_shrink_ratio _UpperCAmelCase = max_ad_position_embeddings
32
'''simple docstring''' from statistics import mean, stdev def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Dict = min(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = max(_SCREAMING_SNAKE_CASE ) # normalize data return [round((x - x_min) / (x_max - x_min) , _SCREAMING_SNAKE_CASE ) for x in data] def a__ ( _SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int = 3 ) -> list: """simple docstring""" UpperCAmelCase_ : Tuple = mean(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = stdev(_SCREAMING_SNAKE_CASE ) # standardize data return [round((x - mu) / (sigma) , _SCREAMING_SNAKE_CASE ) for x in data]
71
0
import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class __magic_name__ (unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] ): snake_case__ = '''| <pad> <unk> <s> </s> a b c d e f g h i j k'''.split() snake_case__ = dict(zip(_a , range(len(_a ) ) ) ) snake_case__ = { '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', } snake_case__ = { '''feature_size''': 1, '''padding_value''': 0.0, '''sampling_rate''': 1_60_00, '''return_attention_mask''': False, '''do_normalize''': True, } snake_case__ = tempfile.mkdtemp() snake_case__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) snake_case__ = os.path.join(self.tmpdirname , _a ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.feature_extraction_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) # load decoder from hub snake_case__ = '''hf-internal-testing/ngram-beam-search-decoder''' def SCREAMING_SNAKE_CASE__ ( self:str , **_a:List[Any] ): snake_case__ = self.add_kwargs_tokens_map.copy() kwargs.update(_a ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **_a ) def SCREAMING_SNAKE_CASE__ ( self:Dict , **_a:List[str] ): return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **_a ) def SCREAMING_SNAKE_CASE__ ( self:Optional[int] , **_a:int ): return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **_a ) def SCREAMING_SNAKE_CASE__ ( self:Tuple ): shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] ): snake_case__ = self.get_tokenizer() snake_case__ = self.get_feature_extractor() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) processor.save_pretrained(self.tmpdirname ) snake_case__ = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , _a ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , _a ) def SCREAMING_SNAKE_CASE__ ( self:str ): snake_case__ = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match snake_case__ = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def SCREAMING_SNAKE_CASE__ ( self:List[Any] ): snake_case__ = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['''xx'''] ) with self.assertRaisesRegex(_a , '''include''' ): WavaVecaProcessorWithLM( tokenizer=_a , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def SCREAMING_SNAKE_CASE__ ( self:List[str] ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = floats_list((3, 10_00) ) snake_case__ = feature_extractor(_a , return_tensors='''np''' ) snake_case__ = processor(_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = '''This is a test string''' snake_case__ = processor(text=_a ) snake_case__ = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] , _a:int=(2, 10, 16) , _a:Dict=77 ): np.random.seed(_a ) return np.random.rand(*_a ) def SCREAMING_SNAKE_CASE__ ( self:str ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = self._get_dummy_logits(shape=(10, 16) , seed=13 ) snake_case__ = processor.decode(_a ) snake_case__ = decoder.decode_beams(_a )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('''</s> <s> </s>''' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['''fork'''], ['''spawn''']] ) def SCREAMING_SNAKE_CASE__ ( self:int , _a:Union[str, Any] ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: snake_case__ = processor.batch_decode(_a ) else: with get_context(_a ).Pool() as pool: snake_case__ = processor.batch_decode(_a , _a ) snake_case__ = list(_a ) with get_context('''fork''' ).Pool() as p: snake_case__ = decoder.decode_beams_batch(_a , _a ) snake_case__ , snake_case__ , snake_case__ = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(_a , decoded_processor.text ) self.assertListEqual(['''<s> <s> </s>''', '''<s> <s> <s>'''] , decoded_processor.text ) self.assertListEqual(_a , decoded_processor.logit_score ) self.assertListEqual(_a , decoded_processor.lm_score ) def SCREAMING_SNAKE_CASE__ ( self:List[Any] ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = self._get_dummy_logits() snake_case__ = 15 snake_case__ = -20.0 snake_case__ = -4.0 snake_case__ = processor.batch_decode( _a , beam_width=_a , beam_prune_logp=_a , token_min_logp=_a , ) snake_case__ = decoded_processor_out.text snake_case__ = list(_a ) with get_context('''fork''' ).Pool() as pool: snake_case__ = decoder.decode_beams_batch( _a , _a , beam_width=_a , beam_prune_logp=_a , token_min_logp=_a , ) snake_case__ = [d[0][0] for d in decoded_decoder_out] snake_case__ = [d[0][2] for d in decoded_decoder_out] snake_case__ = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(_a , _a ) self.assertListEqual(['''</s> <s> <s>''', '''<s> <s> <s>'''] , _a ) self.assertTrue(np.array_equal(_a , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-20.054, -18.447] , _a , atol=1e-3 ) ) self.assertTrue(np.array_equal(_a , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-15.554, -13.9474] , _a , atol=1e-3 ) ) def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) snake_case__ = self._get_dummy_logits() snake_case__ = 2.0 snake_case__ = 5.0 snake_case__ = -20.0 snake_case__ = True snake_case__ = processor.batch_decode( _a , alpha=_a , beta=_a , unk_score_offset=_a , lm_score_boundary=_a , ) snake_case__ = decoded_processor_out.text snake_case__ = list(_a ) decoder.reset_params( alpha=_a , beta=_a , unk_score_offset=_a , lm_score_boundary=_a , ) with get_context('''fork''' ).Pool() as pool: snake_case__ = decoder.decode_beams_batch( _a , _a , ) snake_case__ = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(_a , _a ) self.assertListEqual(['''<s> </s> <s> </s> </s>''', '''</s> </s> <s> </s> </s>'''] , _a ) snake_case__ = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -20.0 ) self.assertEqual(lm_model.score_boundary , _a ) def SCREAMING_SNAKE_CASE__ ( self:int ): snake_case__ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) snake_case__ = processor.decoder.model_container[processor.decoder._model_key] snake_case__ = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() snake_case__ = os.listdir(_a ) snake_case__ = ['''alphabet.json''', '''language_model'''] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(_a , _a ) def SCREAMING_SNAKE_CASE__ ( self:int ): snake_case__ = snapshot_download('''hf-internal-testing/processor_with_lm''' ) snake_case__ = WavaVecaProcessorWithLM.from_pretrained(_a ) snake_case__ = processor.decoder.model_container[processor.decoder._model_key] snake_case__ = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() snake_case__ = os.listdir(_a ) snake_case__ = os.listdir(_a ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(_a , _a ) def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] ): snake_case__ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) snake_case__ = AutoProcessor.from_pretrained('''hf-internal-testing/processor_with_lm''' ) snake_case__ = floats_list((3, 10_00) ) snake_case__ = processor_wavaveca(_a , return_tensors='''np''' ) snake_case__ = processor_auto(_a , return_tensors='''np''' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1e-2 ) snake_case__ = self._get_dummy_logits() snake_case__ = processor_wavaveca.batch_decode(_a ) snake_case__ = processor_auto.batch_decode(_a ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def SCREAMING_SNAKE_CASE__ ( self:Dict ): snake_case__ = self.get_feature_extractor() snake_case__ = self.get_tokenizer() snake_case__ = self.get_decoder() snake_case__ = WavaVecaProcessorWithLM(tokenizer=_a , feature_extractor=_a , decoder=_a ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , ) @staticmethod def SCREAMING_SNAKE_CASE__ ( _a:Union[str, Any] , _a:Dict ): snake_case__ = [d[key] for d in offsets] return retrieved_list def SCREAMING_SNAKE_CASE__ ( self:List[Any] ): snake_case__ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) snake_case__ = self._get_dummy_logits()[0] snake_case__ = processor.decode(_a , output_word_offsets=_a ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_a , _a ) ) self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''end_offset''' ) , [1, 3, 5] ) def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] ): snake_case__ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) snake_case__ = self._get_dummy_logits() snake_case__ = processor.batch_decode(_a , output_word_offsets=_a ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_a , _a ) ) self.assertListEqual( [''' '''.join(self.get_from_offsets(_a , '''word''' ) ) for o in outputs['''word_offsets''']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''end_offset''' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def SCREAMING_SNAKE_CASE__ ( self:Any ): import torch snake_case__ = load_dataset('''common_voice''' , '''en''' , split='''train''' , streaming=_a ) snake_case__ = ds.cast_column('''audio''' , datasets.Audio(sampling_rate=1_60_00 ) ) snake_case__ = iter(_a ) snake_case__ = next(_a ) snake_case__ = AutoProcessor.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) snake_case__ = WavaVecaForCTC.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train snake_case__ = processor(sample['''audio''']['''array'''] , return_tensors='''pt''' ).input_values with torch.no_grad(): snake_case__ = model(_a ).logits.cpu().numpy() snake_case__ = processor.decode(logits[0] , output_word_offsets=_a ) snake_case__ = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate snake_case__ = [ { '''start_time''': d['''start_offset'''] * time_offset, '''end_time''': d['''end_offset'''] * time_offset, '''word''': d['''word'''], } for d in output['''word_offsets'''] ] snake_case__ = '''WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL''' # output words self.assertEqual(''' '''.join(self.get_from_offsets(_a , '''word''' ) ) , _a ) self.assertEqual(''' '''.join(self.get_from_offsets(_a , '''word''' ) ) , output.text ) # output times snake_case__ = torch.tensor(self.get_from_offsets(_a , '''start_time''' ) ) snake_case__ = torch.tensor(self.get_from_offsets(_a , '''end_time''' ) ) # fmt: off snake_case__ = torch.tensor([1.4199, 1.6599, 2.2599, 3.0, 3.24, 3.5999, 3.7999, 4.0999, 4.26, 4.94, 5.28, 5.6599, 5.78, 5.94, 6.32, 6.5399, 6.6599] ) snake_case__ = torch.tensor([1.5399, 1.8999, 2.9, 3.16, 3.5399, 3.72, 4.0199, 4.1799, 4.76, 5.1599, 5.5599, 5.6999, 5.86, 6.1999, 6.38, 6.6199, 6.94] ) # fmt: on self.assertTrue(torch.allclose(_a , _a , atol=0.01 ) ) self.assertTrue(torch.allclose(_a , _a , atol=0.01 ) )
33
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[Any]: """simple docstring""" UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Tuple = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Union[str, Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[int] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[int] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : int = 8 else: UpperCAmelCase_ : Optional[Any] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Any = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : int ) -> int: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize accelerator UpperCAmelCase_ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : str = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : Tuple = int(config["seed"] ) UpperCAmelCase_ : Union[str, Any] = int(config["batch_size"] ) UpperCAmelCase_ : List[str] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=_SCREAMING_SNAKE_CASE ) def inner_training_loop(_SCREAMING_SNAKE_CASE : List[str] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(_SCREAMING_SNAKE_CASE ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Dict = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Dict = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : str = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = outputs.loss accelerator.backward(_SCREAMING_SNAKE_CASE ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : str = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : Dict = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) UpperCAmelCase_ : Tuple = parser.parse_args() UpperCAmelCase_ : int = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
71
0
"""simple docstring""" import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow SCREAMING_SNAKE_CASE_ = False class snake_case_ ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self , lowerCamelCase_=3_2) -> Dict: set_seed(0) UpperCamelCase = UNetaDModel(sample_size=lowerCamelCase_ , in_channels=3 , out_channels=3) UpperCamelCase = torch.optim.SGD(model.parameters() , lr=0.0001) return model, optimizer @slow def UpperCAmelCase__ ( self) -> int: UpperCamelCase = '''cpu''' # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable UpperCamelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.0001 , beta_end=0.02 , beta_schedule='''linear''' , clip_sample=lowerCamelCase_ , ) UpperCamelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.0001 , beta_end=0.02 , beta_schedule='''linear''' , clip_sample=lowerCamelCase_ , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0) UpperCamelCase = [torch.randn((4, 3, 3_2, 3_2)).clip(-1 , 1).to(lowerCamelCase_) for _ in range(4)] UpperCamelCase = [torch.randn((4, 3, 3_2, 3_2)).to(lowerCamelCase_) for _ in range(4)] UpperCamelCase = [torch.randint(0 , 1_0_0_0 , (4,)).long().to(lowerCamelCase_) for _ in range(4)] # train with a DDPM scheduler UpperCamelCase , UpperCamelCase = self.get_model_optimizer(resolution=3_2) model.train().to(lowerCamelCase_) for i in range(4): optimizer.zero_grad() UpperCamelCase = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i]) UpperCamelCase = model(lowerCamelCase_ , timesteps[i]).sample UpperCamelCase = torch.nn.functional.mse_loss(lowerCamelCase_ , noise[i]) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM UpperCamelCase , UpperCamelCase = self.get_model_optimizer(resolution=3_2) model.train().to(lowerCamelCase_) for i in range(4): optimizer.zero_grad() UpperCamelCase = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i]) UpperCamelCase = model(lowerCamelCase_ , timesteps[i]).sample UpperCamelCase = torch.nn.functional.mse_loss(lowerCamelCase_ , noise[i]) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1e-5)) self.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1e-5))
34
'''simple docstring''' from __future__ import annotations def a__ ( _SCREAMING_SNAKE_CASE : int ) -> list[int]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = 2 UpperCAmelCase_ : Optional[int] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
71
0
import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: a_ :Union[str, Any] = None a_ :str = logging.get_logger(__name__) a_ :str = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} a_ :Union[str, Any] = { 'vocab_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model', 't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model', 't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model', }, 'tokenizer_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/tokenizer.json', 't5-base': 'https://huggingface.co/t5-base/resolve/main/tokenizer.json', 't5-large': 'https://huggingface.co/t5-large/resolve/main/tokenizer.json', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/tokenizer.json', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/tokenizer.json', }, } # TODO(PVP) - this should be removed in Transformers v5 a_ :Optional[int] = { 't5-small': 5_12, 't5-base': 5_12, 't5-large': 5_12, 't5-3b': 5_12, 't5-11b': 5_12, } class lowercase ( _UpperCAmelCase ): lowerCamelCase : Any = VOCAB_FILES_NAMES lowerCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : str = ['''input_ids''', '''attention_mask'''] lowerCamelCase : Dict = TaTokenizer lowerCamelCase : List[int] = [] def __init__( self : Optional[int] , _lowercase : Any=None , _lowercase : List[Any]=None , _lowercase : Dict="</s>" , _lowercase : int="<unk>" , _lowercase : int="<pad>" , _lowercase : List[Any]=1_00 , _lowercase : Dict=None , **_lowercase : Tuple , ): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE__ : List[str] = [f"""<extra_id_{i}>""" for i in range(_lowercase )] 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__ : Union[str, Any] = len(set(filter(lambda _lowercase : bool('''extra_id_''' in str(_lowercase ) ) , _lowercase ) ) ) 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__( _lowercase , tokenizer_file=_lowercase , eos_token=_lowercase , unk_token=_lowercase , pad_token=_lowercase , extra_ids=_lowercase , additional_special_tokens=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE__ : int = vocab_file SCREAMING_SNAKE_CASE__ : List[Any] = False if not self.vocab_file else True SCREAMING_SNAKE_CASE__ : Tuple = extra_ids @staticmethod def lowercase__ ( _lowercase : Any , _lowercase : str , _lowercase : Union[str, Any] ): if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE__ : Union[str, Any] = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( '''This tokenizer was incorrectly instantiated with a model max length of''' f""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this""" ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with''' ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on''' f""" {pretrained_model_name_or_path} automatically truncating your input to""" f""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences""" f""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with""" ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please''' ''' instantiate this tokenizer with `model_max_length` set to your preferred value.''' , _lowercase , ) return max_model_length def lowercase__ ( self : List[str] , _lowercase : str , _lowercase : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(_lowercase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE__ : List[Any] = os.path.join( _lowercase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowercase ): copyfile(self.vocab_file , _lowercase ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def lowercase__ ( self : List[Any] , _lowercase : List[int] , _lowercase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE__ : Any = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def lowercase__ ( self : List[str] , _lowercase : List[int] , _lowercase : Optional[List[int]] = None ): 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 lowercase__ ( self : List[str] ): return list( set(filter(lambda _lowercase : bool(re.search(R'''<extra_id_\d+>''' , _lowercase ) ) is not None , self.additional_special_tokens ) ) ) def lowercase__ ( self : List[str] ): return [self.convert_tokens_to_ids(_lowercase ) for token in self.get_sentinel_tokens()]
35
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCamelCase = """\ @misc{wu2016googles, title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _lowerCamelCase = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the 'GLEU score'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score's range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ _lowerCamelCase = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: 'google_bleu': google_bleu score Examples: Example 1: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always', ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat'] >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which', ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never', ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat'] >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never', ... 'heed', 'the', 'cat', 'commands'] >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions', ... 'of', 'the', 'cat'] >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was', ... 'interested', 'in', 'world', 'history'] >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history', ... 'because', 'he', 'read', 'the', 'book'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _snake_case (datasets.Metric): def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" ,id="token" ) ,id="sequence" ) ,id="references" ), } ) ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case = 1 ,_snake_case = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case ,hypotheses=_snake_case ,min_len=_snake_case ,max_len=_snake_case ) }
71
0
import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch __lowercase : Any = logging.get_logger(__name__) class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' if not conversation_id: snake_case : str = uuid.uuida() if past_user_inputs is None: snake_case : Any = [] if generated_responses is None: snake_case : Dict = [] snake_case : uuid.UUID = conversation_id snake_case : List[str] = past_user_inputs snake_case : List[str] = generated_responses snake_case : Optional[str] = text def __eq__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten """ F"""with: \"{text}\".""" ) snake_case : Optional[Any] = text else: logger.warning( F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" new input """ F"""ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input""" ) else: snake_case : Any = text def snake_case_ ( self ): '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) snake_case : Tuple = None def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' self.generated_responses.append(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs ,self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self ): '''simple docstring''' snake_case : List[str] = F"""Conversation id: {self.uuid} \n""" for is_user, text in self.iter_texts(): snake_case : List[Any] = """user""" if is_user else """bot""" output += F"""{name} >> {text} \n""" return output @add_end_docstrings( snake_case , R''' min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. ''' , ) class _A ( snake_case ): '''simple docstring''' def __init__( self ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if self.tokenizer.pad_token_id is None: snake_case : int = self.tokenizer.eos_token def snake_case_ ( self ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Union[str, Any] = {} snake_case : str = {} snake_case : List[Any] = {} if min_length_for_response is not None: snake_case : Optional[int] = min_length_for_response if minimum_tokens is not None: snake_case : Dict = minimum_tokens if "max_length" in generate_kwargs: snake_case : Optional[Any] = generate_kwargs["""max_length"""] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: snake_case : Union[str, Any] = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(SCREAMING_SNAKE_CASE_ ) return preprocess_params, forward_params, postprocess_params def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=0 ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ ,num_workers=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) == 1: return outputs[0] return outputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=32 ): '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): raise ValueError("""ConversationalPipeline, expects Conversation as inputs""" ) if conversation.new_user_input is None: raise ValueError( F"""Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. """ """Add user inputs with the conversation's `add_user_input` method""" ) if hasattr(self.tokenizer ,"""_build_conversation_input_ids""" ): snake_case : Optional[Any] = self.tokenizer._build_conversation_input_ids(SCREAMING_SNAKE_CASE_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version snake_case : List[str] = self._legacy_parse_and_tokenize(SCREAMING_SNAKE_CASE_ ) if self.framework == "pt": snake_case : Dict = torch.LongTensor([input_ids] ) elif self.framework == "tf": snake_case : Dict = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=10 ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = generate_kwargs.get("""max_length""" ,self.model.config.max_length ) snake_case : Any = model_inputs["""input_ids"""].shape[1] if max_length - minimum_tokens < n: logger.warning(F"""Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})""" ) snake_case : List[str] = max_length - minimum_tokens snake_case : List[str] = model_inputs["""input_ids"""][:, -trim:] if "attention_mask" in model_inputs: snake_case : Dict = model_inputs["""attention_mask"""][:, -trim:] snake_case : Tuple = model_inputs.pop("""conversation""" ) snake_case : Tuple = max_length snake_case : List[Any] = self.model.generate(**SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if self.model.config.is_encoder_decoder: snake_case : Tuple = 1 else: snake_case : List[str] = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=True ): '''simple docstring''' snake_case : Optional[int] = model_outputs["""output_ids"""] snake_case : List[str] = self.tokenizer.decode( output_ids[0] ,skip_special_tokens=SCREAMING_SNAKE_CASE_ ,clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ,) snake_case : List[str] = model_outputs["""conversation"""] conversation.mark_processed() conversation.append_response(SCREAMING_SNAKE_CASE_ ) return conversation def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = self.tokenizer.eos_token_id snake_case : Tuple = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) > self.tokenizer.model_max_length: snake_case : Tuple = input_ids[-self.tokenizer.model_max_length :] return input_ids
36
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _lowerCamelCase = logging.getLogger(__name__) @dataclass class _snake_case : __A : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __A : Optional[str] =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether tp freeze the encoder."}) __A : bool =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to freeze the embeddings."}) @dataclass class _snake_case : __A : str =field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}) __A : Optional[str] =field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __A : Optional[int] =field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __A : Optional[int] =field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __A : Optional[int] =field(default=-1 , metadata={"help": "# training examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# validation examples. -1 means use all."}) __A : Optional[int] =field(default=-1 , metadata={"help": "# test examples. -1 means use all."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Source language id for translation."}) __A : Optional[str] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Target language id for translation."}) __A : Optional[int] =field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "# num_beams to use for evaluation."}) __A : bool =field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str ) -> Union[str, Any]: """simple docstring""" logger.info(F'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(F''' {key} = {metrics[key]}''' ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , F'''{split}_results.json''' ) ) def a__ ( ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : List[Any] = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: UpperCAmelCase_ : Dict = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = tokenizer.lang_code_to_id[data_args.tgt_lang] else: UpperCAmelCase_ : List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) UpperCAmelCase_ : Dict = SeqaSeqDataset # Get datasets UpperCAmelCase_ : Tuple = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) UpperCAmelCase_ : Dict = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) UpperCAmelCase_ : int = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer UpperCAmelCase_ : Optional[Any] = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) UpperCAmelCase_ : List[str] = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : List[Any] = {} # Training if training_args.do_train: logger.info("*** Train ***" ) UpperCAmelCase_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) UpperCAmelCase_ : int = train_result.metrics UpperCAmelCase_ : Dict = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ : Union[str, Any] = trainer.evaluate(metric_key_prefix="val" ) UpperCAmelCase_ : Optional[Any] = data_args.n_val UpperCAmelCase_ : Union[str, Any] = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("*** Predict ***" ) UpperCAmelCase_ : List[Any] = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="test" ) UpperCAmelCase_ : List[str] = test_output.metrics UpperCAmelCase_ : int = data_args.n_test if trainer.is_world_process_zero(): UpperCAmelCase_ : Optional[Any] = round(metrics["test_loss"] , 4 ) handle_metrics("test" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: UpperCAmelCase_ : Optional[int] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def a__ ( _SCREAMING_SNAKE_CASE : str ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
71
0
from __future__ import annotations from dataclasses import dataclass @dataclass class A__ : """simple docstring""" _lowercase = 42 _lowercase = None _lowercase = None def UpperCamelCase_ ( __a ) -> bool: # Validation def is_valid_tree(__a ) -> bool: if node is None: return True if not isinstance(__a , __a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( "Each node should be type of TreeNode and data should be float." ) def is_binary_search_tree_recursive_check( __a , __a , __a ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left , __a , node.data ) and is_binary_search_tree_recursive_check( node.right , node.data , __a ) ) return is_binary_search_tree_recursive_check(__a , -float("inf" ) , float("inf" ) ) if __name__ == "__main__": import doctest doctest.testmod()
37
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class _snake_case : __A : Dict =BlenderbotConfig __A : Union[str, Any] ={} __A : Any ="gelu" def __init__( self ,_snake_case ,_snake_case=13 ,_snake_case=7 ,_snake_case=True ,_snake_case=False ,_snake_case=99 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=20 ,_snake_case=2 ,_snake_case=1 ,_snake_case=0 ,): UpperCAmelCase_ : List[Any] = parent UpperCAmelCase_ : str = batch_size UpperCAmelCase_ : Dict = seq_length UpperCAmelCase_ : int = is_training UpperCAmelCase_ : Optional[Any] = use_labels UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = num_hidden_layers UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Any = hidden_dropout_prob UpperCAmelCase_ : Optional[int] = attention_probs_dropout_prob UpperCAmelCase_ : List[Any] = max_position_embeddings UpperCAmelCase_ : str = eos_token_id UpperCAmelCase_ : List[Any] = pad_token_id UpperCAmelCase_ : List[Any] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) UpperCAmelCase_ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) UpperCAmelCase_ : Optional[Any] = tf.concat([input_ids, eos_tensor] ,axis=1 ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) UpperCAmelCase_ : List[str] = prepare_blenderbot_inputs_dict(_snake_case ,_snake_case ,_snake_case ) return config, inputs_dict def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Tuple = TFBlenderbotModel(config=_snake_case ).get_decoder() UpperCAmelCase_ : int = inputs_dict["input_ids"] UpperCAmelCase_ : Dict = input_ids[:1, :] UpperCAmelCase_ : Any = inputs_dict["attention_mask"][:1, :] UpperCAmelCase_ : int = inputs_dict["head_mask"] UpperCAmelCase_ : Optional[int] = 1 # first forward pass UpperCAmelCase_ : List[str] = model(_snake_case ,attention_mask=_snake_case ,head_mask=_snake_case ,use_cache=_snake_case ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) ,config.vocab_size ) UpperCAmelCase_ : Any = tf.cast(ids_tensor((self.batch_size, 3) ,2 ) ,tf.inta ) # append to next input_ids and UpperCAmelCase_ : Union[str, Any] = tf.concat([input_ids, next_tokens] ,axis=-1 ) UpperCAmelCase_ : Any = tf.concat([attention_mask, next_attn_mask] ,axis=-1 ) UpperCAmelCase_ : Any = model(_snake_case ,attention_mask=_snake_case )[0] UpperCAmelCase_ : List[Any] = model(_snake_case ,attention_mask=_snake_case ,past_key_values=_snake_case )[0] self.parent.assertEqual(next_tokens.shape[1] ,output_from_past.shape[1] ) # select random slice UpperCAmelCase_ : str = int(ids_tensor((1,) ,output_from_past.shape[-1] ) ) UpperCAmelCase_ : List[str] = output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase_ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_snake_case ,_snake_case ,rtol=1E-3 ) def a__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : List[str]=None , _SCREAMING_SNAKE_CASE : Dict=None , ) -> Union[str, Any]: """simple docstring""" if attention_mask is None: UpperCAmelCase_ : Dict = tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase_ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase_ : List[Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase_ : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase_ : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __A : List[str] =(TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __A : Dict =( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __A : Any =True __A : Dict =False __A : Dict =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = TFBlenderbotModelTester(self ) UpperCAmelCase_ : int = ConfigTester(self ,config_class=_snake_case ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_snake_case ) @require_tokenizers @require_tf class _snake_case (unittest.TestCase): __A : Optional[int] =["My friends are cool but they eat too many carbs."] __A : Optional[Any] ="facebook/blenderbot-400M-distill" @cached_property def UpperCamelCase__ ( self ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[Any] = self.tokenizer(self.src_text ,return_tensors="tf" ) UpperCAmelCase_ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,) UpperCAmelCase_ : str = self.tokenizer.batch_decode(generated_ids.numpy() ,skip_special_tokens=_snake_case )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
71
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. snake_case__ : int = [[1, 2, 4], [1, 2, 3, 4]] snake_case__ : Any = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) self.assertTrue(isinstance(dc.token_ids , __SCREAMING_SNAKE_CASE ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def __UpperCamelCase ( self ): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). snake_case__ : Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) # fails here def __UpperCamelCase ( self ): snake_case__ : List[str] = [[1, 2, 3], [1, 2, 4]] snake_case__ : Optional[int] = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : Any = dc.update(1 ) snake_case__ : Any = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : Tuple = dc.update(2 ) snake_case__ : Tuple = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(3 ) snake_case__ : List[str] = stepped is True and completed is True and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] snake_case__ : int = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
38
'''simple docstring''' from numpy import exp, pi, sqrt def a__ ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : float = 0.0 , _SCREAMING_SNAKE_CASE : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
71
0
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) lowerCAmelCase_ = logging.getLogger() def __SCREAMING_SNAKE_CASE (): snake_case_ = argparse.ArgumentParser() parser.add_argument('''-f''' ) snake_case_ = parser.parse_args() return args.f def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = {} snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''all_results.json''' ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): with open(SCREAMING_SNAKE_CASE__ , '''r''' ) as f: snake_case_ = json.load(SCREAMING_SNAKE_CASE__ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def __SCREAMING_SNAKE_CASE (): snake_case_ = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() lowerCAmelCase_ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class snake_case_ ( __A ): '''simple docstring''' @classmethod def snake_case__( cls : Tuple ) ->Optional[int]: # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ = tempfile.mkdtemp() snake_case_ = os.path.join(cls.tmpdir , '''default_config.yml''' ) write_basic_config(save_location=cls.configPath ) snake_case_ = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def snake_case__( cls : List[Any] ) ->Union[str, Any]: shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : Union[str, Any] ) ->Tuple: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''glue_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[Any] ) ->Union[str, Any]: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertLess(result['''perplexity'''] , 1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''clm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[Any] ) ->int: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertLess(result['''perplexity'''] , 4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''mlm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[str] ) ->List[Any]: # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ = 7 if get_gpu_count() > 1 else 2 snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertLess(result['''train_loss'''] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''ner_no_trainer''' ) ) ) @unittest.skip(reason='''Fix me @muellerzr''' ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[str] ) ->List[str]: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 2_8 ) self.assertGreaterEqual(result['''eval_exact'''] , 2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''qa_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : int ) ->List[str]: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''swag_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[Any] ) ->List[Any]: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_rouge1'''] , 1_0 ) self.assertGreaterEqual(result['''eval_rouge2'''] , 2 ) self.assertGreaterEqual(result['''eval_rougeL'''] , 7 ) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''summarization_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : Union[str, Any] ) ->str: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_bleu'''] , 3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''translation_no_trainer''' ) ) ) @slow def snake_case__( self : List[Any] ) ->Dict: snake_case_ = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.10 ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__( self : List[Any] ) ->Optional[int]: snake_case_ = self.get_auto_remove_tmp_dir() snake_case_ = f''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) snake_case_ = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''step_1''' ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , '''image_classification_no_trainer''' ) ) )
39
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _snake_case (nn.Module): def __init__( self ,_snake_case = 16 ,_snake_case = 88 ,_snake_case = None ,_snake_case = 1 ,_snake_case = 0.0 ,_snake_case = 32 ,_snake_case = None ,_snake_case = False ,_snake_case = None ,_snake_case = None ,_snake_case = "geglu" ,_snake_case = None ,): super().__init__() UpperCAmelCase_ : Optional[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case ,attention_head_dim=_snake_case ,in_channels=_snake_case ,num_layers=_snake_case ,dropout=_snake_case ,norm_num_groups=_snake_case ,cross_attention_dim=_snake_case ,attention_bias=_snake_case ,sample_size=_snake_case ,num_vector_embeds=_snake_case ,activation_fn=_snake_case ,num_embeds_ada_norm=_snake_case ,) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference UpperCAmelCase_ : List[str] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` UpperCAmelCase_ : int = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` UpperCAmelCase_ : List[Any] = [1, 0] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case = True ,): UpperCAmelCase_ : List[str] = hidden_states UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[int] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens UpperCAmelCase_ : Any = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] UpperCAmelCase_ : Any = self.transformer_index_for_condition[i] UpperCAmelCase_ : int = self.transformers[transformer_index]( _snake_case ,encoder_hidden_states=_snake_case ,timestep=_snake_case ,cross_attention_kwargs=_snake_case ,return_dict=_snake_case ,)[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] UpperCAmelCase_ : Dict = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) UpperCAmelCase_ : List[Any] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
71
0