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
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def _A ( __snake_case :List[str]=None ) -> List[str]: """simple docstring""" if subparsers is not None: __SCREAMING_SNAKE_CASE = subparsers.add_parser("test" ) else: __SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=__snake_case , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__snake_case ) return parser def _A ( __snake_case :Optional[Any] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: __SCREAMING_SNAKE_CASE = script_name else: __SCREAMING_SNAKE_CASE = f'''--config_file={args.config_file} {script_name}''' __SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() __SCREAMING_SNAKE_CASE = execute_subprocess_async(__snake_case , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def _A ( ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE = test_command_parser() __SCREAMING_SNAKE_CASE = parser.parse_args() test_command(__snake_case ) if __name__ == "__main__": main()
693
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Dict: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 20_00, _a = None, _a = "pil", _a = True, **_a, ) -> Union[ImagePipelineOutput, Tuple]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a ) * self.scheduler.init_noise_sigma __SCREAMING_SNAKE_CASE = sample.to(self.device ) self.scheduler.set_timesteps(_a ) self.scheduler.set_sigmas(_a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): __SCREAMING_SNAKE_CASE = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): __SCREAMING_SNAKE_CASE = self.unet(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct(_a, _a, generator=_a ).prev_sample # prediction step __SCREAMING_SNAKE_CASE = model(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_pred(_a, _a, _a, generator=_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = output.prev_sample, output.prev_sample_mean __SCREAMING_SNAKE_CASE = sample_mean.clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_a )
693
1
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> str: pass @is_pipeline_test @require_vision @require_torch class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): SCREAMING_SNAKE_CASE__ =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def __lowerCAmelCase ( self, _a, _a, _a ) -> Optional[int]: __SCREAMING_SNAKE_CASE = pipeline( "zero-shot-object-detection", model="hf-internal-testing/tiny-random-owlvit-object-detection" ) __SCREAMING_SNAKE_CASE = [ { "image": "./tests/fixtures/tests_samples/COCO/000000039769.png", "candidate_labels": ["cat", "remote", "couch"], } ] return object_detector, examples def __lowerCAmelCase ( self, _a, _a ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = object_detector(examples[0], threshold=0.0 ) __SCREAMING_SNAKE_CASE = len(_a ) self.assertGreater(_a, 0 ) self.assertEqual( _a, [ { "score": ANY(_a ), "label": ANY(_a ), "box": {"xmin": ANY(_a ), "ymin": ANY(_a ), "xmax": ANY(_a ), "ymax": ANY(_a )}, } for i in range(_a ) ], ) @require_tf @unittest.skip("Zero Shot Object Detection not implemented in TF" ) def __lowerCAmelCase ( self ) -> str: pass @require_torch def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = pipeline( "zero-shot-object-detection", model="hf-internal-testing/tiny-random-owlvit-object-detection" ) __SCREAMING_SNAKE_CASE = object_detector( "./tests/fixtures/tests_samples/COCO/000000039769.png", candidate_labels=["cat", "remote", "couch"], threshold=0.64, ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ {"score": 0.7235, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.7218, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.7184, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.6748, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6656, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6614, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6456, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}}, {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}}, {"score": 0.6419, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}}, ], ) __SCREAMING_SNAKE_CASE = object_detector( [ { "image": "./tests/fixtures/tests_samples/COCO/000000039769.png", "candidate_labels": ["cat", "remote", "couch"], } ], threshold=0.64, ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ [ {"score": 0.7235, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.7218, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.7184, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}}, {"score": 0.6748, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6656, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6614, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}}, {"score": 0.6456, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}}, {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}}, {"score": 0.6419, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}}, ] ], ) @require_torch @slow def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = pipeline("zero-shot-object-detection" ) __SCREAMING_SNAKE_CASE = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg", candidate_labels=["cat", "remote", "couch"], ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}}, {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}}, ], ) __SCREAMING_SNAKE_CASE = object_detector( [ { "image": "http://images.cocodataset.org/val2017/000000039769.jpg", "candidate_labels": ["cat", "remote", "couch"], }, { "image": "http://images.cocodataset.org/val2017/000000039769.jpg", "candidate_labels": ["cat", "remote", "couch"], }, ], ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}}, {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}}, ], [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}}, {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}}, ], ], ) @require_tf @unittest.skip("Zero Shot Object Detection not implemented in TF" ) def __lowerCAmelCase ( self ) -> List[str]: pass @require_torch @slow def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = 0.2 __SCREAMING_SNAKE_CASE = pipeline("zero-shot-object-detection" ) __SCREAMING_SNAKE_CASE = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg", candidate_labels=["cat", "remote", "couch"], threshold=_a, ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}}, {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}}, ], ) @require_torch @slow def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = pipeline("zero-shot-object-detection" ) __SCREAMING_SNAKE_CASE = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg", candidate_labels=["cat", "remote", "couch"], top_k=_a, ) self.assertEqual( nested_simplify(_a, decimals=4 ), [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}}, {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}}, ], )
693
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =LayoutLMTokenizer SCREAMING_SNAKE_CASE__ =LayoutLMTokenizerFast SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True def __lowerCAmelCase ( self ) -> str: super().setUp() __SCREAMING_SNAKE_CASE = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def __lowerCAmelCase ( self, **_a ) -> List[Any]: return LayoutLMTokenizer.from_pretrained(self.tmpdirname, **_a ) def __lowerCAmelCase ( self, _a ) -> List[str]: __SCREAMING_SNAKE_CASE = "UNwant\u00E9d,running" __SCREAMING_SNAKE_CASE = "unwanted, running" return input_text, output_text def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(_a, ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ), [7, 4, 5, 10, 8, 9] ) def __lowerCAmelCase ( self ) -> Tuple: pass
693
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
1
import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": _snake_case : int = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, required=True, help='Path to the checkpoint to convert.' ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( '--original_config_file', default=None, type=str, help='The YAML config file corresponding to the original architecture.', ) parser.add_argument( '--num_in_channels', default=None, type=int, help='The number of input channels. If `None` number of input channels will be automatically inferred.', ) parser.add_argument( '--scheduler_type', default='pndm', type=str, help='Type of scheduler to use. Should be one of [\'pndm\', \'lms\', \'ddim\', \'euler\', \'euler-ancestral\', \'dpm\']', ) parser.add_argument( '--pipeline_type', default=None, type=str, help=( 'The pipeline type. One of \'FrozenOpenCLIPEmbedder\', \'FrozenCLIPEmbedder\', \'PaintByExample\'' '. If `None` pipeline will be automatically inferred.' ), ) parser.add_argument( '--image_size', default=None, type=int, help=( 'The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2' ' Base. Use 768 for Stable Diffusion v2.' ), ) parser.add_argument( '--prediction_type', default=None, type=str, help=( 'The prediction type that the model was trained on. Use \'epsilon\' for Stable Diffusion v1.X and Stable' ' Diffusion v2 Base. Use \'v_prediction\' for Stable Diffusion v2.' ), ) parser.add_argument( '--extract_ema', action='store_true', help=( 'Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights' ' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield' ' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.' ), ) parser.add_argument( '--upcast_attention', action='store_true', help=( 'Whether the attention computation should always be upcasted. This is necessary when running stable' ' diffusion 2.1.' ), ) parser.add_argument( '--from_safetensors', action='store_true', help='If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.', ) parser.add_argument( '--to_safetensors', action='store_true', help='Whether to store pipeline in safetensors format or not.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument('--device', type=str, help='Device to use (e.g. cpu, cuda:0, cuda:1, etc.)') parser.add_argument( '--stable_unclip', type=str, default=None, required=False, help='Set if this is a stable unCLIP model. One of \'txt2img\' or \'img2img\'.', ) parser.add_argument( '--stable_unclip_prior', type=str, default=None, required=False, help='Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.', ) parser.add_argument( '--clip_stats_path', type=str, help='Path to the clip stats file. Only required if the stable unclip model\'s config specifies `model.params.noise_aug_config.params.clip_stats_path`.', required=False, ) parser.add_argument( '--controlnet', action='store_true', default=None, help='Set flag if this is a controlnet checkpoint.' ) parser.add_argument('--half', action='store_true', help='Save weights in half precision.') parser.add_argument( '--vae_path', type=str, default=None, required=False, help='Set to a path, hub id to an already converted vae to not convert it again.', ) _snake_case : Dict = parser.parse_args() _snake_case : List[Any] = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
693
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> Any: __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None def __repr__( self ) -> str: return f'''Node({self.data})''' class __SCREAMING_SNAKE_CASE : def __init__( self ) -> Tuple: __SCREAMING_SNAKE_CASE = None def __iter__( self ) -> Any: __SCREAMING_SNAKE_CASE = self.head while node: yield node.data __SCREAMING_SNAKE_CASE = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_a ) for item in self] ) def __getitem__( self, _a ) -> Any: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self, _a, _a ) -> None: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) __SCREAMING_SNAKE_CASE = self.head for _ in range(_a ): __SCREAMING_SNAKE_CASE = current.next __SCREAMING_SNAKE_CASE = data def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(len(self ), _a ) def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(0, _a ) def __lowerCAmelCase ( self, _a, _a ) -> None: if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) __SCREAMING_SNAKE_CASE = Node(_a ) if self.head is None: __SCREAMING_SNAKE_CASE = new_node elif index == 0: __SCREAMING_SNAKE_CASE = self.head # link new_node to head __SCREAMING_SNAKE_CASE = new_node else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self ) -> None: # print every node data print(self ) def __lowerCAmelCase ( self ) -> Any: return self.delete_nth(0 ) def __lowerCAmelCase ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowerCAmelCase ( self, _a = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) __SCREAMING_SNAKE_CASE = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE = self.head.next else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next.next return delete_node.data def __lowerCAmelCase ( self ) -> bool: return self.head is None def __lowerCAmelCase ( self ) -> None: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE = prev def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = LinkedList() assert linked_list.is_empty() is True assert str(__snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(__snake_case ) == i linked_list.insert_nth(__snake_case , i + 1 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(__snake_case ) == 9 assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(-8 , 1 ) ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_9_2.5_5_5_5_5, "Hello, world!", 7_7.9, Node(10 ), None, None, 1_2.2_0, ] __SCREAMING_SNAKE_CASE = LinkedList() for i in test_input: linked_list.insert_tail(__snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(__snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE = linked_list.delete_head() assert result == -9 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE = linked_list.delete_tail() assert result == 1_2.2 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE = linked_list.delete_nth(10 ) assert result is None assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(__snake_case ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(__snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _A ( ) -> Union[str, Any]: """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(__snake_case ) print("\nReading/changing Node data using indexing:" ) print(f'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE = input("Enter New Value: " ).strip() print("New list:" ) print(__snake_case ) print(f'''length of linked_list is : {len(__snake_case )}''' ) if __name__ == "__main__": main()
693
1
from __future__ import annotations def _A ( __snake_case :list[float] ) -> float: """simple docstring""" __SCREAMING_SNAKE_CASE = 0.0_0 __SCREAMING_SNAKE_CASE = 0 for resistor in resistors: if resistor <= 0: __SCREAMING_SNAKE_CASE = f'''Resistor at index {index} has a negative or zero value!''' raise ValueError(__snake_case ) first_sum += 1 / float(__snake_case ) index += 1 return 1 / first_sum def _A ( __snake_case :list[float] ) -> float: """simple docstring""" __SCREAMING_SNAKE_CASE = 0.0_0 __SCREAMING_SNAKE_CASE = 0 for resistor in resistors: sum_r += resistor if resistor < 0: __SCREAMING_SNAKE_CASE = f'''Resistor at index {index} has a negative value!''' raise ValueError(__snake_case ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
693
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
1
import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor _snake_case : Union[str, Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, **_a ) -> None: warnings.warn( "The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use ImageGPTImageProcessor instead.", _a, ) super().__init__(*_a, **_a )
693
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
1
def _A ( __snake_case :int ) -> bool: """simple docstring""" if p < 2: raise ValueError("p should not be less than 2!" ) elif p == 2: return True __SCREAMING_SNAKE_CASE = 4 __SCREAMING_SNAKE_CASE = (1 << p) - 1 for _ in range(p - 2 ): __SCREAMING_SNAKE_CASE = ((s * s) - 2) % m return s == 0 if __name__ == "__main__": print(lucas_lehmer_test(7)) print(lucas_lehmer_test(11))
693
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
1
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetrImageProcessor class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self, _a, _a=7, _a=3, _a=30, _a=4_00, _a=True, _a=None, _a=True, _a=1 / 2_55, _a=True, _a=[0.5, 0.5, 0.5], _a=[0.5, 0.5, 0.5], _a=True, ) -> str: # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p __SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 13_33} __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = min_resolution __SCREAMING_SNAKE_CASE = max_resolution __SCREAMING_SNAKE_CASE = do_resize __SCREAMING_SNAKE_CASE = size __SCREAMING_SNAKE_CASE = do_rescale __SCREAMING_SNAKE_CASE = rescale_factor __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std __SCREAMING_SNAKE_CASE = do_pad def __lowerCAmelCase ( self ) -> Tuple: return { "do_resize": self.do_resize, "size": self.size, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_pad": self.do_pad, } def __lowerCAmelCase ( self, _a, _a=False ) -> Tuple: if not batched: __SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_a, Image.Image ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = image.size else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: __SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) __SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: __SCREAMING_SNAKE_CASE = self.size["shortest_edge"] __SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: __SCREAMING_SNAKE_CASE = self.size["shortest_edge"] __SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: __SCREAMING_SNAKE_CASE = [] for image in image_inputs: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __SCREAMING_SNAKE_CASE = max(_a, key=lambda _a : item[0] )[0] __SCREAMING_SNAKE_CASE = max(_a, key=lambda _a : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =DetrImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = DetrImageProcessingTester(self ) @property def __lowerCAmelCase ( self ) -> Dict: return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a, "image_mean" ) ) self.assertTrue(hasattr(_a, "image_std" ) ) self.assertTrue(hasattr(_a, "do_normalize" ) ) self.assertTrue(hasattr(_a, "do_rescale" ) ) self.assertTrue(hasattr(_a, "rescale_factor" ) ) self.assertTrue(hasattr(_a, "do_resize" ) ) self.assertTrue(hasattr(_a, "size" ) ) self.assertTrue(hasattr(_a, "do_pad" ) ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 13_33} ) self.assertEqual(image_processor.do_pad, _a ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict( self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=_a ) self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84} ) self.assertEqual(image_processor.do_pad, _a ) def __lowerCAmelCase ( self ) -> Optional[Any]: pass def __lowerCAmelCase ( self ) -> List[str]: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a, Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape, (1, self.image_processor_tester.num_channels, expected_height, expected_width), ) # Test batched __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a, batched=_a ) __SCREAMING_SNAKE_CASE = image_processing(_a, return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ), ) def __lowerCAmelCase ( self ) -> str: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_a, numpify=_a ) for image in image_inputs: self.assertIsInstance(_a, np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape, (1, self.image_processor_tester.num_channels, expected_height, expected_width), ) # Test batched __SCREAMING_SNAKE_CASE = image_processing(_a, return_tensors="pt" ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a, batched=_a ) self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ), ) def __lowerCAmelCase ( self ) -> str: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_a, torchify=_a ) for image in image_inputs: self.assertIsInstance(_a, torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape, (1, self.image_processor_tester.num_channels, expected_height, expected_width), ) # Test batched __SCREAMING_SNAKE_CASE = image_processing(_a, return_tensors="pt" ).pixel_values __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_a, batched=_a ) self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ), ) @slow def __lowerCAmelCase ( self ) -> int: # prepare image and target __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt", "r" ) as f: __SCREAMING_SNAKE_CASE = json.loads(f.read() ) __SCREAMING_SNAKE_CASE = {"image_id": 3_97_69, "annotations": target} # encode them __SCREAMING_SNAKE_CASE = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50" ) __SCREAMING_SNAKE_CASE = image_processing(images=_a, annotations=_a, return_tensors="pt" ) # verify pixel values __SCREAMING_SNAKE_CASE = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["pixel_values"].shape, _a ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3], _a, atol=1E-4 ) ) # verify area __SCREAMING_SNAKE_CASE = torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"], _a ) ) # verify boxes __SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape, _a ) __SCREAMING_SNAKE_CASE = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0], _a, atol=1E-3 ) ) # verify image_id __SCREAMING_SNAKE_CASE = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"], _a ) ) # verify is_crowd __SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"], _a ) ) # verify class_labels __SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], _a ) ) # verify orig_size __SCREAMING_SNAKE_CASE = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], _a ) ) # verify size __SCREAMING_SNAKE_CASE = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"], _a ) ) @slow def __lowerCAmelCase ( self ) -> List[Any]: # prepare image, target and masks_path __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt", "r" ) as f: __SCREAMING_SNAKE_CASE = json.loads(f.read() ) __SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 3_97_69, "segments_info": target} __SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them __SCREAMING_SNAKE_CASE = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50-panoptic" ) __SCREAMING_SNAKE_CASE = image_processing(images=_a, annotations=_a, masks_path=_a, return_tensors="pt" ) # verify pixel values __SCREAMING_SNAKE_CASE = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["pixel_values"].shape, _a ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3], _a, atol=1E-4 ) ) # verify area __SCREAMING_SNAKE_CASE = torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"], _a ) ) # verify boxes __SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape, _a ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0], _a, atol=1E-3 ) ) # verify image_id __SCREAMING_SNAKE_CASE = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"], _a ) ) # verify is_crowd __SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"], _a ) ) # verify class_labels __SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], _a ) ) # verify masks __SCREAMING_SNAKE_CASE = 82_28_73 self.assertEqual(encoding["labels"][0]["masks"].sum().item(), _a ) # verify orig_size __SCREAMING_SNAKE_CASE = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], _a ) ) # verify size __SCREAMING_SNAKE_CASE = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"], _a ) )
693
import random from .binary_exp_mod import bin_exp_mod def _A ( __snake_case :List[Any] , __snake_case :Union[str, Any]=1000 ) -> int: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __SCREAMING_SNAKE_CASE = n - 1 __SCREAMING_SNAKE_CASE = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __SCREAMING_SNAKE_CASE = 0 while count < prec: __SCREAMING_SNAKE_CASE = random.randint(2 , n - 1 ) __SCREAMING_SNAKE_CASE = bin_exp_mod(__snake_case , __snake_case , __snake_case ) if b != 1: __SCREAMING_SNAKE_CASE = True for _ in range(__snake_case ): if b == n - 1: __SCREAMING_SNAKE_CASE = False break __SCREAMING_SNAKE_CASE = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _snake_case : int = 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)))
693
1
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int ) -> np.ndarray: """simple docstring""" if (ksize % 2) == 0: __SCREAMING_SNAKE_CASE = ksize + 1 __SCREAMING_SNAKE_CASE = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(__snake_case ): for x in range(__snake_case ): # distance from center __SCREAMING_SNAKE_CASE = x - ksize // 2 __SCREAMING_SNAKE_CASE = y - ksize // 2 # degree to radiant __SCREAMING_SNAKE_CASE = theta / 180 * np.pi __SCREAMING_SNAKE_CASE = np.cos(_theta ) __SCREAMING_SNAKE_CASE = np.sin(_theta ) # get kernel x __SCREAMING_SNAKE_CASE = cos_theta * px + sin_theta * py # get kernel y __SCREAMING_SNAKE_CASE = -sin_theta * px + cos_theta * py # fill kernel __SCREAMING_SNAKE_CASE = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _snake_case : Union[str, Any] = imread('../image_data/lena.jpg') # turn image in gray scale value _snake_case : List[str] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _snake_case : int = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 1_20, 1_50]: _snake_case : List[str] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _snake_case : Optional[Any] = out / out.max() * 2_55 _snake_case : Union[str, Any] = out.astype(np.uinta) imshow('Original', gray) imshow('Gabor filter with 20x20 mask and 6 directions', out) waitKey(0)
693
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig _snake_case : List[str] = logging.get_logger(__name__) _snake_case : Optional[int] = { 'Intel/dpt-large': 'https://huggingface.co/Intel/dpt-large/resolve/main/config.json', # See all DPT models at https://huggingface.co/models?filter=dpt } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""dpt""" def __init__( self, _a=7_68, _a=12, _a=12, _a=30_72, _a="gelu", _a=0.0, _a=0.0, _a=0.02, _a=1E-1_2, _a=3_84, _a=16, _a=3, _a=False, _a=True, _a=[2, 5, 8, 11], _a="project", _a=[4, 2, 1, 0.5], _a=[96, 1_92, 3_84, 7_68], _a=2_56, _a=-1, _a=False, _a=True, _a=0.4, _a=2_55, _a=0.1, _a=[1, 10_24, 24, 24], _a=[0, 1], _a=None, **_a, ) -> List[str]: super().__init__(**_a ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info("Initializing the config with a `BiT` backbone." ) __SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, } __SCREAMING_SNAKE_CASE = BitConfig(**_a ) elif isinstance(_a, _a ): logger.info("Initializing the config with a `BiT` backbone." ) __SCREAMING_SNAKE_CASE = BitConfig(**_a ) elif isinstance(_a, _a ): __SCREAMING_SNAKE_CASE = backbone_config else: raise ValueError( f'''backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.''' ) __SCREAMING_SNAKE_CASE = backbone_featmap_shape __SCREAMING_SNAKE_CASE = neck_ignore_stages if readout_type != "project": raise ValueError("Readout type must be 'project' when using `DPT-hybrid` mode." ) else: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = qkv_bias __SCREAMING_SNAKE_CASE = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError("Readout_type must be one of ['ignore', 'add', 'project']" ) __SCREAMING_SNAKE_CASE = readout_type __SCREAMING_SNAKE_CASE = reassemble_factors __SCREAMING_SNAKE_CASE = neck_hidden_sizes __SCREAMING_SNAKE_CASE = fusion_hidden_size __SCREAMING_SNAKE_CASE = head_in_index __SCREAMING_SNAKE_CASE = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) __SCREAMING_SNAKE_CASE = use_auxiliary_head __SCREAMING_SNAKE_CASE = auxiliary_loss_weight __SCREAMING_SNAKE_CASE = semantic_loss_ignore_index __SCREAMING_SNAKE_CASE = semantic_classifier_dropout def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
693
def _A ( __snake_case :int ) -> int: """simple docstring""" assert isinstance(__snake_case , __snake_case ), f'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: __SCREAMING_SNAKE_CASE = f'''The input value of [n={number}] has to be > 0''' raise ValueError(__snake_case ) else: __SCREAMING_SNAKE_CASE = sylvester(number - 1 ) __SCREAMING_SNAKE_CASE = num - 1 __SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
693
1
import unittest from knapsack import knapsack as k class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = [0] __SCREAMING_SNAKE_CASE = [0] __SCREAMING_SNAKE_CASE = len(_a ) self.assertEqual(k.knapsack(_a, _a, _a, _a ), 0 ) __SCREAMING_SNAKE_CASE = [60] __SCREAMING_SNAKE_CASE = [10] __SCREAMING_SNAKE_CASE = len(_a ) self.assertEqual(k.knapsack(_a, _a, _a, _a ), 0 ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = [1, 2, 3] __SCREAMING_SNAKE_CASE = [3, 2, 1] __SCREAMING_SNAKE_CASE = len(_a ) self.assertEqual(k.knapsack(_a, _a, _a, _a ), 5 ) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = 50 __SCREAMING_SNAKE_CASE = [60, 1_00, 1_20] __SCREAMING_SNAKE_CASE = [10, 20, 30] __SCREAMING_SNAKE_CASE = len(_a ) self.assertEqual(k.knapsack(_a, _a, _a, _a ), 2_20 ) if __name__ == "__main__": unittest.main()
693
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @require_tf def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf" ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(_a ), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @slow @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
693
1
from __future__ import annotations import math def _A ( __snake_case :int , __snake_case :int , __snake_case :bool , __snake_case :list[int] , __snake_case :float ) -> int: """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__snake_case ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) return min( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [90, 23, 6, 33, 21, 65, 123, 3_4423] __SCREAMING_SNAKE_CASE = math.log(len(__snake_case ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , __snake_case , __snake_case , __snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
from __future__ import annotations import math def _A ( __snake_case :int , __snake_case :int , __snake_case :bool , __snake_case :list[int] , __snake_case :float ) -> int: """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__snake_case ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) return min( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [90, 23, 6, 33, 21, 65, 123, 3_4423] __SCREAMING_SNAKE_CASE = math.log(len(__snake_case ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , __snake_case , __snake_case , __snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
1
import copy 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 ..auto import CONFIG_MAPPING _snake_case : Tuple = logging.get_logger(__name__) _snake_case : Any = { 'microsoft/conditional-detr-resnet-50': ( 'https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json' ), } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""conditional_detr""" SCREAMING_SNAKE_CASE__ =["""past_key_values"""] SCREAMING_SNAKE_CASE__ ={ """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self, _a=True, _a=None, _a=3, _a=3_00, _a=6, _a=20_48, _a=8, _a=6, _a=20_48, _a=8, _a=0.0, _a=0.0, _a=True, _a="relu", _a=2_56, _a=0.1, _a=0.0, _a=0.0, _a=0.02, _a=1.0, _a=False, _a="sine", _a="resnet50", _a=True, _a=False, _a=2, _a=5, _a=2, _a=1, _a=1, _a=2, _a=5, _a=2, _a=0.25, **_a, ) -> int: if backbone_config is not None and use_timm_backbone: raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`." ) if not use_timm_backbone: if backbone_config is None: logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone." ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(_a, _a ): __SCREAMING_SNAKE_CASE = backbone_config.get("model_type" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(_a ) __SCREAMING_SNAKE_CASE = use_timm_backbone __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = decoder_layerdrop __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type __SCREAMING_SNAKE_CASE = backbone __SCREAMING_SNAKE_CASE = use_pretrained_backbone __SCREAMING_SNAKE_CASE = dilation # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = cls_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = focal_alpha super().__init__(is_encoder_decoder=_a, **_a ) @property def __lowerCAmelCase ( self ) -> int: return self.encoder_attention_heads @property def __lowerCAmelCase ( self ) -> int: return self.d_model def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =version.parse("""1.11""" ) @property def __lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __lowerCAmelCase ( self ) -> float: return 1E-5 @property def __lowerCAmelCase ( self ) -> int: return 12
693
def _A ( __snake_case :bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def _A ( __snake_case :str ) -> bytes: """simple docstring""" if (len(__snake_case ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _snake_case : List[Any] = { 'configuration_poolformer': [ 'POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PoolFormerConfig', 'PoolFormerOnnxConfig', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Any = ['PoolFormerFeatureExtractor'] _snake_case : str = ['PoolFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : str = [ 'POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'PoolFormerForImageClassification', 'PoolFormerModel', 'PoolFormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_poolformer import ( POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, PoolFormerConfig, PoolFormerOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_poolformer import PoolFormerFeatureExtractor from .image_processing_poolformer import PoolFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_poolformer import ( POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, PoolFormerForImageClassification, PoolFormerModel, PoolFormerPreTrainedModel, ) else: import sys _snake_case : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure)
693
from functools import lru_cache def _A ( __snake_case :int ) -> set: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(__snake_case ) if n > 1: factors.add(__snake_case ) return factors @lru_cache def _A ( __snake_case :int ) -> int: """simple docstring""" return len(unique_prime_factors(__snake_case ) ) def _A ( __snake_case :list ) -> bool: """simple docstring""" return len(set(__snake_case ) ) in (0, 1) def _A ( __snake_case :int ) -> list: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 while True: # Increment each value of a generated range __SCREAMING_SNAKE_CASE = [base + i for i in range(__snake_case )] # Run elements through out unique_prime_factors function # Append our target number to the end. __SCREAMING_SNAKE_CASE = [upf_len(__snake_case ) for x in group] checker.append(__snake_case ) # If all numbers in the list are equal, return the group variable. if equality(__snake_case ): return group # Increment our base variable by 1 base += 1 def _A ( __snake_case :int = 4 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = run(__snake_case ) return results[0] if len(__snake_case ) else None if __name__ == "__main__": print(solution())
693
1
class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> None: __SCREAMING_SNAKE_CASE = set_counts __SCREAMING_SNAKE_CASE = max(_a ) __SCREAMING_SNAKE_CASE = len(_a ) __SCREAMING_SNAKE_CASE = [1] * num_sets __SCREAMING_SNAKE_CASE = list(range(_a ) ) def __lowerCAmelCase ( self, _a, _a ) -> bool: __SCREAMING_SNAKE_CASE = self.get_parent(_a ) __SCREAMING_SNAKE_CASE = self.get_parent(_a ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 __SCREAMING_SNAKE_CASE = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = src_parent __SCREAMING_SNAKE_CASE = self.set_counts[src_parent] __SCREAMING_SNAKE_CASE = max(self.max_set, _a ) return True def __lowerCAmelCase ( self, _a ) -> int: if self.parents[disj_set] == disj_set: return disj_set __SCREAMING_SNAKE_CASE = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
693
import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _A ( __snake_case :Dict ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = VideoMAEConfig() set_architecture_configs(__snake_case , __snake_case ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = False if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = "huggingface/label-files" if "kinetics" in model_name: __SCREAMING_SNAKE_CASE = 400 __SCREAMING_SNAKE_CASE = "kinetics400-id2label.json" elif "ssv2" in model_name: __SCREAMING_SNAKE_CASE = 174 __SCREAMING_SNAKE_CASE = "something-something-v2-id2label.json" else: raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned." ) __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type="dataset" ) , "r" ) ) __SCREAMING_SNAKE_CASE = {int(__snake_case ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def _A ( __snake_case :Dict , __snake_case :Optional[Any] ) -> List[Any]: """simple docstring""" if "small" in model_name: __SCREAMING_SNAKE_CASE = 384 __SCREAMING_SNAKE_CASE = 1536 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 192 __SCREAMING_SNAKE_CASE = 768 elif "large" in model_name: __SCREAMING_SNAKE_CASE = 1024 __SCREAMING_SNAKE_CASE = 4096 __SCREAMING_SNAKE_CASE = 24 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 512 __SCREAMING_SNAKE_CASE = 2048 elif "huge" in model_name: __SCREAMING_SNAKE_CASE = 1280 __SCREAMING_SNAKE_CASE = 5120 __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 640 __SCREAMING_SNAKE_CASE = 2560 elif "base" not in model_name: raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\"" ) def _A ( __snake_case :List[Any] ) -> Optional[int]: """simple docstring""" if "encoder." in name: __SCREAMING_SNAKE_CASE = name.replace("encoder." , "" ) if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("cls_token" , "videomae.embeddings.cls_token" ) if "decoder_pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pos_embed" , "decoder.decoder_pos_embed" ) if "pos_embed" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("pos_embed" , "videomae.embeddings.position_embeddings" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.proj" , "videomae.embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.norm" , "videomae.embeddings.norm" ) if "decoder.blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder.blocks" , "decoder.decoder_layers" ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("blocks" , "videomae.encoder.layer" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name and "bias" not in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.self" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.attention" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc2" , "output.dense" ) if "decoder_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_embed" , "decoder.decoder_embed" ) if "decoder_norm" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_norm" , "decoder.decoder_norm" ) if "decoder_pred" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pred" , "decoder.decoder_pred" ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.weight" , "videomae.layernorm.weight" ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.bias" , "videomae.layernorm.bias" ) if "head" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("head" , "classifier" ) return name def _A ( __snake_case :Union[str, Any] , __snake_case :Optional[int] ) -> Optional[Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__snake_case ) if key.startswith("encoder." ): __SCREAMING_SNAKE_CASE = key.replace("encoder." , "" ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split("." ) if key.startswith("decoder.blocks" ): __SCREAMING_SNAKE_CASE = config.decoder_hidden_size __SCREAMING_SNAKE_CASE = int(key_split[2] ) __SCREAMING_SNAKE_CASE = "decoder.decoder_layers." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = config.hidden_size __SCREAMING_SNAKE_CASE = int(key_split[1] ) __SCREAMING_SNAKE_CASE = "videomae.encoder.layer." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def _A ( ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) __SCREAMING_SNAKE_CASE = np.load(__snake_case ) return list(__snake_case ) def _A ( __snake_case :Optional[int] , __snake_case :List[str] , __snake_case :Union[str, Any] , __snake_case :Optional[Any] ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = get_videomae_config(__snake_case ) if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = VideoMAEForVideoClassification(__snake_case ) else: __SCREAMING_SNAKE_CASE = VideoMAEForPreTraining(__snake_case ) # download original checkpoint, hosted on Google Drive __SCREAMING_SNAKE_CASE = "pytorch_model.bin" gdown.cached_download(__snake_case , __snake_case , quiet=__snake_case ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) if "model" in files: __SCREAMING_SNAKE_CASE = files["model"] else: __SCREAMING_SNAKE_CASE = files["module"] __SCREAMING_SNAKE_CASE = convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) model.eval() # verify model on basic input __SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) __SCREAMING_SNAKE_CASE = prepare_video() __SCREAMING_SNAKE_CASE = image_processor(__snake_case , return_tensors="pt" ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case ) __SCREAMING_SNAKE_CASE = model(**__snake_case ) __SCREAMING_SNAKE_CASE = outputs.logits __SCREAMING_SNAKE_CASE = [ "videomae-small-finetuned-kinetics", "videomae-small-finetuned-ssv2", # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) "videomae-base-short", "videomae-base-short-finetuned-kinetics", "videomae-base", "videomae-base-finetuned-kinetics", "videomae-large", "videomae-large-finetuned-kinetics", "videomae-huge-finetuned-kinetics", # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) "videomae-base-short-ssv2", "videomae-base-short-finetuned-ssv2", "videomae-base-ssv2", "videomae-base-finetuned-ssv2", ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.9_2_9_1, -0.4_0_6_1, -0.9_3_0_7] ) elif model_name == "videomae-small-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_7_1, -0.4_6_8_9, -0.8_2_3_5] ) elif model_name == "videomae-base": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_7_3_9, 0.7_9_6_8, 0.7_0_8_9], [0.6_7_0_1, 0.7_4_8_7, 0.6_2_0_9], [0.4_2_8_7, 0.5_1_5_8, 0.4_7_7_3]] ) elif model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ) # we verified the loss both for normalized and unnormalized targets for this one __SCREAMING_SNAKE_CASE = torch.tensor([0.5_1_4_2] ) if config.norm_pix_loss else torch.tensor([0.6_4_6_9] ) elif model_name == "videomae-large": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_1_4_9, 0.7_9_9_7, 0.6_9_6_6], [0.6_7_6_8, 0.7_8_6_9, 0.6_9_4_8], [0.5_1_3_9, 0.6_2_2_1, 0.5_6_0_5]] ) elif model_name == "videomae-large-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.0_7_7_1, 0.0_0_1_1, -0.3_6_2_5] ) elif model_name == "videomae-huge-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_4_3_3, 0.1_6_3_2, -0.4_8_9_4] ) elif model_name == "videomae-base-short-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.6_5_8_8, 0.0_9_9_0, -0.2_4_9_3] ) elif model_name == "videomae-base-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ) elif model_name == "videomae-base-short-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.4_7_1_2, 0.5_2_9_6, 0.5_7_8_6], [0.2_2_7_8, 0.2_7_2_9, 0.4_0_2_6], [0.0_3_5_2, 0.0_7_3_0, 0.2_5_0_6]] ) elif model_name == "videomae-base-short-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.0_5_3_7, -0.1_5_3_9, -0.3_2_6_6] ) elif model_name == "videomae-base-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.8_1_3_1, 0.8_7_2_7, 0.8_5_4_6], [0.7_3_6_6, 0.9_3_7_7, 0.8_8_7_0], [0.5_9_3_5, 0.8_8_7_4, 0.8_5_6_4]] ) elif model_name == "videomae-base-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.1_9_6_1, -0.8_3_3_7, -0.6_3_8_9] ) else: raise ValueError(f'''Model name not supported. Should be one of {model_names}''' ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) else: print("Logits:" , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , __snake_case , atol=1e-4 ) print("Logits ok!" ) # verify loss, if applicable if model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = outputs.loss assert torch.allclose(__snake_case , __snake_case , atol=1e-4 ) print("Loss ok!" ) if pytorch_dump_folder_path is not None: print(f'''Saving model and image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__snake_case ) model.save_pretrained(__snake_case ) if push_to_hub: print("Pushing to the hub..." ) model.push_to_hub(__snake_case , organization="nielsr" ) if __name__ == "__main__": _snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) _snake_case : Optional[int] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
693
1
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : Optional[int] = { 'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json', } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""blip_2_vision_model""" def __init__( self, _a=14_08, _a=61_44, _a=39, _a=16, _a=2_24, _a=14, _a="gelu", _a=0.0_0001, _a=0.0, _a=1E-1_0, _a=True, **_a, ) -> Optional[int]: super().__init__(**_a ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = qkv_bias @classmethod def __lowerCAmelCase ( cls, _a, **_a ) -> "PretrainedConfig": cls._set_token_in_kwargs(_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = cls.get_config_dict(_a, **_a ) # get the vision config dict if we are loading from Blip2Config if config_dict.get("model_type" ) == "blip-2": __SCREAMING_SNAKE_CASE = config_dict["vision_config"] if "model_type" in config_dict and hasattr(cls, "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a, **_a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""blip_2_qformer""" def __init__( self, _a=3_05_22, _a=7_68, _a=12, _a=12, _a=30_72, _a="gelu", _a=0.1, _a=0.1, _a=5_12, _a=0.02, _a=1E-1_2, _a=0, _a="absolute", _a=2, _a=14_08, **_a, ) -> str: super().__init__(pad_token_id=_a, **_a ) __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = position_embedding_type __SCREAMING_SNAKE_CASE = cross_attention_frequency __SCREAMING_SNAKE_CASE = encoder_hidden_size @classmethod def __lowerCAmelCase ( cls, _a, **_a ) -> "PretrainedConfig": cls._set_token_in_kwargs(_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = cls.get_config_dict(_a, **_a ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get("model_type" ) == "blip-2": __SCREAMING_SNAKE_CASE = config_dict["qformer_config"] if "model_type" in config_dict and hasattr(cls, "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_a, **_a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""blip-2""" SCREAMING_SNAKE_CASE__ =True def __init__( self, _a=None, _a=None, _a=None, _a=32, **_a ) -> List[str]: super().__init__(**_a ) if vision_config is None: __SCREAMING_SNAKE_CASE = {} logger.info("vision_config is None. initializing the Blip2VisionConfig with default values." ) if qformer_config is None: __SCREAMING_SNAKE_CASE = {} logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values." ) if text_config is None: __SCREAMING_SNAKE_CASE = {} logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)." ) __SCREAMING_SNAKE_CASE = BlipaVisionConfig(**_a ) __SCREAMING_SNAKE_CASE = BlipaQFormerConfig(**_a ) __SCREAMING_SNAKE_CASE = text_config["model_type"] if "model_type" in text_config else "opt" __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[text_model_type](**_a ) __SCREAMING_SNAKE_CASE = self.text_config.tie_word_embeddings __SCREAMING_SNAKE_CASE = self.text_config.is_encoder_decoder __SCREAMING_SNAKE_CASE = num_query_tokens __SCREAMING_SNAKE_CASE = self.vision_config.hidden_size __SCREAMING_SNAKE_CASE = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __SCREAMING_SNAKE_CASE = 1.0 __SCREAMING_SNAKE_CASE = 0.02 @classmethod def __lowerCAmelCase ( cls, _a, _a, _a, **_a, ) -> int: return cls( vision_config=vision_config.to_dict(), qformer_config=qformer_config.to_dict(), text_config=text_config.to_dict(), **_a, ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE = self.vision_config.to_dict() __SCREAMING_SNAKE_CASE = self.qformer_config.to_dict() __SCREAMING_SNAKE_CASE = self.text_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output
693
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, **_a ) -> None: warnings.warn( "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use CLIPImageProcessor instead.", _a, ) super().__init__(*_a, **_a )
693
1
import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =OpenAIGPTTokenizer SCREAMING_SNAKE_CASE__ =OpenAIGPTTokenizerFast SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =False def __lowerCAmelCase ( self ) -> str: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __SCREAMING_SNAKE_CASE = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "w</w>", "r</w>", "t</w>", "lo", "low", "er</w>", "low</w>", "lowest</w>", "newer</w>", "wider</w>", "<unk>", ] __SCREAMING_SNAKE_CASE = dict(zip(_a, range(len(_a ) ) ) ) __SCREAMING_SNAKE_CASE = ["#version: 0.2", "l o", "lo w", "e r</w>", ""] __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file, "w" ) as fp: fp.write(json.dumps(_a ) ) with open(self.merges_file, "w" ) as fp: fp.write("\n".join(_a ) ) def __lowerCAmelCase ( self, _a ) -> Union[str, Any]: return "lower newer", "lower newer" def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = OpenAIGPTTokenizer(self.vocab_file, self.merges_file ) __SCREAMING_SNAKE_CASE = "lower" __SCREAMING_SNAKE_CASE = ["low", "er</w>"] __SCREAMING_SNAKE_CASE = tokenizer.tokenize(_a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = tokens + ["<unk>"] __SCREAMING_SNAKE_CASE = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ), _a ) def __lowerCAmelCase ( self, _a=15 ) -> int: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a, **_a ) # Simple input __SCREAMING_SNAKE_CASE = "This is a simple input" __SCREAMING_SNAKE_CASE = ["This is a simple input 1", "This is a simple input 2"] __SCREAMING_SNAKE_CASE = ("This is a simple input", "This is a pair") __SCREAMING_SNAKE_CASE = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests self.assertRaises(_a, tokenizer_r.encode, _a, max_length=_a, padding="max_length" ) # Simple input self.assertRaises(_a, tokenizer_r.encode_plus, _a, max_length=_a, padding="max_length" ) # Simple input self.assertRaises( _a, tokenizer_r.batch_encode_plus, _a, max_length=_a, padding="max_length", ) # Pair input self.assertRaises(_a, tokenizer_r.encode, _a, max_length=_a, padding="max_length" ) # Pair input self.assertRaises(_a, tokenizer_r.encode_plus, _a, max_length=_a, padding="max_length" ) # Pair input self.assertRaises( _a, tokenizer_r.batch_encode_plus, _a, max_length=_a, padding="max_length", ) def __lowerCAmelCase ( self ) -> Dict: pass @require_ftfy @require_spacy @require_tokenizers class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): pass
693
from math import sqrt def _A ( __snake_case :int ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 for i in range(1 , int(sqrt(__snake_case ) + 1 ) ): if n % i == 0 and i != sqrt(__snake_case ): total += i + n // i elif i == sqrt(__snake_case ): total += i return total - n def _A ( __snake_case :int = 1_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = sum( i for i in range(1 , __snake_case ) if sum_of_divisors(sum_of_divisors(__snake_case ) ) == i and sum_of_divisors(__snake_case ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
693
1
_snake_case : Dict = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _snake_case : Optional[Any] = [{'type': 'code', 'content': INSTALL_CONTENT}] _snake_case : Union[str, Any] = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
693
def _A ( __snake_case :int , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def _A ( __snake_case :Union[str, Any] , __snake_case :List[Any]=False ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = OmegaConf.load(__snake_case ) if display: print(yaml.dump(OmegaConf.to_container(__snake_case ) ) ) return config def _A ( __snake_case :str , __snake_case :List[str]=None , __snake_case :Dict=None ) -> Optional[int]: """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE = "./model_checkpoints/vqgan_only.yaml" __SCREAMING_SNAKE_CASE = load_config(__snake_case , display=__snake_case ) __SCREAMING_SNAKE_CASE = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE = "./model_checkpoints/vqgan_only.pt" __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location=__snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE = sd["state_dict"] model.load_state_dict(__snake_case , strict=__snake_case ) model.to(__snake_case ) del sd return model def _A ( __snake_case :Optional[int] , __snake_case :Dict ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = model.encode(__snake_case ) print(f'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE = model.decode(__snake_case ) return xrec def _A ( __snake_case :List[Any] , __snake_case :List[str]=False ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = string.rsplit("." , 1 ) if reload: __SCREAMING_SNAKE_CASE = importlib.import_module(__snake_case ) importlib.reload(__snake_case ) return getattr(importlib.import_module(__snake_case , package=__snake_case ) , cls ) def _A ( __snake_case :Union[str, Any] ) -> List[Any]: """simple docstring""" if "target" not in config: raise KeyError("Expected key `target` to instantiate." ) return get_obj_from_str(config["target"] )(**config.get("params" , {} ) ) def _A ( __snake_case :Any , __snake_case :List[Any] , __snake_case :Tuple=True , __snake_case :int=True ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = instantiate_from_config(__snake_case ) if sd is not None: model.load_state_dict(__snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def _A ( __snake_case :Union[str, Any] , __snake_case :List[Any] , __snake_case :Any , __snake_case :Tuple ) -> Union[str, Any]: """simple docstring""" if ckpt: __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) __SCREAMING_SNAKE_CASE = pl_sd["global_step"] print(f'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE = {"state_dict": None} __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = load_model_from_config(config.model , pl_sd["state_dict"] , gpu=__snake_case , eval_mode=__snake_case )["model"] return model, global_step
693
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a=99, _a=13, _a=7, _a=9, _a=True, _a=True, _a=False, _a=32, _a=5, _a=4, _a=37, _a=8, _a=0.1, _a=0.002, _a=1, _a=0, _a=0, _a=None, _a=None, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = encoder_seq_length __SCREAMING_SNAKE_CASE = decoder_seq_length # For common tests __SCREAMING_SNAKE_CASE = self.decoder_seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_attention_mask __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = d_ff __SCREAMING_SNAKE_CASE = relative_attention_num_buckets __SCREAMING_SNAKE_CASE = dropout_rate __SCREAMING_SNAKE_CASE = initializer_factor __SCREAMING_SNAKE_CASE = eos_token_id __SCREAMING_SNAKE_CASE = pad_token_id __SCREAMING_SNAKE_CASE = decoder_start_token_id __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = decoder_layers def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig.from_pretrained("google/umt5-base" ) def __lowerCAmelCase ( self, _a, _a, _a, _a=None, _a=None, _a=None, _a=None, _a=None, ) -> int: if attention_mask is None: __SCREAMING_SNAKE_CASE = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: __SCREAMING_SNAKE_CASE = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_hidden_layers, config.num_attention_heads, device=_a ) if decoder_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_decoder_layers, config.num_attention_heads, device=_a ) if cross_attn_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_attention_heads, device=_a ) 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, } def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.encoder_seq_length], self.vocab_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.decoder_seq_length], self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input __SCREAMING_SNAKE_CASE = input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = decoder_input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = self.get_config() __SCREAMING_SNAKE_CASE = config.num_attention_heads __SCREAMING_SNAKE_CASE = self.prepare_inputs_dict(_a, _a, _a ) return config, input_dict def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig( vocab_size=1_66, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self ) -> Union[str, Any]: return TaConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ) model.to(_a ) model.eval() __SCREAMING_SNAKE_CASE = model( input_ids=_a, decoder_input_ids=_a, attention_mask=_a, decoder_attention_mask=_a, ) __SCREAMING_SNAKE_CASE = model(input_ids=_a, decoder_input_ids=_a ) __SCREAMING_SNAKE_CASE = result.last_hidden_state __SCREAMING_SNAKE_CASE = result.past_key_values __SCREAMING_SNAKE_CASE = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size(), (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size(), (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_a ), config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ), 4 ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Tuple: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).get_decoder().to(_a ).eval() # first forward pass __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) self.parent.assertTrue(len(_a ) == len(_a ) ) self.parent.assertTrue(len(_a ) == len(_a ) + 1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 1), config.vocab_size ) # append to next input_ids and __SCREAMING_SNAKE_CASE = torch.cat([input_ids, next_tokens], dim=-1 ) __SCREAMING_SNAKE_CASE = model(_a )["last_hidden_state"] __SCREAMING_SNAKE_CASE = model(_a, past_key_values=_a )["last_hidden_state"] # select random slice __SCREAMING_SNAKE_CASE = ids_tensor((1,), output_from_past.shape[-1] ).item() __SCREAMING_SNAKE_CASE = output_from_no_past[:, -1, random_slice_idx].detach() __SCREAMING_SNAKE_CASE = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_a, _a, atol=1E-3 ) ) def __lowerCAmelCase ( self, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).to(_a ).half().eval() __SCREAMING_SNAKE_CASE = model(**_a )["last_hidden_state"] self.parent.assertFalse(torch.isnan(_a ).any().item() ) @require_torch class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ =(UMTaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ =( { """conversational""": UMTaForConditionalGeneration, """feature-extraction""": UMTaModel, """summarization""": UMTaForConditionalGeneration, """text2text-generation""": UMTaForConditionalGeneration, """translation""": UMTaForConditionalGeneration, """question-answering""": UMTaForQuestionAnswering, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True # The small UMT5 model needs higher percentages for CPU/MP tests SCREAMING_SNAKE_CASE__ =[0.8, 0.9] def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = UMTaModelTester(self ) @unittest.skip("Test has a segmentation fault on torch 1.8.0" ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = UMTaModel(config_and_inputs[0] ).to(_a ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _a, (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]), f'''{tmpdirname}/t5_test.onnx''', export_params=_a, opset_version=9, input_names=["input_ids", "decoder_input_ids"], ) @unittest.skipIf(torch_device == "cpu", "Cant do half precision" ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_a ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ["encoder_attentions", "decoder_attentions", "cross_attentions"] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = config_and_inputs[0] __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration(_a ).eval() model.to(_a ) __SCREAMING_SNAKE_CASE = { "head_mask": torch.zeros(config.num_layers, config.num_heads, device=_a ), "decoder_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), "cross_attn_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), } for attn_name, (name, mask) in zip(_a, head_masking.items() ): __SCREAMING_SNAKE_CASE = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_heads, device=_a ) __SCREAMING_SNAKE_CASE = model.generate( config_and_inputs[1]["input_ids"], num_beams=1, max_length=3, output_attentions=_a, return_dict_in_generate=_a, **_a, ) # We check the state of decoder_attentions and cross_attentions just from the last step __SCREAMING_SNAKE_CASE = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ), 0.0 ) @unittest.skip("Does not work on the tiny model as we keep hitting edge cases." ) def __lowerCAmelCase ( self ) -> int: pass @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow @unittest.skip( "Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged" ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration.from_pretrained("google/umt5-small", return_dict=_a ).to(_a ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("google/umt5-small", use_fast=_a, legacy=_a ) __SCREAMING_SNAKE_CASE = [ "Bonjour monsieur <extra_id_0> bien <extra_id_1>.", "No se como puedo <extra_id_0>.", "This is the reason why we <extra_id_0> them.", "The <extra_id_0> walks in <extra_id_1>, seats", "A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.", ] __SCREAMING_SNAKE_CASE = tokenizer(_a, return_tensors="pt", padding=_a ).input_ids # fmt: off __SCREAMING_SNAKE_CASE = torch.tensor( [ [ 3_85_30, 21_07_03, 25_62_99, 14_10, 25_62_98, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_26, 3_21, 6_71, 2_59_22, 25_62_99, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 14_60, 3_39, 3_12, 1_90_14, 1_06_20, 7_58, 25_62_99, 23_55,2_74, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_17, 25_62_99, 1_48_69, 2_81, 3_01, 25_62_98, 2_75, 11_99_83,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_20, 25_62_99, 1_48_69, 2_81, 22_34, 2_89, 22_75, 3_33,6_13_91, 2_89, 25_62_98, 5_43, 25_62_97, 16_87_14, 3_29, 25_62_96,2_74, 1], ] ) # fmt: on torch.testing.assert_allclose(_a, _a ) __SCREAMING_SNAKE_CASE = model.generate(input_ids.to(_a ) ) __SCREAMING_SNAKE_CASE = [ "<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>", "<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", ] __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, _a )
693
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: _snake_case : Union[str, Any] = None _snake_case : Dict = logging.get_logger(__name__) _snake_case : List[str] = '▁' _snake_case : Dict = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} _snake_case : int = { 'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'}, 'tokenizer_file': { 'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json' }, } _snake_case : List[str] = { 'google/pegasus-xsum': 5_12, } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ =PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ =PegasusTokenizer SCREAMING_SNAKE_CASE__ =["""input_ids""", """attention_mask"""] def __init__( self, _a=None, _a=None, _a="<pad>", _a="</s>", _a="<unk>", _a="<mask_2>", _a="<mask_1>", _a=None, _a=1_03, **_a, ) -> Any: __SCREAMING_SNAKE_CASE = offset if additional_special_tokens is not None: if not isinstance(_a, _a ): raise TypeError( f'''additional_special_tokens should be of type {type(_a )}, but is''' f''' {type(_a )}''' ) __SCREAMING_SNAKE_CASE = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f'''<unk_{i}>''' for i in range(len(_a ), self.offset - 1 ) ] if len(set(_a ) ) != len(_a ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" f''' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.''' ) __SCREAMING_SNAKE_CASE = additional_special_tokens_extended else: __SCREAMING_SNAKE_CASE = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f'''<unk_{i}>''' for i in range(2, self.offset )] super().__init__( _a, tokenizer_file=_a, pad_token=_a, eos_token=_a, unk_token=_a, mask_token=_a, mask_token_sent=_a, offset=_a, additional_special_tokens=_a, **_a, ) __SCREAMING_SNAKE_CASE = vocab_file __SCREAMING_SNAKE_CASE = False if not self.vocab_file else True def __lowerCAmelCase ( self, _a ) -> int: __SCREAMING_SNAKE_CASE = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" f''' {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}''' ) return [1 if x in all_special_ids else 0 for x in seq] def __lowerCAmelCase ( self, _a, _a = None, _a = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(_a ) elif token_ids_a is None: return self._special_token_mask(_a ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def __lowerCAmelCase ( self, _a, _a=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def __lowerCAmelCase ( self, _a, _a = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(_a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __SCREAMING_SNAKE_CASE = os.path.join( _a, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file, _a ) return (out_vocab_file,)
693
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _A ( __snake_case :BertModel , __snake_case :str , __snake_case :str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") __SCREAMING_SNAKE_CASE = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__snake_case ): os.makedirs(__snake_case ) __SCREAMING_SNAKE_CASE = model.state_dict() def to_tf_var_name(__snake_case :str ): for patt, repl in iter(__snake_case ): __SCREAMING_SNAKE_CASE = name.replace(__snake_case , __snake_case ) return f'''bert/{name}''' def create_tf_var(__snake_case :np.ndarray , __snake_case :str , __snake_case :tf.Session ): __SCREAMING_SNAKE_CASE = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE = tf.get_variable(dtype=__snake_case , shape=tensor.shape , name=__snake_case , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__snake_case ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE = to_tf_var_name(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE = torch_tensor.T __SCREAMING_SNAKE_CASE = create_tf_var(tensor=__snake_case , name=__snake_case , session=__snake_case ) tf.keras.backend.set_value(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = session.run(__snake_case ) print(f'''Successfully created {tf_name}: {np.allclose(__snake_case , __snake_case )}''' ) __SCREAMING_SNAKE_CASE = tf.train.Saver(tf.trainable_variables() ) saver.save(__snake_case , os.path.join(__snake_case , model_name.replace("-" , "_" ) + ".ckpt" ) ) def _A ( __snake_case :str=None ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__snake_case , required=__snake_case , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__snake_case , default=__snake_case , required=__snake_case , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__snake_case , required=__snake_case , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__snake_case , required=__snake_case , help="Directory in which to save tensorflow model" ) __SCREAMING_SNAKE_CASE = parser.parse_args(__snake_case ) __SCREAMING_SNAKE_CASE = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__snake_case , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
693
1
import re from typing import Callable, List, Optional, Union import tensorflow as tf try: from tensorflow.keras.optimizers.legacy import Adam except ImportError: from tensorflow.keras.optimizers import Adam class __SCREAMING_SNAKE_CASE ( tf.keras.optimizers.schedules.LearningRateSchedule ): def __init__( self, _a, _a, _a, _a = 1.0, _a = None, ) -> Optional[int]: super().__init__() __SCREAMING_SNAKE_CASE = initial_learning_rate __SCREAMING_SNAKE_CASE = warmup_steps __SCREAMING_SNAKE_CASE = power __SCREAMING_SNAKE_CASE = decay_schedule_fn __SCREAMING_SNAKE_CASE = name def __call__( self, _a ) -> Tuple: with tf.name_scope(self.name or "WarmUp" ) as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. __SCREAMING_SNAKE_CASE = tf.cast(_a, tf.floataa ) __SCREAMING_SNAKE_CASE = tf.cast(self.warmup_steps, tf.floataa ) __SCREAMING_SNAKE_CASE = global_step_float / warmup_steps_float __SCREAMING_SNAKE_CASE = self.initial_learning_rate * tf.math.pow(_a, self.power ) return tf.cond( global_step_float < warmup_steps_float, lambda: warmup_learning_rate, lambda: self.decay_schedule_fn(step - self.warmup_steps ), name=_a, ) def __lowerCAmelCase ( self ) -> Optional[Any]: return { "initial_learning_rate": self.initial_learning_rate, "decay_schedule_fn": self.decay_schedule_fn, "warmup_steps": self.warmup_steps, "power": self.power, "name": self.name, } def _A ( __snake_case :float , __snake_case :int , __snake_case :int , __snake_case :float = 0.0 , __snake_case :float = 0.9 , __snake_case :float = 0.9_9_9 , __snake_case :float = 1e-8 , __snake_case :Optional[float] = None , __snake_case :Optional[float] = None , __snake_case :float = 0.0 , __snake_case :float = 1.0 , __snake_case :Optional[List[str]] = None , ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = tf.keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=__snake_case , decay_steps=num_train_steps - num_warmup_steps , end_learning_rate=init_lr * min_lr_ratio , power=__snake_case , ) if num_warmup_steps: __SCREAMING_SNAKE_CASE = WarmUp( initial_learning_rate=__snake_case , decay_schedule_fn=__snake_case , warmup_steps=__snake_case , ) if weight_decay_rate > 0.0: __SCREAMING_SNAKE_CASE = AdamWeightDecay( learning_rate=__snake_case , weight_decay_rate=__snake_case , beta_a=__snake_case , beta_a=__snake_case , epsilon=__snake_case , clipnorm=__snake_case , global_clipnorm=__snake_case , exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"] , include_in_weight_decay=__snake_case , ) else: __SCREAMING_SNAKE_CASE = tf.keras.optimizers.Adam( learning_rate=__snake_case , beta_a=__snake_case , beta_a=__snake_case , epsilon=__snake_case , clipnorm=__snake_case , global_clipnorm=__snake_case , ) # We return the optimizer and the LR scheduler in order to better track the # evolution of the LR independently of the optimizer. return optimizer, lr_schedule class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, _a = 0.001, _a = 0.9, _a = 0.999, _a = 1E-7, _a = False, _a = 0.0, _a = None, _a = None, _a = "AdamWeightDecay", **_a, ) -> str: super().__init__(_a, _a, _a, _a, _a, _a, **_a ) __SCREAMING_SNAKE_CASE = weight_decay_rate __SCREAMING_SNAKE_CASE = include_in_weight_decay __SCREAMING_SNAKE_CASE = exclude_from_weight_decay @classmethod def __lowerCAmelCase ( cls, _a ) -> int: __SCREAMING_SNAKE_CASE = {"WarmUp": WarmUp} return super(_a, cls ).from_config(_a, custom_objects=_a ) def __lowerCAmelCase ( self, _a, _a, _a ) -> List[str]: super(_a, self )._prepare_local(_a, _a, _a ) __SCREAMING_SNAKE_CASE = tf.constant( self.weight_decay_rate, name="adam_weight_decay_rate" ) def __lowerCAmelCase ( self, _a, _a, _a ) -> Tuple: __SCREAMING_SNAKE_CASE = self._do_use_weight_decay(var.name ) if do_decay: return var.assign_sub( learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["weight_decay_rate"], use_locking=self._use_locking, ) return tf.no_op() def __lowerCAmelCase ( self, _a, _a=None, **_a ) -> Optional[Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = list(zip(*_a ) ) return super(_a, self ).apply_gradients(zip(_a, _a ), name=_a, **_a ) def __lowerCAmelCase ( self, _a, _a, _a ) -> Optional[Any]: if apply_state is None: return self._decayed_lr_t[var_dtype], {} __SCREAMING_SNAKE_CASE = apply_state or {} __SCREAMING_SNAKE_CASE = apply_state.get((var_device, var_dtype) ) if coefficients is None: __SCREAMING_SNAKE_CASE = self._fallback_apply_state(_a, _a ) __SCREAMING_SNAKE_CASE = coefficients return coefficients["lr_t"], {"apply_state": apply_state} def __lowerCAmelCase ( self, _a, _a, _a=None ) -> Optional[int]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self._get_lr(var.device, var.dtype.base_dtype, _a ) __SCREAMING_SNAKE_CASE = self._decay_weights_op(_a, _a, _a ) with tf.control_dependencies([decay] ): return super(_a, self )._resource_apply_dense(_a, _a, **_a ) def __lowerCAmelCase ( self, _a, _a, _a, _a=None ) -> List[Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self._get_lr(var.device, var.dtype.base_dtype, _a ) __SCREAMING_SNAKE_CASE = self._decay_weights_op(_a, _a, _a ) with tf.control_dependencies([decay] ): return super(_a, self )._resource_apply_sparse(_a, _a, _a, **_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = super().get_config() config.update({"weight_decay_rate": self.weight_decay_rate} ) return config def __lowerCAmelCase ( self, _a ) -> List[str]: if self.weight_decay_rate == 0: return False if self._include_in_weight_decay: for r in self._include_in_weight_decay: if re.search(_a, _a ) is not None: return True if self._exclude_from_weight_decay: for r in self._exclude_from_weight_decay: if re.search(_a, _a ) is not None: return False return True class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self ) -> List[str]: __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = None @property def __lowerCAmelCase ( self ) -> str: if self._accum_steps is None: __SCREAMING_SNAKE_CASE = tf.Variable( tf.constant(0, dtype=tf.intaa ), trainable=_a, synchronization=tf.VariableSynchronization.ON_READ, aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA, ) return self._accum_steps.value() @property def __lowerCAmelCase ( self ) -> List[str]: if not self._gradients: raise ValueError("The accumulator should be called first to initialize the gradients" ) return [gradient.value() if gradient is not None else gradient for gradient in self._gradients] def __call__( self, _a ) -> Tuple: if not self._gradients: __SCREAMING_SNAKE_CASE = self.step # Create the step variable. self._gradients.extend( [ tf.Variable( tf.zeros_like(_a ), trainable=_a, synchronization=tf.VariableSynchronization.ON_READ, aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA, ) if gradient is not None else gradient for gradient in gradients ] ) if len(_a ) != len(self._gradients ): raise ValueError(f'''Expected {len(self._gradients )} gradients, but got {len(_a )}''' ) for accum_gradient, gradient in zip(self._gradients, _a ): if accum_gradient is not None and gradient is not None: accum_gradient.assign_add(_a ) self._accum_steps.assign_add(1 ) def __lowerCAmelCase ( self ) -> int: if not self._gradients: return self._accum_steps.assign(0 ) for gradient in self._gradients: if gradient is not None: gradient.assign(tf.zeros_like(_a ) )
693
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
1
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _A ( __snake_case :BertModel , __snake_case :str , __snake_case :str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") __SCREAMING_SNAKE_CASE = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__snake_case ): os.makedirs(__snake_case ) __SCREAMING_SNAKE_CASE = model.state_dict() def to_tf_var_name(__snake_case :str ): for patt, repl in iter(__snake_case ): __SCREAMING_SNAKE_CASE = name.replace(__snake_case , __snake_case ) return f'''bert/{name}''' def create_tf_var(__snake_case :np.ndarray , __snake_case :str , __snake_case :tf.Session ): __SCREAMING_SNAKE_CASE = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE = tf.get_variable(dtype=__snake_case , shape=tensor.shape , name=__snake_case , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__snake_case ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE = to_tf_var_name(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE = torch_tensor.T __SCREAMING_SNAKE_CASE = create_tf_var(tensor=__snake_case , name=__snake_case , session=__snake_case ) tf.keras.backend.set_value(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = session.run(__snake_case ) print(f'''Successfully created {tf_name}: {np.allclose(__snake_case , __snake_case )}''' ) __SCREAMING_SNAKE_CASE = tf.train.Saver(tf.trainable_variables() ) saver.save(__snake_case , os.path.join(__snake_case , model_name.replace("-" , "_" ) + ".ckpt" ) ) def _A ( __snake_case :str=None ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__snake_case , required=__snake_case , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__snake_case , default=__snake_case , required=__snake_case , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__snake_case , required=__snake_case , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__snake_case , required=__snake_case , help="Directory in which to save tensorflow model" ) __SCREAMING_SNAKE_CASE = parser.parse_args(__snake_case ) __SCREAMING_SNAKE_CASE = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__snake_case , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
693
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Dict: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 20_00, _a = None, _a = "pil", _a = True, **_a, ) -> Union[ImagePipelineOutput, Tuple]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a ) * self.scheduler.init_noise_sigma __SCREAMING_SNAKE_CASE = sample.to(self.device ) self.scheduler.set_timesteps(_a ) self.scheduler.set_sigmas(_a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): __SCREAMING_SNAKE_CASE = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): __SCREAMING_SNAKE_CASE = self.unet(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct(_a, _a, generator=_a ).prev_sample # prediction step __SCREAMING_SNAKE_CASE = model(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_pred(_a, _a, _a, generator=_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = output.prev_sample, output.prev_sample_mean __SCREAMING_SNAKE_CASE = sample_mean.clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_a )
693
1
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, ) _snake_case : str = logging.getLogger(__name__) @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Whether tp freeze the encoder."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Whether to freeze the embeddings."""} ) @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =field( metadata={"""help""": """The input data dir. Should contain the .tsv files (or other data files) for the task."""} ) SCREAMING_SNAKE_CASE__ =field( default="""summarization""" , metadata={"""help""": """Task name, summarization (or summarization_{dataset} for pegasus) or translation"""} , ) SCREAMING_SNAKE_CASE__ =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.""" ) } , ) SCREAMING_SNAKE_CASE__ =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.""" ) } , ) SCREAMING_SNAKE_CASE__ =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``.""" ) } , ) SCREAMING_SNAKE_CASE__ =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.""" ) } , ) SCREAMING_SNAKE_CASE__ =field(default=-1 , metadata={"""help""": """# training examples. -1 means use all."""} ) SCREAMING_SNAKE_CASE__ =field(default=-1 , metadata={"""help""": """# validation examples. -1 means use all."""} ) SCREAMING_SNAKE_CASE__ =field(default=-1 , metadata={"""help""": """# test examples. -1 means use all."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Source language id for translation."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Target language id for translation."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """# num_beams to use for evaluation."""} ) SCREAMING_SNAKE_CASE__ =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 ( __snake_case :Optional[Any] , __snake_case :Union[str, Any] , __snake_case :Optional[Any] ) -> str: """simple docstring""" logger.info(f'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(f''' {key} = {metrics[key]}''' ) save_json(__snake_case , os.path.join(__snake_case , f'''{split}_results.json''' ) ) def _A ( ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() check_output_dir(__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" , __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. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __SCREAMING_SNAKE_CASE = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(__snake_case , __snake_case , __snake_case ): assert hasattr(__snake_case , __snake_case ), f'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(__snake_case , __snake_case , getattr(__snake_case , __snake_case ) ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=__snake_case , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(__snake_case , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: __SCREAMING_SNAKE_CASE = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(__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(__snake_case , __snake_case ): __SCREAMING_SNAKE_CASE = tokenizer.lang_code_to_id[data_args.tgt_lang] else: __SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(__snake_case ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) __SCREAMING_SNAKE_CASE = SeqaSeqDataset # Get datasets __SCREAMING_SNAKE_CASE = ( dataset_class( __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 ) __SCREAMING_SNAKE_CASE = ( dataset_class( __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 ) __SCREAMING_SNAKE_CASE = ( dataset_class( __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 __SCREAMING_SNAKE_CASE = ( build_compute_metrics_fn(data_args.task , __snake_case ) if training_args.predict_with_generate else None ) __SCREAMING_SNAKE_CASE = SeqaSeqTrainer( model=__snake_case , args=__snake_case , data_args=__snake_case , train_dataset=__snake_case , eval_dataset=__snake_case , data_collator=SeqaSeqDataCollator( __snake_case , __snake_case , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=__snake_case , tokenizer=__snake_case , ) __SCREAMING_SNAKE_CASE = {} # Training if training_args.do_train: logger.info("*** Train ***" ) __SCREAMING_SNAKE_CASE = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) __SCREAMING_SNAKE_CASE = train_result.metrics __SCREAMING_SNAKE_CASE = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , __snake_case , training_args.output_dir ) all_metrics.update(__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 ***" ) __SCREAMING_SNAKE_CASE = trainer.evaluate(metric_key_prefix="val" ) __SCREAMING_SNAKE_CASE = data_args.n_val __SCREAMING_SNAKE_CASE = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , __snake_case , training_args.output_dir ) all_metrics.update(__snake_case ) if training_args.do_predict: logger.info("*** Predict ***" ) __SCREAMING_SNAKE_CASE = trainer.predict(test_dataset=__snake_case , metric_key_prefix="test" ) __SCREAMING_SNAKE_CASE = test_output.metrics __SCREAMING_SNAKE_CASE = data_args.n_test if trainer.is_world_process_zero(): __SCREAMING_SNAKE_CASE = round(metrics["test_loss"] , 4 ) handle_metrics("test" , __snake_case , training_args.output_dir ) all_metrics.update(__snake_case ) if training_args.predict_with_generate: __SCREAMING_SNAKE_CASE = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) __SCREAMING_SNAKE_CASE = lmap(str.strip , __snake_case ) write_txt_file(__snake_case , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(__snake_case , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def _A ( __snake_case :Optional[Any] ) -> List[Any]: """simple docstring""" main() if __name__ == "__main__": main()
693
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel 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, skip_mps from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =CycleDiffusionPipeline SCREAMING_SNAKE_CASE__ =TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { """negative_prompt""", """height""", """width""", """negative_prompt_embeds""", } SCREAMING_SNAKE_CASE__ =PipelineTesterMixin.required_optional_params - {"""latents"""} SCREAMING_SNAKE_CASE__ =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""source_prompt"""} ) SCREAMING_SNAKE_CASE__ =IMAGE_TO_IMAGE_IMAGE_PARAMS SCREAMING_SNAKE_CASE__ =IMAGE_TO_IMAGE_IMAGE_PARAMS def __lowerCAmelCase ( self ) -> List[str]: torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = 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, ) __SCREAMING_SNAKE_CASE = DDIMScheduler( beta_start=0.0_0085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=10_00, clip_sample=_a, set_alpha_to_one=_a, ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = AutoencoderKL( block_out_channels=[32, 64], in_channels=3, out_channels=3, down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"], up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"], latent_channels=4, ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTextConfig( bos_token_id=0, eos_token_id=2, hidden_size=32, intermediate_size=37, layer_norm_eps=1E-0_5, num_attention_heads=4, num_hidden_layers=5, pad_token_id=1, vocab_size=10_00, ) __SCREAMING_SNAKE_CASE = CLIPTextModel(_a ) __SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) __SCREAMING_SNAKE_CASE = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def __lowerCAmelCase ( self, _a, _a=0 ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 32, 32), rng=random.Random(_a ) ).to(_a ) __SCREAMING_SNAKE_CASE = image / 2 + 0.5 if str(_a ).startswith("mps" ): __SCREAMING_SNAKE_CASE = torch.manual_seed(_a ) else: __SCREAMING_SNAKE_CASE = torch.Generator(device=_a ).manual_seed(_a ) __SCREAMING_SNAKE_CASE = { "prompt": "An astronaut riding an elephant", "source_prompt": "An astronaut riding a horse", "image": image, "generator": generator, "num_inference_steps": 2, "eta": 0.1, "strength": 0.8, "guidance_scale": 3, "source_guidance_scale": 1, "output_type": "numpy", } return inputs def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = "cpu" # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE = self.get_dummy_components() __SCREAMING_SNAKE_CASE = CycleDiffusionPipeline(**_a ) __SCREAMING_SNAKE_CASE = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_a ) __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) __SCREAMING_SNAKE_CASE = np.array([0.4459, 0.4943, 0.4544, 0.6643, 0.5474, 0.4327, 0.5701, 0.5959, 0.5179] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf(torch_device != "cuda", "This test requires a GPU" ) def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = self.get_dummy_components() for name, module in components.items(): if hasattr(_a, "half" ): __SCREAMING_SNAKE_CASE = module.half() __SCREAMING_SNAKE_CASE = CycleDiffusionPipeline(**_a ) __SCREAMING_SNAKE_CASE = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_a ) __SCREAMING_SNAKE_CASE = pipe(**_a ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) __SCREAMING_SNAKE_CASE = np.array([0.3506, 0.4543, 0.446, 0.4575, 0.5195, 0.4155, 0.5273, 0.518, 0.4116] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def __lowerCAmelCase ( self ) -> Dict: return super().test_save_load_local() @unittest.skip("non-deterministic pipeline" ) def __lowerCAmelCase ( self ) -> Union[str, Any]: return super().test_inference_batch_single_identical() @skip_mps def __lowerCAmelCase ( self ) -> Any: return super().test_dict_tuple_outputs_equivalent() @skip_mps def __lowerCAmelCase ( self ) -> Any: return super().test_save_load_optional_components() @skip_mps def __lowerCAmelCase ( self ) -> List[str]: return super().test_attention_slicing_forward_pass() @slow @require_torch_gpu class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/cycle-diffusion/black_colored_car.png" ) __SCREAMING_SNAKE_CASE = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy" ) __SCREAMING_SNAKE_CASE = init_image.resize((5_12, 5_12) ) __SCREAMING_SNAKE_CASE = "CompVis/stable-diffusion-v1-4" __SCREAMING_SNAKE_CASE = DDIMScheduler.from_pretrained(_a, subfolder="scheduler" ) __SCREAMING_SNAKE_CASE = CycleDiffusionPipeline.from_pretrained( _a, scheduler=_a, safety_checker=_a, torch_dtype=torch.floataa, revision="fp16" ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __SCREAMING_SNAKE_CASE = "A black colored car" __SCREAMING_SNAKE_CASE = "A blue colored car" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( prompt=_a, source_prompt=_a, image=_a, num_inference_steps=1_00, eta=0.1, strength=0.85, guidance_scale=3, source_guidance_scale=1, generator=_a, output_type="np", ) __SCREAMING_SNAKE_CASE = output.images # the values aren't exactly equal, but the images look the same visually assert np.abs(image - expected_image ).max() < 5E-1 def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/cycle-diffusion/black_colored_car.png" ) __SCREAMING_SNAKE_CASE = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy" ) __SCREAMING_SNAKE_CASE = init_image.resize((5_12, 5_12) ) __SCREAMING_SNAKE_CASE = "CompVis/stable-diffusion-v1-4" __SCREAMING_SNAKE_CASE = DDIMScheduler.from_pretrained(_a, subfolder="scheduler" ) __SCREAMING_SNAKE_CASE = CycleDiffusionPipeline.from_pretrained(_a, scheduler=_a, safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __SCREAMING_SNAKE_CASE = "A black colored car" __SCREAMING_SNAKE_CASE = "A blue colored car" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( prompt=_a, source_prompt=_a, image=_a, num_inference_steps=1_00, eta=0.1, strength=0.85, guidance_scale=3, source_guidance_scale=1, generator=_a, output_type="np", ) __SCREAMING_SNAKE_CASE = output.images assert np.abs(image - expected_image ).max() < 2E-2
693
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
1
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> Any: __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None def __repr__( self ) -> str: return f'''Node({self.data})''' class __SCREAMING_SNAKE_CASE : def __init__( self ) -> Tuple: __SCREAMING_SNAKE_CASE = None def __iter__( self ) -> Any: __SCREAMING_SNAKE_CASE = self.head while node: yield node.data __SCREAMING_SNAKE_CASE = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_a ) for item in self] ) def __getitem__( self, _a ) -> Any: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self, _a, _a ) -> None: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) __SCREAMING_SNAKE_CASE = self.head for _ in range(_a ): __SCREAMING_SNAKE_CASE = current.next __SCREAMING_SNAKE_CASE = data def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(len(self ), _a ) def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(0, _a ) def __lowerCAmelCase ( self, _a, _a ) -> None: if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) __SCREAMING_SNAKE_CASE = Node(_a ) if self.head is None: __SCREAMING_SNAKE_CASE = new_node elif index == 0: __SCREAMING_SNAKE_CASE = self.head # link new_node to head __SCREAMING_SNAKE_CASE = new_node else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self ) -> None: # print every node data print(self ) def __lowerCAmelCase ( self ) -> Any: return self.delete_nth(0 ) def __lowerCAmelCase ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowerCAmelCase ( self, _a = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) __SCREAMING_SNAKE_CASE = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE = self.head.next else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next.next return delete_node.data def __lowerCAmelCase ( self ) -> bool: return self.head is None def __lowerCAmelCase ( self ) -> None: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE = prev def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = LinkedList() assert linked_list.is_empty() is True assert str(__snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(__snake_case ) == i linked_list.insert_nth(__snake_case , i + 1 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(__snake_case ) == 9 assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(-8 , 1 ) ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_9_2.5_5_5_5_5, "Hello, world!", 7_7.9, Node(10 ), None, None, 1_2.2_0, ] __SCREAMING_SNAKE_CASE = LinkedList() for i in test_input: linked_list.insert_tail(__snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(__snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE = linked_list.delete_head() assert result == -9 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE = linked_list.delete_tail() assert result == 1_2.2 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE = linked_list.delete_nth(10 ) assert result is None assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(__snake_case ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(__snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _A ( ) -> Union[str, Any]: """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(__snake_case ) print("\nReading/changing Node data using indexing:" ) print(f'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE = input("Enter New Value: " ).strip() print("New list:" ) print(__snake_case ) print(f'''length of linked_list is : {len(__snake_case )}''' ) if __name__ == "__main__": main()
693
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _snake_case : int = logging.get_logger(__name__) _snake_case : Optional[int] = { 'microsoft/swin-tiny-patch4-window7-224': ( 'https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json' ), # See all Swin models at https://huggingface.co/models?filter=swin } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""swin""" SCREAMING_SNAKE_CASE__ ={ """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self, _a=2_24, _a=4, _a=3, _a=96, _a=[2, 2, 6, 2], _a=[3, 6, 12, 24], _a=7, _a=4.0, _a=True, _a=0.0, _a=0.0, _a=0.1, _a="gelu", _a=False, _a=0.02, _a=1E-5, _a=32, _a=None, _a=None, **_a, ) -> Any: super().__init__(**_a ) __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = embed_dim __SCREAMING_SNAKE_CASE = depths __SCREAMING_SNAKE_CASE = len(_a ) __SCREAMING_SNAKE_CASE = num_heads __SCREAMING_SNAKE_CASE = window_size __SCREAMING_SNAKE_CASE = mlp_ratio __SCREAMING_SNAKE_CASE = qkv_bias __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = drop_path_rate __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = use_absolute_embeddings __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = encoder_stride # 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 __SCREAMING_SNAKE_CASE = int(embed_dim * 2 ** (len(_a ) - 1) ) __SCREAMING_SNAKE_CASE = ["stem"] + [f'''stage{idx}''' for idx in range(1, len(_a ) + 1 )] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = get_aligned_output_features_output_indices( out_features=_a, out_indices=_a, stage_names=self.stage_names ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =version.parse("""1.11""" ) @property def __lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __lowerCAmelCase ( self ) -> float: return 1E-4
693
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
1
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. _snake_case : int = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. _snake_case : str = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. _snake_case : Dict = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def _A ( __snake_case :str , __snake_case :str ) -> tuple[str, float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len([g for position, g in enumerate(__snake_case ) if g == main_target[position]] ) return (item, float(__snake_case )) def _A ( __snake_case :str , __snake_case :str ) -> tuple[str, str]: """simple docstring""" __SCREAMING_SNAKE_CASE = random.randint(0 , len(__snake_case ) - 1 ) __SCREAMING_SNAKE_CASE = parent_a[:random_slice] + parent_a[random_slice:] __SCREAMING_SNAKE_CASE = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def _A ( __snake_case :str , __snake_case :list[str] ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = list(__snake_case ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: __SCREAMING_SNAKE_CASE = random.choice(__snake_case ) return "".join(__snake_case ) def _A ( __snake_case :tuple[str, float] , __snake_case :list[tuple[str, float]] , __snake_case :list[str] , ) -> list[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] # Generate more children proportionally to the fitness score. __SCREAMING_SNAKE_CASE = int(parent_a[1] * 100 ) + 1 __SCREAMING_SNAKE_CASE = 10 if child_n >= 10 else child_n for _ in range(__snake_case ): __SCREAMING_SNAKE_CASE = population_score[random.randint(0 , __snake_case )][0] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = crossover(parent_a[0] , __snake_case ) # Append new string to the population list. pop.append(mutate(__snake_case , __snake_case ) ) pop.append(mutate(__snake_case , __snake_case ) ) return pop def _A ( __snake_case :str , __snake_case :list[str] , __snake_case :bool = True ) -> tuple[int, int, str]: """simple docstring""" if N_POPULATION < N_SELECTED: __SCREAMING_SNAKE_CASE = f'''{N_POPULATION} must be bigger than {N_SELECTED}''' raise ValueError(__snake_case ) # Verify that the target contains no genes besides the ones inside genes variable. __SCREAMING_SNAKE_CASE = sorted({c for c in target if c not in genes} ) if not_in_genes_list: __SCREAMING_SNAKE_CASE = f'''{not_in_genes_list} is not in genes list, evolution cannot converge''' raise ValueError(__snake_case ) # Generate random starting population. __SCREAMING_SNAKE_CASE = [] for _ in range(__snake_case ): population.append("".join([random.choice(__snake_case ) for i in range(len(__snake_case ) )] ) ) # Just some logs to know what the algorithms is doing. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(__snake_case ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. __SCREAMING_SNAKE_CASE = [evaluate(__snake_case , __snake_case ) for item in population] # Check if there is a matching evolution. __SCREAMING_SNAKE_CASE = sorted(__snake_case , key=lambda __snake_case : x[1] , reverse=__snake_case ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( f'''\nGeneration: {generation}''' f'''\nTotal Population:{total_population}''' f'''\nBest score: {population_score[0][1]}''' f'''\nBest string: {population_score[0][0]}''' ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. __SCREAMING_SNAKE_CASE = population[: int(N_POPULATION / 3 )] population.clear() population.extend(__snake_case ) # Normalize population score to be between 0 and 1. __SCREAMING_SNAKE_CASE = [ (item, score / len(__snake_case )) for item, score in population_score ] # This is selection for i in range(__snake_case ): population.extend(select(population_score[int(__snake_case )] , __snake_case , __snake_case ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(__snake_case ) > N_POPULATION: break if __name__ == "__main__": _snake_case : Dict = ( 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!' ) _snake_case : int = list( ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm' 'nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\' ) _snake_case , _snake_case , _snake_case : int = basic(target_str, genes_list) print( F"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}""" )
693
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def _A ( __snake_case :Tuple ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = [ "decoder.version", "decoder.output_projection.weight", "_float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(__snake_case , __snake_case ) def _A ( __snake_case :Optional[Any] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = emb.weight.shape __SCREAMING_SNAKE_CASE = nn.Linear(__snake_case , __snake_case , bias=__snake_case ) __SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def _A ( __snake_case :Dict ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) __SCREAMING_SNAKE_CASE = Namespace(**checkpoint["cfg"]["model"] ) __SCREAMING_SNAKE_CASE = checkpoint["model"] remove_ignore_keys_(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict["decoder.embed_tokens.weight"].shape[0] __SCREAMING_SNAKE_CASE = {key.replace("decoder" , "model" ): val for key, val in state_dict.items()} __SCREAMING_SNAKE_CASE = XGLMConfig( vocab_size=__snake_case , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="gelu" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) __SCREAMING_SNAKE_CASE = XGLMForCausalLM(__snake_case ) __SCREAMING_SNAKE_CASE = model.load_state_dict(__snake_case , strict=__snake_case ) print(__snake_case ) __SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": _snake_case : str = argparse.ArgumentParser() # Required parameters parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') _snake_case : int = parser.parse_args() _snake_case : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
693
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""image_processor""", """tokenizer"""] SCREAMING_SNAKE_CASE__ ="""ChineseCLIPImageProcessor""" SCREAMING_SNAKE_CASE__ =("""BertTokenizer""", """BertTokenizerFast""") def __init__( self, _a=None, _a=None, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead.", _a, ) __SCREAMING_SNAKE_CASE = kwargs.pop("feature_extractor" ) __SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(_a, _a ) __SCREAMING_SNAKE_CASE = self.image_processor def __call__( self, _a=None, _a=None, _a=None, **_a ) -> int: if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: __SCREAMING_SNAKE_CASE = self.tokenizer(_a, return_tensors=_a, **_a ) if images is not None: __SCREAMING_SNAKE_CASE = self.image_processor(_a, return_tensors=_a, **_a ) if text is not None and images is not None: __SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_a ), tensor_type=_a ) def __lowerCAmelCase ( self, *_a, **_a ) -> str: return self.tokenizer.batch_decode(*_a, **_a ) def __lowerCAmelCase ( self, *_a, **_a ) -> Dict: return self.tokenizer.decode(*_a, **_a ) @property def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names __SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self ) -> Optional[int]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.", _a, ) return self.image_processor_class
693
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
1
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @require_tf def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf" ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(_a ), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @slow @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
693
import random from .binary_exp_mod import bin_exp_mod def _A ( __snake_case :List[Any] , __snake_case :Union[str, Any]=1000 ) -> int: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __SCREAMING_SNAKE_CASE = n - 1 __SCREAMING_SNAKE_CASE = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __SCREAMING_SNAKE_CASE = 0 while count < prec: __SCREAMING_SNAKE_CASE = random.randint(2 , n - 1 ) __SCREAMING_SNAKE_CASE = bin_exp_mod(__snake_case , __snake_case , __snake_case ) if b != 1: __SCREAMING_SNAKE_CASE = True for _ in range(__snake_case ): if b == n - 1: __SCREAMING_SNAKE_CASE = False break __SCREAMING_SNAKE_CASE = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _snake_case : int = 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)))
693
1
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": _snake_case : List[Any] = '%20'.join(argv[1:]) if len(argv) > 1 else quote(str(input('Search: '))) print('Googling.....') _snake_case : List[Any] = F"""https://www.google.com/search?q={query}&num=100""" _snake_case : int = requests.get( url, headers={'User-Agent': str(UserAgent().random)}, ) try: _snake_case : List[Any] = ( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'yuRUbf'}) .find('a') .get('href') ) except AttributeError: _snake_case : List[Any] = parse_qs( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'kCrYT'}) .find('a') .get('href') )['url'][0] webbrowser.open(link)
693
import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int ) -> np.ndarray: """simple docstring""" if (ksize % 2) == 0: __SCREAMING_SNAKE_CASE = ksize + 1 __SCREAMING_SNAKE_CASE = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(__snake_case ): for x in range(__snake_case ): # distance from center __SCREAMING_SNAKE_CASE = x - ksize // 2 __SCREAMING_SNAKE_CASE = y - ksize // 2 # degree to radiant __SCREAMING_SNAKE_CASE = theta / 180 * np.pi __SCREAMING_SNAKE_CASE = np.cos(_theta ) __SCREAMING_SNAKE_CASE = np.sin(_theta ) # get kernel x __SCREAMING_SNAKE_CASE = cos_theta * px + sin_theta * py # get kernel y __SCREAMING_SNAKE_CASE = -sin_theta * px + cos_theta * py # fill kernel __SCREAMING_SNAKE_CASE = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _snake_case : Union[str, Any] = imread('../image_data/lena.jpg') # turn image in gray scale value _snake_case : List[str] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _snake_case : int = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 1_20, 1_50]: _snake_case : List[str] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _snake_case : Optional[Any] = out / out.max() * 2_55 _snake_case : Union[str, Any] = out.astype(np.uinta) imshow('Original', gray) imshow('Gabor filter with 20x20 mask and 6 directions', out) waitKey(0)
693
1
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""image_processor""", """tokenizer"""] SCREAMING_SNAKE_CASE__ ="""BlipImageProcessor""" SCREAMING_SNAKE_CASE__ =("""BertTokenizer""", """BertTokenizerFast""") def __init__( self, _a, _a ) -> Tuple: __SCREAMING_SNAKE_CASE = False super().__init__(_a, _a ) __SCREAMING_SNAKE_CASE = self.image_processor def __call__( self, _a = None, _a = None, _a = True, _a = False, _a = None, _a = None, _a = 0, _a = None, _a = None, _a = False, _a = False, _a = False, _a = False, _a = False, _a = True, _a = None, **_a, ) -> BatchEncoding: if images is None and text is None: raise ValueError("You have to specify either images or text." ) # Get only text if images is None: __SCREAMING_SNAKE_CASE = self.tokenizer __SCREAMING_SNAKE_CASE = self.tokenizer( text=_a, add_special_tokens=_a, padding=_a, truncation=_a, max_length=_a, stride=_a, pad_to_multiple_of=_a, return_attention_mask=_a, return_overflowing_tokens=_a, return_special_tokens_mask=_a, return_offsets_mapping=_a, return_token_type_ids=_a, return_length=_a, verbose=_a, return_tensors=_a, **_a, ) return text_encoding # add pixel_values __SCREAMING_SNAKE_CASE = self.image_processor(_a, return_tensors=_a ) if text is not None: __SCREAMING_SNAKE_CASE = self.tokenizer( text=_a, add_special_tokens=_a, padding=_a, truncation=_a, max_length=_a, stride=_a, pad_to_multiple_of=_a, return_attention_mask=_a, return_overflowing_tokens=_a, return_special_tokens_mask=_a, return_offsets_mapping=_a, return_token_type_ids=_a, return_length=_a, verbose=_a, return_tensors=_a, **_a, ) else: __SCREAMING_SNAKE_CASE = None if text_encoding is not None: encoding_image_processor.update(_a ) return encoding_image_processor def __lowerCAmelCase ( self, *_a, **_a ) -> Any: return self.tokenizer.batch_decode(*_a, **_a ) def __lowerCAmelCase ( self, *_a, **_a ) -> Optional[int]: return self.tokenizer.decode(*_a, **_a ) @property def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names __SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
693
def _A ( __snake_case :int ) -> int: """simple docstring""" assert isinstance(__snake_case , __snake_case ), f'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: __SCREAMING_SNAKE_CASE = f'''The input value of [n={number}] has to be > 0''' raise ValueError(__snake_case ) else: __SCREAMING_SNAKE_CASE = sylvester(number - 1 ) __SCREAMING_SNAKE_CASE = num - 1 __SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
693
1
from __future__ import annotations import unittest from transformers import FunnelConfig, is_tf_available from transformers.testing_utils import require_tf from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, ) class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a=13, _a=7, _a=True, _a=True, _a=True, _a=True, _a=99, _a=[1, 1, 2], _a=1, _a=32, _a=4, _a=8, _a=37, _a="gelu_new", _a=0.1, _a=0.1, _a=0.0, _a=5_12, _a=3, _a=0.02, _a=3, _a=4, _a=None, _a=False, ) -> str: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_input_mask __SCREAMING_SNAKE_CASE = use_token_type_ids __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = block_sizes __SCREAMING_SNAKE_CASE = num_decoder_layers __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = n_head __SCREAMING_SNAKE_CASE = d_head __SCREAMING_SNAKE_CASE = d_inner __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = num_labels __SCREAMING_SNAKE_CASE = num_choices __SCREAMING_SNAKE_CASE = scope __SCREAMING_SNAKE_CASE = initializer_std # Used in the tests to check the size of the first attention layer __SCREAMING_SNAKE_CASE = n_head # Used in the tests to check the size of the first hidden state __SCREAMING_SNAKE_CASE = self.d_model # Used in the tests to check the number of output hidden states/attentions __SCREAMING_SNAKE_CASE = sum(self.block_sizes ) + (0 if base else self.num_decoder_layers) # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with # the last hidden state of the first block (which is the first hidden state of the decoder). if not base: __SCREAMING_SNAKE_CASE = self.num_hidden_layers + 2 def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) __SCREAMING_SNAKE_CASE = None if self.use_input_mask: __SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) __SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None if self.use_labels: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size], self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size], self.num_choices ) __SCREAMING_SNAKE_CASE = FunnelConfig( vocab_size=self.vocab_size, block_sizes=self.block_sizes, num_decoder_layers=self.num_decoder_layers, d_model=self.d_model, n_head=self.n_head, d_head=self.d_head, d_inner=self.d_inner, hidden_act=self.hidden_act, hidden_dropout=self.hidden_dropout, attention_dropout=self.attention_dropout, activation_dropout=self.activation_dropout, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_std=self.initializer_std, ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = TFFunnelModel(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = [input_ids, input_mask] __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.d_model) ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = TFFunnelModel(config=_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.d_model) ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = TFFunnelModel(config=_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.d_model) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> str: __SCREAMING_SNAKE_CASE = TFFunnelBaseModel(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = [input_ids, input_mask] __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, 2, self.d_model) ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = TFFunnelBaseModel(config=_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, 3, self.d_model) ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = TFFunnelBaseModel(config=_a ) __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, 2, self.d_model) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> str: __SCREAMING_SNAKE_CASE = TFFunnelForPreTraining(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = TFFunnelForMaskedLM(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> str: __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = TFFunnelForSequenceClassification(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.num_choices __SCREAMING_SNAKE_CASE = TFFunnelForMultipleChoice(config=_a ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(_a, 1 ), (1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(_a, 1 ), (1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(_a, 1 ), (1, self.num_choices, 1) ) __SCREAMING_SNAKE_CASE = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> Dict: __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = TFFunnelForTokenClassification(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_a ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, _a, ) -> List[str]: __SCREAMING_SNAKE_CASE = TFFunnelForQuestionAnswering(config=_a ) __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __SCREAMING_SNAKE_CASE = model(_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 __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( ( TFFunnelModel, TFFunnelForMaskedLM, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForTokenClassification, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE__ =( { """feature-extraction""": (TFFunnelBaseModel, TFFunnelModel), """fill-mask""": TFFunnelForMaskedLM, """question-answering""": TFFunnelForQuestionAnswering, """text-classification""": TFFunnelForSequenceClassification, """token-classification""": TFFunnelForTokenClassification, """zero-shot""": TFFunnelForSequenceClassification, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = TFFunnelModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self, config_class=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_a ) def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_a ) @require_tf class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else () ) SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = TFFunnelModelTester(self, base=_a ) __SCREAMING_SNAKE_CASE = ConfigTester(self, config_class=_a ) def __lowerCAmelCase ( self ) -> str: self.config_tester.run_common_tests() def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_base_model(*_a ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_a ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_a )
693
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @require_tf def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf" ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(_a ), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @slow @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
693
1
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
from __future__ import annotations import math def _A ( __snake_case :int , __snake_case :int , __snake_case :bool , __snake_case :list[int] , __snake_case :float ) -> int: """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__snake_case ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) return min( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [90, 23, 6, 33, 21, 65, 123, 3_4423] __SCREAMING_SNAKE_CASE = math.log(len(__snake_case ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , __snake_case , __snake_case , __snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
1
import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) _snake_case : Tuple = logging.getLogger() _snake_case : Dict = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __lowerCAmelCase ( self, _a ) -> Tuple: os.makedirs(_a, exist_ok=_a ) __SCREAMING_SNAKE_CASE = {"source": "What is love ?", "target": "life"} __SCREAMING_SNAKE_CASE = {"train": 12, "val": 2, "test": 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: __SCREAMING_SNAKE_CASE = "\n".join([contents[field]] * n_lines[split] ) with open(os.path.join(_a, f'''{split}.{field}''' ), "w" ) as f: f.write(_a ) def __lowerCAmelCase ( self, _a, _a = "pytorch" ) -> str: __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = os.path.join(_a, "output" ) __SCREAMING_SNAKE_CASE = os.path.join(_a, "data" ) self._create_dummy_data(data_dir=_a ) __SCREAMING_SNAKE_CASE = f''' --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ '''.split() if gpus > 0: testargs.append(f'''--gpus={gpus}''' ) if is_apex_available(): testargs.append("--fp16" ) else: testargs.append("--gpus=0" ) testargs.append("--distributed_backend=ddp_cpu" ) testargs.append("--num_processes=2" ) __SCREAMING_SNAKE_CASE = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(_a, env=self.get_env() ) __SCREAMING_SNAKE_CASE = os.path.join(_a, "metrics.json" ) with open(_a ) as f: __SCREAMING_SNAKE_CASE = json.load(_a ) return result @require_torch_gpu def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result["test"][0]["test_avg_em"], 0.2 ) @require_torch_multi_gpu def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result["test"][0]["test_avg_em"], 0.2 ) @require_torch_gpu @require_ray def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self._run_finetune(gpus=1, distributed_retriever="ray" ) self.assertGreaterEqual(result["test"][0]["test_avg_em"], 0.2 ) @require_torch_multi_gpu @require_ray def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self._run_finetune(gpus=1, distributed_retriever="ray" ) self.assertGreaterEqual(result["test"][0]["test_avg_em"], 0.2 )
693
def _A ( __snake_case :bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def _A ( __snake_case :str ) -> bytes: """simple docstring""" if (len(__snake_case ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL _snake_case : Union[str, Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""pixel_values"""] def __init__( self, _a = True, _a = None, _a = PILImageResampling.BICUBIC, _a = True, _a = 1 / 2_55, _a = True, _a = None, _a = None, _a = True, **_a, ) -> None: super().__init__(**_a ) __SCREAMING_SNAKE_CASE = size if size is not None else {"height": 3_84, "width": 3_84} __SCREAMING_SNAKE_CASE = get_size_dict(_a, default_to_square=_a ) __SCREAMING_SNAKE_CASE = do_resize __SCREAMING_SNAKE_CASE = size __SCREAMING_SNAKE_CASE = resample __SCREAMING_SNAKE_CASE = do_rescale __SCREAMING_SNAKE_CASE = rescale_factor __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else OPENAI_CLIP_MEAN __SCREAMING_SNAKE_CASE = image_std if image_std is not None else OPENAI_CLIP_STD __SCREAMING_SNAKE_CASE = do_convert_rgb def __lowerCAmelCase ( self, _a, _a, _a = PILImageResampling.BICUBIC, _a = None, **_a, ) -> np.ndarray: __SCREAMING_SNAKE_CASE = get_size_dict(_a, default_to_square=_a ) if "height" not in size or "width" not in size: raise ValueError(f'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}''' ) __SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_a, size=_a, resample=_a, data_format=_a, **_a ) def __lowerCAmelCase ( self, _a, _a, _a = None, **_a, ) -> List[str]: return rescale(_a, scale=_a, data_format=_a, **_a ) def __lowerCAmelCase ( self, _a, _a, _a, _a = None, **_a, ) -> np.ndarray: return normalize(_a, mean=_a, std=_a, data_format=_a, **_a ) def __lowerCAmelCase ( self, _a, _a = None, _a = None, _a = None, _a = None, _a = None, _a = None, _a = None, _a = None, _a = None, _a = None, _a = ChannelDimension.FIRST, **_a, ) -> PIL.Image.Image: __SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize __SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample __SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale __SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor __SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize __SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean __SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std __SCREAMING_SNAKE_CASE = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __SCREAMING_SNAKE_CASE = size if size is not None else self.size __SCREAMING_SNAKE_CASE = get_size_dict(_a, default_to_square=_a ) __SCREAMING_SNAKE_CASE = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # PIL RGBA images are converted to RGB if do_convert_rgb: __SCREAMING_SNAKE_CASE = [convert_to_rgb(_a ) for image in images] # All transformations expect numpy arrays. __SCREAMING_SNAKE_CASE = [to_numpy_array(_a ) for image in images] if do_resize: __SCREAMING_SNAKE_CASE = [self.resize(image=_a, size=_a, resample=_a ) for image in images] if do_rescale: __SCREAMING_SNAKE_CASE = [self.rescale(image=_a, scale=_a ) for image in images] if do_normalize: __SCREAMING_SNAKE_CASE = [self.normalize(image=_a, mean=_a, std=_a ) for image in images] __SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_a, _a ) for image in images] __SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images}, tensor_type=_a ) return encoded_outputs
693
from functools import lru_cache def _A ( __snake_case :int ) -> set: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(__snake_case ) if n > 1: factors.add(__snake_case ) return factors @lru_cache def _A ( __snake_case :int ) -> int: """simple docstring""" return len(unique_prime_factors(__snake_case ) ) def _A ( __snake_case :list ) -> bool: """simple docstring""" return len(set(__snake_case ) ) in (0, 1) def _A ( __snake_case :int ) -> list: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 while True: # Increment each value of a generated range __SCREAMING_SNAKE_CASE = [base + i for i in range(__snake_case )] # Run elements through out unique_prime_factors function # Append our target number to the end. __SCREAMING_SNAKE_CASE = [upf_len(__snake_case ) for x in group] checker.append(__snake_case ) # If all numbers in the list are equal, return the group variable. if equality(__snake_case ): return group # Increment our base variable by 1 base += 1 def _A ( __snake_case :int = 4 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = run(__snake_case ) return results[0] if len(__snake_case ) else None if __name__ == "__main__": print(solution())
693
1
import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset _snake_case : List[str] = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self, _a ) -> Dict: super().__init__() __SCREAMING_SNAKE_CASE = torchvision.models.resnetaaa(pretrained=_a ) __SCREAMING_SNAKE_CASE = list(model.children() )[:-2] __SCREAMING_SNAKE_CASE = nn.Sequential(*_a ) __SCREAMING_SNAKE_CASE = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def __lowerCAmelCase ( self, _a ) -> Optional[int]: # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 __SCREAMING_SNAKE_CASE = self.pool(self.model(_a ) ) __SCREAMING_SNAKE_CASE = torch.flatten(_a, start_dim=2 ) __SCREAMING_SNAKE_CASE = out.transpose(1, 2 ).contiguous() return out # BxNx2048 class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, _a, _a, _a, _a, _a ) -> Optional[int]: __SCREAMING_SNAKE_CASE = [json.loads(_a ) for l in open(_a )] __SCREAMING_SNAKE_CASE = os.path.dirname(_a ) __SCREAMING_SNAKE_CASE = tokenizer __SCREAMING_SNAKE_CASE = labels __SCREAMING_SNAKE_CASE = len(_a ) __SCREAMING_SNAKE_CASE = max_seq_length __SCREAMING_SNAKE_CASE = transforms def __len__( self ) -> List[str]: return len(self.data ) def __getitem__( self, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = torch.LongTensor(self.tokenizer.encode(self.data[index]["text"], add_special_tokens=_a ) ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sentence[0], sentence[1:-1], sentence[-1] __SCREAMING_SNAKE_CASE = sentence[: self.max_seq_length] __SCREAMING_SNAKE_CASE = torch.zeros(self.n_classes ) __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = Image.open(os.path.join(self.data_dir, self.data[index]["img"] ) ).convert("RGB" ) __SCREAMING_SNAKE_CASE = self.transforms(_a ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = Counter() for row in self.data: label_freqs.update(row["label"] ) return label_freqs def _A ( __snake_case :str ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = [len(row["sentence"] ) for row in batch] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = len(__snake_case ), max(__snake_case ) __SCREAMING_SNAKE_CASE = torch.zeros(__snake_case , __snake_case , dtype=torch.long ) __SCREAMING_SNAKE_CASE = torch.zeros(__snake_case , __snake_case , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(__snake_case , __snake_case ) ): __SCREAMING_SNAKE_CASE = input_row["sentence"] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = torch.stack([row["image"] for row in batch] ) __SCREAMING_SNAKE_CASE = torch.stack([row["label"] for row in batch] ) __SCREAMING_SNAKE_CASE = torch.stack([row["image_start_token"] for row in batch] ) __SCREAMING_SNAKE_CASE = torch.stack([row["image_end_token"] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def _A ( ) -> Optional[Any]: """simple docstring""" return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def _A ( ) -> Optional[Any]: """simple docstring""" return transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize( mean=[0.4_6_7_7_7_0_4_4, 0.4_4_5_3_1_4_2_9, 0.4_0_6_6_1_0_1_7] , std=[0.1_2_2_2_1_9_9_4, 0.1_2_1_4_5_8_3_5, 0.1_4_3_8_0_4_6_9] , ), ] )
693
import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _A ( __snake_case :Dict ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = VideoMAEConfig() set_architecture_configs(__snake_case , __snake_case ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = False if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = "huggingface/label-files" if "kinetics" in model_name: __SCREAMING_SNAKE_CASE = 400 __SCREAMING_SNAKE_CASE = "kinetics400-id2label.json" elif "ssv2" in model_name: __SCREAMING_SNAKE_CASE = 174 __SCREAMING_SNAKE_CASE = "something-something-v2-id2label.json" else: raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned." ) __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type="dataset" ) , "r" ) ) __SCREAMING_SNAKE_CASE = {int(__snake_case ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def _A ( __snake_case :Dict , __snake_case :Optional[Any] ) -> List[Any]: """simple docstring""" if "small" in model_name: __SCREAMING_SNAKE_CASE = 384 __SCREAMING_SNAKE_CASE = 1536 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 192 __SCREAMING_SNAKE_CASE = 768 elif "large" in model_name: __SCREAMING_SNAKE_CASE = 1024 __SCREAMING_SNAKE_CASE = 4096 __SCREAMING_SNAKE_CASE = 24 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 512 __SCREAMING_SNAKE_CASE = 2048 elif "huge" in model_name: __SCREAMING_SNAKE_CASE = 1280 __SCREAMING_SNAKE_CASE = 5120 __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 640 __SCREAMING_SNAKE_CASE = 2560 elif "base" not in model_name: raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\"" ) def _A ( __snake_case :List[Any] ) -> Optional[int]: """simple docstring""" if "encoder." in name: __SCREAMING_SNAKE_CASE = name.replace("encoder." , "" ) if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("cls_token" , "videomae.embeddings.cls_token" ) if "decoder_pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pos_embed" , "decoder.decoder_pos_embed" ) if "pos_embed" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("pos_embed" , "videomae.embeddings.position_embeddings" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.proj" , "videomae.embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.norm" , "videomae.embeddings.norm" ) if "decoder.blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder.blocks" , "decoder.decoder_layers" ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("blocks" , "videomae.encoder.layer" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name and "bias" not in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.self" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.attention" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc2" , "output.dense" ) if "decoder_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_embed" , "decoder.decoder_embed" ) if "decoder_norm" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_norm" , "decoder.decoder_norm" ) if "decoder_pred" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pred" , "decoder.decoder_pred" ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.weight" , "videomae.layernorm.weight" ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.bias" , "videomae.layernorm.bias" ) if "head" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("head" , "classifier" ) return name def _A ( __snake_case :Union[str, Any] , __snake_case :Optional[int] ) -> Optional[Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__snake_case ) if key.startswith("encoder." ): __SCREAMING_SNAKE_CASE = key.replace("encoder." , "" ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split("." ) if key.startswith("decoder.blocks" ): __SCREAMING_SNAKE_CASE = config.decoder_hidden_size __SCREAMING_SNAKE_CASE = int(key_split[2] ) __SCREAMING_SNAKE_CASE = "decoder.decoder_layers." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = config.hidden_size __SCREAMING_SNAKE_CASE = int(key_split[1] ) __SCREAMING_SNAKE_CASE = "videomae.encoder.layer." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def _A ( ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) __SCREAMING_SNAKE_CASE = np.load(__snake_case ) return list(__snake_case ) def _A ( __snake_case :Optional[int] , __snake_case :List[str] , __snake_case :Union[str, Any] , __snake_case :Optional[Any] ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = get_videomae_config(__snake_case ) if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = VideoMAEForVideoClassification(__snake_case ) else: __SCREAMING_SNAKE_CASE = VideoMAEForPreTraining(__snake_case ) # download original checkpoint, hosted on Google Drive __SCREAMING_SNAKE_CASE = "pytorch_model.bin" gdown.cached_download(__snake_case , __snake_case , quiet=__snake_case ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) if "model" in files: __SCREAMING_SNAKE_CASE = files["model"] else: __SCREAMING_SNAKE_CASE = files["module"] __SCREAMING_SNAKE_CASE = convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) model.eval() # verify model on basic input __SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) __SCREAMING_SNAKE_CASE = prepare_video() __SCREAMING_SNAKE_CASE = image_processor(__snake_case , return_tensors="pt" ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case ) __SCREAMING_SNAKE_CASE = model(**__snake_case ) __SCREAMING_SNAKE_CASE = outputs.logits __SCREAMING_SNAKE_CASE = [ "videomae-small-finetuned-kinetics", "videomae-small-finetuned-ssv2", # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) "videomae-base-short", "videomae-base-short-finetuned-kinetics", "videomae-base", "videomae-base-finetuned-kinetics", "videomae-large", "videomae-large-finetuned-kinetics", "videomae-huge-finetuned-kinetics", # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) "videomae-base-short-ssv2", "videomae-base-short-finetuned-ssv2", "videomae-base-ssv2", "videomae-base-finetuned-ssv2", ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.9_2_9_1, -0.4_0_6_1, -0.9_3_0_7] ) elif model_name == "videomae-small-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_7_1, -0.4_6_8_9, -0.8_2_3_5] ) elif model_name == "videomae-base": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_7_3_9, 0.7_9_6_8, 0.7_0_8_9], [0.6_7_0_1, 0.7_4_8_7, 0.6_2_0_9], [0.4_2_8_7, 0.5_1_5_8, 0.4_7_7_3]] ) elif model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ) # we verified the loss both for normalized and unnormalized targets for this one __SCREAMING_SNAKE_CASE = torch.tensor([0.5_1_4_2] ) if config.norm_pix_loss else torch.tensor([0.6_4_6_9] ) elif model_name == "videomae-large": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_1_4_9, 0.7_9_9_7, 0.6_9_6_6], [0.6_7_6_8, 0.7_8_6_9, 0.6_9_4_8], [0.5_1_3_9, 0.6_2_2_1, 0.5_6_0_5]] ) elif model_name == "videomae-large-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.0_7_7_1, 0.0_0_1_1, -0.3_6_2_5] ) elif model_name == "videomae-huge-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_4_3_3, 0.1_6_3_2, -0.4_8_9_4] ) elif model_name == "videomae-base-short-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.6_5_8_8, 0.0_9_9_0, -0.2_4_9_3] ) elif model_name == "videomae-base-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ) elif model_name == "videomae-base-short-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.4_7_1_2, 0.5_2_9_6, 0.5_7_8_6], [0.2_2_7_8, 0.2_7_2_9, 0.4_0_2_6], [0.0_3_5_2, 0.0_7_3_0, 0.2_5_0_6]] ) elif model_name == "videomae-base-short-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.0_5_3_7, -0.1_5_3_9, -0.3_2_6_6] ) elif model_name == "videomae-base-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.8_1_3_1, 0.8_7_2_7, 0.8_5_4_6], [0.7_3_6_6, 0.9_3_7_7, 0.8_8_7_0], [0.5_9_3_5, 0.8_8_7_4, 0.8_5_6_4]] ) elif model_name == "videomae-base-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.1_9_6_1, -0.8_3_3_7, -0.6_3_8_9] ) else: raise ValueError(f'''Model name not supported. Should be one of {model_names}''' ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) else: print("Logits:" , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , __snake_case , atol=1e-4 ) print("Logits ok!" ) # verify loss, if applicable if model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = outputs.loss assert torch.allclose(__snake_case , __snake_case , atol=1e-4 ) print("Loss ok!" ) if pytorch_dump_folder_path is not None: print(f'''Saving model and image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__snake_case ) model.save_pretrained(__snake_case ) if push_to_hub: print("Pushing to the hub..." ) model.push_to_hub(__snake_case , organization="nielsr" ) if __name__ == "__main__": _snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) _snake_case : Optional[int] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
693
1
import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : str = { 'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json', # See all DETR models at https://huggingface.co/models?filter=detr } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""detr""" SCREAMING_SNAKE_CASE__ =["""past_key_values"""] SCREAMING_SNAKE_CASE__ ={ """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self, _a=True, _a=None, _a=3, _a=1_00, _a=6, _a=20_48, _a=8, _a=6, _a=20_48, _a=8, _a=0.0, _a=0.0, _a=True, _a="relu", _a=2_56, _a=0.1, _a=0.0, _a=0.0, _a=0.02, _a=1.0, _a=False, _a="sine", _a="resnet50", _a=True, _a=False, _a=1, _a=5, _a=2, _a=1, _a=1, _a=5, _a=2, _a=0.1, **_a, ) -> List[str]: if backbone_config is not None and use_timm_backbone: raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`." ) if not use_timm_backbone: if backbone_config is None: logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone." ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(_a, _a ): __SCREAMING_SNAKE_CASE = backbone_config.get("model_type" ) __SCREAMING_SNAKE_CASE = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE = config_class.from_dict(_a ) # set timm attributes to None __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None, None, None __SCREAMING_SNAKE_CASE = use_timm_backbone __SCREAMING_SNAKE_CASE = backbone_config __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = num_queries __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = encoder_ffn_dim __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = encoder_attention_heads __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = init_xavier_std __SCREAMING_SNAKE_CASE = encoder_layerdrop __SCREAMING_SNAKE_CASE = decoder_layerdrop __SCREAMING_SNAKE_CASE = encoder_layers __SCREAMING_SNAKE_CASE = auxiliary_loss __SCREAMING_SNAKE_CASE = position_embedding_type __SCREAMING_SNAKE_CASE = backbone __SCREAMING_SNAKE_CASE = use_pretrained_backbone __SCREAMING_SNAKE_CASE = dilation # Hungarian matcher __SCREAMING_SNAKE_CASE = class_cost __SCREAMING_SNAKE_CASE = bbox_cost __SCREAMING_SNAKE_CASE = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE = mask_loss_coefficient __SCREAMING_SNAKE_CASE = dice_loss_coefficient __SCREAMING_SNAKE_CASE = bbox_loss_coefficient __SCREAMING_SNAKE_CASE = giou_loss_coefficient __SCREAMING_SNAKE_CASE = eos_coefficient super().__init__(is_encoder_decoder=_a, **_a ) @property def __lowerCAmelCase ( self ) -> int: return self.encoder_attention_heads @property def __lowerCAmelCase ( self ) -> int: return self.d_model @classmethod def __lowerCAmelCase ( cls, _a, **_a ) -> Any: return cls(backbone_config=_a, **_a ) def __lowerCAmelCase ( self ) -> Dict[str, any]: __SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: __SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE = self.__class__.model_type return output class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =version.parse("""1.11""" ) @property def __lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __lowerCAmelCase ( self ) -> float: return 1E-5 @property def __lowerCAmelCase ( self ) -> int: return 12
693
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, **_a ) -> None: warnings.warn( "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use CLIPImageProcessor instead.", _a, ) super().__init__(*_a, **_a )
693
1
import logging import os import sys from pathlib import Path from unittest.mock import patch from parameterized import parameterized from run_eval import run_generate from run_eval_search import run_search from transformers.testing_utils import CaptureStdout, TestCasePlus, slow from utils import ROUGE_KEYS logging.basicConfig(level=logging.DEBUG) _snake_case : List[Any] = logging.getLogger() def _A ( __snake_case :Path , __snake_case :list ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = "\n".join(__snake_case ) Path(__snake_case ).open("w" ).writelines(__snake_case ) _snake_case : str = 'patrickvonplaten/t5-tiny-random' _snake_case : Optional[Any] = 'sshleifer/bart-tiny-random' _snake_case : List[str] = 'sshleifer/tiny-mbart' _snake_case : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) logging.disable(logging.CRITICAL) # remove noisy download output from tracebacks class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __lowerCAmelCase ( self, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = Path(self.get_auto_remove_tmp_dir() ) / "utest_input.source" __SCREAMING_SNAKE_CASE = input_file_name.parent / "utest_output.txt" assert not output_file_name.exists() __SCREAMING_SNAKE_CASE = [" New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County."] _dump_articles(_a, _a ) __SCREAMING_SNAKE_CASE = str(Path(self.get_auto_remove_tmp_dir() ) / "scores.json" ) __SCREAMING_SNAKE_CASE = "translation_en_to_de" if model == T5_TINY else "summarization" __SCREAMING_SNAKE_CASE = f''' run_eval_search.py {model} {input_file_name} {output_file_name} --score_path {score_path} --task {task} --num_beams 2 --length_penalty 2.0 '''.split() with patch.object(_a, "argv", _a ): run_generate() assert Path(_a ).exists() # os.remove(Path(output_file_name)) def __lowerCAmelCase ( self ) -> Optional[Any]: self.run_eval_tester(_a ) @parameterized.expand([BART_TINY, MBART_TINY] ) @slow def __lowerCAmelCase ( self, _a ) -> str: self.run_eval_tester(_a ) @parameterized.expand([T5_TINY, MBART_TINY] ) @slow def __lowerCAmelCase ( self, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = Path(self.get_auto_remove_tmp_dir() ) / "utest_input.source" __SCREAMING_SNAKE_CASE = input_file_name.parent / "utest_output.txt" assert not output_file_name.exists() __SCREAMING_SNAKE_CASE = { "en": ["Machine learning is great, isn't it?", "I like to eat bananas", "Tomorrow is another great day!"], "de": [ "Maschinelles Lernen ist großartig, oder?", "Ich esse gerne Bananen", "Morgen ist wieder ein toller Tag!", ], } __SCREAMING_SNAKE_CASE = Path(self.get_auto_remove_tmp_dir() ) __SCREAMING_SNAKE_CASE = str(tmp_dir / "scores.json" ) __SCREAMING_SNAKE_CASE = str(tmp_dir / "val.target" ) _dump_articles(_a, text["en"] ) _dump_articles(_a, text["de"] ) __SCREAMING_SNAKE_CASE = "translation_en_to_de" if model == T5_TINY else "summarization" __SCREAMING_SNAKE_CASE = f''' run_eval_search.py {model} {str(_a )} {str(_a )} --score_path {score_path} --reference_path {reference_path} --task {task} '''.split() testargs.extend(["--search", "num_beams=1:2 length_penalty=0.9:1.0"] ) with patch.object(_a, "argv", _a ): with CaptureStdout() as cs: run_search() __SCREAMING_SNAKE_CASE = [" num_beams | length_penalty", model, "Best score args"] __SCREAMING_SNAKE_CASE = ["Info"] if "translation" in task: expected_strings.append("bleu" ) else: expected_strings.extend(_a ) for w in expected_strings: assert w in cs.out for w in un_expected_strings: assert w not in cs.out assert Path(_a ).exists() os.remove(Path(_a ) )
693
from math import sqrt def _A ( __snake_case :int ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 for i in range(1 , int(sqrt(__snake_case ) + 1 ) ): if n % i == 0 and i != sqrt(__snake_case ): total += i + n // i elif i == sqrt(__snake_case ): total += i return total - n def _A ( __snake_case :int = 1_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = sum( i for i in range(1 , __snake_case ) if sum_of_divisors(sum_of_divisors(__snake_case ) ) == i and sum_of_divisors(__snake_case ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
693
1
def _A ( __snake_case :int , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
def _A ( __snake_case :int , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
import os import tempfile import unittest import numpy as np from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline @require_flax class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: # pipeline has Flax weights __SCREAMING_SNAKE_CASE = FlaxDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=_a, cache_dir=_a ) __SCREAMING_SNAKE_CASE = [t[-1] for t in os.walk(os.path.join(_a, os.listdir(_a )[0], "snapshots" ) )] __SCREAMING_SNAKE_CASE = [item for sublist in all_root_files for item in sublist] # None of the downloaded files should be a PyTorch file even if we have some here: # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin assert not any(f.endswith(".bin" ) for f in files ) @slow @require_flax class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=_a ) __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.random.PRNGKey(0 ) __SCREAMING_SNAKE_CASE = 4 __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) # shard inputs and rng __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = jax.random.split(_a, _a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 64, 64, 3) if jax.device_count() == 8: assert np.abs(np.abs(images[0, 0, :2, :2, -2:], dtype=np.floataa ).sum() - 4.151_4745 ) < 1E-3 assert np.abs(np.abs(_a, dtype=np.floataa ).sum() - 4_9947.875 ) < 5E-1 __SCREAMING_SNAKE_CASE = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:] ) ) ) assert len(_a ) == num_samples def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="flax", safety_checker=_a ) __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.random.PRNGKey(0 ) __SCREAMING_SNAKE_CASE = 50 __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) # shard inputs and rng __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = jax.random.split(_a, _a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 5_12, 5_12, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.floataa ).sum() - 0.0565_2401) ) < 1E-3 assert np.abs((np.abs(_a, dtype=np.floataa ).sum() - 238_3808.2) ) < 5E-1 def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jnp.bfloataa, safety_checker=_a ) __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.random.PRNGKey(0 ) __SCREAMING_SNAKE_CASE = 50 __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) # shard inputs and rng __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = jax.random.split(_a, _a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 5_12, 5_12, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.floataa ).sum() - 0.0400_3906) ) < 1E-3 assert np.abs((np.abs(_a, dtype=np.floataa ).sum() - 237_3516.75) ) < 5E-1 def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jnp.bfloataa ) __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.random.PRNGKey(0 ) __SCREAMING_SNAKE_CASE = 50 __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) # shard inputs and rng __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = jax.random.split(_a, _a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 5_12, 5_12, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.floataa ).sum() - 0.0400_3906) ) < 1E-3 assert np.abs((np.abs(_a, dtype=np.floataa ).sum() - 237_3516.75) ) < 5E-1 def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = FlaxDDIMScheduler( beta_start=0.0_0085, beta_end=0.012, beta_schedule="scaled_linear", set_alpha_to_one=_a, steps_offset=1, ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jnp.bfloataa, scheduler=_a, safety_checker=_a, ) __SCREAMING_SNAKE_CASE = scheduler.create_state() __SCREAMING_SNAKE_CASE = scheduler_state __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.random.PRNGKey(0 ) __SCREAMING_SNAKE_CASE = 50 __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) # shard inputs and rng __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = jax.random.split(_a, _a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 5_12, 5_12, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.floataa ).sum() - 0.0_4504_3945) ) < 1E-3 assert np.abs((np.abs(_a, dtype=np.floataa ).sum() - 234_7693.5) ) < 5E-1 def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) __SCREAMING_SNAKE_CASE = jax.device_count() __SCREAMING_SNAKE_CASE = num_samples * [prompt] __SCREAMING_SNAKE_CASE = jax.random.split(jax.random.PRNGKey(0 ), _a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jnp.bfloataa, safety_checker=_a, ) __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, jit=_a ).images assert images.shape == (num_samples, 1, 5_12, 5_12, 3) __SCREAMING_SNAKE_CASE = images[2, 0, 2_56, 10:17, 1] # With memory efficient attention __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jnp.bfloataa, safety_checker=_a, use_memory_efficient_attention=_a, ) __SCREAMING_SNAKE_CASE = replicate(_a ) __SCREAMING_SNAKE_CASE = pipeline.prepare_inputs(_a ) __SCREAMING_SNAKE_CASE = shard(_a ) __SCREAMING_SNAKE_CASE = pipeline(_a, _a, _a, jit=_a ).images assert images_eff.shape == (num_samples, 1, 5_12, 5_12, 3) __SCREAMING_SNAKE_CASE = images[2, 0, 2_56, 10:17, 1] # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum` # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now. assert abs(slice_eff - slice ).max() < 1E-2
693
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a=99, _a=13, _a=7, _a=9, _a=True, _a=True, _a=False, _a=32, _a=5, _a=4, _a=37, _a=8, _a=0.1, _a=0.002, _a=1, _a=0, _a=0, _a=None, _a=None, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = encoder_seq_length __SCREAMING_SNAKE_CASE = decoder_seq_length # For common tests __SCREAMING_SNAKE_CASE = self.decoder_seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_attention_mask __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = d_ff __SCREAMING_SNAKE_CASE = relative_attention_num_buckets __SCREAMING_SNAKE_CASE = dropout_rate __SCREAMING_SNAKE_CASE = initializer_factor __SCREAMING_SNAKE_CASE = eos_token_id __SCREAMING_SNAKE_CASE = pad_token_id __SCREAMING_SNAKE_CASE = decoder_start_token_id __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = decoder_layers def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig.from_pretrained("google/umt5-base" ) def __lowerCAmelCase ( self, _a, _a, _a, _a=None, _a=None, _a=None, _a=None, _a=None, ) -> int: if attention_mask is None: __SCREAMING_SNAKE_CASE = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: __SCREAMING_SNAKE_CASE = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_hidden_layers, config.num_attention_heads, device=_a ) if decoder_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_decoder_layers, config.num_attention_heads, device=_a ) if cross_attn_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_attention_heads, device=_a ) 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, } def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.encoder_seq_length], self.vocab_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.decoder_seq_length], self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input __SCREAMING_SNAKE_CASE = input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = decoder_input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = self.get_config() __SCREAMING_SNAKE_CASE = config.num_attention_heads __SCREAMING_SNAKE_CASE = self.prepare_inputs_dict(_a, _a, _a ) return config, input_dict def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig( vocab_size=1_66, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self ) -> Union[str, Any]: return TaConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ) model.to(_a ) model.eval() __SCREAMING_SNAKE_CASE = model( input_ids=_a, decoder_input_ids=_a, attention_mask=_a, decoder_attention_mask=_a, ) __SCREAMING_SNAKE_CASE = model(input_ids=_a, decoder_input_ids=_a ) __SCREAMING_SNAKE_CASE = result.last_hidden_state __SCREAMING_SNAKE_CASE = result.past_key_values __SCREAMING_SNAKE_CASE = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size(), (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size(), (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_a ), config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ), 4 ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Tuple: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).get_decoder().to(_a ).eval() # first forward pass __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) self.parent.assertTrue(len(_a ) == len(_a ) ) self.parent.assertTrue(len(_a ) == len(_a ) + 1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 1), config.vocab_size ) # append to next input_ids and __SCREAMING_SNAKE_CASE = torch.cat([input_ids, next_tokens], dim=-1 ) __SCREAMING_SNAKE_CASE = model(_a )["last_hidden_state"] __SCREAMING_SNAKE_CASE = model(_a, past_key_values=_a )["last_hidden_state"] # select random slice __SCREAMING_SNAKE_CASE = ids_tensor((1,), output_from_past.shape[-1] ).item() __SCREAMING_SNAKE_CASE = output_from_no_past[:, -1, random_slice_idx].detach() __SCREAMING_SNAKE_CASE = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_a, _a, atol=1E-3 ) ) def __lowerCAmelCase ( self, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).to(_a ).half().eval() __SCREAMING_SNAKE_CASE = model(**_a )["last_hidden_state"] self.parent.assertFalse(torch.isnan(_a ).any().item() ) @require_torch class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ =(UMTaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ =( { """conversational""": UMTaForConditionalGeneration, """feature-extraction""": UMTaModel, """summarization""": UMTaForConditionalGeneration, """text2text-generation""": UMTaForConditionalGeneration, """translation""": UMTaForConditionalGeneration, """question-answering""": UMTaForQuestionAnswering, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True # The small UMT5 model needs higher percentages for CPU/MP tests SCREAMING_SNAKE_CASE__ =[0.8, 0.9] def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = UMTaModelTester(self ) @unittest.skip("Test has a segmentation fault on torch 1.8.0" ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = UMTaModel(config_and_inputs[0] ).to(_a ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _a, (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]), f'''{tmpdirname}/t5_test.onnx''', export_params=_a, opset_version=9, input_names=["input_ids", "decoder_input_ids"], ) @unittest.skipIf(torch_device == "cpu", "Cant do half precision" ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_a ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ["encoder_attentions", "decoder_attentions", "cross_attentions"] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = config_and_inputs[0] __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration(_a ).eval() model.to(_a ) __SCREAMING_SNAKE_CASE = { "head_mask": torch.zeros(config.num_layers, config.num_heads, device=_a ), "decoder_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), "cross_attn_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), } for attn_name, (name, mask) in zip(_a, head_masking.items() ): __SCREAMING_SNAKE_CASE = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_heads, device=_a ) __SCREAMING_SNAKE_CASE = model.generate( config_and_inputs[1]["input_ids"], num_beams=1, max_length=3, output_attentions=_a, return_dict_in_generate=_a, **_a, ) # We check the state of decoder_attentions and cross_attentions just from the last step __SCREAMING_SNAKE_CASE = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ), 0.0 ) @unittest.skip("Does not work on the tiny model as we keep hitting edge cases." ) def __lowerCAmelCase ( self ) -> int: pass @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow @unittest.skip( "Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged" ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration.from_pretrained("google/umt5-small", return_dict=_a ).to(_a ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("google/umt5-small", use_fast=_a, legacy=_a ) __SCREAMING_SNAKE_CASE = [ "Bonjour monsieur <extra_id_0> bien <extra_id_1>.", "No se como puedo <extra_id_0>.", "This is the reason why we <extra_id_0> them.", "The <extra_id_0> walks in <extra_id_1>, seats", "A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.", ] __SCREAMING_SNAKE_CASE = tokenizer(_a, return_tensors="pt", padding=_a ).input_ids # fmt: off __SCREAMING_SNAKE_CASE = torch.tensor( [ [ 3_85_30, 21_07_03, 25_62_99, 14_10, 25_62_98, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_26, 3_21, 6_71, 2_59_22, 25_62_99, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 14_60, 3_39, 3_12, 1_90_14, 1_06_20, 7_58, 25_62_99, 23_55,2_74, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_17, 25_62_99, 1_48_69, 2_81, 3_01, 25_62_98, 2_75, 11_99_83,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_20, 25_62_99, 1_48_69, 2_81, 22_34, 2_89, 22_75, 3_33,6_13_91, 2_89, 25_62_98, 5_43, 25_62_97, 16_87_14, 3_29, 25_62_96,2_74, 1], ] ) # fmt: on torch.testing.assert_allclose(_a, _a ) __SCREAMING_SNAKE_CASE = model.generate(input_ids.to(_a ) ) __SCREAMING_SNAKE_CASE = [ "<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>", "<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", ] __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, _a )
693
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case : Tuple = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Optional[int] = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys _snake_case : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
693
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _A ( __snake_case :BertModel , __snake_case :str , __snake_case :str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") __SCREAMING_SNAKE_CASE = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__snake_case ): os.makedirs(__snake_case ) __SCREAMING_SNAKE_CASE = model.state_dict() def to_tf_var_name(__snake_case :str ): for patt, repl in iter(__snake_case ): __SCREAMING_SNAKE_CASE = name.replace(__snake_case , __snake_case ) return f'''bert/{name}''' def create_tf_var(__snake_case :np.ndarray , __snake_case :str , __snake_case :tf.Session ): __SCREAMING_SNAKE_CASE = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE = tf.get_variable(dtype=__snake_case , shape=tensor.shape , name=__snake_case , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__snake_case ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE = to_tf_var_name(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE = torch_tensor.T __SCREAMING_SNAKE_CASE = create_tf_var(tensor=__snake_case , name=__snake_case , session=__snake_case ) tf.keras.backend.set_value(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = session.run(__snake_case ) print(f'''Successfully created {tf_name}: {np.allclose(__snake_case , __snake_case )}''' ) __SCREAMING_SNAKE_CASE = tf.train.Saver(tf.trainable_variables() ) saver.save(__snake_case , os.path.join(__snake_case , model_name.replace("-" , "_" ) + ".ckpt" ) ) def _A ( __snake_case :str=None ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__snake_case , required=__snake_case , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__snake_case , default=__snake_case , required=__snake_case , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__snake_case , required=__snake_case , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__snake_case , required=__snake_case , help="Directory in which to save tensorflow model" ) __SCREAMING_SNAKE_CASE = parser.parse_args(__snake_case ) __SCREAMING_SNAKE_CASE = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__snake_case , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
693
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case : str = { 'configuration_table_transformer': [ 'TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TableTransformerConfig', 'TableTransformerOnnxConfig', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Dict = [ 'TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TableTransformerForObjectDetection', 'TableTransformerModel', 'TableTransformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys _snake_case : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
693
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
1
import argparse from collections import defaultdict import yaml _snake_case : Any = 'docs/source/en/_toctree.yml' def _A ( __snake_case :str ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = defaultdict(__snake_case ) for doc in model_doc: counts[doc["local"]] += 1 __SCREAMING_SNAKE_CASE = [key for key, value in counts.items() if value > 1] __SCREAMING_SNAKE_CASE = [] for duplicate_key in duplicates: __SCREAMING_SNAKE_CASE = list({doc["title"] for doc in model_doc if doc["local"] == duplicate_key} ) if len(__snake_case ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' "`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the " "others." ) # Only add this once new_doc.append({"local": duplicate_key, "title": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc["local"]] == 1] ) # Sort return sorted(__snake_case , key=lambda __snake_case : s["title"].lower() ) def _A ( __snake_case :Optional[Any]=False ) -> int: """simple docstring""" with open(__snake_case , encoding="utf-8" ) as f: __SCREAMING_SNAKE_CASE = yaml.safe_load(f.read() ) # Get to the API doc __SCREAMING_SNAKE_CASE = 0 while content[api_idx]["title"] != "API": api_idx += 1 __SCREAMING_SNAKE_CASE = content[api_idx]["sections"] # Then to the model doc __SCREAMING_SNAKE_CASE = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 __SCREAMING_SNAKE_CASE = api_doc[model_idx]["sections"] __SCREAMING_SNAKE_CASE = [(idx, section) for idx, section in enumerate(__snake_case ) if "sections" in section] __SCREAMING_SNAKE_CASE = False for idx, modality_doc in modalities_docs: __SCREAMING_SNAKE_CASE = modality_doc["sections"] __SCREAMING_SNAKE_CASE = clean_model_doc_toc(__snake_case ) if old_modality_doc != new_modality_doc: __SCREAMING_SNAKE_CASE = True if overwrite: __SCREAMING_SNAKE_CASE = new_modality_doc if diff: if overwrite: __SCREAMING_SNAKE_CASE = model_doc __SCREAMING_SNAKE_CASE = api_doc with open(__snake_case , "w" , encoding="utf-8" ) as f: f.write(yaml.dump(__snake_case , allow_unicode=__snake_case ) ) else: raise ValueError( "The model doc part of the table of content is not properly sorted, run `make style` to fix this." ) if __name__ == "__main__": _snake_case : Any = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') _snake_case : Tuple = parser.parse_args() check_model_doc(args.fix_and_overwrite)
693
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Dict: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 20_00, _a = None, _a = "pil", _a = True, **_a, ) -> Union[ImagePipelineOutput, Tuple]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a ) * self.scheduler.init_noise_sigma __SCREAMING_SNAKE_CASE = sample.to(self.device ) self.scheduler.set_timesteps(_a ) self.scheduler.set_sigmas(_a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): __SCREAMING_SNAKE_CASE = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): __SCREAMING_SNAKE_CASE = self.unet(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct(_a, _a, generator=_a ).prev_sample # prediction step __SCREAMING_SNAKE_CASE = model(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_pred(_a, _a, _a, generator=_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = output.prev_sample, output.prev_sample_mean __SCREAMING_SNAKE_CASE = sample_mean.clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_a )
693
1
import os from pathlib import Path def _A ( ) -> Tuple: """simple docstring""" from torch.utils.cpp_extension import load __SCREAMING_SNAKE_CASE = Path(__snake_case ).resolve().parent.parent.parent / "kernels" / "deformable_detr" __SCREAMING_SNAKE_CASE = [ root / filename for filename in [ "vision.cpp", os.path.join("cpu" , "ms_deform_attn_cpu.cpp" ), os.path.join("cuda" , "ms_deform_attn_cuda.cu" ), ] ] load( "MultiScaleDeformableAttention" , __snake_case , with_cuda=__snake_case , extra_include_paths=[str(__snake_case )] , extra_cflags=["-DWITH_CUDA=1"] , extra_cuda_cflags=[ "-DCUDA_HAS_FP16=1", "-D__CUDA_NO_HALF_OPERATORS__", "-D__CUDA_NO_HALF_CONVERSIONS__", "-D__CUDA_NO_HALF2_OPERATORS__", ] , ) import MultiScaleDeformableAttention as MSDA return MSDA
693
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger _snake_case : Union[str, Any] = get_logger(__name__) class __SCREAMING_SNAKE_CASE : def __init__( self, _a = None ) -> Dict: __SCREAMING_SNAKE_CASE = ( os.path.join(_a, config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) __SCREAMING_SNAKE_CASE = Extractor def __lowerCAmelCase ( self, _a ) -> str: from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" __SCREAMING_SNAKE_CASE = os.path.abspath(_a ) return os.path.join(self.extract_dir, hash_url_to_filename(_a ) ) def __lowerCAmelCase ( self, _a, _a ) -> bool: return force_extract or ( not os.path.isfile(_a ) and not (os.path.isdir(_a ) and os.listdir(_a )) ) def __lowerCAmelCase ( self, _a, _a = False ) -> str: __SCREAMING_SNAKE_CASE = self.extractor.infer_extractor_format(_a ) if not extractor_format: return input_path __SCREAMING_SNAKE_CASE = self._get_output_path(_a ) if self._do_extract(_a, _a ): self.extractor.extract(_a, _a, _a ) return output_path class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): @classmethod @abstractmethod def __lowerCAmelCase ( cls, _a, **_a ) -> bool: ... @staticmethod @abstractmethod def __lowerCAmelCase ( _a, _a ) -> None: ... class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[] @staticmethod def __lowerCAmelCase ( _a, _a ) -> Optional[Any]: with open(_a, "rb" ) as f: return f.read(_a ) @classmethod def __lowerCAmelCase ( cls, _a, _a = b"" ) -> bool: if not magic_number: __SCREAMING_SNAKE_CASE = max(len(_a ) for cls_magic_number in cls.magic_numbers ) try: __SCREAMING_SNAKE_CASE = cls.read_magic_number(_a, _a ) except OSError: return False return any(magic_number.startswith(_a ) for cls_magic_number in cls.magic_numbers ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): @classmethod def __lowerCAmelCase ( cls, _a, **_a ) -> bool: return tarfile.is_tarfile(_a ) @staticmethod def __lowerCAmelCase ( _a, _a ) -> Optional[int]: def resolved(_a ) -> str: return os.path.realpath(os.path.abspath(_a ) ) def badpath(_a, _a ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(_a, _a ) ).startswith(_a ) def badlink(_a, _a ) -> bool: # Links are interpreted relative to the directory containing the link __SCREAMING_SNAKE_CASE = resolved(os.path.join(_a, os.path.dirname(info.name ) ) ) return badpath(info.linkname, base=_a ) __SCREAMING_SNAKE_CASE = resolved(_a ) for finfo in members: if badpath(finfo.name, _a ): logger.error(f'''Extraction of {finfo.name} is blocked (illegal path)''' ) elif finfo.issym() and badlink(_a, _a ): logger.error(f'''Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}''' ) elif finfo.islnk() and badlink(_a, _a ): logger.error(f'''Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}''' ) else: yield finfo @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: os.makedirs(_a, exist_ok=_a ) __SCREAMING_SNAKE_CASE = tarfile.open(_a ) tar_file.extractall(_a, members=TarExtractor.safemembers(_a, _a ) ) tar_file.close() class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\x1F\x8B"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: with gzip.open(_a, "rb" ) as gzip_file: with open(_a, "wb" ) as extracted_file: shutil.copyfileobj(_a, _a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[ B"""PK\x03\x04""", B"""PK\x05\x06""", # empty archive B"""PK\x07\x08""", # spanned archive ] @classmethod def __lowerCAmelCase ( cls, _a, _a = b"" ) -> bool: if super().is_extractable(_a, magic_number=_a ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(_a, "rb" ) as fp: __SCREAMING_SNAKE_CASE = _EndRecData(_a ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: __SCREAMING_SNAKE_CASE = fp.read(_a ) # CD is where we expect it to be if len(_a ) == sizeCentralDir: __SCREAMING_SNAKE_CASE = struct.unpack(_a, _a ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: os.makedirs(_a, exist_ok=_a ) with zipfile.ZipFile(_a, "r" ) as zip_file: zip_file.extractall(_a ) zip_file.close() class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\xFD\x37\x7A\x58\x5A\x00"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: with lzma.open(_a ) as compressed_file: with open(_a, "wb" ) as extracted_file: shutil.copyfileobj(_a, _a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""Rar!\x1a\x07\x00""", B"""Rar!\x1a\x07\x01\x00"""] # RAR_ID # RAR5_ID @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(_a, exist_ok=_a ) __SCREAMING_SNAKE_CASE = rarfile.RarFile(_a ) rf.extractall(_a ) rf.close() class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\x28\xb5\x2F\xFD"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd __SCREAMING_SNAKE_CASE = zstd.ZstdDecompressor() with open(_a, "rb" ) as ifh, open(_a, "wb" ) as ofh: dctx.copy_stream(_a, _a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\x42\x5A\x68"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: with bza.open(_a, "rb" ) as compressed_file: with open(_a, "wb" ) as extracted_file: shutil.copyfileobj(_a, _a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\x37\x7A\xBC\xAF\x27\x1C"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(_a, exist_ok=_a ) with pyazr.SevenZipFile(_a, "r" ) as archive: archive.extractall(_a ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =[B"""\x04\x22\x4D\x18"""] @staticmethod def __lowerCAmelCase ( _a, _a ) -> None: if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(_a, "rb" ) as compressed_file: with open(_a, "wb" ) as extracted_file: shutil.copyfileobj(_a, _a ) class __SCREAMING_SNAKE_CASE : # Put zip file to the last, b/c it is possible wrongly detected as zip (I guess it means: as tar or gzip) SCREAMING_SNAKE_CASE__ ={ "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def __lowerCAmelCase ( cls ) -> List[str]: return max( len(_a ) for extractor in cls.extractors.values() if issubclass(_a, _a ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def __lowerCAmelCase ( _a, _a ) -> List[str]: try: return MagicNumberBaseExtractor.read_magic_number(_a, magic_number_length=_a ) except OSError: return b"" @classmethod def __lowerCAmelCase ( cls, _a, _a = False ) -> bool: warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead.", category=_a, ) __SCREAMING_SNAKE_CASE = cls.infer_extractor_format(_a ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def __lowerCAmelCase ( cls, _a ) -> str: # <Added version="2.4.0"/> __SCREAMING_SNAKE_CASE = cls._get_magic_number_max_length() __SCREAMING_SNAKE_CASE = cls._read_magic_number(_a, _a ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(_a, magic_number=_a ): return extractor_format @classmethod def __lowerCAmelCase ( cls, _a, _a, _a = None, _a = "deprecated", ) -> None: os.makedirs(os.path.dirname(_a ), exist_ok=_a ) # Prevent parallel extractions __SCREAMING_SNAKE_CASE = str(Path(_a ).with_suffix(".lock" ) ) with FileLock(_a ): shutil.rmtree(_a, ignore_errors=_a ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(_a, _a ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead.", category=_a, ) __SCREAMING_SNAKE_CASE = extractor if extractor != "deprecated" else extractor_format else: __SCREAMING_SNAKE_CASE = cls.extractors[extractor_format] return extractor.extract(_a, _a ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0.", category=_a, ) for extractor in cls.extractors.values(): if extractor.is_extractable(_a ): return extractor.extract(_a, _a )
693
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
1
import argparse import json import subprocess def _A ( __snake_case :Dict , __snake_case :Dict ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = ( f'''curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer {token}"''' " https://api.github.com/repos/huggingface/transformers/actions/runners" ) __SCREAMING_SNAKE_CASE = subprocess.run(__snake_case , shell=__snake_case , stdout=subprocess.PIPE ) __SCREAMING_SNAKE_CASE = output.stdout.decode("utf-8" ) __SCREAMING_SNAKE_CASE = json.loads(__snake_case ) __SCREAMING_SNAKE_CASE = status["runners"] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(__snake_case ) # save the result so we can report them on Slack with open("offline_runners.txt" , "w" ) as fp: fp.write(json.dumps(__snake_case ) ) if len(__snake_case ) > 0: __SCREAMING_SNAKE_CASE = "\n".join([x["name"] for x in offline_runners] ) raise ValueError(f'''The following runners are offline:\n{failed}''' ) if __name__ == "__main__": def _A ( __snake_case :int ) -> str: """simple docstring""" return values.split("," ) _snake_case : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--target_runners', default=None, type=list_str, required=True, help='Comma-separated list of runners to check status.', ) parser.add_argument( '--token', default=None, type=str, required=True, help='A token that has actions:read permission.' ) _snake_case : List[Any] = parser.parse_args() get_runner_status(args.target_runners, args.token)
693
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> Any: __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None def __repr__( self ) -> str: return f'''Node({self.data})''' class __SCREAMING_SNAKE_CASE : def __init__( self ) -> Tuple: __SCREAMING_SNAKE_CASE = None def __iter__( self ) -> Any: __SCREAMING_SNAKE_CASE = self.head while node: yield node.data __SCREAMING_SNAKE_CASE = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_a ) for item in self] ) def __getitem__( self, _a ) -> Any: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self, _a, _a ) -> None: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) __SCREAMING_SNAKE_CASE = self.head for _ in range(_a ): __SCREAMING_SNAKE_CASE = current.next __SCREAMING_SNAKE_CASE = data def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(len(self ), _a ) def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(0, _a ) def __lowerCAmelCase ( self, _a, _a ) -> None: if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) __SCREAMING_SNAKE_CASE = Node(_a ) if self.head is None: __SCREAMING_SNAKE_CASE = new_node elif index == 0: __SCREAMING_SNAKE_CASE = self.head # link new_node to head __SCREAMING_SNAKE_CASE = new_node else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self ) -> None: # print every node data print(self ) def __lowerCAmelCase ( self ) -> Any: return self.delete_nth(0 ) def __lowerCAmelCase ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowerCAmelCase ( self, _a = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) __SCREAMING_SNAKE_CASE = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE = self.head.next else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next.next return delete_node.data def __lowerCAmelCase ( self ) -> bool: return self.head is None def __lowerCAmelCase ( self ) -> None: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE = prev def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = LinkedList() assert linked_list.is_empty() is True assert str(__snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(__snake_case ) == i linked_list.insert_nth(__snake_case , i + 1 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(__snake_case ) == 9 assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(-8 , 1 ) ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_9_2.5_5_5_5_5, "Hello, world!", 7_7.9, Node(10 ), None, None, 1_2.2_0, ] __SCREAMING_SNAKE_CASE = LinkedList() for i in test_input: linked_list.insert_tail(__snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(__snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE = linked_list.delete_head() assert result == -9 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE = linked_list.delete_tail() assert result == 1_2.2 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE = linked_list.delete_nth(10 ) assert result is None assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(__snake_case ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(__snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _A ( ) -> Union[str, Any]: """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(__snake_case ) print("\nReading/changing Node data using indexing:" ) print(f'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE = input("Enter New Value: " ).strip() print("New list:" ) print(__snake_case ) print(f'''length of linked_list is : {len(__snake_case )}''' ) if __name__ == "__main__": main()
693
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : Optional[Any] = logging.get_logger(__name__) _snake_case : Tuple = { 'microsoft/wavlm-base': 'https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""wavlm""" def __init__( self, _a=32, _a=7_68, _a=12, _a=12, _a=30_72, _a="gelu", _a=0.1, _a=0.1, _a=0.1, _a=0.0, _a=0.1, _a=0.1, _a=0.02, _a=1E-5, _a="group", _a="gelu", _a=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12), _a=(5, 2, 2, 2, 2, 2, 2), _a=(10, 3, 3, 3, 3, 2, 2), _a=False, _a=1_28, _a=16, _a=3_20, _a=8_00, _a=False, _a=True, _a=0.05, _a=10, _a=2, _a=0.0, _a=10, _a=3_20, _a=2, _a=0.1, _a=1_00, _a=2_56, _a=2_56, _a=0.1, _a="mean", _a=False, _a=False, _a=2_56, _a=(5_12, 5_12, 5_12, 5_12, 15_00), _a=(5, 3, 3, 1, 1), _a=(1, 2, 3, 1, 1), _a=5_12, _a=80, _a=0, _a=1, _a=2, _a=False, _a=3, _a=2, _a=3, _a=None, **_a, ) -> int: super().__init__(**_a, pad_token_id=_a, bos_token_id=_a, eos_token_id=_a ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_buckets __SCREAMING_SNAKE_CASE = max_bucket_distance __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = num_ctc_classes __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = do_stable_layer_norm __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length # parameters for pretraining with codevector quantized representations __SCREAMING_SNAKE_CASE = num_codevectors_per_group __SCREAMING_SNAKE_CASE = num_codevector_groups __SCREAMING_SNAKE_CASE = contrastive_logits_temperature __SCREAMING_SNAKE_CASE = num_negatives __SCREAMING_SNAKE_CASE = codevector_dim __SCREAMING_SNAKE_CASE = proj_codevector_dim __SCREAMING_SNAKE_CASE = diversity_loss_weight # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # adapter __SCREAMING_SNAKE_CASE = add_adapter __SCREAMING_SNAKE_CASE = adapter_kernel_size __SCREAMING_SNAKE_CASE = adapter_stride __SCREAMING_SNAKE_CASE = num_adapter_layers __SCREAMING_SNAKE_CASE = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. __SCREAMING_SNAKE_CASE = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = xvector_output_dim @property def __lowerCAmelCase ( self ) -> Optional[int]: return functools.reduce(operator.mul, self.conv_stride, 1 )
693
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
1
def _A ( __snake_case :bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def _A ( __snake_case :str ) -> bytes: """simple docstring""" if (len(__snake_case ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
1
def _A ( __snake_case :list[list] ) -> list[list]: """simple docstring""" __SCREAMING_SNAKE_CASE = current_set.copy() for row_index, row in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = row[0] for column_index, column in enumerate(__snake_case ): if magnitude == 0: __SCREAMING_SNAKE_CASE = column continue __SCREAMING_SNAKE_CASE = column / magnitude # Subtract to cancel term __SCREAMING_SNAKE_CASE = current_set[0] __SCREAMING_SNAKE_CASE = [first_row] __SCREAMING_SNAKE_CASE = current_set[1::] for row in current_set: __SCREAMING_SNAKE_CASE = [] # If first term is 0, it is already in form we want, so we preserve it if row[0] == 0: final_set.append(__snake_case ) continue for column_index in range(len(__snake_case ) ): temp_row.append(first_row[column_index] - row[column_index] ) final_set.append(__snake_case ) # Create next recursion iteration set if len(final_set[0] ) != 3: __SCREAMING_SNAKE_CASE = final_set[0] __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [] for row in final_set[1::]: current_first_column.append(row[0] ) next_iteration.append(row[1::] ) __SCREAMING_SNAKE_CASE = simplify(__snake_case ) for i in range(len(__snake_case ) ): resultant[i].insert(0 , current_first_column[i] ) resultant.insert(0 , __snake_case ) __SCREAMING_SNAKE_CASE = resultant return final_set def _A ( __snake_case :list[list] ) -> list: """simple docstring""" if len(__snake_case ) == 0: raise IndexError("solve_simultaneous() requires n lists of length n+1" ) __SCREAMING_SNAKE_CASE = len(__snake_case ) + 1 if any(len(__snake_case ) != _length for item in equations ): raise IndexError("solve_simultaneous() requires n lists of length n+1" ) for row in equations: if any(not isinstance(__snake_case , (int, float) ) for column in row ): raise ValueError("solve_simultaneous() requires lists of integers" ) if len(__snake_case ) == 1: return [equations[0][-1] / equations[0][0]] __SCREAMING_SNAKE_CASE = equations.copy() if any(0 in row for row in data_set ): __SCREAMING_SNAKE_CASE = data_set.copy() __SCREAMING_SNAKE_CASE = [] for row_index, row in enumerate(__snake_case ): if 0 not in row: __SCREAMING_SNAKE_CASE = data_set.pop(__snake_case ) break if not full_row: raise ValueError("solve_simultaneous() requires at least 1 full equation" ) data_set.insert(0 , __snake_case ) __SCREAMING_SNAKE_CASE = data_set.copy() __SCREAMING_SNAKE_CASE = simplify(__snake_case ) __SCREAMING_SNAKE_CASE = simplified[::-1] __SCREAMING_SNAKE_CASE = [] for row in simplified: __SCREAMING_SNAKE_CASE = row[-1] if not solutions: if row[-2] == 0: solutions.append(0 ) continue solutions.append(current_solution / row[-2] ) continue __SCREAMING_SNAKE_CASE = row.copy()[: len(__snake_case ) - 1 :] while temp_row[0] == 0: temp_row.pop(0 ) if len(__snake_case ) == 0: solutions.append(0 ) continue __SCREAMING_SNAKE_CASE = temp_row[1::] __SCREAMING_SNAKE_CASE = temp_row[::-1] for column_index, column in enumerate(__snake_case ): current_solution -= column * solutions[column_index] solutions.append(__snake_case ) __SCREAMING_SNAKE_CASE = [] for item in solutions: final.append(float(round(__snake_case , 5 ) ) ) return final[::-1] if __name__ == "__main__": import doctest doctest.testmod() _snake_case : List[Any] = [ [2, 1, 1, 1, 1, 4], [1, 2, 1, 1, 1, 5], [1, 1, 2, 1, 1, 6], [1, 1, 1, 2, 1, 7], [1, 1, 1, 1, 2, 8], ] print(solve_simultaneous(eq)) print(solve_simultaneous([[4, 2]]))
693
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
1
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging _snake_case : Any = logging.get_logger(__name__) def _A ( __snake_case :Optional[int] , __snake_case :Union[str, Any] ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = set() __SCREAMING_SNAKE_CASE = [] def parse_line(__snake_case :Optional[int] ): for line in fp: if isinstance(__snake_case , __snake_case ): __SCREAMING_SNAKE_CASE = line.decode("UTF-8" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(" " ): # process a single warning and move it to `selected_warnings`. if len(__snake_case ) > 0: __SCREAMING_SNAKE_CASE = "\n".join(__snake_case ) # Only keep the warnings specified in `targets` if any(f''': {x}: ''' in warning for x in targets ): selected_warnings.add(__snake_case ) buffer.clear() continue else: __SCREAMING_SNAKE_CASE = line.strip() buffer.append(__snake_case ) if from_gh: for filename in os.listdir(__snake_case ): __SCREAMING_SNAKE_CASE = os.path.join(__snake_case , __snake_case ) if not os.path.isdir(__snake_case ): # read the file if filename != "warnings.txt": continue with open(__snake_case ) as fp: parse_line(__snake_case ) else: try: with zipfile.ZipFile(__snake_case ) as z: for filename in z.namelist(): if not os.path.isdir(__snake_case ): # read the file if filename != "warnings.txt": continue with z.open(__snake_case ) as fp: parse_line(__snake_case ) except Exception: logger.warning( f'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.''' ) return selected_warnings def _A ( __snake_case :List[str] , __snake_case :Tuple ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = set() __SCREAMING_SNAKE_CASE = [os.path.join(__snake_case , __snake_case ) for p in os.listdir(__snake_case ) if (p.endswith(".zip" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(__snake_case , __snake_case ) ) return selected_warnings if __name__ == "__main__": def _A ( __snake_case :Optional[Any] ) -> List[str]: """simple docstring""" return values.split("," ) _snake_case : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') # optional parameters parser.add_argument( '--targets', default='DeprecationWarning,UserWarning,FutureWarning', type=list_str, help='Comma-separated list of target warning(s) which we want to extract.', ) parser.add_argument( '--from_gh', action='store_true', help='If running from a GitHub action workflow and collecting warnings from its artifacts.', ) _snake_case : int = parser.parse_args() _snake_case : Union[str, Any] = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links _snake_case : List[Any] = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('=' * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts _snake_case : int = extract_warnings(args.output_dir, args.targets) _snake_case : Optional[int] = sorted(selected_warnings) with open(os.path.join(args.output_dir, 'selected_warnings.json'), 'w', encoding='UTF-8') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
693
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
1
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =1 @register_to_config def __init__( self, _a = 20_00, _a = 0.15, _a = 0.01, _a = 1348.0, _a = 1E-5, _a = 1, ) -> Tuple: # standard deviation of the initial noise distribution __SCREAMING_SNAKE_CASE = sigma_max # setable values __SCREAMING_SNAKE_CASE = None self.set_sigmas(_a, _a, _a, _a ) def __lowerCAmelCase ( self, _a, _a = None ) -> torch.FloatTensor: return sample def __lowerCAmelCase ( self, _a, _a = None, _a = None ) -> int: __SCREAMING_SNAKE_CASE = sampling_eps if sampling_eps is not None else self.config.sampling_eps __SCREAMING_SNAKE_CASE = torch.linspace(1, _a, _a, device=_a ) def __lowerCAmelCase ( self, _a, _a = None, _a = None, _a = None ) -> List[Any]: __SCREAMING_SNAKE_CASE = sigma_min if sigma_min is not None else self.config.sigma_min __SCREAMING_SNAKE_CASE = sigma_max if sigma_max is not None else self.config.sigma_max __SCREAMING_SNAKE_CASE = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(_a, _a ) __SCREAMING_SNAKE_CASE = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) __SCREAMING_SNAKE_CASE = torch.exp(torch.linspace(math.log(_a ), math.log(_a ), _a ) ) __SCREAMING_SNAKE_CASE = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def __lowerCAmelCase ( self, _a, _a ) -> List[Any]: return torch.where( timesteps == 0, torch.zeros_like(t.to(timesteps.device ) ), self.discrete_sigmas[timesteps - 1].to(timesteps.device ), ) def __lowerCAmelCase ( self, _a, _a, _a, _a = None, _a = True, ) -> Union[SdeVeOutput, Tuple]: if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) __SCREAMING_SNAKE_CASE = timestep * torch.ones( sample.shape[0], device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) __SCREAMING_SNAKE_CASE = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda __SCREAMING_SNAKE_CASE = timesteps.to(self.discrete_sigmas.device ) __SCREAMING_SNAKE_CASE = self.discrete_sigmas[timesteps].to(sample.device ) __SCREAMING_SNAKE_CASE = self.get_adjacent_sigma(_a, _a ).to(sample.device ) __SCREAMING_SNAKE_CASE = torch.zeros_like(_a ) __SCREAMING_SNAKE_CASE = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods __SCREAMING_SNAKE_CASE = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): __SCREAMING_SNAKE_CASE = diffusion.unsqueeze(-1 ) __SCREAMING_SNAKE_CASE = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of __SCREAMING_SNAKE_CASE = randn_tensor( sample.shape, layout=sample.layout, generator=_a, device=sample.device, dtype=sample.dtype ) __SCREAMING_SNAKE_CASE = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? __SCREAMING_SNAKE_CASE = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=_a, prev_sample_mean=_a ) def __lowerCAmelCase ( self, _a, _a, _a = None, _a = True, ) -> Union[SchedulerOutput, Tuple]: if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction __SCREAMING_SNAKE_CASE = randn_tensor(sample.shape, layout=sample.layout, generator=_a ).to(sample.device ) # compute step size from the model_output, the noise, and the snr __SCREAMING_SNAKE_CASE = torch.norm(model_output.reshape(model_output.shape[0], -1 ), dim=-1 ).mean() __SCREAMING_SNAKE_CASE = torch.norm(noise.reshape(noise.shape[0], -1 ), dim=-1 ).mean() __SCREAMING_SNAKE_CASE = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 __SCREAMING_SNAKE_CASE = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term __SCREAMING_SNAKE_CASE = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): __SCREAMING_SNAKE_CASE = step_size.unsqueeze(-1 ) __SCREAMING_SNAKE_CASE = sample + step_size * model_output __SCREAMING_SNAKE_CASE = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_a ) def __lowerCAmelCase ( self, _a, _a, _a, ) -> torch.FloatTensor: # Make sure sigmas and timesteps have the same device and dtype as original_samples __SCREAMING_SNAKE_CASE = timesteps.to(original_samples.device ) __SCREAMING_SNAKE_CASE = self.discrete_sigmas.to(original_samples.device )[timesteps] __SCREAMING_SNAKE_CASE = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(_a ) * sigmas[:, None, None, None] ) __SCREAMING_SNAKE_CASE = noise + original_samples return noisy_samples def __len__( self ) -> int: return self.config.num_train_timesteps
693
import random from .binary_exp_mod import bin_exp_mod def _A ( __snake_case :List[Any] , __snake_case :Union[str, Any]=1000 ) -> int: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __SCREAMING_SNAKE_CASE = n - 1 __SCREAMING_SNAKE_CASE = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __SCREAMING_SNAKE_CASE = 0 while count < prec: __SCREAMING_SNAKE_CASE = random.randint(2 , n - 1 ) __SCREAMING_SNAKE_CASE = bin_exp_mod(__snake_case , __snake_case , __snake_case ) if b != 1: __SCREAMING_SNAKE_CASE = True for _ in range(__snake_case ): if b == n - 1: __SCREAMING_SNAKE_CASE = False break __SCREAMING_SNAKE_CASE = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _snake_case : int = 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)))
693
1
def _A ( ) -> int: """simple docstring""" for n in range(1 , 100_0000 ): yield n * (n + 1) // 2 def _A ( __snake_case :Dict ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 while i * i <= n: __SCREAMING_SNAKE_CASE = 0 while n % i == 0: n //= i multiplicity += 1 divisors_count *= multiplicity + 1 i += 1 if n > 1: divisors_count *= 2 return divisors_count def _A ( ) -> str: """simple docstring""" return next(i for i in triangle_number_generator() if count_divisors(__snake_case ) > 500 ) if __name__ == "__main__": print(solution())
693
import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int ) -> np.ndarray: """simple docstring""" if (ksize % 2) == 0: __SCREAMING_SNAKE_CASE = ksize + 1 __SCREAMING_SNAKE_CASE = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(__snake_case ): for x in range(__snake_case ): # distance from center __SCREAMING_SNAKE_CASE = x - ksize // 2 __SCREAMING_SNAKE_CASE = y - ksize // 2 # degree to radiant __SCREAMING_SNAKE_CASE = theta / 180 * np.pi __SCREAMING_SNAKE_CASE = np.cos(_theta ) __SCREAMING_SNAKE_CASE = np.sin(_theta ) # get kernel x __SCREAMING_SNAKE_CASE = cos_theta * px + sin_theta * py # get kernel y __SCREAMING_SNAKE_CASE = -sin_theta * px + cos_theta * py # fill kernel __SCREAMING_SNAKE_CASE = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _snake_case : Union[str, Any] = imread('../image_data/lena.jpg') # turn image in gray scale value _snake_case : List[str] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _snake_case : int = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 1_20, 1_50]: _snake_case : List[str] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _snake_case : Optional[Any] = out / out.max() * 2_55 _snake_case : Union[str, Any] = out.astype(np.uinta) imshow('Original', gray) imshow('Gabor filter with 20x20 mask and 6 directions', out) waitKey(0)
693
1
from numpy import exp, pi, sqrt def _A ( __snake_case :Optional[Any] , __snake_case :float = 0.0 , __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()
693
def _A ( __snake_case :int ) -> int: """simple docstring""" assert isinstance(__snake_case , __snake_case ), f'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: __SCREAMING_SNAKE_CASE = f'''The input value of [n={number}] has to be > 0''' raise ValueError(__snake_case ) else: __SCREAMING_SNAKE_CASE = sylvester(number - 1 ) __SCREAMING_SNAKE_CASE = num - 1 __SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
693
1
from __future__ import annotations from collections.abc import MutableSequence class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a ) -> None: if len(_a ) != degree + 1: raise ValueError( "The number of coefficients should be equal to the degree + 1." ) __SCREAMING_SNAKE_CASE = list(_a ) __SCREAMING_SNAKE_CASE = degree def __add__( self, _a ) -> Polynomial: if self.degree > polynomial_a.degree: __SCREAMING_SNAKE_CASE = self.coefficients[:] for i in range(polynomial_a.degree + 1 ): coefficients[i] += polynomial_a.coefficients[i] return Polynomial(self.degree, _a ) else: __SCREAMING_SNAKE_CASE = polynomial_a.coefficients[:] for i in range(self.degree + 1 ): coefficients[i] += self.coefficients[i] return Polynomial(polynomial_a.degree, _a ) def __sub__( self, _a ) -> Polynomial: return self + polynomial_a * Polynomial(0, [-1] ) def __neg__( self ) -> Polynomial: return Polynomial(self.degree, [-c for c in self.coefficients] ) def __mul__( self, _a ) -> Polynomial: __SCREAMING_SNAKE_CASE = [0] * (self.degree + polynomial_a.degree + 1) for i in range(self.degree + 1 ): for j in range(polynomial_a.degree + 1 ): coefficients[i + j] += ( self.coefficients[i] * polynomial_a.coefficients[j] ) return Polynomial(self.degree + polynomial_a.degree, _a ) def __lowerCAmelCase ( self, _a ) -> int | float: __SCREAMING_SNAKE_CASE = 0 for i in range(self.degree + 1 ): result += self.coefficients[i] * (substitution**i) return result def __str__( self ) -> str: __SCREAMING_SNAKE_CASE = "" for i in range(self.degree, -1, -1 ): if self.coefficients[i] == 0: continue elif self.coefficients[i] > 0: if polynomial: polynomial += " + " else: polynomial += " - " if i == 0: polynomial += str(abs(self.coefficients[i] ) ) elif i == 1: polynomial += str(abs(self.coefficients[i] ) ) + "x" else: polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(_a ) return polynomial def __repr__( self ) -> str: return self.__str__() def __lowerCAmelCase ( self ) -> Polynomial: __SCREAMING_SNAKE_CASE = [0] * self.degree for i in range(self.degree ): __SCREAMING_SNAKE_CASE = self.coefficients[i + 1] * (i + 1) return Polynomial(self.degree - 1, _a ) def __lowerCAmelCase ( self, _a = 0 ) -> Polynomial: __SCREAMING_SNAKE_CASE = [0] * (self.degree + 2) __SCREAMING_SNAKE_CASE = constant for i in range(self.degree + 1 ): __SCREAMING_SNAKE_CASE = self.coefficients[i] / (i + 1) return Polynomial(self.degree + 1, _a ) def __eq__( self, _a ) -> bool: if not isinstance(_a, _a ): return False if self.degree != polynomial_a.degree: return False for i in range(self.degree + 1 ): if self.coefficients[i] != polynomial_a.coefficients[i]: return False return True def __ne__( self, _a ) -> bool: return not self.__eq__(_a )
693
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @require_tf def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf" ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(_a ), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @slow @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
693
1
def _A ( __snake_case :List[str] ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = [0] * len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(__snake_case ) ): if indegree[i] == 0: queue.append(__snake_case ) while queue: __SCREAMING_SNAKE_CASE = queue.pop(0 ) cnt += 1 topo.append(__snake_case ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(__snake_case ) if cnt != len(__snake_case ): print("Cycle exists" ) else: print(__snake_case ) # Adjacency List of Graph _snake_case : str = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
693
from __future__ import annotations import math def _A ( __snake_case :int , __snake_case :int , __snake_case :bool , __snake_case :list[int] , __snake_case :float ) -> int: """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__snake_case ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) return min( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [90, 23, 6, 33, 21, 65, 123, 3_4423] __SCREAMING_SNAKE_CASE = math.log(len(__snake_case ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , __snake_case , __snake_case , __snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
1
import shutil import tempfile import unittest from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast from transformers.testing_utils import require_sentencepiece, require_torchaudio from .test_feature_extraction_clap import floats_list @require_torchaudio @require_sentencepiece class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = "laion/clap-htsat-unfused" __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() def __lowerCAmelCase ( self, **_a ) -> Optional[Any]: return RobertaTokenizer.from_pretrained(self.checkpoint, **_a ) def __lowerCAmelCase ( self, **_a ) -> Optional[Any]: return ClapFeatureExtractor.from_pretrained(self.checkpoint, **_a ) def __lowerCAmelCase ( self ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = self.get_feature_extractor() __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=_a, feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE = ClapProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer, _a ) self.assertEqual(processor.feature_extractor.to_json_string(), feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor, _a ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=self.get_tokenizer(), feature_extractor=self.get_feature_extractor() ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)" ) __SCREAMING_SNAKE_CASE = self.get_feature_extractor(do_normalize=_a, padding_value=1.0 ) __SCREAMING_SNAKE_CASE = ClapProcessor.from_pretrained( self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=_a, padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer, _a ) self.assertEqual(processor.feature_extractor.to_json_string(), feature_extractor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.feature_extractor, _a ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.get_feature_extractor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=_a, feature_extractor=_a ) __SCREAMING_SNAKE_CASE = floats_list((3, 10_00) ) __SCREAMING_SNAKE_CASE = feature_extractor(_a, return_tensors="np" ) __SCREAMING_SNAKE_CASE = processor(audios=_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 __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = self.get_feature_extractor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=_a, feature_extractor=_a ) __SCREAMING_SNAKE_CASE = "This is a test string" __SCREAMING_SNAKE_CASE = processor(text=_a ) __SCREAMING_SNAKE_CASE = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key], encoded_processor[key] ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.get_feature_extractor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=_a, feature_extractor=_a ) __SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __SCREAMING_SNAKE_CASE = processor.batch_decode(_a ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertListEqual(_a, _a ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.get_feature_extractor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = ClapProcessor(tokenizer=_a, feature_extractor=_a ) self.assertListEqual( processor.model_input_names[2:], feature_extractor.model_input_names, msg="`processor` and `feature_extractor` model input names do not match", )
693
def _A ( __snake_case :bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def _A ( __snake_case :str ) -> bytes: """simple docstring""" if (len(__snake_case ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
def _A ( __snake_case :int ) -> int: """simple docstring""" if not isinstance(__snake_case , __snake_case ): raise ValueError("multiplicative_persistence() only accepts integral values" ) if num < 0: raise ValueError("multiplicative_persistence() does not accept negative values" ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = str(__snake_case ) while len(__snake_case ) != 1: __SCREAMING_SNAKE_CASE = [int(__snake_case ) for i in num_string] __SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(__snake_case ) ): total *= numbers[i] __SCREAMING_SNAKE_CASE = str(__snake_case ) steps += 1 return steps def _A ( __snake_case :int ) -> int: """simple docstring""" if not isinstance(__snake_case , __snake_case ): raise ValueError("additive_persistence() only accepts integral values" ) if num < 0: raise ValueError("additive_persistence() does not accept negative values" ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = str(__snake_case ) while len(__snake_case ) != 1: __SCREAMING_SNAKE_CASE = [int(__snake_case ) for i in num_string] __SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(__snake_case ) ): total += numbers[i] __SCREAMING_SNAKE_CASE = str(__snake_case ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
693
from functools import lru_cache def _A ( __snake_case :int ) -> set: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(__snake_case ) if n > 1: factors.add(__snake_case ) return factors @lru_cache def _A ( __snake_case :int ) -> int: """simple docstring""" return len(unique_prime_factors(__snake_case ) ) def _A ( __snake_case :list ) -> bool: """simple docstring""" return len(set(__snake_case ) ) in (0, 1) def _A ( __snake_case :int ) -> list: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 while True: # Increment each value of a generated range __SCREAMING_SNAKE_CASE = [base + i for i in range(__snake_case )] # Run elements through out unique_prime_factors function # Append our target number to the end. __SCREAMING_SNAKE_CASE = [upf_len(__snake_case ) for x in group] checker.append(__snake_case ) # If all numbers in the list are equal, return the group variable. if equality(__snake_case ): return group # Increment our base variable by 1 base += 1 def _A ( __snake_case :int = 4 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = run(__snake_case ) return results[0] if len(__snake_case ) else None if __name__ == "__main__": print(solution())
693
1
from __future__ import annotations def _A ( __snake_case :int , __snake_case :int ) -> list[list[int]]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] create_all_state(1 , __snake_case , __snake_case , [] , __snake_case ) return result def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :list[int] , __snake_case :list[list[int]] , ) -> None: """simple docstring""" if level == 0: total_list.append(current_list[:] ) return for i in range(__snake_case , total_number - level + 2 ): current_list.append(__snake_case ) create_all_state(i + 1 , __snake_case , level - 1 , __snake_case , __snake_case ) current_list.pop() def _A ( __snake_case :list[list[int]] ) -> None: """simple docstring""" for i in total_list: print(*__snake_case ) if __name__ == "__main__": _snake_case : Optional[Any] = 4 _snake_case : List[str] = 2 _snake_case : str = generate_all_combinations(n, k) print_all_state(total_list)
693
import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _A ( __snake_case :Dict ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = VideoMAEConfig() set_architecture_configs(__snake_case , __snake_case ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = False if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = "huggingface/label-files" if "kinetics" in model_name: __SCREAMING_SNAKE_CASE = 400 __SCREAMING_SNAKE_CASE = "kinetics400-id2label.json" elif "ssv2" in model_name: __SCREAMING_SNAKE_CASE = 174 __SCREAMING_SNAKE_CASE = "something-something-v2-id2label.json" else: raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned." ) __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type="dataset" ) , "r" ) ) __SCREAMING_SNAKE_CASE = {int(__snake_case ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def _A ( __snake_case :Dict , __snake_case :Optional[Any] ) -> List[Any]: """simple docstring""" if "small" in model_name: __SCREAMING_SNAKE_CASE = 384 __SCREAMING_SNAKE_CASE = 1536 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 192 __SCREAMING_SNAKE_CASE = 768 elif "large" in model_name: __SCREAMING_SNAKE_CASE = 1024 __SCREAMING_SNAKE_CASE = 4096 __SCREAMING_SNAKE_CASE = 24 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 512 __SCREAMING_SNAKE_CASE = 2048 elif "huge" in model_name: __SCREAMING_SNAKE_CASE = 1280 __SCREAMING_SNAKE_CASE = 5120 __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 640 __SCREAMING_SNAKE_CASE = 2560 elif "base" not in model_name: raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\"" ) def _A ( __snake_case :List[Any] ) -> Optional[int]: """simple docstring""" if "encoder." in name: __SCREAMING_SNAKE_CASE = name.replace("encoder." , "" ) if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("cls_token" , "videomae.embeddings.cls_token" ) if "decoder_pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pos_embed" , "decoder.decoder_pos_embed" ) if "pos_embed" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("pos_embed" , "videomae.embeddings.position_embeddings" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.proj" , "videomae.embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.norm" , "videomae.embeddings.norm" ) if "decoder.blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder.blocks" , "decoder.decoder_layers" ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("blocks" , "videomae.encoder.layer" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name and "bias" not in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.self" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.attention" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc2" , "output.dense" ) if "decoder_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_embed" , "decoder.decoder_embed" ) if "decoder_norm" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_norm" , "decoder.decoder_norm" ) if "decoder_pred" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pred" , "decoder.decoder_pred" ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.weight" , "videomae.layernorm.weight" ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.bias" , "videomae.layernorm.bias" ) if "head" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("head" , "classifier" ) return name def _A ( __snake_case :Union[str, Any] , __snake_case :Optional[int] ) -> Optional[Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__snake_case ) if key.startswith("encoder." ): __SCREAMING_SNAKE_CASE = key.replace("encoder." , "" ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split("." ) if key.startswith("decoder.blocks" ): __SCREAMING_SNAKE_CASE = config.decoder_hidden_size __SCREAMING_SNAKE_CASE = int(key_split[2] ) __SCREAMING_SNAKE_CASE = "decoder.decoder_layers." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = config.hidden_size __SCREAMING_SNAKE_CASE = int(key_split[1] ) __SCREAMING_SNAKE_CASE = "videomae.encoder.layer." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def _A ( ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) __SCREAMING_SNAKE_CASE = np.load(__snake_case ) return list(__snake_case ) def _A ( __snake_case :Optional[int] , __snake_case :List[str] , __snake_case :Union[str, Any] , __snake_case :Optional[Any] ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = get_videomae_config(__snake_case ) if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = VideoMAEForVideoClassification(__snake_case ) else: __SCREAMING_SNAKE_CASE = VideoMAEForPreTraining(__snake_case ) # download original checkpoint, hosted on Google Drive __SCREAMING_SNAKE_CASE = "pytorch_model.bin" gdown.cached_download(__snake_case , __snake_case , quiet=__snake_case ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) if "model" in files: __SCREAMING_SNAKE_CASE = files["model"] else: __SCREAMING_SNAKE_CASE = files["module"] __SCREAMING_SNAKE_CASE = convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) model.eval() # verify model on basic input __SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) __SCREAMING_SNAKE_CASE = prepare_video() __SCREAMING_SNAKE_CASE = image_processor(__snake_case , return_tensors="pt" ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case ) __SCREAMING_SNAKE_CASE = model(**__snake_case ) __SCREAMING_SNAKE_CASE = outputs.logits __SCREAMING_SNAKE_CASE = [ "videomae-small-finetuned-kinetics", "videomae-small-finetuned-ssv2", # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) "videomae-base-short", "videomae-base-short-finetuned-kinetics", "videomae-base", "videomae-base-finetuned-kinetics", "videomae-large", "videomae-large-finetuned-kinetics", "videomae-huge-finetuned-kinetics", # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) "videomae-base-short-ssv2", "videomae-base-short-finetuned-ssv2", "videomae-base-ssv2", "videomae-base-finetuned-ssv2", ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.9_2_9_1, -0.4_0_6_1, -0.9_3_0_7] ) elif model_name == "videomae-small-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_7_1, -0.4_6_8_9, -0.8_2_3_5] ) elif model_name == "videomae-base": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_7_3_9, 0.7_9_6_8, 0.7_0_8_9], [0.6_7_0_1, 0.7_4_8_7, 0.6_2_0_9], [0.4_2_8_7, 0.5_1_5_8, 0.4_7_7_3]] ) elif model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ) # we verified the loss both for normalized and unnormalized targets for this one __SCREAMING_SNAKE_CASE = torch.tensor([0.5_1_4_2] ) if config.norm_pix_loss else torch.tensor([0.6_4_6_9] ) elif model_name == "videomae-large": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_1_4_9, 0.7_9_9_7, 0.6_9_6_6], [0.6_7_6_8, 0.7_8_6_9, 0.6_9_4_8], [0.5_1_3_9, 0.6_2_2_1, 0.5_6_0_5]] ) elif model_name == "videomae-large-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.0_7_7_1, 0.0_0_1_1, -0.3_6_2_5] ) elif model_name == "videomae-huge-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_4_3_3, 0.1_6_3_2, -0.4_8_9_4] ) elif model_name == "videomae-base-short-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.6_5_8_8, 0.0_9_9_0, -0.2_4_9_3] ) elif model_name == "videomae-base-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ) elif model_name == "videomae-base-short-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.4_7_1_2, 0.5_2_9_6, 0.5_7_8_6], [0.2_2_7_8, 0.2_7_2_9, 0.4_0_2_6], [0.0_3_5_2, 0.0_7_3_0, 0.2_5_0_6]] ) elif model_name == "videomae-base-short-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.0_5_3_7, -0.1_5_3_9, -0.3_2_6_6] ) elif model_name == "videomae-base-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.8_1_3_1, 0.8_7_2_7, 0.8_5_4_6], [0.7_3_6_6, 0.9_3_7_7, 0.8_8_7_0], [0.5_9_3_5, 0.8_8_7_4, 0.8_5_6_4]] ) elif model_name == "videomae-base-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.1_9_6_1, -0.8_3_3_7, -0.6_3_8_9] ) else: raise ValueError(f'''Model name not supported. Should be one of {model_names}''' ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) else: print("Logits:" , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , __snake_case , atol=1e-4 ) print("Logits ok!" ) # verify loss, if applicable if model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = outputs.loss assert torch.allclose(__snake_case , __snake_case , atol=1e-4 ) print("Loss ok!" ) if pytorch_dump_folder_path is not None: print(f'''Saving model and image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__snake_case ) model.save_pretrained(__snake_case ) if push_to_hub: print("Pushing to the hub..." ) model.push_to_hub(__snake_case , organization="nielsr" ) if __name__ == "__main__": _snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) _snake_case : Optional[int] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
693
1
import os def _A ( ) -> int: """simple docstring""" with open(os.path.dirname(__snake_case ) + "/grid.txt" ) as f: __SCREAMING_SNAKE_CASE = [] # noqa: E741 for _ in range(20 ): l.append([int(__snake_case ) for x in f.readline().split()] ) __SCREAMING_SNAKE_CASE = 0 # right for i in range(20 ): for j in range(17 ): __SCREAMING_SNAKE_CASE = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: __SCREAMING_SNAKE_CASE = temp # down for i in range(17 ): for j in range(20 ): __SCREAMING_SNAKE_CASE = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: __SCREAMING_SNAKE_CASE = temp # diagonal 1 for i in range(17 ): for j in range(17 ): __SCREAMING_SNAKE_CASE = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: __SCREAMING_SNAKE_CASE = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): __SCREAMING_SNAKE_CASE = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: __SCREAMING_SNAKE_CASE = temp return maximum if __name__ == "__main__": print(solution())
693
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, **_a ) -> None: warnings.warn( "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use CLIPImageProcessor instead.", _a, ) super().__init__(*_a, **_a )
693
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available, ) _snake_case : List[Any] = { 'configuration_speecht5': [ 'SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP', 'SpeechT5Config', 'SpeechT5HifiGanConfig', ], 'feature_extraction_speecht5': ['SpeechT5FeatureExtractor'], 'processing_speecht5': ['SpeechT5Processor'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Union[str, Any] = ['SpeechT5Tokenizer'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : List[Any] = [ 'SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST', 'SpeechT5ForSpeechToText', 'SpeechT5ForSpeechToSpeech', 'SpeechT5ForTextToSpeech', 'SpeechT5Model', 'SpeechT5PreTrainedModel', 'SpeechT5HifiGan', ] if TYPE_CHECKING: from .configuration_speechta import ( SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP, SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP, SpeechTaConfig, SpeechTaHifiGanConfig, ) from .feature_extraction_speechta import SpeechTaFeatureExtractor from .processing_speechta import SpeechTaProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speechta import SpeechTaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speechta import ( SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaModel, SpeechTaPreTrainedModel, ) else: import sys _snake_case : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
693
from math import sqrt def _A ( __snake_case :int ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 for i in range(1 , int(sqrt(__snake_case ) + 1 ) ): if n % i == 0 and i != sqrt(__snake_case ): total += i + n // i elif i == sqrt(__snake_case ): total += i return total - n def _A ( __snake_case :int = 1_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = sum( i for i in range(1 , __snake_case ) if sum_of_divisors(sum_of_divisors(__snake_case ) ) == i and sum_of_divisors(__snake_case ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
693
1
from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging _snake_case : List[Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""audio_values""", """audio_mask"""] def __init__( self, _a=20_48, _a=1, _a=[16, 16], _a=1_28, _a=4_41_00, _a=86, _a=20_48, _a=0.0, **_a, ) -> str: super().__init__( feature_size=_a, sampling_rate=_a, padding_value=_a, **_a, ) __SCREAMING_SNAKE_CASE = spectrogram_length __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = feature_size // self.patch_size[1] __SCREAMING_SNAKE_CASE = n_fft __SCREAMING_SNAKE_CASE = sampling_rate // hop_length_to_sampling_rate __SCREAMING_SNAKE_CASE = sampling_rate __SCREAMING_SNAKE_CASE = padding_value __SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=1 + n_fft // 2, num_mel_filters=_a, min_frequency=0.0, max_frequency=2_2050.0, sampling_rate=_a, norm="slaney", mel_scale="slaney", ).T def __lowerCAmelCase ( self, _a ) -> np.ndarray: __SCREAMING_SNAKE_CASE = spectrogram( _a, window_function(self.n_fft, "hann" ), frame_length=self.n_fft, hop_length=self.hop_length, power=2.0, mel_filters=self.mel_filters.T, log_mel="dB", db_range=80.0, ) __SCREAMING_SNAKE_CASE = log_spec[:, :-1] __SCREAMING_SNAKE_CASE = log_spec - 20.0 __SCREAMING_SNAKE_CASE = np.clip(log_spec / 40.0, -2.0, 0.0 ) + 1.0 return log_spec def __call__( self, _a, _a = None, _a = True, _a = None, _a = False, _a = False, **_a, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( "This feature extractor is set to support sampling rate" f''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled''' f''' with {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) __SCREAMING_SNAKE_CASE = isinstance(_a, np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'''Only mono-channel audio is supported for input to {self}''' ) __SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_a, (list, tuple) ) and (isinstance(raw_speech[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray([speech], dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis __SCREAMING_SNAKE_CASE = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0], _a ): __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ) for feature in audio_features] # Create audio attention mask __SCREAMING_SNAKE_CASE = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: __SCREAMING_SNAKE_CASE = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] __SCREAMING_SNAKE_CASE = np.array(_a ).astype(np.floataa ) # convert into correct format for padding __SCREAMING_SNAKE_CASE = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch __SCREAMING_SNAKE_CASE = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) __SCREAMING_SNAKE_CASE = padded_audio_features * self.padding_value for i in range(len(_a ) ): __SCREAMING_SNAKE_CASE = audio_features[i] __SCREAMING_SNAKE_CASE = feature # return as BatchFeature if return_attention_mask: __SCREAMING_SNAKE_CASE = {"audio_values": padded_audio_features, "audio_mask": audio_mask} else: __SCREAMING_SNAKE_CASE = {"audio_values": padded_audio_features} __SCREAMING_SNAKE_CASE = BatchFeature(data=_a, tensor_type=_a ) return encoded_inputs
693
def _A ( __snake_case :int , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Sequence, Value from .base import TaskTemplate @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): # `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization SCREAMING_SNAKE_CASE__ =field(default="""question-answering-extractive""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) SCREAMING_SNAKE_CASE__ =Features({"""question""": Value("""string""" ), """context""": Value("""string""" )} ) SCREAMING_SNAKE_CASE__ =Features( { """answers""": Sequence( { """text""": Value("""string""" ), """answer_start""": Value("""int32""" ), } ) } ) SCREAMING_SNAKE_CASE__ ="question" SCREAMING_SNAKE_CASE__ ="context" SCREAMING_SNAKE_CASE__ ="answers" @property def __lowerCAmelCase ( self ) -> Dict[str, str]: return {self.question_column: "question", self.context_column: "context", self.answers_column: "answers"}
693
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a=99, _a=13, _a=7, _a=9, _a=True, _a=True, _a=False, _a=32, _a=5, _a=4, _a=37, _a=8, _a=0.1, _a=0.002, _a=1, _a=0, _a=0, _a=None, _a=None, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = encoder_seq_length __SCREAMING_SNAKE_CASE = decoder_seq_length # For common tests __SCREAMING_SNAKE_CASE = self.decoder_seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_attention_mask __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = d_ff __SCREAMING_SNAKE_CASE = relative_attention_num_buckets __SCREAMING_SNAKE_CASE = dropout_rate __SCREAMING_SNAKE_CASE = initializer_factor __SCREAMING_SNAKE_CASE = eos_token_id __SCREAMING_SNAKE_CASE = pad_token_id __SCREAMING_SNAKE_CASE = decoder_start_token_id __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = decoder_layers def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig.from_pretrained("google/umt5-base" ) def __lowerCAmelCase ( self, _a, _a, _a, _a=None, _a=None, _a=None, _a=None, _a=None, ) -> int: if attention_mask is None: __SCREAMING_SNAKE_CASE = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: __SCREAMING_SNAKE_CASE = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_hidden_layers, config.num_attention_heads, device=_a ) if decoder_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_decoder_layers, config.num_attention_heads, device=_a ) if cross_attn_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_attention_heads, device=_a ) 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, } def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.encoder_seq_length], self.vocab_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.decoder_seq_length], self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input __SCREAMING_SNAKE_CASE = input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = decoder_input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = self.get_config() __SCREAMING_SNAKE_CASE = config.num_attention_heads __SCREAMING_SNAKE_CASE = self.prepare_inputs_dict(_a, _a, _a ) return config, input_dict def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig( vocab_size=1_66, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self ) -> Union[str, Any]: return TaConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ) model.to(_a ) model.eval() __SCREAMING_SNAKE_CASE = model( input_ids=_a, decoder_input_ids=_a, attention_mask=_a, decoder_attention_mask=_a, ) __SCREAMING_SNAKE_CASE = model(input_ids=_a, decoder_input_ids=_a ) __SCREAMING_SNAKE_CASE = result.last_hidden_state __SCREAMING_SNAKE_CASE = result.past_key_values __SCREAMING_SNAKE_CASE = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size(), (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size(), (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_a ), config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ), 4 ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Tuple: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).get_decoder().to(_a ).eval() # first forward pass __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) self.parent.assertTrue(len(_a ) == len(_a ) ) self.parent.assertTrue(len(_a ) == len(_a ) + 1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 1), config.vocab_size ) # append to next input_ids and __SCREAMING_SNAKE_CASE = torch.cat([input_ids, next_tokens], dim=-1 ) __SCREAMING_SNAKE_CASE = model(_a )["last_hidden_state"] __SCREAMING_SNAKE_CASE = model(_a, past_key_values=_a )["last_hidden_state"] # select random slice __SCREAMING_SNAKE_CASE = ids_tensor((1,), output_from_past.shape[-1] ).item() __SCREAMING_SNAKE_CASE = output_from_no_past[:, -1, random_slice_idx].detach() __SCREAMING_SNAKE_CASE = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_a, _a, atol=1E-3 ) ) def __lowerCAmelCase ( self, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).to(_a ).half().eval() __SCREAMING_SNAKE_CASE = model(**_a )["last_hidden_state"] self.parent.assertFalse(torch.isnan(_a ).any().item() ) @require_torch class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ =(UMTaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ =( { """conversational""": UMTaForConditionalGeneration, """feature-extraction""": UMTaModel, """summarization""": UMTaForConditionalGeneration, """text2text-generation""": UMTaForConditionalGeneration, """translation""": UMTaForConditionalGeneration, """question-answering""": UMTaForQuestionAnswering, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True # The small UMT5 model needs higher percentages for CPU/MP tests SCREAMING_SNAKE_CASE__ =[0.8, 0.9] def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = UMTaModelTester(self ) @unittest.skip("Test has a segmentation fault on torch 1.8.0" ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = UMTaModel(config_and_inputs[0] ).to(_a ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _a, (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]), f'''{tmpdirname}/t5_test.onnx''', export_params=_a, opset_version=9, input_names=["input_ids", "decoder_input_ids"], ) @unittest.skipIf(torch_device == "cpu", "Cant do half precision" ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_a ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ["encoder_attentions", "decoder_attentions", "cross_attentions"] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = config_and_inputs[0] __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration(_a ).eval() model.to(_a ) __SCREAMING_SNAKE_CASE = { "head_mask": torch.zeros(config.num_layers, config.num_heads, device=_a ), "decoder_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), "cross_attn_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), } for attn_name, (name, mask) in zip(_a, head_masking.items() ): __SCREAMING_SNAKE_CASE = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_heads, device=_a ) __SCREAMING_SNAKE_CASE = model.generate( config_and_inputs[1]["input_ids"], num_beams=1, max_length=3, output_attentions=_a, return_dict_in_generate=_a, **_a, ) # We check the state of decoder_attentions and cross_attentions just from the last step __SCREAMING_SNAKE_CASE = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ), 0.0 ) @unittest.skip("Does not work on the tiny model as we keep hitting edge cases." ) def __lowerCAmelCase ( self ) -> int: pass @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow @unittest.skip( "Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged" ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration.from_pretrained("google/umt5-small", return_dict=_a ).to(_a ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("google/umt5-small", use_fast=_a, legacy=_a ) __SCREAMING_SNAKE_CASE = [ "Bonjour monsieur <extra_id_0> bien <extra_id_1>.", "No se como puedo <extra_id_0>.", "This is the reason why we <extra_id_0> them.", "The <extra_id_0> walks in <extra_id_1>, seats", "A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.", ] __SCREAMING_SNAKE_CASE = tokenizer(_a, return_tensors="pt", padding=_a ).input_ids # fmt: off __SCREAMING_SNAKE_CASE = torch.tensor( [ [ 3_85_30, 21_07_03, 25_62_99, 14_10, 25_62_98, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_26, 3_21, 6_71, 2_59_22, 25_62_99, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 14_60, 3_39, 3_12, 1_90_14, 1_06_20, 7_58, 25_62_99, 23_55,2_74, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_17, 25_62_99, 1_48_69, 2_81, 3_01, 25_62_98, 2_75, 11_99_83,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_20, 25_62_99, 1_48_69, 2_81, 22_34, 2_89, 22_75, 3_33,6_13_91, 2_89, 25_62_98, 5_43, 25_62_97, 16_87_14, 3_29, 25_62_96,2_74, 1], ] ) # fmt: on torch.testing.assert_allclose(_a, _a ) __SCREAMING_SNAKE_CASE = model.generate(input_ids.to(_a ) ) __SCREAMING_SNAKE_CASE = [ "<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>", "<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", ] __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, _a )
693
1
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _A ( __snake_case :BertModel , __snake_case :str , __snake_case :str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") __SCREAMING_SNAKE_CASE = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__snake_case ): os.makedirs(__snake_case ) __SCREAMING_SNAKE_CASE = model.state_dict() def to_tf_var_name(__snake_case :str ): for patt, repl in iter(__snake_case ): __SCREAMING_SNAKE_CASE = name.replace(__snake_case , __snake_case ) return f'''bert/{name}''' def create_tf_var(__snake_case :np.ndarray , __snake_case :str , __snake_case :tf.Session ): __SCREAMING_SNAKE_CASE = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE = tf.get_variable(dtype=__snake_case , shape=tensor.shape , name=__snake_case , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__snake_case ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE = to_tf_var_name(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE = torch_tensor.T __SCREAMING_SNAKE_CASE = create_tf_var(tensor=__snake_case , name=__snake_case , session=__snake_case ) tf.keras.backend.set_value(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = session.run(__snake_case ) print(f'''Successfully created {tf_name}: {np.allclose(__snake_case , __snake_case )}''' ) __SCREAMING_SNAKE_CASE = tf.train.Saver(tf.trainable_variables() ) saver.save(__snake_case , os.path.join(__snake_case , model_name.replace("-" , "_" ) + ".ckpt" ) ) def _A ( __snake_case :str=None ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__snake_case , required=__snake_case , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__snake_case , default=__snake_case , required=__snake_case , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__snake_case , required=__snake_case , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__snake_case , required=__snake_case , help="Directory in which to save tensorflow model" ) __SCREAMING_SNAKE_CASE = parser.parse_args(__snake_case ) __SCREAMING_SNAKE_CASE = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__snake_case , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
693
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = BlipImageProcessor() __SCREAMING_SNAKE_CASE = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) __SCREAMING_SNAKE_CASE = BlipaProcessor(_a, _a ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self, **_a ) -> Any: return AutoProcessor.from_pretrained(self.tmpdirname, **_a ).tokenizer def __lowerCAmelCase ( self, **_a ) -> Optional[int]: return AutoProcessor.from_pretrained(self.tmpdirname, **_a ).image_processor def __lowerCAmelCase ( self ) -> Tuple: shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = [np.random.randint(2_55, size=(3, 30, 4_00), dtype=np.uinta )] __SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(_a, 0, -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)" ) __SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=_a, padding_value=1.0 ) __SCREAMING_SNAKE_CASE = BlipaProcessor.from_pretrained( self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=_a, padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer, _a ) self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor, _a ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=_a, image_processor=_a ) __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = image_processor(_a, return_tensors="np" ) __SCREAMING_SNAKE_CASE = processor(images=_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 __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=_a, image_processor=_a ) __SCREAMING_SNAKE_CASE = "lower newer" __SCREAMING_SNAKE_CASE = processor(text=_a ) __SCREAMING_SNAKE_CASE = tokenizer(_a, return_token_type_ids=_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key], encoded_processor[key] ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=_a, image_processor=_a ) __SCREAMING_SNAKE_CASE = "lower newer" __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = processor(text=_a, images=_a ) self.assertListEqual(list(inputs.keys() ), ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=_a, image_processor=_a ) __SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __SCREAMING_SNAKE_CASE = processor.batch_decode(_a ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertListEqual(_a, _a ) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.get_image_processor() __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = BlipaProcessor(tokenizer=_a, image_processor=_a ) __SCREAMING_SNAKE_CASE = "lower newer" __SCREAMING_SNAKE_CASE = self.prepare_image_inputs() __SCREAMING_SNAKE_CASE = processor(text=_a, images=_a ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ), ["pixel_values", "input_ids", "attention_mask"] )
693
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
1
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, _a=None, _a=None, **_a ) -> Dict: super().__init__(*_a, **_a ) __SCREAMING_SNAKE_CASE = eval_examples __SCREAMING_SNAKE_CASE = post_process_function def __lowerCAmelCase ( self, _a=None, _a=None, _a=None, _a = "eval" ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset __SCREAMING_SNAKE_CASE = self.get_eval_dataloader(_a ) __SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. __SCREAMING_SNAKE_CASE = self.compute_metrics __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop __SCREAMING_SNAKE_CASE = time.time() try: __SCREAMING_SNAKE_CASE = eval_loop( _a, description="Evaluation", prediction_loss_only=True if compute_metrics is None else None, ignore_keys=_a, metric_key_prefix=_a, ) finally: __SCREAMING_SNAKE_CASE = compute_metrics __SCREAMING_SNAKE_CASE = self.args.eval_batch_size * self.args.world_size if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( _a, _a, num_samples=output.num_samples, num_steps=math.ceil(output.num_samples / total_batch_size ), ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default __SCREAMING_SNAKE_CASE = self.post_process_function(_a, _a, output.predictions ) __SCREAMING_SNAKE_CASE = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): __SCREAMING_SNAKE_CASE = metrics.pop(_a ) metrics.update(output.metrics ) else: __SCREAMING_SNAKE_CASE = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(_a ) 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() ) __SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args, self.state, self.control, _a ) return metrics def __lowerCAmelCase ( self, _a, _a, _a=None, _a = "test" ) -> Tuple: __SCREAMING_SNAKE_CASE = self.get_test_dataloader(_a ) # Temporarily disable metric computation, we will do it in the loop here. __SCREAMING_SNAKE_CASE = self.compute_metrics __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop __SCREAMING_SNAKE_CASE = time.time() try: __SCREAMING_SNAKE_CASE = eval_loop( _a, description="Prediction", prediction_loss_only=True if compute_metrics is None else None, ignore_keys=_a, metric_key_prefix=_a, ) finally: __SCREAMING_SNAKE_CASE = compute_metrics __SCREAMING_SNAKE_CASE = self.args.eval_batch_size * self.args.world_size if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( _a, _a, num_samples=output.num_samples, num_steps=math.ceil(output.num_samples / total_batch_size ), ) ) if self.post_process_function is None or self.compute_metrics is None: return output __SCREAMING_SNAKE_CASE = self.post_process_function(_a, _a, output.predictions, "predict" ) __SCREAMING_SNAKE_CASE = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'''{metric_key_prefix}_''' ): __SCREAMING_SNAKE_CASE = metrics.pop(_a ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions, label_ids=predictions.label_ids, metrics=_a )
693
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Dict: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 20_00, _a = None, _a = "pil", _a = True, **_a, ) -> Union[ImagePipelineOutput, Tuple]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a ) * self.scheduler.init_noise_sigma __SCREAMING_SNAKE_CASE = sample.to(self.device ) self.scheduler.set_timesteps(_a ) self.scheduler.set_sigmas(_a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): __SCREAMING_SNAKE_CASE = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): __SCREAMING_SNAKE_CASE = self.unet(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct(_a, _a, generator=_a ).prev_sample # prediction step __SCREAMING_SNAKE_CASE = model(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_pred(_a, _a, _a, generator=_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = output.prev_sample, output.prev_sample_mean __SCREAMING_SNAKE_CASE = sample_mean.clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_a )
693
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : 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', 'adapter_layer': 'encoder.layers.*.adapter_layer', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', 'pooling_layer.linear': 'projector', 'pooling_layer.projection': 'classifier', } _snake_case : Tuple = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'projector', 'classifier', ] def _A ( __snake_case :Any ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = {} with open(__snake_case , "r" ) as file: for line_number, line in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = line.strip() if line: __SCREAMING_SNAKE_CASE = line.split() __SCREAMING_SNAKE_CASE = line_number __SCREAMING_SNAKE_CASE = words[0] __SCREAMING_SNAKE_CASE = value return result def _A ( __snake_case :Dict , __snake_case :str , __snake_case :Any , __snake_case :int , __snake_case :str ) -> Tuple: """simple docstring""" for attribute in key.split("." ): __SCREAMING_SNAKE_CASE = getattr(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__snake_case ): __SCREAMING_SNAKE_CASE = PARAM_MAPPING[full_name.split("." )[-1]] __SCREAMING_SNAKE_CASE = "param" if weight_type is not None and weight_type != "param": __SCREAMING_SNAKE_CASE = getattr(__snake_case , __snake_case ).shape elif weight_type is not None and weight_type == "param": __SCREAMING_SNAKE_CASE = hf_pointer for attribute in hf_param_name.split("." ): __SCREAMING_SNAKE_CASE = getattr(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = shape_pointer.shape # let's reduce dimension __SCREAMING_SNAKE_CASE = value[0] else: __SCREAMING_SNAKE_CASE = 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": __SCREAMING_SNAKE_CASE = value elif weight_type == "weight_g": __SCREAMING_SNAKE_CASE = value elif weight_type == "weight_v": __SCREAMING_SNAKE_CASE = value elif weight_type == "bias": __SCREAMING_SNAKE_CASE = value elif weight_type == "param": for attribute in hf_param_name.split("." ): __SCREAMING_SNAKE_CASE = getattr(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = value else: __SCREAMING_SNAKE_CASE = value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def _A ( __snake_case :Optional[Any] , __snake_case :Union[str, Any] , __snake_case :Tuple , __snake_case :str , __snake_case :Dict ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__snake_case ): __SCREAMING_SNAKE_CASE = PARAM_MAPPING[full_name.split("." )[-1]] __SCREAMING_SNAKE_CASE = "param" if weight_type is not None and weight_type != "param": __SCREAMING_SNAKE_CASE = ".".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": __SCREAMING_SNAKE_CASE = ".".join([key, hf_param_name] ) else: __SCREAMING_SNAKE_CASE = key __SCREAMING_SNAKE_CASE = value if "lm_head" in full_key else value[0] _snake_case : List[str] = { 'W_a': 'linear_1.weight', 'W_b': 'linear_2.weight', 'b_a': 'linear_1.bias', 'b_b': 'linear_2.bias', 'ln_W': 'norm.weight', 'ln_b': 'norm.bias', } def _A ( __snake_case :Any , __snake_case :Optional[Any] , __snake_case :Tuple=None , __snake_case :int=None ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = False for key, mapped_key in MAPPING.items(): __SCREAMING_SNAKE_CASE = "wav2vec2." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: __SCREAMING_SNAKE_CASE = True if "*" in mapped_key: __SCREAMING_SNAKE_CASE = name.split(__snake_case )[0].split("." )[-2] __SCREAMING_SNAKE_CASE = mapped_key.replace("*" , __snake_case ) if "weight_g" in name: __SCREAMING_SNAKE_CASE = "weight_g" elif "weight_v" in name: __SCREAMING_SNAKE_CASE = "weight_v" elif "bias" in name: __SCREAMING_SNAKE_CASE = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj __SCREAMING_SNAKE_CASE = "weight" else: __SCREAMING_SNAKE_CASE = None if hf_dict is not None: rename_dict(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) else: set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) return is_used return is_used def _A ( __snake_case :Optional[int] , __snake_case :str , __snake_case :Optional[Any] ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = fairseq_model.state_dict() __SCREAMING_SNAKE_CASE = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): __SCREAMING_SNAKE_CASE = False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == "group" , ) __SCREAMING_SNAKE_CASE = True else: __SCREAMING_SNAKE_CASE = load_wavaveca_layer(__snake_case , __snake_case , __snake_case ) if not is_used: unused_weights.append(__snake_case ) logger.warning(f'''Unused weights: {unused_weights}''' ) def _A ( __snake_case :Union[str, Any] , __snake_case :int , __snake_case :List[Any] , __snake_case :List[str] , __snake_case :int ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = full_name.split("conv_layers." )[-1] __SCREAMING_SNAKE_CASE = name.split("." ) __SCREAMING_SNAKE_CASE = int(items[0] ) __SCREAMING_SNAKE_CASE = 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.''' ) __SCREAMING_SNAKE_CASE = 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.''' ) __SCREAMING_SNAKE_CASE = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''' ) __SCREAMING_SNAKE_CASE = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''' ) __SCREAMING_SNAKE_CASE = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__snake_case ) @torch.no_grad() def _A ( __snake_case :List[str] , __snake_case :List[Any] , __snake_case :Union[str, Any]=None , __snake_case :Tuple=None , __snake_case :Tuple=True , __snake_case :Union[str, Any]=False ) -> Tuple: """simple docstring""" if config_path is not None: __SCREAMING_SNAKE_CASE = WavaVecaConfig.from_pretrained(__snake_case ) else: __SCREAMING_SNAKE_CASE = WavaVecaConfig() if is_seq_class: __SCREAMING_SNAKE_CASE = read_txt_into_dict(__snake_case ) __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = WavaVecaForSequenceClassification(__snake_case ) __SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) feature_extractor.save_pretrained(__snake_case ) elif is_finetuned: if dict_path: __SCREAMING_SNAKE_CASE = Dictionary.load(__snake_case ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __SCREAMING_SNAKE_CASE = target_dict.pad_index __SCREAMING_SNAKE_CASE = target_dict.bos_index __SCREAMING_SNAKE_CASE = target_dict.eos_index __SCREAMING_SNAKE_CASE = len(target_dict.symbols ) __SCREAMING_SNAKE_CASE = os.path.join(__snake_case , "vocab.json" ) if not os.path.isdir(__snake_case ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(__snake_case ) ) return os.makedirs(__snake_case , exist_ok=__snake_case ) __SCREAMING_SNAKE_CASE = target_dict.indices # fairseq has the <pad> and <s> switched __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 1 with open(__snake_case , "w" , encoding="utf-8" ) as vocab_handle: json.dump(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = WavaVecaCTCTokenizer( __snake_case , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=__snake_case , ) __SCREAMING_SNAKE_CASE = True if config.feat_extract_norm == "layer" else False __SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) __SCREAMING_SNAKE_CASE = WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) processor.save_pretrained(__snake_case ) __SCREAMING_SNAKE_CASE = WavaVecaForCTC(__snake_case ) else: __SCREAMING_SNAKE_CASE = WavaVecaForPreTraining(__snake_case ) if is_finetuned or is_seq_class: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: __SCREAMING_SNAKE_CASE = argparse.Namespace(task="audio_pretraining" ) __SCREAMING_SNAKE_CASE = fairseq.tasks.setup_task(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__snake_case ) __SCREAMING_SNAKE_CASE = model[0].eval() recursively_load_weights(__snake_case , __snake_case , not is_finetuned ) hf_wavavec.save_pretrained(__snake_case ) if __name__ == "__main__": _snake_case : Tuple = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) parser.add_argument( '--is_seq_class', action='store_true', help='Whether the model to convert is a fine-tuned sequence classification model or not', ) _snake_case : Tuple = parser.parse_args() _snake_case : Any = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
693
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
1
from __future__ import annotations import collections import pprint from pathlib import Path def _A ( __snake_case :str ) -> str: """simple docstring""" return "".join(sorted(__snake_case ) ) def _A ( __snake_case :str ) -> list[str]: """simple docstring""" return word_by_signature[signature(__snake_case )] _snake_case : str = Path(__file__).parent.joinpath('words.txt').read_text(encoding='utf-8') _snake_case : List[str] = sorted({word.strip().lower() for word in data.splitlines()}) _snake_case : Any = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": _snake_case : Tuple = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open('anagrams.txt', 'w') as file: file.write('all_anagrams = \n ') file.write(pprint.pformat(all_anagrams))
693
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
1
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Optional[int]: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 50, _a = None, _a = "pil", _a = True, **_a, ) -> Union[Tuple, ImagePipelineOutput]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a, device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(_a ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper __SCREAMING_SNAKE_CASE = self.scheduler.schedule[t] __SCREAMING_SNAKE_CASE = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.scheduler.add_noise_to_input(_a, _a, generator=_a ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. __SCREAMING_SNAKE_CASE = (sigma_hat / 2) * model((sample_hat + 1) / 2, sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev __SCREAMING_SNAKE_CASE = self.scheduler.step(_a, _a, _a, _a ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. __SCREAMING_SNAKE_CASE = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2, sigma_prev / 2 ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct( _a, _a, _a, _a, step_output.prev_sample, step_output["derivative"], ) __SCREAMING_SNAKE_CASE = step_output.prev_sample __SCREAMING_SNAKE_CASE = (sample / 2 + 0.5).clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
693
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> Any: __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None def __repr__( self ) -> str: return f'''Node({self.data})''' class __SCREAMING_SNAKE_CASE : def __init__( self ) -> Tuple: __SCREAMING_SNAKE_CASE = None def __iter__( self ) -> Any: __SCREAMING_SNAKE_CASE = self.head while node: yield node.data __SCREAMING_SNAKE_CASE = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_a ) for item in self] ) def __getitem__( self, _a ) -> Any: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self, _a, _a ) -> None: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) __SCREAMING_SNAKE_CASE = self.head for _ in range(_a ): __SCREAMING_SNAKE_CASE = current.next __SCREAMING_SNAKE_CASE = data def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(len(self ), _a ) def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(0, _a ) def __lowerCAmelCase ( self, _a, _a ) -> None: if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) __SCREAMING_SNAKE_CASE = Node(_a ) if self.head is None: __SCREAMING_SNAKE_CASE = new_node elif index == 0: __SCREAMING_SNAKE_CASE = self.head # link new_node to head __SCREAMING_SNAKE_CASE = new_node else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self ) -> None: # print every node data print(self ) def __lowerCAmelCase ( self ) -> Any: return self.delete_nth(0 ) def __lowerCAmelCase ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowerCAmelCase ( self, _a = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) __SCREAMING_SNAKE_CASE = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE = self.head.next else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next.next return delete_node.data def __lowerCAmelCase ( self ) -> bool: return self.head is None def __lowerCAmelCase ( self ) -> None: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE = prev def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = LinkedList() assert linked_list.is_empty() is True assert str(__snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(__snake_case ) == i linked_list.insert_nth(__snake_case , i + 1 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(__snake_case ) == 9 assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(-8 , 1 ) ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_9_2.5_5_5_5_5, "Hello, world!", 7_7.9, Node(10 ), None, None, 1_2.2_0, ] __SCREAMING_SNAKE_CASE = LinkedList() for i in test_input: linked_list.insert_tail(__snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(__snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE = linked_list.delete_head() assert result == -9 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE = linked_list.delete_tail() assert result == 1_2.2 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE = linked_list.delete_nth(10 ) assert result is None assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(__snake_case ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(__snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _A ( ) -> Union[str, Any]: """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(__snake_case ) print("\nReading/changing Node data using indexing:" ) print(f'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE = input("Enter New Value: " ).strip() print("New list:" ) print(__snake_case ) print(f'''length of linked_list is : {len(__snake_case )}''' ) if __name__ == "__main__": main()
693
1
import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. _snake_case : Optional[Any] = {'LayoutLMv2Config', 'LayoutLMv3Config'} @is_pipeline_test class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): SCREAMING_SNAKE_CASE__ =MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE__ =TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: SCREAMING_SNAKE_CASE__ ={config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: SCREAMING_SNAKE_CASE__ ={ config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def __lowerCAmelCase ( self, _a, _a, _a ) -> Tuple: __SCREAMING_SNAKE_CASE = ZeroShotClassificationPipeline( model=_a, tokenizer=_a, candidate_labels=["polics", "health"] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def __lowerCAmelCase ( self, _a, _a ) -> Tuple: __SCREAMING_SNAKE_CASE = classifier("Who are you voting for in 2020?", candidate_labels="politics" ) self.assertEqual(_a, {"sequence": ANY(_a ), "labels": [ANY(_a )], "scores": [ANY(_a )]} ) # No kwarg __SCREAMING_SNAKE_CASE = classifier("Who are you voting for in 2020?", ["politics"] ) self.assertEqual(_a, {"sequence": ANY(_a ), "labels": [ANY(_a )], "scores": [ANY(_a )]} ) __SCREAMING_SNAKE_CASE = classifier("Who are you voting for in 2020?", candidate_labels=["politics"] ) self.assertEqual(_a, {"sequence": ANY(_a ), "labels": [ANY(_a )], "scores": [ANY(_a )]} ) __SCREAMING_SNAKE_CASE = classifier("Who are you voting for in 2020?", candidate_labels="politics, public health" ) self.assertEqual( _a, {"sequence": ANY(_a ), "labels": [ANY(_a ), ANY(_a )], "scores": [ANY(_a ), ANY(_a )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ), 1.0 ) __SCREAMING_SNAKE_CASE = classifier("Who are you voting for in 2020?", candidate_labels=["politics", "public health"] ) self.assertEqual( _a, {"sequence": ANY(_a ), "labels": [ANY(_a ), ANY(_a )], "scores": [ANY(_a ), ANY(_a )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ), 1.0 ) __SCREAMING_SNAKE_CASE = classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="This text is about {}" ) self.assertEqual(_a, {"sequence": ANY(_a ), "labels": [ANY(_a )], "scores": [ANY(_a )]} ) # https://github.com/huggingface/transformers/issues/13846 __SCREAMING_SNAKE_CASE = classifier(["I am happy"], ["positive", "negative"] ) self.assertEqual( _a, [ {"sequence": ANY(_a ), "labels": [ANY(_a ), ANY(_a )], "scores": [ANY(_a ), ANY(_a )]} for i in range(1 ) ], ) __SCREAMING_SNAKE_CASE = classifier(["I am happy", "I am sad"], ["positive", "negative"] ) self.assertEqual( _a, [ {"sequence": ANY(_a ), "labels": [ANY(_a ), ANY(_a )], "scores": [ANY(_a ), ANY(_a )]} for i in range(2 ) ], ) with self.assertRaises(_a ): classifier("", candidate_labels="politics" ) with self.assertRaises(_a ): classifier(_a, candidate_labels="politics" ) with self.assertRaises(_a ): classifier("Who are you voting for in 2020?", candidate_labels="" ) with self.assertRaises(_a ): classifier("Who are you voting for in 2020?", candidate_labels=_a ) with self.assertRaises(_a ): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="Not formatting template", ) with self.assertRaises(_a ): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template=_a, ) self.run_entailment_id(_a ) def __lowerCAmelCase ( self, _a ) -> str: __SCREAMING_SNAKE_CASE = zero_shot_classifier.model.config __SCREAMING_SNAKE_CASE = config.labelaid __SCREAMING_SNAKE_CASE = zero_shot_classifier.entailment_id __SCREAMING_SNAKE_CASE = {"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2} self.assertEqual(zero_shot_classifier.entailment_id, -1 ) __SCREAMING_SNAKE_CASE = {"entailment": 0, "neutral": 1, "contradiction": 2} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) __SCREAMING_SNAKE_CASE = {"ENTAIL": 0, "NON-ENTAIL": 1} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) __SCREAMING_SNAKE_CASE = {"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0} self.assertEqual(zero_shot_classifier.entailment_id, 2 ) __SCREAMING_SNAKE_CASE = original_labelaid self.assertEqual(_a, zero_shot_classifier.entailment_id ) @require_torch def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="pt", ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( "Who are you voting for in 2020?" * 1_00, candidate_labels=["politics", "public health", "science"] ) @require_torch def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="pt", ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(_a ), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @require_tf def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="tf", ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(_a ), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @slow @require_torch def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="pt" ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(_a ), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data.", candidate_labels=["machine learning", "statistics", "translation", "vision"], multi_label=_a, ) self.assertEqual( nested_simplify(_a ), { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], }, ) @slow @require_tf def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="tf" ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(_a ), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) __SCREAMING_SNAKE_CASE = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data.", candidate_labels=["machine learning", "statistics", "translation", "vision"], multi_label=_a, ) self.assertEqual( nested_simplify(_a ), { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], }, )
693
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
1
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =XGLMConfig SCREAMING_SNAKE_CASE__ ={} SCREAMING_SNAKE_CASE__ ="""gelu""" def __init__( self, _a, _a=14, _a=7, _a=True, _a=True, _a=True, _a=99, _a=32, _a=2, _a=4, _a=37, _a="gelu", _a=0.1, _a=0.1, _a=5_12, _a=0.02, ) -> str: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_input_mask __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = ffn_dim __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 1 def __lowerCAmelCase ( self ) -> str: return XGLMConfig.from_pretrained("facebook/xglm-564M" ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length], self.vocab_size ), clip_value_min=0, clip_value_max=3 ) __SCREAMING_SNAKE_CASE = None if self.use_input_mask: __SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) __SCREAMING_SNAKE_CASE = self.get_config() __SCREAMING_SNAKE_CASE = floats_tensor([self.num_hidden_layers, self.num_attention_heads], 2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowerCAmelCase ( self ) -> Optional[Any]: return XGLMConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, num_layers=self.num_hidden_layers, attention_heads=self.num_attention_heads, ffn_dim=self.ffn_dim, activation_function=self.activation_function, activation_dropout=self.activation_dropout, attention_dropout=self.attention_dropout, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, use_cache=_a, bos_token_id=self.bos_token_id, eos_token_id=self.eos_token_id, pad_token_id=self.pad_token_id, return_dict=_a, ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ( __SCREAMING_SNAKE_CASE ) , ) = config_and_inputs __SCREAMING_SNAKE_CASE = { "input_ids": input_ids, "head_mask": head_mask, } return config, inputs_dict @require_tf class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () SCREAMING_SNAKE_CASE__ =(TFXGLMForCausalLM,) if is_tf_available() else () SCREAMING_SNAKE_CASE__ =( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = TFXGLMModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self, config_class=_a, n_embd=37 ) def __lowerCAmelCase ( self ) -> List[str]: self.config_tester.run_common_tests() @slow def __lowerCAmelCase ( self ) -> Union[str, Any]: for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE = TFXGLMModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor." ) def __lowerCAmelCase ( self ) -> int: super().test_resize_token_embeddings() @require_tf class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def __lowerCAmelCase ( self, _a=True ) -> Any: __SCREAMING_SNAKE_CASE = TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M" ) __SCREAMING_SNAKE_CASE = tf.convert_to_tensor([[2, 2_68, 98_65]], dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __SCREAMING_SNAKE_CASE = [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81] # fmt: on __SCREAMING_SNAKE_CASE = model.generate(_a, do_sample=_a, num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist(), _a ) @slow def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) __SCREAMING_SNAKE_CASE = TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M" ) tf.random.set_seed(0 ) __SCREAMING_SNAKE_CASE = tokenizer("Today is a nice day and", return_tensors="tf" ) __SCREAMING_SNAKE_CASE = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(":/CPU:0" ): __SCREAMING_SNAKE_CASE = model.generate(_a, do_sample=_a, seed=[7, 0] ) __SCREAMING_SNAKE_CASE = tokenizer.decode(output_ids[0], skip_special_tokens=_a ) __SCREAMING_SNAKE_CASE = ( "Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due" ) self.assertEqual(_a, _a ) @slow def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M" ) __SCREAMING_SNAKE_CASE = XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) __SCREAMING_SNAKE_CASE = "left" # use different length sentences to test batching __SCREAMING_SNAKE_CASE = [ "This is an extremelly long sentence that only exists to test the ability of the model to cope with " "left-padding, such as in batched generation. The output for the sequence below should be the same " "regardless of whether left padding is applied or not. When", "Hello, my dog is a little", ] __SCREAMING_SNAKE_CASE = tokenizer(_a, return_tensors="tf", padding=_a ) __SCREAMING_SNAKE_CASE = inputs["input_ids"] __SCREAMING_SNAKE_CASE = model.generate(input_ids=_a, attention_mask=inputs["attention_mask"], max_new_tokens=12 ) __SCREAMING_SNAKE_CASE = tokenizer(sentences[0], return_tensors="tf" ).input_ids __SCREAMING_SNAKE_CASE = model.generate(input_ids=_a, max_new_tokens=12 ) __SCREAMING_SNAKE_CASE = tokenizer(sentences[1], return_tensors="tf" ).input_ids __SCREAMING_SNAKE_CASE = model.generate(input_ids=_a, max_new_tokens=12 ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a, skip_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer.decode(output_non_padded[0], skip_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer.decode(output_padded[0], skip_special_tokens=_a ) __SCREAMING_SNAKE_CASE = [ "This is an extremelly long sentence that only exists to test the ability of the model to cope with " "left-padding, such as in batched generation. The output for the sequence below should be the same " "regardless of whether left padding is applied or not. When left padding is applied, the sequence will be " "a single", "Hello, my dog is a little bit of a shy one, but he is very friendly", ] self.assertListEqual(_a, _a ) self.assertListEqual(_a, [non_padded_sentence, padded_sentence] )
693
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
1
import os import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from huggingface_hub.file_download import http_get from requests.exceptions import HTTPError from transformers import ( AlbertTokenizer, AutoTokenizer, BertTokenizer, BertTokenizerFast, GPTaTokenizerFast, is_tokenizers_available, ) from transformers.testing_utils import TOKEN, USER, is_staging_test, require_tokenizers from transformers.tokenization_utils import Trie sys.path.append(str(Path(__file__).parent.parent / 'utils')) from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Optional[int]: # A mock response for an HTTP head request to emulate server down __SCREAMING_SNAKE_CASE = mock.Mock() __SCREAMING_SNAKE_CASE = 5_00 __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = HTTPError __SCREAMING_SNAKE_CASE = {} # Download this model to make sure it's in the cache. __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request", return_value=_a ) as mock_head: __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) # This check we did call the fake head request mock_head.assert_called() @require_tokenizers def __lowerCAmelCase ( self ) -> str: # A mock response for an HTTP head request to emulate server down __SCREAMING_SNAKE_CASE = mock.Mock() __SCREAMING_SNAKE_CASE = 5_00 __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = HTTPError __SCREAMING_SNAKE_CASE = {} # Download this model to make sure it's in the cache. __SCREAMING_SNAKE_CASE = GPTaTokenizerFast.from_pretrained("gpt2" ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request", return_value=_a ) as mock_head: __SCREAMING_SNAKE_CASE = GPTaTokenizerFast.from_pretrained("gpt2" ) # This check we did call the fake head request mock_head.assert_called() def __lowerCAmelCase ( self ) -> int: # This test is for deprecated behavior and can be removed in v5 try: __SCREAMING_SNAKE_CASE = tempfile.mktemp() with open(_a, "wb" ) as f: http_get("https://huggingface.co/albert-base-v1/resolve/main/spiece.model", _a ) __SCREAMING_SNAKE_CASE = AlbertTokenizer.from_pretrained(_a ) finally: os.remove(_a ) # Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in # the current folder and have the right name. if os.path.isfile("tokenizer.json" ): # We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it. return try: with open("tokenizer.json", "wb" ) as f: http_get("https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json", _a ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) # The tiny random BERT has a vocab size of 1024, tiny gpt2 as a vocab size of 1000 self.assertEqual(tokenizer.vocab_size, 10_00 ) # Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file. finally: os.remove("tokenizer.json" ) def __lowerCAmelCase ( self ) -> Optional[Any]: # This test is for deprecated behavior and can be removed in v5 __SCREAMING_SNAKE_CASE = AlbertTokenizer.from_pretrained("https://huggingface.co/albert-base-v1/resolve/main/spiece.model" ) @is_staging_test class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): SCREAMING_SNAKE_CASE__ =["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""] @classmethod def __lowerCAmelCase ( cls ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = TOKEN HfFolder.save_token(_a ) @classmethod def __lowerCAmelCase ( cls ) -> Optional[int]: try: delete_repo(token=cls._token, repo_id="test-tokenizer" ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id="valid_org/test-tokenizer-org" ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id="test-dynamic-tokenizer" ) except HTTPError: pass def __lowerCAmelCase ( self ) -> Any: with tempfile.TemporaryDirectory() as tmp_dir: __SCREAMING_SNAKE_CASE = os.path.join(_a, "vocab.txt" ) with open(_a, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = BertTokenizer(_a ) tokenizer.push_to_hub("test-tokenizer", use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained(f'''{USER}/test-tokenizer''' ) self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab ) # Reset repo delete_repo(token=self._token, repo_id="test-tokenizer" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_a, repo_id="test-tokenizer", push_to_hub=_a, use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained(f'''{USER}/test-tokenizer''' ) self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab ) def __lowerCAmelCase ( self ) -> str: with tempfile.TemporaryDirectory() as tmp_dir: __SCREAMING_SNAKE_CASE = os.path.join(_a, "vocab.txt" ) with open(_a, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = BertTokenizer(_a ) tokenizer.push_to_hub("valid_org/test-tokenizer-org", use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("valid_org/test-tokenizer-org" ) self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab ) # Reset repo delete_repo(token=self._token, repo_id="valid_org/test-tokenizer-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained( _a, repo_id="valid_org/test-tokenizer-org", push_to_hub=_a, use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("valid_org/test-tokenizer-org" ) self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab ) @require_tokenizers def __lowerCAmelCase ( self ) -> List[Any]: CustomTokenizer.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: __SCREAMING_SNAKE_CASE = os.path.join(_a, "vocab.txt" ) with open(_a, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = CustomTokenizer(_a ) # No fast custom tokenizer tokenizer.push_to_hub("test-dynamic-tokenizer", use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(f'''{USER}/test-dynamic-tokenizer''', trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizer" ) # Fast and slow custom tokenizer CustomTokenizerFast.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: __SCREAMING_SNAKE_CASE = os.path.join(_a, "vocab.txt" ) with open(_a, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained(_a ) bert_tokenizer.save_pretrained(_a ) __SCREAMING_SNAKE_CASE = CustomTokenizerFast.from_pretrained(_a ) tokenizer.push_to_hub("test-dynamic-tokenizer", use_auth_token=self._token ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(f'''{USER}/test-dynamic-tokenizer''', trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizerFast" ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( f'''{USER}/test-dynamic-tokenizer''', use_fast=_a, trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizer" ) class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = Trie() trie.add("Hello 友達" ) self.assertEqual(trie.data, {"H": {"e": {"l": {"l": {"o": {" ": {"友": {"達": {"": 1}}}}}}}}} ) trie.add("Hello" ) trie.data self.assertEqual(trie.data, {"H": {"e": {"l": {"l": {"o": {"": 1, " ": {"友": {"達": {"": 1}}}}}}}}} ) def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = Trie() self.assertEqual(trie.split("[CLS] This is a extra_id_100" ), ["[CLS] This is a extra_id_100"] ) trie.add("[CLS]" ) trie.add("extra_id_1" ) trie.add("extra_id_100" ) self.assertEqual(trie.split("[CLS] This is a extra_id_100" ), ["[CLS]", " This is a ", "extra_id_100"] ) def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = Trie() trie.add("A" ) self.assertEqual(trie.split("ABC" ), ["A", "BC"] ) self.assertEqual(trie.split("BCA" ), ["BC", "A"] ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = Trie() trie.add("TOKEN]" ) trie.add("[SPECIAL_TOKEN]" ) self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]" ), ["This is something ", "[SPECIAL_TOKEN]"] ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = Trie() trie.add("A" ) trie.add("P" ) trie.add("[SPECIAL_TOKEN]" ) self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]" ), ["This is something ", "[SPECIAL_TOKEN]"] ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = Trie() trie.add("AB" ) trie.add("B" ) trie.add("C" ) self.assertEqual(trie.split("ABC" ), ["AB", "C"] ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = Trie() trie.add("ABC" ) trie.add("B" ) trie.add("CD" ) self.assertEqual(trie.split("ABCD" ), ["ABC", "D"] ) def __lowerCAmelCase ( self ) -> Any: # Even if the offsets are wrong, we necessarily output correct string # parts. __SCREAMING_SNAKE_CASE = Trie() __SCREAMING_SNAKE_CASE = trie.cut_text("ABC", [0, 0, 2, 1, 2, 3] ) self.assertEqual(_a, ["AB", "C"] )
693
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
1
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class __SCREAMING_SNAKE_CASE ( datasets.BuilderConfig ): SCREAMING_SNAKE_CASE__ =None class __SCREAMING_SNAKE_CASE ( datasets.ArrowBasedBuilder ): SCREAMING_SNAKE_CASE__ =PandasConfig def __lowerCAmelCase ( self ) -> Union[str, Any]: return datasets.DatasetInfo(features=self.config.features ) def __lowerCAmelCase ( self, _a ) -> List[str]: if not self.config.data_files: raise ValueError(f'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) __SCREAMING_SNAKE_CASE = dl_manager.download_and_extract(self.config.data_files ) if isinstance(_a, (str, list, tuple) ): __SCREAMING_SNAKE_CASE = data_files if isinstance(_a, _a ): __SCREAMING_SNAKE_CASE = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __SCREAMING_SNAKE_CASE = [dl_manager.iter_files(_a ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": files} )] __SCREAMING_SNAKE_CASE = [] for split_name, files in data_files.items(): if isinstance(_a, _a ): __SCREAMING_SNAKE_CASE = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __SCREAMING_SNAKE_CASE = [dl_manager.iter_files(_a ) for file in files] splits.append(datasets.SplitGenerator(name=_a, gen_kwargs={"files": files} ) ) return splits def __lowerCAmelCase ( self, _a ) -> pa.Table: if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example __SCREAMING_SNAKE_CASE = table_cast(_a, self.config.features.arrow_schema ) return pa_table def __lowerCAmelCase ( self, _a ) -> List[str]: for i, file in enumerate(itertools.chain.from_iterable(_a ) ): with open(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = pa.Table.from_pandas(pd.read_pickle(_a ) ) yield i, self._cast_table(_a )
693
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
1
import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =MobileBertTokenizer SCREAMING_SNAKE_CASE__ =MobileBertTokenizerFast SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =filter_non_english SCREAMING_SNAKE_CASE__ ="""google/mobilebert-uncased""" def __lowerCAmelCase ( self ) -> Tuple: super().setUp() __SCREAMING_SNAKE_CASE = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) __SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def __lowerCAmelCase ( self, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = "UNwant\u00E9d,running" __SCREAMING_SNAKE_CASE = "unwanted, running" return input_text, output_text def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(_a, ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ), [9, 6, 7, 12, 10, 11] ) def __lowerCAmelCase ( self ) -> List[Any]: if not self.test_rust_tokenizer: return __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() __SCREAMING_SNAKE_CASE = "UNwant\u00E9d,running" __SCREAMING_SNAKE_CASE = tokenizer.tokenize(_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = tokenizer.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a, add_special_tokens=_a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() __SCREAMING_SNAKE_CASE = tokenizer.encode(_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a ) self.assertListEqual(_a, _a ) # With lower casing __SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=_a ) __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=_a ) __SCREAMING_SNAKE_CASE = "UNwant\u00E9d,running" __SCREAMING_SNAKE_CASE = tokenizer.tokenize(_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = tokenizer.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a, add_special_tokens=_a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() __SCREAMING_SNAKE_CASE = tokenizer.encode(_a ) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a ) self.assertListEqual(_a, _a ) def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("ah\u535A\u63A8zz" ), ["ah", "\u535A", "\u63A8", "zz"] ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? " ), ["hello", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ), ["hello"] ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a, strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ), ["hällo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ), ["h\u00E9llo"] ) def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a, strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ), ["hallo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ), ["hello"] ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ), ["hallo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ), ["hello"] ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? " ), ["HeLLo", "!", "how", "Are", "yoU", "?"] ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a, strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ), ["HäLLo", "!", "how", "Are", "yoU", "?"] ) def __lowerCAmelCase ( self ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a, strip_accents=_a ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ), ["HaLLo", "!", "how", "Are", "yoU", "?"] ) def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=_a, never_split=["[UNK]"] ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? [UNK]" ), ["HeLLo", "!", "how", "Are", "yoU", "?", "[UNK]"] ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = ["[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing"] __SCREAMING_SNAKE_CASE = {} for i, token in enumerate(_a ): __SCREAMING_SNAKE_CASE = i __SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=_a, unk_token="[UNK]" ) self.assertListEqual(tokenizer.tokenize("" ), [] ) self.assertListEqual(tokenizer.tokenize("unwanted running" ), ["un", "##want", "##ed", "runn", "##ing"] ) self.assertListEqual(tokenizer.tokenize("unwantedX running" ), ["[UNK]", "runn", "##ing"] ) def __lowerCAmelCase ( self ) -> List[Any]: self.assertTrue(_is_whitespace(" " ) ) self.assertTrue(_is_whitespace("\t" ) ) self.assertTrue(_is_whitespace("\r" ) ) self.assertTrue(_is_whitespace("\n" ) ) self.assertTrue(_is_whitespace("\u00A0" ) ) self.assertFalse(_is_whitespace("A" ) ) self.assertFalse(_is_whitespace("-" ) ) def __lowerCAmelCase ( self ) -> Optional[Any]: self.assertTrue(_is_control("\u0005" ) ) self.assertFalse(_is_control("A" ) ) self.assertFalse(_is_control(" " ) ) self.assertFalse(_is_control("\t" ) ) self.assertFalse(_is_control("\r" ) ) def __lowerCAmelCase ( self ) -> Optional[Any]: self.assertTrue(_is_punctuation("-" ) ) self.assertTrue(_is_punctuation("$" ) ) self.assertTrue(_is_punctuation("`" ) ) self.assertTrue(_is_punctuation("." ) ) self.assertFalse(_is_punctuation("A" ) ) self.assertFalse(_is_punctuation(" " ) ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_a ) for t in ["Test", "\xad", "test"]], [["[UNK]"], [], ["[UNK]"]] ) self.assertListEqual( [rust_tokenizer.tokenize(_a ) for t in ["Test", "\xad", "test"]], [["[UNK]"], [], ["[UNK]"]] ) @slow def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("google/mobilebert-uncased" ) __SCREAMING_SNAKE_CASE = tokenizer.encode("sequence builders", add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer.encode("multi-sequence build", add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(_a ) __SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(_a, _a ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def __lowerCAmelCase ( self ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = f'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' __SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( _a, return_attention_mask=_a, return_token_type_ids=_a, return_offsets_mapping=_a, add_special_tokens=_a, ) __SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(_a, "do_lower_case" ) else False __SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "A"), ((1, 2), ","), ((3, 5), "na"), ((5, 6), "##ï"), ((6, 8), "##ve"), ((9, 15), tokenizer_r.mask_token), ((16, 21), "Allen"), ((21, 23), "##NL"), ((23, 24), "##P"), ((25, 33), "sentence"), ((33, 34), "."), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "a"), ((1, 2), ","), ((3, 8), "naive"), ((9, 15), tokenizer_r.mask_token), ((16, 21), "allen"), ((21, 23), "##nl"), ((23, 24), "##p"), ((25, 33), "sentence"), ((33, 34), "."), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results], tokenizer_r.convert_ids_to_tokens(tokens["input_ids"] ) ) self.assertEqual([e[0] for e in expected_results], tokens["offset_mapping"] ) def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = ["的", "人", "有"] __SCREAMING_SNAKE_CASE = "".join(_a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer_r.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_a, _a ) self.assertListEqual(_a, _a ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = tokenizer_r.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.encode(_a, add_special_tokens=_a ) __SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(_a ) # it is expected that only the first Chinese character is not preceded by "##". __SCREAMING_SNAKE_CASE = [ f'''##{token}''' if idx != 0 else token for idx, token in enumerate(_a ) ] self.assertListEqual(_a, _a ) self.assertListEqual(_a, _a )
693
import random from .binary_exp_mod import bin_exp_mod def _A ( __snake_case :List[Any] , __snake_case :Union[str, Any]=1000 ) -> int: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __SCREAMING_SNAKE_CASE = n - 1 __SCREAMING_SNAKE_CASE = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __SCREAMING_SNAKE_CASE = 0 while count < prec: __SCREAMING_SNAKE_CASE = random.randint(2 , n - 1 ) __SCREAMING_SNAKE_CASE = bin_exp_mod(__snake_case , __snake_case , __snake_case ) if b != 1: __SCREAMING_SNAKE_CASE = True for _ in range(__snake_case ): if b == n - 1: __SCREAMING_SNAKE_CASE = False break __SCREAMING_SNAKE_CASE = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _snake_case : int = 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)))
693
1
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self, _a = 16, _a = 88, _a = None, _a = 1, _a = 0.0, _a = 32, _a = None, _a = False, _a = None, _a = None, _a = "geglu", _a = None, ) -> Any: super().__init__() __SCREAMING_SNAKE_CASE = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_a, attention_head_dim=_a, in_channels=_a, num_layers=_a, dropout=_a, norm_num_groups=_a, cross_attention_dim=_a, attention_bias=_a, sample_size=_a, num_vector_embeds=_a, activation_fn=_a, num_embeds_ada_norm=_a, ) 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 __SCREAMING_SNAKE_CASE = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` __SCREAMING_SNAKE_CASE = [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])` __SCREAMING_SNAKE_CASE = [1, 0] def __lowerCAmelCase ( self, _a, _a, _a=None, _a=None, _a=None, _a = True, ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = hidden_states __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens __SCREAMING_SNAKE_CASE = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] __SCREAMING_SNAKE_CASE = self.transformer_index_for_condition[i] __SCREAMING_SNAKE_CASE = self.transformers[transformer_index]( _a, encoder_hidden_states=_a, timestep=_a, cross_attention_kwargs=_a, return_dict=_a, )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] __SCREAMING_SNAKE_CASE = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) __SCREAMING_SNAKE_CASE = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_a )
693
import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int ) -> np.ndarray: """simple docstring""" if (ksize % 2) == 0: __SCREAMING_SNAKE_CASE = ksize + 1 __SCREAMING_SNAKE_CASE = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(__snake_case ): for x in range(__snake_case ): # distance from center __SCREAMING_SNAKE_CASE = x - ksize // 2 __SCREAMING_SNAKE_CASE = y - ksize // 2 # degree to radiant __SCREAMING_SNAKE_CASE = theta / 180 * np.pi __SCREAMING_SNAKE_CASE = np.cos(_theta ) __SCREAMING_SNAKE_CASE = np.sin(_theta ) # get kernel x __SCREAMING_SNAKE_CASE = cos_theta * px + sin_theta * py # get kernel y __SCREAMING_SNAKE_CASE = -sin_theta * px + cos_theta * py # fill kernel __SCREAMING_SNAKE_CASE = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _snake_case : Union[str, Any] = imread('../image_data/lena.jpg') # turn image in gray scale value _snake_case : List[str] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _snake_case : int = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 1_20, 1_50]: _snake_case : List[str] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _snake_case : Optional[Any] = out / out.max() * 2_55 _snake_case : Union[str, Any] = out.astype(np.uinta) imshow('Original', gray) imshow('Gabor filter with 20x20 mask and 6 directions', out) waitKey(0)
693
1
from ....configuration_utils import PretrainedConfig from ....utils import logging _snake_case : Dict = logging.get_logger(__name__) # TODO: upload to AWS _snake_case : Dict = { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json' ), } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""retribert""" def __init__( self, _a=3_05_22, _a=7_68, _a=8, _a=12, _a=30_72, _a="gelu", _a=0.1, _a=0.1, _a=5_12, _a=2, _a=0.02, _a=1E-1_2, _a=True, _a=1_28, _a=0, **_a, ) -> Any: super().__init__(pad_token_id=_a, **_a ) __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = share_encoders __SCREAMING_SNAKE_CASE = projection_dim
693
def _A ( __snake_case :int ) -> int: """simple docstring""" assert isinstance(__snake_case , __snake_case ), f'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: __SCREAMING_SNAKE_CASE = f'''The input value of [n={number}] has to be > 0''' raise ValueError(__snake_case ) else: __SCREAMING_SNAKE_CASE = sylvester(number - 1 ) __SCREAMING_SNAKE_CASE = num - 1 __SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
693
1
from collections import defaultdict from math import gcd def _A ( __snake_case :int = 150_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = defaultdict(__snake_case ) __SCREAMING_SNAKE_CASE = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , __snake_case , 2 ): if gcd(__snake_case , __snake_case ) > 1: continue __SCREAMING_SNAKE_CASE = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__snake_case , limit + 1 , __snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
693
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __SCREAMING_SNAKE_CASE : @staticmethod def __lowerCAmelCase ( *_a, **_a ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @require_tf def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf" ) __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(_a ), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], [ {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, {"score": 0.333, "label": ANY(_a )}, ], ], ) @slow @require_torch def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf" ) # This is an image of 2 cats with remotes and no planes __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) __SCREAMING_SNAKE_CASE = image_classifier(_a, candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(_a ), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) __SCREAMING_SNAKE_CASE = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2 ) self.assertEqual( nested_simplify(_a ), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
693
1
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin _snake_case : Optional[int] = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right _snake_case : Any = 25_00_04 _snake_case : str = 25_00_20 @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =MBartaaTokenizer SCREAMING_SNAKE_CASE__ =MBartaaTokenizerFast SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True def __lowerCAmelCase ( self ) -> Dict: super().setUp() # We have a SentencePiece fixture for testing __SCREAMING_SNAKE_CASE = MBartaaTokenizer(_a, src_lang="en_XX", tgt_lang="ro_RO", keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = "<s>" __SCREAMING_SNAKE_CASE = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ), _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ), _a ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0], "<s>" ) self.assertEqual(vocab_keys[1], "<pad>" ) self.assertEqual(vocab_keys[-1], "<mask>" ) self.assertEqual(len(_a ), 10_54 ) def __lowerCAmelCase ( self ) -> str: self.assertEqual(self.get_tokenizer().vocab_size, 10_54 ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = MBartaaTokenizer(_a, src_lang="en_XX", tgt_lang="ro_RO", keep_accents=_a ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("This is a test" ) self.assertListEqual(_a, ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ), [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]], ) __SCREAMING_SNAKE_CASE = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( _a, [SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", "."], ) __SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a, [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ], ) __SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a, [SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", "."], ) @slow def __lowerCAmelCase ( self ) -> Any: # fmt: off __SCREAMING_SNAKE_CASE = {"input_ids": [[25_00_04, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [25_00_04, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_00_04, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a, model_name="facebook/mbart-large-50", revision="d3913889c59cd5c9e456b269c376325eabad57e2", ) def __lowerCAmelCase ( self ) -> int: if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return __SCREAMING_SNAKE_CASE = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-mbart50", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(_a, **_a ) __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(_a ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) __SCREAMING_SNAKE_CASE = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(_a, _a ) # Checks everything loads correctly in the same way __SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a, _a ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(_a ) # Save tokenizer rust, legacy_format=True __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(_a, legacy_format=_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(_a ) # Checks it save with the same files self.assertSequenceEqual(_a, _a ) # Checks everything loads correctly in the same way __SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a, _a ) ) shutil.rmtree(_a ) # Save tokenizer rust, legacy_format=False __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(_a, legacy_format=_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(_a ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way __SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a, _a ) ) shutil.rmtree(_a ) @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): SCREAMING_SNAKE_CASE__ ="""facebook/mbart-large-50-one-to-many-mmt""" SCREAMING_SNAKE_CASE__ =[ """ UN Chief Says There Is No Military Solution in Syria""", """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""", ] SCREAMING_SNAKE_CASE__ =[ """Şeful ONU declară că nu există o soluţie militară în Siria""", """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei""" """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor""" """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""", ] SCREAMING_SNAKE_CASE__ =[EN_CODE, 82_74, 12_78_73, 2_59_16, 7, 86_22, 20_71, 4_38, 6_74_85, 53, 18_78_95, 23, 5_17_12, 2] @classmethod def __lowerCAmelCase ( cls ) -> Any: __SCREAMING_SNAKE_CASE = MBartaaTokenizer.from_pretrained( cls.checkpoint_name, src_lang="en_XX", tgt_lang="ro_RO" ) __SCREAMING_SNAKE_CASE = 1 return cls def __lowerCAmelCase ( self ) -> int: self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ar_AR"], 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["en_EN"], 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ro_RO"], 25_00_20 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["mr_IN"], 25_00_38 ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens, _a ) def __lowerCAmelCase ( self ) -> int: self.assertIn(_a, self.tokenizer.all_special_ids ) __SCREAMING_SNAKE_CASE = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] __SCREAMING_SNAKE_CASE = self.tokenizer.decode(_a, skip_special_tokens=_a ) __SCREAMING_SNAKE_CASE = self.tokenizer.decode(generated_ids[1:], skip_special_tokens=_a ) self.assertEqual(_a, _a ) self.assertNotIn(self.tokenizer.eos_token, _a ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0], _a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.tokenizer(_a, max_length=_a, truncation=_a ).input_ids[0] self.assertEqual(ids[0], _a ) self.assertEqual(ids[-1], 2 ) self.assertEqual(len(_a ), _a ) def __lowerCAmelCase ( self ) -> Optional[int]: self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ), [25_00_53, 25_00_01] ) def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE = tempfile.mkdtemp() __SCREAMING_SNAKE_CASE = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_a ) __SCREAMING_SNAKE_CASE = MBartaaTokenizer.from_pretrained(_a ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids, _a ) @require_torch def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text, text_target=self.tgt_text, padding=_a, return_tensors="pt" ) __SCREAMING_SNAKE_CASE = shift_tokens_right(batch["labels"], self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.tokenizer( self.src_text, text_target=self.tgt_text, padding=_a, truncation=_a, max_length=len(self.expected_src_tokens ), return_tensors="pt", ) __SCREAMING_SNAKE_CASE = shift_tokens_right(batch["labels"], self.tokenizer.pad_token_id ) self.assertIsInstance(_a, _a ) self.assertEqual((2, 14), batch.input_ids.shape ) self.assertEqual((2, 14), batch.attention_mask.shape ) __SCREAMING_SNAKE_CASE = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens, _a ) self.assertEqual(2, batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens, [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens, [self.tokenizer.eos_token_id] ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text, padding=_a, truncation=_a, max_length=3, return_tensors="pt" ) __SCREAMING_SNAKE_CASE = self.tokenizer( text_target=self.tgt_text, padding=_a, truncation=_a, max_length=10, return_tensors="pt" ) __SCREAMING_SNAKE_CASE = targets["input_ids"] __SCREAMING_SNAKE_CASE = shift_tokens_right(_a, self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1], 3 ) self.assertEqual(batch.decoder_input_ids.shape[1], 10 ) @require_torch def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.tokenizer._build_translation_inputs( "A test", return_tensors="pt", src_lang="en_XX", tgt_lang="ar_AR" ) self.assertEqual( nested_simplify(_a ), { # en_XX, A, test, EOS "input_ids": [[25_00_04, 62, 30_34, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 25_00_01, }, )
693
from __future__ import annotations import math def _A ( __snake_case :int , __snake_case :int , __snake_case :bool , __snake_case :list[int] , __snake_case :float ) -> int: """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__snake_case ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) return min( minimax(depth + 1 , node_index * 2 , __snake_case , __snake_case , __snake_case ) , minimax(depth + 1 , node_index * 2 + 1 , __snake_case , __snake_case , __snake_case ) , ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [90, 23, 6, 33, 21, 65, 123, 3_4423] __SCREAMING_SNAKE_CASE = math.log(len(__snake_case ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , __snake_case , __snake_case , __snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
1
from __future__ import annotations from PIL import Image # Define glider example _snake_case : Optional[int] = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example _snake_case : List[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def _A ( __snake_case :list[list[int]] ) -> list[list[int]]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i in range(len(__snake_case ) ): __SCREAMING_SNAKE_CASE = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours __SCREAMING_SNAKE_CASE = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__snake_case ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__snake_case ) - 1: neighbour_count += cells[i + 1][j] if i < len(__snake_case ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. __SCREAMING_SNAKE_CASE = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__snake_case ) return next_generation def _A ( __snake_case :list[list[int]] , __snake_case :int ) -> list[Image.Image]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for _ in range(__snake_case ): # Create output image __SCREAMING_SNAKE_CASE = Image.new("RGB" , (len(cells[0] ), len(__snake_case )) ) __SCREAMING_SNAKE_CASE = img.load() # Save cells to image for x in range(len(__snake_case ) ): for y in range(len(cells[0] ) ): __SCREAMING_SNAKE_CASE = 255 - cells[y][x] * 255 __SCREAMING_SNAKE_CASE = (colour, colour, colour) # Save image images.append(__snake_case ) __SCREAMING_SNAKE_CASE = new_generation(__snake_case ) return images if __name__ == "__main__": _snake_case : str = generate_images(GLIDER, 16) images[0].save('out.gif', save_all=True, append_images=images[1:])
693
def _A ( __snake_case :bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def _A ( __snake_case :str ) -> bytes: """simple docstring""" if (len(__snake_case ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =WavaVecaPhonemeCTCTokenizer SCREAMING_SNAKE_CASE__ =False def __lowerCAmelCase ( self ) -> Optional[int]: super().setUp() __SCREAMING_SNAKE_CASE = ( "<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː " "ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː " "ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 " "oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ " "pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ " "yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ " "əʊ S ɡʲ onɡ2 u\" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ " "ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ " "ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ " "uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ " "ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ " "ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ " "ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4" ).split(" " ) __SCREAMING_SNAKE_CASE = dict(zip(_a, range(len(_a ) ) ) ) __SCREAMING_SNAKE_CASE = {"pad_token": "<pad>", "unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>"} __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(_a ) + "\n" ) def __lowerCAmelCase ( self, _a, _a=False, _a=20, _a=5 ) -> Tuple[str, list]: __SCREAMING_SNAKE_CASE = [(i, tokenizer.decode([i], clean_up_tokenization_spaces=_a )) for i in range(len(_a ) )] __SCREAMING_SNAKE_CASE = list(filter(lambda _a : [t[0]] == tokenizer.encode(t[1], do_phonemize=_a ), _a ) ) if max_length is not None and len(_a ) > max_length: __SCREAMING_SNAKE_CASE = toks[:max_length] if min_length is not None and len(_a ) < min_length and len(_a ) > 0: while len(_a ) < min_length: __SCREAMING_SNAKE_CASE = toks + toks # toks_str = [t[1] for t in toks] __SCREAMING_SNAKE_CASE = [t[0] for t in toks] # Ensure consistency __SCREAMING_SNAKE_CASE = tokenizer.decode(_a, clean_up_tokenization_spaces=_a ) if " " not in output_txt and len(_a ) > 1: __SCREAMING_SNAKE_CASE = ( tokenizer.decode([toks_ids[0]], clean_up_tokenization_spaces=_a ) + " " + tokenizer.decode(toks_ids[1:], clean_up_tokenization_spaces=_a ) ) if with_prefix_space: __SCREAMING_SNAKE_CASE = " " + output_txt __SCREAMING_SNAKE_CASE = tokenizer.encode(_a, add_special_tokens=_a ) return output_txt, output_ids def __lowerCAmelCase ( self, **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname, **_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) # check adding a single token tokenizer.add_tokens("xxx" ) __SCREAMING_SNAKE_CASE = tokenizer("m xxx ɪ", do_phonemize=_a ).input_ids self.assertEqual(_a, [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(["aaa", "bbb", "ccc"] ) __SCREAMING_SNAKE_CASE = tokenizer("m aaa ɪ ccc", do_phonemize=_a ).input_ids self.assertEqual(_a, [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa __SCREAMING_SNAKE_CASE = tokenizer("maɪ c", do_phonemize=_a ).input_ids self.assertEqual(_a, [3, 2_00] ) # mai should be <unk> (=3) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) self.assertEqual(_a, "h ə l oʊ h aʊ ɑːɹ j uː" ) def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) self.assertEqual(tokenizer(_a ).input_ids, tokenizer(_a, do_phonemize=_a ).input_ids ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) __SCREAMING_SNAKE_CASE = tokenizer.decode(tokenizer(_a ).input_ids ) self.assertEqual(_a, _a ) def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) __SCREAMING_SNAKE_CASE = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] __SCREAMING_SNAKE_CASE = tokenizer.decode(sample_ids[0] ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, batch_tokens[0] ) self.assertEqual(_a, ["k s ɾ ɾ l ɭʲ", "j ð s j ð s oːɹ"] ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token="|" ) tokenizer.add_tokens("|" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) self.assertEqual(_a, "h ə l oʊ | h aʊ | ɑːɹ | j uː |" ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token="|" ) tokenizer.add_tokens("|" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) self.assertEqual(tokenizer(_a ).input_ids, tokenizer(_a, do_phonemize=_a ).input_ids ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token="|" ) tokenizer.add_tokens("|" ) # fmt: off __SCREAMING_SNAKE_CASE = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter __SCREAMING_SNAKE_CASE = tokenizer.decode(sample_ids[0] ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, batch_tokens[0] ) self.assertEqual(_a, ["k s ɾ ɾ l ɭʲ", "j ð s j ð s oːɹ"] ) # decode with no word_del_token filter __SCREAMING_SNAKE_CASE = tokenizer.decode(sample_ids[0], filter_word_delimiter_token=_a ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a, filter_word_delimiter_token=_a ) self.assertEqual(_a, batch_tokens[0] ) self.assertEqual(_a, ["k s ɾ | ɾ l | ɭʲ", "| j ð | s j ð s oːɹ"] ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token="|" ) tokenizer.add_tokens("|" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) __SCREAMING_SNAKE_CASE = tokenizer.decode(tokenizer(_a ).input_ids, filter_word_delimiter_token=_a ) self.assertEqual(_a, _a ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token="|" ) tokenizer.add_tokens("|" ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer.phonemize(_a, phonemizer_lang="en-us" ) __SCREAMING_SNAKE_CASE = tokenizer.decode(tokenizer(_a ).input_ids, filter_word_delimiter_token=_a ) self.assertEqual(" ".join([p.strip() for p in phonemes.split(" |" )] ).strip(), _a ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft", word_delimiter_token=_a ) __SCREAMING_SNAKE_CASE = "Hello how are you" __SCREAMING_SNAKE_CASE = tokenizer(_a, phonemizer_lang="en-us" ).input_ids __SCREAMING_SNAKE_CASE = tokenizer(_a, phonemizer_lang="fr-fr" ).input_ids self.assertNotEqual(_a, _a ) __SCREAMING_SNAKE_CASE = tokenizer.decode(_a ) __SCREAMING_SNAKE_CASE = tokenizer.decode(_a ) self.assertEqual(_a, "h ə l oʊ h aʊ ɑːɹ j uː" ) self.assertEqual(_a, "ɛ l o h aʊ a ʁ j u" ) def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) __SCREAMING_SNAKE_CASE = "Hello how Are you" __SCREAMING_SNAKE_CASE = "hello how are you" __SCREAMING_SNAKE_CASE = tokenizer(_a ).input_ids __SCREAMING_SNAKE_CASE = tokenizer(_a ).input_ids self.assertEqual(_a, _a ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft" ) tokenizer.add_tokens(["!", "?"] ) tokenizer.add_special_tokens({"cls_token": "$$$"} ) # fmt: off __SCREAMING_SNAKE_CASE = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, ["k s ɾ ɾ l ɭʲ!?!? $$$", "j ð s j ð s oːɹ $$$"] ) @staticmethod def __lowerCAmelCase ( _a, _a ) -> Optional[int]: __SCREAMING_SNAKE_CASE = [d[key] for d in offsets] return retrieved_list def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = self.get_tokenizer(word_delimiter_token="|" ) tokenizer.add_tokens("|" ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" __SCREAMING_SNAKE_CASE = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on __SCREAMING_SNAKE_CASE = tokenizer.decode(_a, output_char_offsets=_a, filter_word_delimiter_token=_a ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ), 2 ) self.assertTrue("text" in outputs ) self.assertTrue("char_offsets" in outputs ) self.assertTrue(isinstance(_a, _a ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(" ".join(self.get_from_offsets(outputs["char_offsets"], "char" ) ), outputs.text ) self.assertListEqual( self.get_from_offsets(outputs["char_offsets"], "char" ), ["k", "s", "ɾ", "ɾ", "|", "ɾ", "l", "|", "ɭʲ"] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs["char_offsets"], "start_offset" ), [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs["char_offsets"], "end_offset" ), [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.get_tokenizer(word_delimiter_token="|" ) def check_list_tuples_equal(_a, _a ): self.assertTrue(isinstance(_a, _a ) ) self.assertTrue(isinstance(outputs_list[0], _a ) ) # transform list to ModelOutput __SCREAMING_SNAKE_CASE = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch["text"], outputs_batch_a["text"] ) def recursive_check(_a, _a ): if isinstance(_a, _a ): [recursive_check(_a, _a ) for la, la in zip(_a, _a )] self.assertEqual(_a, _a ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch["char_offsets"], outputs_batch_a["char_offsets"] ) # fmt: off __SCREAMING_SNAKE_CASE = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a, output_char_offsets=_a ) __SCREAMING_SNAKE_CASE = [tokenizer.decode(_a, output_char_offsets=_a ) for ids in sample_ids] check_list_tuples_equal(_a, _a ) @unittest.skip("Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes" ) def __lowerCAmelCase ( self ) -> Tuple: pass @unittest.skip("Wav2Vec2PhonemeTokenizer always puts spaces between phonemes" ) def __lowerCAmelCase ( self ) -> Union[str, Any]: pass @unittest.skip("encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency" ) def __lowerCAmelCase ( self ) -> List[Any]: pass @unittest.skip("Wav2Vec2PhonemeModel has no max model length => no testing" ) def __lowerCAmelCase ( self ) -> int: pass def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __SCREAMING_SNAKE_CASE = tokenizer.vocab_size __SCREAMING_SNAKE_CASE = len(_a ) self.assertNotEqual(_a, 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) __SCREAMING_SNAKE_CASE = ["aaaaa bbbbbb", "cccccccccdddddddd"] __SCREAMING_SNAKE_CASE = tokenizer.add_tokens(_a ) __SCREAMING_SNAKE_CASE = tokenizer.vocab_size __SCREAMING_SNAKE_CASE = len(_a ) self.assertNotEqual(_a, 0 ) self.assertEqual(_a, _a ) self.assertEqual(_a, len(_a ) ) self.assertEqual(_a, all_size + len(_a ) ) __SCREAMING_SNAKE_CASE = tokenizer.encode("aaaaa bbbbbb low cccccccccdddddddd l", add_special_tokens=_a ) self.assertGreaterEqual(len(_a ), 4 ) self.assertGreater(tokens[0], tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3], tokenizer.vocab_size - 1 ) __SCREAMING_SNAKE_CASE = {"eos_token": ">>>>|||<||<<|<<", "pad_token": "<<<<<|||>|>>>>|>"} __SCREAMING_SNAKE_CASE = tokenizer.add_special_tokens(_a ) __SCREAMING_SNAKE_CASE = tokenizer.vocab_size __SCREAMING_SNAKE_CASE = len(_a ) self.assertNotEqual(_a, 0 ) self.assertEqual(_a, _a ) self.assertEqual(_a, len(_a ) ) self.assertEqual(_a, all_size_a + len(_a ) ) __SCREAMING_SNAKE_CASE = tokenizer.encode( ">>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l", add_special_tokens=_a ) self.assertGreaterEqual(len(_a ), 6 ) self.assertGreater(tokens[0], tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0], tokens[1] ) self.assertGreater(tokens[-3], tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3], tokens[-4] ) self.assertEqual(tokens[0], tokenizer.eos_token_id ) self.assertEqual(tokens[-3], tokenizer.pad_token_id ) @unittest.skip("The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode." ) def __lowerCAmelCase ( self ) -> Dict: pass @unittest.skip("The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode." ) def __lowerCAmelCase ( self ) -> Optional[Any]: pass def __lowerCAmelCase ( self ) -> Optional[int]: # The default common tokenizer tests assumes that the output of `convert_tokens_to_string` is a string which # is not the case for Wav2Vec2PhonemeCTCTokenizer. __SCREAMING_SNAKE_CASE = self.get_tokenizers(fast=_a, do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __SCREAMING_SNAKE_CASE = ["ð", "ɪ", "s", "ɪ", "z", "ɐ", "t", "ɛ", "k", "s", "t"] __SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_string(_a ) self.assertIsInstance(output["text"], _a )
693
from functools import lru_cache def _A ( __snake_case :int ) -> set: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(__snake_case ) if n > 1: factors.add(__snake_case ) return factors @lru_cache def _A ( __snake_case :int ) -> int: """simple docstring""" return len(unique_prime_factors(__snake_case ) ) def _A ( __snake_case :list ) -> bool: """simple docstring""" return len(set(__snake_case ) ) in (0, 1) def _A ( __snake_case :int ) -> list: """simple docstring""" __SCREAMING_SNAKE_CASE = 2 while True: # Increment each value of a generated range __SCREAMING_SNAKE_CASE = [base + i for i in range(__snake_case )] # Run elements through out unique_prime_factors function # Append our target number to the end. __SCREAMING_SNAKE_CASE = [upf_len(__snake_case ) for x in group] checker.append(__snake_case ) # If all numbers in the list are equal, return the group variable. if equality(__snake_case ): return group # Increment our base variable by 1 base += 1 def _A ( __snake_case :int = 4 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = run(__snake_case ) return results[0] if len(__snake_case ) else None if __name__ == "__main__": print(solution())
693
1
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 _snake_case : List[Any] = 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 __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """The column name of the images in the files."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """A folder containing the training data."""} ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """A folder containing the validation data."""} ) SCREAMING_SNAKE_CASE__ =field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) SCREAMING_SNAKE_CASE__ =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 __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = {} if self.train_dir is not None: __SCREAMING_SNAKE_CASE = self.train_dir if self.validation_dir is not None: __SCREAMING_SNAKE_CASE = self.validation_dir __SCREAMING_SNAKE_CASE = data_files if data_files else None @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =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.""" ) } , ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) SCREAMING_SNAKE_CASE__ =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""" ) } , ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) SCREAMING_SNAKE_CASE__ =field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) SCREAMING_SNAKE_CASE__ =field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Name or path of preprocessor config."""} ) SCREAMING_SNAKE_CASE__ =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).""" ) } , ) SCREAMING_SNAKE_CASE__ =field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) SCREAMING_SNAKE_CASE__ =field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def _A ( __snake_case :Union[str, Any] ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = torch.stack([example["pixel_values"] for example in examples] ) return {"pixel_values": pixel_values} def _A ( ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 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. __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 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" , __snake_case , __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() __SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(__snake_case ) transformers.utils.logging.set_verbosity(__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. __SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __SCREAMING_SNAKE_CASE = 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. __SCREAMING_SNAKE_CASE = 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. __SCREAMING_SNAKE_CASE = None if "validation" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __snake_case ) and data_args.train_val_split > 0.0: __SCREAMING_SNAKE_CASE = ds["train"].train_test_split(data_args.train_val_split ) __SCREAMING_SNAKE_CASE = split["train"] __SCREAMING_SNAKE_CASE = 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. __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, } if model_args.config_name: __SCREAMING_SNAKE_CASE = ViTMAEConfig.from_pretrained(model_args.config_name , **__snake_case ) elif model_args.model_name_or_path: __SCREAMING_SNAKE_CASE = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__snake_case ) else: __SCREAMING_SNAKE_CASE = 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: __SCREAMING_SNAKE_CASE = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__snake_case ) elif model_args.model_name_or_path: __SCREAMING_SNAKE_CASE = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__snake_case ) else: __SCREAMING_SNAKE_CASE = ViTImageProcessor() # create model if model_args.model_name_or_path: __SCREAMING_SNAKE_CASE = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=__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" ) __SCREAMING_SNAKE_CASE = ViTMAEForPreTraining(__snake_case ) if training_args.do_train: __SCREAMING_SNAKE_CASE = ds["train"].column_names else: __SCREAMING_SNAKE_CASE = ds["validation"].column_names if data_args.image_column_name is not None: __SCREAMING_SNAKE_CASE = data_args.image_column_name elif "image" in column_names: __SCREAMING_SNAKE_CASE = "image" elif "img" in column_names: __SCREAMING_SNAKE_CASE = "img" else: __SCREAMING_SNAKE_CASE = 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: __SCREAMING_SNAKE_CASE = image_processor.size["shortest_edge"] else: __SCREAMING_SNAKE_CASE = (image_processor.size["height"], image_processor.size["width"]) __SCREAMING_SNAKE_CASE = Compose( [ Lambda(lambda __snake_case : img.convert("RGB" ) if img.mode != "RGB" else img ), RandomResizedCrop(__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(__snake_case :Any ): __SCREAMING_SNAKE_CASE = [transforms(__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: __SCREAMING_SNAKE_CASE = ds["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__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: __SCREAMING_SNAKE_CASE = ( ds["validation"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__snake_case ) # Compute absolute learning rate __SCREAMING_SNAKE_CASE = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: __SCREAMING_SNAKE_CASE = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer __SCREAMING_SNAKE_CASE = Trainer( model=__snake_case , args=__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=__snake_case , data_collator=__snake_case , ) # Training if training_args.do_train: __SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: __SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: __SCREAMING_SNAKE_CASE = last_checkpoint __SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=__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: __SCREAMING_SNAKE_CASE = trainer.evaluate() trainer.log_metrics("eval" , __snake_case ) trainer.save_metrics("eval" , __snake_case ) # Write model card and (optionally) push to hub __SCREAMING_SNAKE_CASE = { "tasks": "masked-auto-encoding", "dataset": data_args.dataset_name, "tags": ["masked-auto-encoding"], } if training_args.push_to_hub: trainer.push_to_hub(**__snake_case ) else: trainer.create_model_card(**__snake_case ) def _A ( __snake_case :Dict ) -> Any: """simple docstring""" main() if __name__ == "__main__": main()
693
import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _A ( __snake_case :Dict ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = VideoMAEConfig() set_architecture_configs(__snake_case , __snake_case ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = False if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = "huggingface/label-files" if "kinetics" in model_name: __SCREAMING_SNAKE_CASE = 400 __SCREAMING_SNAKE_CASE = "kinetics400-id2label.json" elif "ssv2" in model_name: __SCREAMING_SNAKE_CASE = 174 __SCREAMING_SNAKE_CASE = "something-something-v2-id2label.json" else: raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned." ) __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type="dataset" ) , "r" ) ) __SCREAMING_SNAKE_CASE = {int(__snake_case ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = idalabel __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def _A ( __snake_case :Dict , __snake_case :Optional[Any] ) -> List[Any]: """simple docstring""" if "small" in model_name: __SCREAMING_SNAKE_CASE = 384 __SCREAMING_SNAKE_CASE = 1536 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 3 __SCREAMING_SNAKE_CASE = 192 __SCREAMING_SNAKE_CASE = 768 elif "large" in model_name: __SCREAMING_SNAKE_CASE = 1024 __SCREAMING_SNAKE_CASE = 4096 __SCREAMING_SNAKE_CASE = 24 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 512 __SCREAMING_SNAKE_CASE = 2048 elif "huge" in model_name: __SCREAMING_SNAKE_CASE = 1280 __SCREAMING_SNAKE_CASE = 5120 __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = 12 __SCREAMING_SNAKE_CASE = 8 __SCREAMING_SNAKE_CASE = 640 __SCREAMING_SNAKE_CASE = 2560 elif "base" not in model_name: raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\"" ) def _A ( __snake_case :List[Any] ) -> Optional[int]: """simple docstring""" if "encoder." in name: __SCREAMING_SNAKE_CASE = name.replace("encoder." , "" ) if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("cls_token" , "videomae.embeddings.cls_token" ) if "decoder_pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pos_embed" , "decoder.decoder_pos_embed" ) if "pos_embed" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("pos_embed" , "videomae.embeddings.position_embeddings" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.proj" , "videomae.embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.norm" , "videomae.embeddings.norm" ) if "decoder.blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder.blocks" , "decoder.decoder_layers" ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("blocks" , "videomae.encoder.layer" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name and "bias" not in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.self" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.attention" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc2" , "output.dense" ) if "decoder_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_embed" , "decoder.decoder_embed" ) if "decoder_norm" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_norm" , "decoder.decoder_norm" ) if "decoder_pred" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pred" , "decoder.decoder_pred" ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.weight" , "videomae.layernorm.weight" ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.bias" , "videomae.layernorm.bias" ) if "head" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("head" , "classifier" ) return name def _A ( __snake_case :Union[str, Any] , __snake_case :Optional[int] ) -> Optional[Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__snake_case ) if key.startswith("encoder." ): __SCREAMING_SNAKE_CASE = key.replace("encoder." , "" ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split("." ) if key.startswith("decoder.blocks" ): __SCREAMING_SNAKE_CASE = config.decoder_hidden_size __SCREAMING_SNAKE_CASE = int(key_split[2] ) __SCREAMING_SNAKE_CASE = "decoder.decoder_layers." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = config.hidden_size __SCREAMING_SNAKE_CASE = int(key_split[1] ) __SCREAMING_SNAKE_CASE = "videomae.encoder.layer." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def _A ( ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) __SCREAMING_SNAKE_CASE = np.load(__snake_case ) return list(__snake_case ) def _A ( __snake_case :Optional[int] , __snake_case :List[str] , __snake_case :Union[str, Any] , __snake_case :Optional[Any] ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = get_videomae_config(__snake_case ) if "finetuned" in model_name: __SCREAMING_SNAKE_CASE = VideoMAEForVideoClassification(__snake_case ) else: __SCREAMING_SNAKE_CASE = VideoMAEForPreTraining(__snake_case ) # download original checkpoint, hosted on Google Drive __SCREAMING_SNAKE_CASE = "pytorch_model.bin" gdown.cached_download(__snake_case , __snake_case , quiet=__snake_case ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case , map_location="cpu" ) if "model" in files: __SCREAMING_SNAKE_CASE = files["model"] else: __SCREAMING_SNAKE_CASE = files["module"] __SCREAMING_SNAKE_CASE = convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) model.eval() # verify model on basic input __SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) __SCREAMING_SNAKE_CASE = prepare_video() __SCREAMING_SNAKE_CASE = image_processor(__snake_case , return_tensors="pt" ) if "finetuned" not in model_name: __SCREAMING_SNAKE_CASE = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) __SCREAMING_SNAKE_CASE = torch.load(__snake_case ) __SCREAMING_SNAKE_CASE = model(**__snake_case ) __SCREAMING_SNAKE_CASE = outputs.logits __SCREAMING_SNAKE_CASE = [ "videomae-small-finetuned-kinetics", "videomae-small-finetuned-ssv2", # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) "videomae-base-short", "videomae-base-short-finetuned-kinetics", "videomae-base", "videomae-base-finetuned-kinetics", "videomae-large", "videomae-large-finetuned-kinetics", "videomae-huge-finetuned-kinetics", # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) "videomae-base-short-ssv2", "videomae-base-short-finetuned-ssv2", "videomae-base-ssv2", "videomae-base-finetuned-ssv2", ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.9_2_9_1, -0.4_0_6_1, -0.9_3_0_7] ) elif model_name == "videomae-small-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_7_1, -0.4_6_8_9, -0.8_2_3_5] ) elif model_name == "videomae-base": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_7_3_9, 0.7_9_6_8, 0.7_0_8_9], [0.6_7_0_1, 0.7_4_8_7, 0.6_2_0_9], [0.4_2_8_7, 0.5_1_5_8, 0.4_7_7_3]] ) elif model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ) # we verified the loss both for normalized and unnormalized targets for this one __SCREAMING_SNAKE_CASE = torch.tensor([0.5_1_4_2] ) if config.norm_pix_loss else torch.tensor([0.6_4_6_9] ) elif model_name == "videomae-large": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.7_1_4_9, 0.7_9_9_7, 0.6_9_6_6], [0.6_7_6_8, 0.7_8_6_9, 0.6_9_4_8], [0.5_1_3_9, 0.6_2_2_1, 0.5_6_0_5]] ) elif model_name == "videomae-large-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.0_7_7_1, 0.0_0_1_1, -0.3_6_2_5] ) elif model_name == "videomae-huge-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.2_4_3_3, 0.1_6_3_2, -0.4_8_9_4] ) elif model_name == "videomae-base-short-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.6_5_8_8, 0.0_9_9_0, -0.2_4_9_3] ) elif model_name == "videomae-base-finetuned-kinetics": __SCREAMING_SNAKE_CASE = torch.Size([1, 400] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ) elif model_name == "videomae-base-short-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.4_7_1_2, 0.5_2_9_6, 0.5_7_8_6], [0.2_2_7_8, 0.2_7_2_9, 0.4_0_2_6], [0.0_3_5_2, 0.0_7_3_0, 0.2_5_0_6]] ) elif model_name == "videomae-base-short-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([-0.0_5_3_7, -0.1_5_3_9, -0.3_2_6_6] ) elif model_name == "videomae-base-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 1408, 1536] ) __SCREAMING_SNAKE_CASE = torch.tensor([[0.8_1_3_1, 0.8_7_2_7, 0.8_5_4_6], [0.7_3_6_6, 0.9_3_7_7, 0.8_8_7_0], [0.5_9_3_5, 0.8_8_7_4, 0.8_5_6_4]] ) elif model_name == "videomae-base-finetuned-ssv2": __SCREAMING_SNAKE_CASE = torch.Size([1, 174] ) __SCREAMING_SNAKE_CASE = torch.tensor([0.1_9_6_1, -0.8_3_3_7, -0.6_3_8_9] ) else: raise ValueError(f'''Model name not supported. Should be one of {model_names}''' ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) else: print("Logits:" , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , __snake_case , atol=1e-4 ) print("Logits ok!" ) # verify loss, if applicable if model_name == "videomae-base-short": __SCREAMING_SNAKE_CASE = outputs.loss assert torch.allclose(__snake_case , __snake_case , atol=1e-4 ) print("Loss ok!" ) if pytorch_dump_folder_path is not None: print(f'''Saving model and image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__snake_case ) model.save_pretrained(__snake_case ) if push_to_hub: print("Pushing to the hub..." ) model.push_to_hub(__snake_case , organization="nielsr" ) if __name__ == "__main__": _snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) _snake_case : Optional[int] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
693
1
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : Union[str, Any] = logging.get_logger(__name__) _snake_case : Optional[int] = { 'transfo-xl-wt103': 'https://huggingface.co/transfo-xl-wt103/resolve/main/config.json', } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""transfo-xl""" SCREAMING_SNAKE_CASE__ =["""mems"""] SCREAMING_SNAKE_CASE__ ={ """n_token""": """vocab_size""", """hidden_size""": """d_model""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self, _a=26_77_35, _a=[2_00_00, 4_00_00, 20_00_00], _a=10_24, _a=10_24, _a=16, _a=64, _a=40_96, _a=4, _a=False, _a=18, _a=16_00, _a=10_00, _a=True, _a=True, _a=0, _a=-1, _a=True, _a=0.1, _a=0.0, _a=True, _a="normal", _a=0.01, _a=0.01, _a=0.02, _a=1E-5, _a=0, **_a, ) -> Tuple: __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = [] self.cutoffs.extend(_a ) if proj_share_all_but_first: __SCREAMING_SNAKE_CASE = [False] + [True] * len(self.cutoffs ) else: __SCREAMING_SNAKE_CASE = [False] + [False] * len(self.cutoffs ) __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = d_embed __SCREAMING_SNAKE_CASE = d_head __SCREAMING_SNAKE_CASE = d_inner __SCREAMING_SNAKE_CASE = div_val __SCREAMING_SNAKE_CASE = pre_lnorm __SCREAMING_SNAKE_CASE = n_layer __SCREAMING_SNAKE_CASE = n_head __SCREAMING_SNAKE_CASE = mem_len __SCREAMING_SNAKE_CASE = same_length __SCREAMING_SNAKE_CASE = attn_type __SCREAMING_SNAKE_CASE = clamp_len __SCREAMING_SNAKE_CASE = sample_softmax __SCREAMING_SNAKE_CASE = adaptive __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = dropatt __SCREAMING_SNAKE_CASE = untie_r __SCREAMING_SNAKE_CASE = init __SCREAMING_SNAKE_CASE = init_range __SCREAMING_SNAKE_CASE = proj_init_std __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = layer_norm_epsilon super().__init__(eos_token_id=_a, **_a ) @property def __lowerCAmelCase ( self ) -> str: # Message copied from Transformer-XL documentation logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' ) return -1 @max_position_embeddings.setter def __lowerCAmelCase ( self, _a ) -> Tuple: # Message copied from Transformer-XL documentation raise NotImplementedError( f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
693
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): def __init__( self, *_a, **_a ) -> None: warnings.warn( "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use CLIPImageProcessor instead.", _a, ) super().__init__(*_a, **_a )
693
1
import collections import importlib.util import os import re from pathlib import Path _snake_case : List[Any] = 'src/transformers' # Matches is_xxx_available() _snake_case : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} _snake_case : str = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] _snake_case : Union[str, Any] = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available _snake_case : Optional[int] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") _snake_case : Dict = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] _snake_case : Optional[int] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", _snake_case : str = re.compile('^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], _snake_case : Any = re.compile('^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo _snake_case : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: _snake_case : Union[str, Any] = re.compile(r'^\s*try:') # Catches a line with else: _snake_case : Dict = re.compile(r'^\s*else:') def _A ( __snake_case :Union[str, Any] ) -> int: """simple docstring""" if _re_test_backend.search(__snake_case ) is None: return None __SCREAMING_SNAKE_CASE = [b[0] for b in _re_backend.findall(__snake_case )] backends.sort() return "_and_".join(__snake_case ) def _A ( __snake_case :int ) -> Dict: """simple docstring""" with open(__snake_case , "r" , encoding="utf-8" , newline="\n" ) as f: __SCREAMING_SNAKE_CASE = f.readlines() __SCREAMING_SNAKE_CASE = 0 while line_index < len(__snake_case ) and not lines[line_index].startswith("_import_structure = {" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__snake_case ): return None # First grab the objects without a specific backend in _import_structure __SCREAMING_SNAKE_CASE = [] while not lines[line_index].startswith("if TYPE_CHECKING" ) and find_backend(lines[line_index] ) is None: __SCREAMING_SNAKE_CASE = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__snake_case ): __SCREAMING_SNAKE_CASE = _re_one_line_import_struct.search(__snake_case ).groups()[0] __SCREAMING_SNAKE_CASE = re.findall("\[([^\]]+)\]" , __snake_case ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(", " )] ) line_index += 1 continue __SCREAMING_SNAKE_CASE = _re_import_struct_key_value.search(__snake_case ) if single_line_import_search is not None: __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(", " ) if len(__snake_case ) > 0] objects.extend(__snake_case ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"none": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("if TYPE_CHECKING" ): # If the line is an if not is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 4 ): __SCREAMING_SNAKE_CASE = lines[line_index] if _re_import_struct_add_one.search(__snake_case ) is not None: objects.append(_re_import_struct_add_one.search(__snake_case ).groups()[0] ) elif _re_import_struct_add_many.search(__snake_case ) is not None: __SCREAMING_SNAKE_CASE = _re_import_struct_add_many.search(__snake_case ).groups()[0].split(", " ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(__snake_case ) > 0] objects.extend(__snake_case ) elif _re_between_brackets.search(__snake_case ) is not None: __SCREAMING_SNAKE_CASE = _re_between_brackets.search(__snake_case ).groups()[0].split(", " ) __SCREAMING_SNAKE_CASE = [obj[1:-1] for obj in imports if len(__snake_case ) > 0] objects.extend(__snake_case ) elif _re_quote_object.search(__snake_case ) is not None: objects.append(_re_quote_object.search(__snake_case ).groups()[0] ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) elif line.startswith(" " * 12 + "\"" ): objects.append(line[13:-3] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __SCREAMING_SNAKE_CASE = [] while ( line_index < len(__snake_case ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("else" ) ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(__snake_case ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = {"none": objects} # Let's continue with backend-specific objects while line_index < len(__snake_case ): # If the line is an if is_backend_available, we grab all objects associated. __SCREAMING_SNAKE_CASE = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __SCREAMING_SNAKE_CASE = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __SCREAMING_SNAKE_CASE = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 8 ): __SCREAMING_SNAKE_CASE = lines[line_index] __SCREAMING_SNAKE_CASE = _re_import.search(__snake_case ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 12 ): objects.append(line[12:-2] ) line_index += 1 __SCREAMING_SNAKE_CASE = objects else: line_index += 1 return import_dict_objects, type_hint_objects def _A ( __snake_case :int , __snake_case :List[Any] ) -> Optional[int]: """simple docstring""" def find_duplicates(__snake_case :List[str] ): return [k for k, v in collections.Counter(__snake_case ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] __SCREAMING_SNAKE_CASE = [] for key in import_dict_objects.keys(): __SCREAMING_SNAKE_CASE = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(f'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) __SCREAMING_SNAKE_CASE = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(f'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): __SCREAMING_SNAKE_CASE = "base imports" if key == "none" else f'''{key} backend''' errors.append(f'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(f''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(f''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def _A ( ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for root, _, files in os.walk(__snake_case ): if "__init__.py" in files: __SCREAMING_SNAKE_CASE = os.path.join(__snake_case , "__init__.py" ) __SCREAMING_SNAKE_CASE = parse_init(__snake_case ) if objects is not None: __SCREAMING_SNAKE_CASE = analyze_results(*__snake_case ) if len(__snake_case ) > 0: __SCREAMING_SNAKE_CASE = f'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("\n".join(__snake_case ) ) if len(__snake_case ) > 0: raise ValueError("\n\n".join(__snake_case ) ) def _A ( ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for path, directories, files in os.walk(__snake_case ): for folder in directories: # Ignore private modules if folder.startswith("_" ): directories.remove(__snake_case ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__snake_case ) / folder).glob("*.py" ) ) ) == 0: continue __SCREAMING_SNAKE_CASE = str((Path(__snake_case ) / folder).relative_to(__snake_case ) ) __SCREAMING_SNAKE_CASE = short_path.replace(os.path.sep , "." ) submodules.append(__snake_case ) for fname in files: if fname == "__init__.py": continue __SCREAMING_SNAKE_CASE = str((Path(__snake_case ) / fname).relative_to(__snake_case ) ) __SCREAMING_SNAKE_CASE = short_path.replace(".py" , "" ).replace(os.path.sep , "." ) if len(submodule.split("." ) ) == 1: submodules.append(__snake_case ) return submodules _snake_case : Tuple = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', ] def _A ( ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = importlib.util.spec_from_file_location( "transformers" , os.path.join(__snake_case , "__init__.py" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) __SCREAMING_SNAKE_CASE = spec.loader.load_module() __SCREAMING_SNAKE_CASE = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(__snake_case ) > 0: __SCREAMING_SNAKE_CASE = "\n".join(f'''- {module}''' for module in module_not_registered ) raise ValueError( "The following submodules are not properly registered in the main init of Transformers:\n" f'''{list_of_modules}\n''' "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value." ) if __name__ == "__main__": check_all_inits() check_submodules()
693
from math import sqrt def _A ( __snake_case :int ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 for i in range(1 , int(sqrt(__snake_case ) + 1 ) ): if n % i == 0 and i != sqrt(__snake_case ): total += i + n // i elif i == sqrt(__snake_case ): total += i return total - n def _A ( __snake_case :int = 1_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = sum( i for i in range(1 , __snake_case ) if sum_of_divisors(sum_of_divisors(__snake_case ) ) == i and sum_of_divisors(__snake_case ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
693
1
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : str = logging.get_logger(__name__) _snake_case : Union[str, Any] = { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json' ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""speech_to_text_2""" SCREAMING_SNAKE_CASE__ =["""past_key_values"""] SCREAMING_SNAKE_CASE__ ={"""num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self, _a=1_00_00, _a=6, _a=20_48, _a=4, _a=0.0, _a=True, _a="relu", _a=2_56, _a=0.1, _a=0.0, _a=0.0, _a=0.02, _a=2, _a=True, _a=1, _a=0, _a=2, _a=10_24, **_a, ) -> int: __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = d_model __SCREAMING_SNAKE_CASE = decoder_ffn_dim __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = decoder_attention_heads __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = activation_function __SCREAMING_SNAKE_CASE = init_std __SCREAMING_SNAKE_CASE = decoder_layerdrop __SCREAMING_SNAKE_CASE = use_cache __SCREAMING_SNAKE_CASE = decoder_layers __SCREAMING_SNAKE_CASE = scale_embedding # scale factor will be sqrt(d_model) if True __SCREAMING_SNAKE_CASE = max_target_positions super().__init__( pad_token_id=_a, bos_token_id=_a, eos_token_id=_a, decoder_start_token_id=_a, **_a, )
693
def _A ( __snake_case :int , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def _A ( __snake_case :float , __snake_case :float , __snake_case :float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
693
1
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __SCREAMING_SNAKE_CASE : def __init__( self, _a, _a=99, _a=13, _a=7, _a=9, _a=True, _a=True, _a=False, _a=32, _a=5, _a=4, _a=37, _a=8, _a=0.1, _a=0.002, _a=1, _a=0, _a=0, _a=None, _a=None, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = encoder_seq_length __SCREAMING_SNAKE_CASE = decoder_seq_length # For common tests __SCREAMING_SNAKE_CASE = self.decoder_seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_attention_mask __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = d_ff __SCREAMING_SNAKE_CASE = relative_attention_num_buckets __SCREAMING_SNAKE_CASE = dropout_rate __SCREAMING_SNAKE_CASE = initializer_factor __SCREAMING_SNAKE_CASE = eos_token_id __SCREAMING_SNAKE_CASE = pad_token_id __SCREAMING_SNAKE_CASE = decoder_start_token_id __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = decoder_layers def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig.from_pretrained("google/umt5-base" ) def __lowerCAmelCase ( self, _a, _a, _a, _a=None, _a=None, _a=None, _a=None, _a=None, ) -> int: if attention_mask is None: __SCREAMING_SNAKE_CASE = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: __SCREAMING_SNAKE_CASE = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_hidden_layers, config.num_attention_heads, device=_a ) if decoder_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones(config.num_decoder_layers, config.num_attention_heads, device=_a ) if cross_attn_head_mask is None: __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_attention_heads, device=_a ) 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, } def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.encoder_seq_length], self.vocab_size ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.decoder_seq_length], self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input __SCREAMING_SNAKE_CASE = input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = decoder_input_ids.clamp(self.pad_token_id + 1 ) __SCREAMING_SNAKE_CASE = self.get_config() __SCREAMING_SNAKE_CASE = config.num_attention_heads __SCREAMING_SNAKE_CASE = self.prepare_inputs_dict(_a, _a, _a ) return config, input_dict def __lowerCAmelCase ( self ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self ) -> Optional[int]: return TaConfig( vocab_size=1_66, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self ) -> Union[str, Any]: return TaConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, d_ff=self.d_ff, d_kv=self.hidden_size // self.num_attention_heads, num_layers=self.num_hidden_layers, num_decoder_layers=self.decoder_layers, num_heads=self.num_attention_heads, relative_attention_num_buckets=self.relative_attention_num_buckets, dropout_rate=self.dropout_rate, initializer_factor=self.initializer_factor, eos_token_id=self.eos_token_id, bos_token_id=self.pad_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.decoder_start_token_id, ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ) model.to(_a ) model.eval() __SCREAMING_SNAKE_CASE = model( input_ids=_a, decoder_input_ids=_a, attention_mask=_a, decoder_attention_mask=_a, ) __SCREAMING_SNAKE_CASE = model(input_ids=_a, decoder_input_ids=_a ) __SCREAMING_SNAKE_CASE = result.last_hidden_state __SCREAMING_SNAKE_CASE = result.past_key_values __SCREAMING_SNAKE_CASE = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size(), (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size(), (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_a ), config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ), 4 ) def __lowerCAmelCase ( self, _a, _a, _a, _a, _a, _a, ) -> Tuple: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).get_decoder().to(_a ).eval() # first forward pass __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) __SCREAMING_SNAKE_CASE = model(_a ) __SCREAMING_SNAKE_CASE = model(_a, use_cache=_a ) self.parent.assertTrue(len(_a ) == len(_a ) ) self.parent.assertTrue(len(_a ) == len(_a ) + 1 ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 1), config.vocab_size ) # append to next input_ids and __SCREAMING_SNAKE_CASE = torch.cat([input_ids, next_tokens], dim=-1 ) __SCREAMING_SNAKE_CASE = model(_a )["last_hidden_state"] __SCREAMING_SNAKE_CASE = model(_a, past_key_values=_a )["last_hidden_state"] # select random slice __SCREAMING_SNAKE_CASE = ids_tensor((1,), output_from_past.shape[-1] ).item() __SCREAMING_SNAKE_CASE = output_from_no_past[:, -1, random_slice_idx].detach() __SCREAMING_SNAKE_CASE = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_a, _a, atol=1E-3 ) ) def __lowerCAmelCase ( self, _a, _a, ) -> Optional[int]: __SCREAMING_SNAKE_CASE = UMTaModel(config=_a ).to(_a ).half().eval() __SCREAMING_SNAKE_CASE = model(**_a )["last_hidden_state"] self.parent.assertFalse(torch.isnan(_a ).any().item() ) @require_torch class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ =(UMTaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ =( { """conversational""": UMTaForConditionalGeneration, """feature-extraction""": UMTaModel, """summarization""": UMTaForConditionalGeneration, """text2text-generation""": UMTaForConditionalGeneration, """translation""": UMTaForConditionalGeneration, """question-answering""": UMTaForQuestionAnswering, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =False SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True # The small UMT5 model needs higher percentages for CPU/MP tests SCREAMING_SNAKE_CASE__ =[0.8, 0.9] def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = UMTaModelTester(self ) @unittest.skip("Test has a segmentation fault on torch 1.8.0" ) def __lowerCAmelCase ( self ) -> Dict: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = UMTaModel(config_and_inputs[0] ).to(_a ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _a, (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]), f'''{tmpdirname}/t5_test.onnx''', export_params=_a, opset_version=9, input_names=["input_ids", "decoder_input_ids"], ) @unittest.skipIf(torch_device == "cpu", "Cant do half precision" ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_a ) def __lowerCAmelCase ( self ) -> Tuple: __SCREAMING_SNAKE_CASE = ["encoder_attentions", "decoder_attentions", "cross_attentions"] __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = config_and_inputs[0] __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration(_a ).eval() model.to(_a ) __SCREAMING_SNAKE_CASE = { "head_mask": torch.zeros(config.num_layers, config.num_heads, device=_a ), "decoder_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), "cross_attn_head_mask": torch.zeros(config.num_decoder_layers, config.num_heads, device=_a ), } for attn_name, (name, mask) in zip(_a, head_masking.items() ): __SCREAMING_SNAKE_CASE = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": __SCREAMING_SNAKE_CASE = torch.ones( config.num_decoder_layers, config.num_heads, device=_a ) __SCREAMING_SNAKE_CASE = model.generate( config_and_inputs[1]["input_ids"], num_beams=1, max_length=3, output_attentions=_a, return_dict_in_generate=_a, **_a, ) # We check the state of decoder_attentions and cross_attentions just from the last step __SCREAMING_SNAKE_CASE = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ), 0.0 ) @unittest.skip("Does not work on the tiny model as we keep hitting edge cases." ) def __lowerCAmelCase ( self ) -> int: pass @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow @unittest.skip( "Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged" ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = UMTaForConditionalGeneration.from_pretrained("google/umt5-small", return_dict=_a ).to(_a ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("google/umt5-small", use_fast=_a, legacy=_a ) __SCREAMING_SNAKE_CASE = [ "Bonjour monsieur <extra_id_0> bien <extra_id_1>.", "No se como puedo <extra_id_0>.", "This is the reason why we <extra_id_0> them.", "The <extra_id_0> walks in <extra_id_1>, seats", "A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.", ] __SCREAMING_SNAKE_CASE = tokenizer(_a, return_tensors="pt", padding=_a ).input_ids # fmt: off __SCREAMING_SNAKE_CASE = torch.tensor( [ [ 3_85_30, 21_07_03, 25_62_99, 14_10, 25_62_98, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_26, 3_21, 6_71, 2_59_22, 25_62_99, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 14_60, 3_39, 3_12, 1_90_14, 1_06_20, 7_58, 25_62_99, 23_55,2_74, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_17, 25_62_99, 1_48_69, 2_81, 3_01, 25_62_98, 2_75, 11_99_83,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_20, 25_62_99, 1_48_69, 2_81, 22_34, 2_89, 22_75, 3_33,6_13_91, 2_89, 25_62_98, 5_43, 25_62_97, 16_87_14, 3_29, 25_62_96,2_74, 1], ] ) # fmt: on torch.testing.assert_allclose(_a, _a ) __SCREAMING_SNAKE_CASE = model.generate(input_ids.to(_a ) ) __SCREAMING_SNAKE_CASE = [ "<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>", "<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", "<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>", ] __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_a ) self.assertEqual(_a, _a )
693
1
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : Any = logging.get_logger(__name__) _snake_case : Union[str, Any] = { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json' ), 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json' ), 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json' ), } class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""dpr""" def __init__( self, _a=3_05_22, _a=7_68, _a=12, _a=12, _a=30_72, _a="gelu", _a=0.1, _a=0.1, _a=5_12, _a=2, _a=0.02, _a=1E-1_2, _a=0, _a="absolute", _a = 0, **_a, ) -> Any: super().__init__(pad_token_id=_a, **_a ) __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = projection_dim __SCREAMING_SNAKE_CASE = position_embedding_type
693
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def _A ( __snake_case :BertModel , __snake_case :str , __snake_case :str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") __SCREAMING_SNAKE_CASE = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__snake_case ): os.makedirs(__snake_case ) __SCREAMING_SNAKE_CASE = model.state_dict() def to_tf_var_name(__snake_case :str ): for patt, repl in iter(__snake_case ): __SCREAMING_SNAKE_CASE = name.replace(__snake_case , __snake_case ) return f'''bert/{name}''' def create_tf_var(__snake_case :np.ndarray , __snake_case :str , __snake_case :tf.Session ): __SCREAMING_SNAKE_CASE = tf.dtypes.as_dtype(tensor.dtype ) __SCREAMING_SNAKE_CASE = tf.get_variable(dtype=__snake_case , shape=tensor.shape , name=__snake_case , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__snake_case ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: __SCREAMING_SNAKE_CASE = to_tf_var_name(__snake_case ) __SCREAMING_SNAKE_CASE = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): __SCREAMING_SNAKE_CASE = torch_tensor.T __SCREAMING_SNAKE_CASE = create_tf_var(tensor=__snake_case , name=__snake_case , session=__snake_case ) tf.keras.backend.set_value(__snake_case , __snake_case ) __SCREAMING_SNAKE_CASE = session.run(__snake_case ) print(f'''Successfully created {tf_name}: {np.allclose(__snake_case , __snake_case )}''' ) __SCREAMING_SNAKE_CASE = tf.train.Saver(tf.trainable_variables() ) saver.save(__snake_case , os.path.join(__snake_case , model_name.replace("-" , "_" ) + ".ckpt" ) ) def _A ( __snake_case :str=None ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__snake_case , required=__snake_case , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__snake_case , default=__snake_case , required=__snake_case , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__snake_case , required=__snake_case , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__snake_case , required=__snake_case , help="Directory in which to save tensorflow model" ) __SCREAMING_SNAKE_CASE = parser.parse_args(__snake_case ) __SCREAMING_SNAKE_CASE = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__snake_case , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
693
1
def _A ( __snake_case :list[list[int]] , __snake_case :int , __snake_case :int , __snake_case :list[int] ) -> bool: """simple docstring""" if graph[path[curr_ind - 1]][next_ver] == 0: return False # 2. Validate that next vertex is not already in path return not any(vertex == next_ver for vertex in path ) def _A ( __snake_case :list[list[int]] , __snake_case :list[int] , __snake_case :int ) -> bool: """simple docstring""" if curr_ind == len(__snake_case ): # return whether path exists between current and starting vertices return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step for next_ver in range(0 , len(__snake_case ) ): if valid_connection(__snake_case , __snake_case , __snake_case , __snake_case ): # Insert current vertex into path as next transition __SCREAMING_SNAKE_CASE = next_ver # Validate created path if util_hamilton_cycle(__snake_case , __snake_case , curr_ind + 1 ): return True # Backtrack __SCREAMING_SNAKE_CASE = -1 return False def _A ( __snake_case :list[list[int]] , __snake_case :int = 0 ) -> list[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = [-1] * (len(__snake_case ) + 1) # initialize start and end of path with starting index __SCREAMING_SNAKE_CASE = __SCREAMING_SNAKE_CASE = start_index # evaluate and if we find answer return path either return empty array return path if util_hamilton_cycle(__snake_case , __snake_case , 1 ) else []
693
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _snake_case : str = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =["""input_values""", """padding_mask"""] def __init__( self, _a = 1, _a = 2_40_00, _a = 0.0, _a = None, _a = None, **_a, ) -> str: super().__init__(feature_size=_a, sampling_rate=_a, padding_value=_a, **_a ) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> 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 ) ) def __call__( self, _a, _a = None, _a = False, _a = None, _a = None, _a = None, ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) if padding and truncation: raise ValueError("Both padding and truncation were set. Make sure you only set one." ) elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(_a, (list, tuple) ) and (isinstance(raw_audio[0], (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a, dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_a, np.ndarray ): __SCREAMING_SNAKE_CASE = np.asarray(_a, dtype=np.floataa ) elif isinstance(_a, np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(_a ).T] # verify inputs are valid for idx, example in enumerate(_a ): if example.ndim > 2: raise ValueError(f'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f'''Expected stereo audio but example has {example.shape[-1]} channels''' ) __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"input_values": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio ) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride ) ) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = "max_length" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( _a, max_length=_a, truncation=_a, padding=_a, return_attention_mask=_a, ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("attention_mask" ) __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("input_values" ): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T ) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(_a ) return padded_inputs
693
1
_snake_case : Any = { 'Pillow': 'Pillow<10.0.0', 'accelerate': 'accelerate>=0.20.3', 'av': 'av==9.2.0', 'beautifulsoup4': 'beautifulsoup4', 'black': 'black~=23.1', 'codecarbon': 'codecarbon==1.2.0', 'cookiecutter': 'cookiecutter==1.7.3', 'dataclasses': 'dataclasses', 'datasets': 'datasets!=2.5.0', 'decord': 'decord==0.6.0', 'deepspeed': 'deepspeed>=0.9.3', 'diffusers': 'diffusers', 'dill': 'dill<0.3.5', 'evaluate': 'evaluate>=0.2.0', 'fairscale': 'fairscale>0.3', 'faiss-cpu': 'faiss-cpu', 'fastapi': 'fastapi', 'filelock': 'filelock', 'flax': 'flax>=0.4.1,<=0.7.0', 'ftfy': 'ftfy', 'fugashi': 'fugashi>=1.0', 'GitPython': 'GitPython<3.1.19', 'hf-doc-builder': 'hf-doc-builder>=0.3.0', 'huggingface-hub': 'huggingface-hub>=0.14.1,<1.0', 'importlib_metadata': 'importlib_metadata', 'ipadic': 'ipadic>=1.0.0,<2.0', 'isort': 'isort>=5.5.4', 'jax': 'jax>=0.2.8,!=0.3.2,<=0.4.13', 'jaxlib': 'jaxlib>=0.1.65,<=0.4.13', 'jieba': 'jieba', 'kenlm': 'kenlm', 'keras-nlp': 'keras-nlp>=0.3.1', 'librosa': 'librosa', 'nltk': 'nltk', 'natten': 'natten>=0.14.6', 'numpy': 'numpy>=1.17', 'onnxconverter-common': 'onnxconverter-common', 'onnxruntime-tools': 'onnxruntime-tools>=1.4.2', 'onnxruntime': 'onnxruntime>=1.4.0', 'opencv-python': 'opencv-python', 'optuna': 'optuna', 'optax': 'optax>=0.0.8,<=0.1.4', 'packaging': 'packaging>=20.0', 'parameterized': 'parameterized', 'phonemizer': 'phonemizer', 'protobuf': 'protobuf', 'psutil': 'psutil', 'pyyaml': 'pyyaml>=5.1', 'pydantic': 'pydantic<2', 'pytest': 'pytest>=7.2.0', 'pytest-timeout': 'pytest-timeout', 'pytest-xdist': 'pytest-xdist', 'python': 'python>=3.8.0', 'ray[tune]': 'ray[tune]', 'regex': 'regex!=2019.12.17', 'requests': 'requests', 'rhoknp': 'rhoknp>=1.1.0,<1.3.1', 'rjieba': 'rjieba', 'rouge-score': 'rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1', 'ruff': 'ruff>=0.0.241,<=0.0.259', 'sacrebleu': 'sacrebleu>=1.4.12,<2.0.0', 'sacremoses': 'sacremoses', 'safetensors': 'safetensors>=0.3.1', 'sagemaker': 'sagemaker>=2.31.0', 'scikit-learn': 'scikit-learn', 'sentencepiece': 'sentencepiece>=0.1.91,!=0.1.92', 'sigopt': 'sigopt', 'starlette': 'starlette', 'sudachipy': 'sudachipy>=0.6.6', 'sudachidict_core': 'sudachidict_core>=20220729', 'tensorflow-cpu': 'tensorflow-cpu>=2.6,<2.14', 'tensorflow': 'tensorflow>=2.6,<2.14', 'tensorflow-text': 'tensorflow-text<2.14', 'tf2onnx': 'tf2onnx', 'timeout-decorator': 'timeout-decorator', 'timm': 'timm', 'tokenizers': 'tokenizers>=0.11.1,!=0.11.3,<0.14', 'torch': 'torch>=1.9,!=1.12.0', 'torchaudio': 'torchaudio', 'torchvision': 'torchvision', 'pyctcdecode': 'pyctcdecode>=0.4.0', 'tqdm': 'tqdm>=4.27', 'unidic': 'unidic>=1.0.2', 'unidic_lite': 'unidic_lite>=1.0.7', 'urllib3': 'urllib3<2.0.0', 'uvicorn': 'uvicorn', }
693
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 SCREAMING_SNAKE_CASE__ =42 def __init__( self, _a, _a ) -> Dict: super().__init__() self.register_modules(unet=_a, scheduler=_a ) @torch.no_grad() def __call__( self, _a = 1, _a = 20_00, _a = None, _a = "pil", _a = True, **_a, ) -> Union[ImagePipelineOutput, Tuple]: __SCREAMING_SNAKE_CASE = self.unet.config.sample_size __SCREAMING_SNAKE_CASE = (batch_size, 3, img_size, img_size) __SCREAMING_SNAKE_CASE = self.unet __SCREAMING_SNAKE_CASE = randn_tensor(_a, generator=_a ) * self.scheduler.init_noise_sigma __SCREAMING_SNAKE_CASE = sample.to(self.device ) self.scheduler.set_timesteps(_a ) self.scheduler.set_sigmas(_a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): __SCREAMING_SNAKE_CASE = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): __SCREAMING_SNAKE_CASE = self.unet(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_correct(_a, _a, generator=_a ).prev_sample # prediction step __SCREAMING_SNAKE_CASE = model(_a, _a ).sample __SCREAMING_SNAKE_CASE = self.scheduler.step_pred(_a, _a, _a, generator=_a ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = output.prev_sample, output.prev_sample_mean __SCREAMING_SNAKE_CASE = sample_mean.clamp(0, 1 ) __SCREAMING_SNAKE_CASE = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE = self.numpy_to_pil(_a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_a )
693
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case : Optional[Any] = { 'configuration_x_clip': [ 'XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XCLIPConfig', 'XCLIPTextConfig', 'XCLIPVisionConfig', ], 'processing_x_clip': ['XCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : int = [ 'XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'XCLIPModel', 'XCLIPPreTrainedModel', 'XCLIPTextModel', 'XCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys _snake_case : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
693
def _A ( __snake_case :int = 400_0000 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__snake_case ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = b, a + b return sum(__snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
693
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case : Optional[int] = { 'configuration_informer': [ 'INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'InformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : List[Any] = [ 'INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'InformerForPrediction', 'InformerModel', 'InformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys _snake_case : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
693
from __future__ import annotations _snake_case : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _snake_case : Optional[int] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(__snake_case ) for i in range(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , __snake_case ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(__snake_case ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(__snake_case ) return result def _A ( __snake_case :list[float] ) -> list[float]: """simple docstring""" __SCREAMING_SNAKE_CASE = len(__snake_case ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(__snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _snake_case : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
693
1
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def _A ( __snake_case :Tuple ) -> List[str]: """simple docstring""" if "cls_token" in name: __SCREAMING_SNAKE_CASE = name.replace("cls_token" , "vit.embeddings.cls_token" ) if "mask_token" in name: __SCREAMING_SNAKE_CASE = name.replace("mask_token" , "decoder.mask_token" ) if "decoder_pos_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pos_embed" , "decoder.decoder_pos_embed" ) if "pos_embed" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("pos_embed" , "vit.embeddings.position_embeddings" ) if "patch_embed.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.proj" , "vit.embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __SCREAMING_SNAKE_CASE = name.replace("patch_embed.norm" , "vit.embeddings.norm" ) if "decoder_blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_blocks" , "decoder.decoder_layers" ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace("blocks" , "vit.encoder.layer" ) if "attn.proj" in name: __SCREAMING_SNAKE_CASE = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: __SCREAMING_SNAKE_CASE = name.replace("attn" , "attention.self" ) if "norm1" in name: __SCREAMING_SNAKE_CASE = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __SCREAMING_SNAKE_CASE = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __SCREAMING_SNAKE_CASE = name.replace("mlp.fc2" , "output.dense" ) if "decoder_embed" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_embed" , "decoder.decoder_embed" ) if "decoder_norm" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_norm" , "decoder.decoder_norm" ) if "decoder_pred" in name: __SCREAMING_SNAKE_CASE = name.replace("decoder_pred" , "decoder.decoder_pred" ) if "norm.weight" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.weight" , "vit.layernorm.weight" ) if "norm.bias" in name and "decoder" not in name: __SCREAMING_SNAKE_CASE = name.replace("norm.bias" , "vit.layernorm.bias" ) return name def _A ( __snake_case :List[Any] , __snake_case :Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = orig_state_dict.pop(__snake_case ) if "qkv" in key: __SCREAMING_SNAKE_CASE = key.split("." ) __SCREAMING_SNAKE_CASE = int(key_split[1] ) if "decoder_blocks" in key: __SCREAMING_SNAKE_CASE = config.decoder_hidden_size __SCREAMING_SNAKE_CASE = "decoder.decoder_layers." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] elif "bias" in key: __SCREAMING_SNAKE_CASE = val[:dim] __SCREAMING_SNAKE_CASE = val[dim : dim * 2] __SCREAMING_SNAKE_CASE = val[-dim:] else: __SCREAMING_SNAKE_CASE = config.hidden_size __SCREAMING_SNAKE_CASE = "vit.encoder.layer." if "weight" in key: __SCREAMING_SNAKE_CASE = val[:dim, :] __SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] __SCREAMING_SNAKE_CASE = val[-dim:, :] elif "bias" in key: __SCREAMING_SNAKE_CASE = val[:dim] __SCREAMING_SNAKE_CASE = val[dim : dim * 2] __SCREAMING_SNAKE_CASE = val[-dim:] else: __SCREAMING_SNAKE_CASE = val return orig_state_dict def _A ( __snake_case :Any , __snake_case :int ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = ViTMAEConfig() if "large" in checkpoint_url: __SCREAMING_SNAKE_CASE = 1024 __SCREAMING_SNAKE_CASE = 4096 __SCREAMING_SNAKE_CASE = 24 __SCREAMING_SNAKE_CASE = 16 elif "huge" in checkpoint_url: __SCREAMING_SNAKE_CASE = 14 __SCREAMING_SNAKE_CASE = 1280 __SCREAMING_SNAKE_CASE = 5120 __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = 16 __SCREAMING_SNAKE_CASE = ViTMAEForPreTraining(__snake_case ) __SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(__snake_case , map_location="cpu" )["model"] __SCREAMING_SNAKE_CASE = ViTMAEImageProcessor(size=config.image_size ) __SCREAMING_SNAKE_CASE = convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) model.eval() __SCREAMING_SNAKE_CASE = "https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg" __SCREAMING_SNAKE_CASE = Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) __SCREAMING_SNAKE_CASE = ViTMAEImageProcessor(size=config.image_size ) __SCREAMING_SNAKE_CASE = image_processor(images=__snake_case , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) __SCREAMING_SNAKE_CASE = model(**__snake_case ) __SCREAMING_SNAKE_CASE = outputs.logits if "large" in checkpoint_url: __SCREAMING_SNAKE_CASE = torch.tensor( [[-0.7_3_0_9, -0.7_1_2_8, -1.0_1_6_9], [-1.0_1_6_1, -0.9_0_5_8, -1.1_8_7_8], [-1.0_4_7_8, -0.9_4_1_1, -1.1_9_1_1]] ) elif "huge" in checkpoint_url: __SCREAMING_SNAKE_CASE = torch.tensor( [[-1.1_5_9_9, -0.9_1_9_9, -1.2_2_2_1], [-1.1_9_5_2, -0.9_2_6_9, -1.2_3_0_7], [-1.2_1_4_3, -0.9_3_3_7, -1.2_2_6_2]] ) else: __SCREAMING_SNAKE_CASE = torch.tensor( [[-0.9_1_9_2, -0.8_4_8_1, -1.1_2_5_9], [-1.1_3_4_9, -1.0_0_3_4, -1.2_5_9_9], [-1.1_7_5_7, -1.0_4_2_9, -1.2_7_2_6]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , __snake_case , atol=1e-4 ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__snake_case ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__snake_case ) if __name__ == "__main__": _snake_case : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth', type=str, help='URL of the checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) _snake_case : str = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
693
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self, _a ) -> Any: __SCREAMING_SNAKE_CASE = data __SCREAMING_SNAKE_CASE = None def __repr__( self ) -> str: return f'''Node({self.data})''' class __SCREAMING_SNAKE_CASE : def __init__( self ) -> Tuple: __SCREAMING_SNAKE_CASE = None def __iter__( self ) -> Any: __SCREAMING_SNAKE_CASE = self.head while node: yield node.data __SCREAMING_SNAKE_CASE = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_a ) for item in self] ) def __getitem__( self, _a ) -> Any: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self, _a, _a ) -> None: if not 0 <= index < len(self ): raise ValueError("list index out of range." ) __SCREAMING_SNAKE_CASE = self.head for _ in range(_a ): __SCREAMING_SNAKE_CASE = current.next __SCREAMING_SNAKE_CASE = data def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(len(self ), _a ) def __lowerCAmelCase ( self, _a ) -> None: self.insert_nth(0, _a ) def __lowerCAmelCase ( self, _a, _a ) -> None: if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) __SCREAMING_SNAKE_CASE = Node(_a ) if self.head is None: __SCREAMING_SNAKE_CASE = new_node elif index == 0: __SCREAMING_SNAKE_CASE = self.head # link new_node to head __SCREAMING_SNAKE_CASE = new_node else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self ) -> None: # print every node data print(self ) def __lowerCAmelCase ( self ) -> Any: return self.delete_nth(0 ) def __lowerCAmelCase ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowerCAmelCase ( self, _a = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) __SCREAMING_SNAKE_CASE = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE = self.head.next else: __SCREAMING_SNAKE_CASE = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next __SCREAMING_SNAKE_CASE = temp.next.next return delete_node.data def __lowerCAmelCase ( self ) -> bool: return self.head is None def __lowerCAmelCase ( self ) -> None: __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE = prev def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = LinkedList() assert linked_list.is_empty() is True assert str(__snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(__snake_case ) == i linked_list.insert_nth(__snake_case , i + 1 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(__snake_case ) == 9 assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(__snake_case ) == "->".join(str(__snake_case ) for i in range(-8 , 1 ) ) def _A ( ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_9_2.5_5_5_5_5, "Hello, world!", 7_7.9, Node(10 ), None, None, 1_2.2_0, ] __SCREAMING_SNAKE_CASE = LinkedList() for i in test_input: linked_list.insert_tail(__snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(__snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE = linked_list.delete_head() assert result == -9 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE = linked_list.delete_tail() assert result == 1_2.2 assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE = linked_list.delete_nth(10 ) assert result is None assert ( str(__snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(__snake_case ) assert ( str(__snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(__snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _A ( ) -> Union[str, Any]: """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(__snake_case ) print("\nReading/changing Node data using indexing:" ) print(f'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE = input("Enter New Value: " ).strip() print("New list:" ) print(__snake_case ) print(f'''length of linked_list is : {len(__snake_case )}''' ) if __name__ == "__main__": main()
693
1
from dataclasses import dataclass from typing import Dict, Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .attention_processor import AttentionProcessor, AttnProcessor from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder @dataclass class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =42 class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =True @register_to_config def __init__( self, _a = 3, _a = 3, _a = ("DownEncoderBlock2D",), _a = ("UpDecoderBlock2D",), _a = (64,), _a = 1, _a = "silu", _a = 4, _a = 32, _a = 32, _a = 0.1_8215, ) -> Union[str, Any]: super().__init__() # pass init params to Encoder __SCREAMING_SNAKE_CASE = Encoder( in_channels=_a, out_channels=_a, down_block_types=_a, block_out_channels=_a, layers_per_block=_a, act_fn=_a, norm_num_groups=_a, double_z=_a, ) # pass init params to Decoder __SCREAMING_SNAKE_CASE = Decoder( in_channels=_a, out_channels=_a, up_block_types=_a, block_out_channels=_a, layers_per_block=_a, norm_num_groups=_a, act_fn=_a, ) __SCREAMING_SNAKE_CASE = nn.Convad(2 * latent_channels, 2 * latent_channels, 1 ) __SCREAMING_SNAKE_CASE = nn.Convad(_a, _a, 1 ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False # only relevant if vae tiling is enabled __SCREAMING_SNAKE_CASE = self.config.sample_size __SCREAMING_SNAKE_CASE = ( self.config.sample_size[0] if isinstance(self.config.sample_size, (list, tuple) ) else self.config.sample_size ) __SCREAMING_SNAKE_CASE = int(sample_size / (2 ** (len(self.config.block_out_channels ) - 1)) ) __SCREAMING_SNAKE_CASE = 0.25 def __lowerCAmelCase ( self, _a, _a=False ) -> Optional[Any]: if isinstance(_a, (Encoder, Decoder) ): __SCREAMING_SNAKE_CASE = value def __lowerCAmelCase ( self, _a = True ) -> List[str]: __SCREAMING_SNAKE_CASE = use_tiling def __lowerCAmelCase ( self ) -> List[str]: self.enable_tiling(_a ) def __lowerCAmelCase ( self ) -> str: __SCREAMING_SNAKE_CASE = True def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = False @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def __lowerCAmelCase ( self ) -> Dict[str, AttentionProcessor]: __SCREAMING_SNAKE_CASE = {} def fn_recursive_add_processors(_a, _a, _a ): if hasattr(_a, "set_processor" ): __SCREAMING_SNAKE_CASE = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f'''{name}.{sub_name}''', _a, _a ) return processors for name, module in self.named_children(): fn_recursive_add_processors(_a, _a, _a ) return processors def __lowerCAmelCase ( self, _a ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = len(self.attn_processors.keys() ) if isinstance(_a, _a ) and len(_a ) != count: raise ValueError( f'''A dict of processors was passed, but the number of processors {len(_a )} does not match the''' f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.''' ) def fn_recursive_attn_processor(_a, _a, _a ): if hasattr(_a, "set_processor" ): if not isinstance(_a, _a ): module.set_processor(_a ) else: module.set_processor(processor.pop(f'''{name}.processor''' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f'''{name}.{sub_name}''', _a, _a ) for name, module in self.named_children(): fn_recursive_attn_processor(_a, _a, _a ) def __lowerCAmelCase ( self ) -> Union[str, Any]: self.set_attn_processor(AttnProcessor() ) @apply_forward_hook def __lowerCAmelCase ( self, _a, _a = True ) -> AutoencoderKLOutput: if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size): return self.tiled_encode(_a, return_dict=_a ) if self.use_slicing and x.shape[0] > 1: __SCREAMING_SNAKE_CASE = [self.encoder(_a ) for x_slice in x.split(1 )] __SCREAMING_SNAKE_CASE = torch.cat(_a ) else: __SCREAMING_SNAKE_CASE = self.encoder(_a ) __SCREAMING_SNAKE_CASE = self.quant_conv(_a ) __SCREAMING_SNAKE_CASE = DiagonalGaussianDistribution(_a ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=_a ) def __lowerCAmelCase ( self, _a, _a = True ) -> Union[DecoderOutput, torch.FloatTensor]: if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size): return self.tiled_decode(_a, return_dict=_a ) __SCREAMING_SNAKE_CASE = self.post_quant_conv(_a ) __SCREAMING_SNAKE_CASE = self.decoder(_a ) if not return_dict: return (dec,) return DecoderOutput(sample=_a ) @apply_forward_hook def __lowerCAmelCase ( self, _a, _a = True ) -> Union[DecoderOutput, torch.FloatTensor]: if self.use_slicing and z.shape[0] > 1: __SCREAMING_SNAKE_CASE = [self._decode(_a ).sample for z_slice in z.split(1 )] __SCREAMING_SNAKE_CASE = torch.cat(_a ) else: __SCREAMING_SNAKE_CASE = self._decode(_a ).sample if not return_dict: return (decoded,) return DecoderOutput(sample=_a ) def __lowerCAmelCase ( self, _a, _a, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = min(a.shape[2], b.shape[2], _a ) for y in range(_a ): __SCREAMING_SNAKE_CASE = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent) return b def __lowerCAmelCase ( self, _a, _a, _a ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = min(a.shape[3], b.shape[3], _a ) for x in range(_a ): __SCREAMING_SNAKE_CASE = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent) return b def __lowerCAmelCase ( self, _a, _a = True ) -> AutoencoderKLOutput: __SCREAMING_SNAKE_CASE = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor) ) __SCREAMING_SNAKE_CASE = int(self.tile_latent_min_size * self.tile_overlap_factor ) __SCREAMING_SNAKE_CASE = self.tile_latent_min_size - blend_extent # Split the image into 512x512 tiles and encode them separately. __SCREAMING_SNAKE_CASE = [] for i in range(0, x.shape[2], _a ): __SCREAMING_SNAKE_CASE = [] for j in range(0, x.shape[3], _a ): __SCREAMING_SNAKE_CASE = x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] __SCREAMING_SNAKE_CASE = self.encoder(_a ) __SCREAMING_SNAKE_CASE = self.quant_conv(_a ) row.append(_a ) rows.append(_a ) __SCREAMING_SNAKE_CASE = [] for i, row in enumerate(_a ): __SCREAMING_SNAKE_CASE = [] for j, tile in enumerate(_a ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: __SCREAMING_SNAKE_CASE = self.blend_v(rows[i - 1][j], _a, _a ) if j > 0: __SCREAMING_SNAKE_CASE = self.blend_h(row[j - 1], _a, _a ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(_a, dim=3 ) ) __SCREAMING_SNAKE_CASE = torch.cat(_a, dim=2 ) __SCREAMING_SNAKE_CASE = DiagonalGaussianDistribution(_a ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=_a ) def __lowerCAmelCase ( self, _a, _a = True ) -> Union[DecoderOutput, torch.FloatTensor]: __SCREAMING_SNAKE_CASE = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor) ) __SCREAMING_SNAKE_CASE = int(self.tile_sample_min_size * self.tile_overlap_factor ) __SCREAMING_SNAKE_CASE = self.tile_sample_min_size - blend_extent # Split z into overlapping 64x64 tiles and decode them separately. # The tiles have an overlap to avoid seams between tiles. __SCREAMING_SNAKE_CASE = [] for i in range(0, z.shape[2], _a ): __SCREAMING_SNAKE_CASE = [] for j in range(0, z.shape[3], _a ): __SCREAMING_SNAKE_CASE = z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size] __SCREAMING_SNAKE_CASE = self.post_quant_conv(_a ) __SCREAMING_SNAKE_CASE = self.decoder(_a ) row.append(_a ) rows.append(_a ) __SCREAMING_SNAKE_CASE = [] for i, row in enumerate(_a ): __SCREAMING_SNAKE_CASE = [] for j, tile in enumerate(_a ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: __SCREAMING_SNAKE_CASE = self.blend_v(rows[i - 1][j], _a, _a ) if j > 0: __SCREAMING_SNAKE_CASE = self.blend_h(row[j - 1], _a, _a ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(_a, dim=3 ) ) __SCREAMING_SNAKE_CASE = torch.cat(_a, dim=2 ) if not return_dict: return (dec,) return DecoderOutput(sample=_a ) def __lowerCAmelCase ( self, _a, _a = False, _a = True, _a = None, ) -> Union[DecoderOutput, torch.FloatTensor]: __SCREAMING_SNAKE_CASE = sample __SCREAMING_SNAKE_CASE = self.encode(_a ).latent_dist if sample_posterior: __SCREAMING_SNAKE_CASE = posterior.sample(generator=_a ) else: __SCREAMING_SNAKE_CASE = posterior.mode() __SCREAMING_SNAKE_CASE = self.decode(_a ).sample if not return_dict: return (dec,) return DecoderOutput(sample=_a )
693
import argparse import json from tqdm import tqdm def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "--src_path" , type=__snake_case , default="biencoder-nq-dev.json" , help="Path to raw DPR training data" , ) parser.add_argument( "--evaluation_set" , type=__snake_case , help="where to store parsed evaluation_set file" , ) parser.add_argument( "--gold_data_path" , type=__snake_case , help="where to store parsed gold_data_path file" , ) __SCREAMING_SNAKE_CASE = parser.parse_args() with open(args.src_path , "r" ) as src_file, open(args.evaluation_set , "w" ) as eval_file, open( args.gold_data_path , "w" ) as gold_file: __SCREAMING_SNAKE_CASE = json.load(__snake_case ) for dpr_record in tqdm(__snake_case ): __SCREAMING_SNAKE_CASE = dpr_record["question"] __SCREAMING_SNAKE_CASE = [context["title"] for context in dpr_record["positive_ctxs"]] eval_file.write(question + "\n" ) gold_file.write("\t".join(__snake_case ) + "\n" ) if __name__ == "__main__": main()
693
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _snake_case : List[str] = '▁' _snake_case : Dict = {'vocab_file': 'spiece.model'} _snake_case : str = { 'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'} } _snake_case : Optional[int] = { 'google/pegasus-xsum': 5_12, } _snake_case : Union[str, Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ =VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ =PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ =["""input_ids""", """attention_mask"""] def __init__( self, _a, _a="<pad>", _a="</s>", _a="<unk>", _a="<mask_2>", _a="<mask_1>", _a=None, _a=1_03, _a = None, **_a, ) -> None: __SCREAMING_SNAKE_CASE = offset if additional_special_tokens is not None: if not isinstance(_a, _a ): raise TypeError( f'''additional_special_tokens should be of type {type(_a )}, but is''' f''' {type(_a )}''' ) __SCREAMING_SNAKE_CASE = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f'''<unk_{i}>''' for i in range(len(_a ), self.offset - 1 ) ] if len(set(_a ) ) != len(_a ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" f''' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.''' ) __SCREAMING_SNAKE_CASE = additional_special_tokens_extended else: __SCREAMING_SNAKE_CASE = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f'''<unk_{i}>''' for i in range(2, self.offset )] __SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=_a, unk_token=_a, mask_token=_a, pad_token=_a, mask_token_sent=_a, offset=_a, additional_special_tokens=_a, sp_model_kwargs=self.sp_model_kwargs, **_a, ) __SCREAMING_SNAKE_CASE = mask_token_sent __SCREAMING_SNAKE_CASE = vocab_file __SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_a ) # add special tokens to encoder dict __SCREAMING_SNAKE_CASE = { 0: self.pad_token, 1: self.eos_token, } if self.mask_token_sent is not None: self.encoder.update( { 2: self.mask_token_sent, 3: self.mask_token, } ) if self.offset > 0: # entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102 # mask_token_sent is already added to list -> so start at 1 self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1, self.offset - 1 )} ) __SCREAMING_SNAKE_CASE = {v: k for k, v in self.encoder.items()} @property def __lowerCAmelCase ( self ) -> int: return len(self.sp_model ) + self.offset def __lowerCAmelCase ( self ) -> Dict[str, int]: __SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.__dict__.copy() __SCREAMING_SNAKE_CASE = None return state def __setstate__( self, _a ) -> List[Any]: __SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self, "sp_model_kwargs" ): __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self, _a ) -> List[str]: return self.sp_model.encode(_a, out_type=_a ) def __lowerCAmelCase ( self, _a ) -> int: if token in self.decoder: return self.decoder[token] elif token in self.added_tokens_decoder: return self.added_tokens_decoder[token] __SCREAMING_SNAKE_CASE = self.sp_model.piece_to_id(_a ) return sp_id + self.offset def __lowerCAmelCase ( self, _a ) -> str: if index in self.encoder: return self.encoder[index] elif index in self.added_tokens_encoder: return self.added_tokens_encoder[index] else: __SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(index - self.offset ) return token def __lowerCAmelCase ( self, _a ) -> Tuple: __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = "" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(_a ) + token __SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(_a ) out_string += self.sp_model.decode(_a ) return out_string.strip() def __lowerCAmelCase ( self, _a=False ) -> Union[str, Any]: return 1 def __lowerCAmelCase ( self, _a ) -> int: __SCREAMING_SNAKE_CASE = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special return [1 if x in all_special_ids else 0 for x in seq] def __lowerCAmelCase ( self, _a, _a = None, _a = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(_a ) elif token_ids_a is None: return self._special_token_mask(_a ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def __lowerCAmelCase ( self, _a, _a=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def __lowerCAmelCase ( self, _a, _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __SCREAMING_SNAKE_CASE = os.path.join( _a, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file, _a ) elif not os.path.isfile(self.vocab_file ): with open(_a, "wb" ) as fi: __SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,)
693
def _A ( __snake_case :int = 10**9 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value __SCREAMING_SNAKE_CASE = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
693
1
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
693
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType _snake_case , _snake_case , _snake_case : List[Any] = False, False, False @dataclass class __SCREAMING_SNAKE_CASE : SCREAMING_SNAKE_CASE__ =None SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =True SCREAMING_SNAKE_CASE__ =None # Automatically constructed SCREAMING_SNAKE_CASE__ ="dict" SCREAMING_SNAKE_CASE__ =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) SCREAMING_SNAKE_CASE__ =field(default="""Audio""" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ) -> Optional[int]: return self.pa_type def __lowerCAmelCase ( self, _a ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(_a, _a ): return {"bytes": None, "path": value} elif isinstance(_a, _a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __SCREAMING_SNAKE_CASE = BytesIO() sf.write(_a, value["array"], value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __SCREAMING_SNAKE_CASE = np.frombuffer(value["bytes"], dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __SCREAMING_SNAKE_CASE = np.memmap(value["path"], dtype="h", mode="r" ).astype(np.floataa ) / 3_27_67 __SCREAMING_SNAKE_CASE = BytesIO(bytes() ) sf.write(_a, _a, value["sampling_rate"], format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'''An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def __lowerCAmelCase ( self, _a, _a = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f'''An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err __SCREAMING_SNAKE_CASE = xsplitext(_a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: __SCREAMING_SNAKE_CASE = token_per_repo_id or {} __SCREAMING_SNAKE_CASE = path.split("::" )[-1] try: __SCREAMING_SNAKE_CASE = string_to_dict(_a, config.HUB_DATASETS_URL )["repo_id"] __SCREAMING_SNAKE_CASE = token_per_repo_id[repo_id] except (ValueError, KeyError): __SCREAMING_SNAKE_CASE = None with xopen(_a, "rb", use_auth_token=_a ) as f: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) else: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sf.read(_a ) __SCREAMING_SNAKE_CASE = array.T if self.mono: __SCREAMING_SNAKE_CASE = librosa.to_mono(_a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __SCREAMING_SNAKE_CASE = librosa.resample(_a, orig_sr=_a, target_sr=self.sampling_rate ) __SCREAMING_SNAKE_CASE = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def __lowerCAmelCase ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def __lowerCAmelCase ( self, _a ) -> pa.StructArray: if pa.types.is_string(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): __SCREAMING_SNAKE_CASE = pa.array([Audio().encode_example(_a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("bytes" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: __SCREAMING_SNAKE_CASE = storage.field("path" ) else: __SCREAMING_SNAKE_CASE = pa.array([None] * len(_a ), type=pa.string() ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) return array_cast(_a, self.pa_type ) def __lowerCAmelCase ( self, _a ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a, "rb" ) as f: __SCREAMING_SNAKE_CASE = f.read() return bytes_ __SCREAMING_SNAKE_CASE = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) __SCREAMING_SNAKE_CASE = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) __SCREAMING_SNAKE_CASE = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(_a, self.pa_type )
693
1
from __future__ import annotations from random import random from typing import Generic, TypeVar _snake_case : Optional[int] = TypeVar('KT') _snake_case : int = TypeVar('VT') class __SCREAMING_SNAKE_CASE ( Generic[KT, VT] ): def __init__( self, _a = "root", _a = None ) -> List[str]: __SCREAMING_SNAKE_CASE = key __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = [] def __repr__( self ) -> str: return f'''Node({self.key}: {self.value})''' @property def __lowerCAmelCase ( self ) -> int: return len(self.forward ) class __SCREAMING_SNAKE_CASE ( Generic[KT, VT] ): def __init__( self, _a = 0.5, _a = 16 ) -> Dict: __SCREAMING_SNAKE_CASE = Node[KT, VT]() __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = p __SCREAMING_SNAKE_CASE = max_level def __str__( self ) -> str: __SCREAMING_SNAKE_CASE = list(self ) if len(_a ) == 0: return f'''SkipList(level={self.level})''' __SCREAMING_SNAKE_CASE = max((len(str(_a ) ) for item in items), default=4 ) __SCREAMING_SNAKE_CASE = max(_a, 4 ) + 4 __SCREAMING_SNAKE_CASE = self.head __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = node.forward.copy() lines.append(f'''[{node.key}]'''.ljust(_a, "-" ) + "* " * len(_a ) ) lines.append(" " * label_size + "| " * len(_a ) ) while len(node.forward ) != 0: __SCREAMING_SNAKE_CASE = node.forward[0] lines.append( f'''[{node.key}]'''.ljust(_a, "-" ) + " ".join(str(n.key ) if n.key == node.key else "|" for n in forwards ) ) lines.append(" " * label_size + "| " * len(_a ) ) __SCREAMING_SNAKE_CASE = node.forward lines.append("None".ljust(_a ) + "* " * len(_a ) ) return f'''SkipList(level={self.level})\n''' + "\n".join(_a ) def __iter__( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.head while len(node.forward ) != 0: yield node.forward[0].key __SCREAMING_SNAKE_CASE = node.forward[0] def __lowerCAmelCase ( self ) -> int: __SCREAMING_SNAKE_CASE = 1 while random() < self.p and level < self.max_level: level += 1 return level def __lowerCAmelCase ( self, _a ) -> tuple[Node[KT, VT] | None, list[Node[KT, VT]]]: __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: __SCREAMING_SNAKE_CASE = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(_a ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def __lowerCAmelCase ( self, _a ) -> Optional[Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self._locate_node(_a ) if node is not None: for i, update_node in enumerate(_a ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: __SCREAMING_SNAKE_CASE = node.forward[i] else: __SCREAMING_SNAKE_CASE = update_node.forward[:i] def __lowerCAmelCase ( self, _a, _a ) -> int: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self._locate_node(_a ) if node is not None: __SCREAMING_SNAKE_CASE = value else: __SCREAMING_SNAKE_CASE = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1, _a ): update_vector.append(self.head ) __SCREAMING_SNAKE_CASE = level __SCREAMING_SNAKE_CASE = Node(_a, _a ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(_a ) else: __SCREAMING_SNAKE_CASE = new_node def __lowerCAmelCase ( self, _a ) -> VT | None: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self._locate_node(_a ) if node is not None: return node.value return None def _A ( ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 3 ) skip_list.insert("Key2" , 12 ) skip_list.insert("Key3" , 41 ) skip_list.insert("Key4" , -19 ) __SCREAMING_SNAKE_CASE = skip_list.head __SCREAMING_SNAKE_CASE = {} while node.level != 0: __SCREAMING_SNAKE_CASE = node.forward[0] __SCREAMING_SNAKE_CASE = node.value assert len(__snake_case ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def _A ( ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 10 ) skip_list.insert("Key1" , 12 ) skip_list.insert("Key5" , 7 ) skip_list.insert("Key7" , 10 ) skip_list.insert("Key10" , 5 ) skip_list.insert("Key7" , 7 ) skip_list.insert("Key5" , 5 ) skip_list.insert("Key10" , 10 ) __SCREAMING_SNAKE_CASE = skip_list.head __SCREAMING_SNAKE_CASE = {} while node.level != 0: __SCREAMING_SNAKE_CASE = node.forward[0] __SCREAMING_SNAKE_CASE = node.value if len(__snake_case ) != 4: print() assert len(__snake_case ) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def _A ( ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() assert skip_list.find("Some key" ) is None def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key2" , 20 ) assert skip_list.find("Key2" ) == 20 skip_list.insert("Some Key" , 10 ) skip_list.insert("Key2" , 8 ) skip_list.insert("V" , 13 ) assert skip_list.find("Y" ) is None assert skip_list.find("Key2" ) == 8 assert skip_list.find("Some Key" ) == 10 assert skip_list.find("V" ) == 13 def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.delete("Some key" ) assert len(skip_list.head.forward ) == 0 def _A ( ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("Key2" ) is None def _A ( ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) == 14 assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("X" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("Key1" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) == 15 skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) is None def _A ( ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 142 ) skip_list.insert("Key2" , 15 ) skip_list.delete("X" ) def traverse_keys(__snake_case :List[str] ): yield node.key for forward_node in node.forward: yield from traverse_keys(__snake_case ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def _A ( ) -> List[str]: """simple docstring""" def is_sorted(__snake_case :Any ): return all(next_item >= item for item, next_item in zip(__snake_case , lst[1:] ) ) __SCREAMING_SNAKE_CASE = SkipList() for i in range(10 ): skip_list.insert(__snake_case , __snake_case ) assert is_sorted(list(__snake_case ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(__snake_case ) ) skip_list.insert(-12 , -12 ) skip_list.insert(77 , 77 ) assert is_sorted(list(__snake_case ) ) def _A ( ) -> List[str]: """simple docstring""" for _ in range(100 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def _A ( ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = SkipList() skip_list.insert(2 , "2" ) skip_list.insert(4 , "4" ) skip_list.insert(6 , "4" ) skip_list.insert(4 , "5" ) skip_list.insert(8 , "4" ) skip_list.insert(9 , "4" ) skip_list.delete(4 ) print(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod() main()
693
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ =(IPNDMScheduler,) SCREAMING_SNAKE_CASE__ =(("""num_inference_steps""", 50),) def __lowerCAmelCase ( self, **_a ) -> str: __SCREAMING_SNAKE_CASE = {"num_train_timesteps": 10_00} config.update(**_a ) return config def __lowerCAmelCase ( self, _a=0, **_a ) -> List[Any]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self ) -> str: pass def __lowerCAmelCase ( self, _a=0, **_a ) -> int: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] if time_step is None: __SCREAMING_SNAKE_CASE = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) __SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = new_scheduler.step(_a, _a, _a, **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self, **_a ) -> Tuple: __SCREAMING_SNAKE_CASE = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE = self.get_scheduler_config(**_a ) __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = 10 __SCREAMING_SNAKE_CASE = self.dummy_model() __SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample for i, t in enumerate(scheduler.timesteps ): __SCREAMING_SNAKE_CASE = model(_a, _a ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a ).prev_sample return sample def __lowerCAmelCase ( self ) -> Optional[int]: __SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) __SCREAMING_SNAKE_CASE = kwargs.pop("num_inference_steps", _a ) for scheduler_class in self.scheduler_classes: __SCREAMING_SNAKE_CASE = self.get_scheduler_config() __SCREAMING_SNAKE_CASE = scheduler_class(**_a ) __SCREAMING_SNAKE_CASE = self.dummy_sample __SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(_a, "set_timesteps" ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a, "set_timesteps" ): __SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __SCREAMING_SNAKE_CASE = dummy_past_residuals[:] __SCREAMING_SNAKE_CASE = scheduler.timesteps[5] __SCREAMING_SNAKE_CASE = scheduler.timesteps[6] __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample __SCREAMING_SNAKE_CASE = scheduler.step(_a, _a, _a, **_a ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def __lowerCAmelCase ( self ) -> str: for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Optional[Any]: for t, num_inference_steps in zip([1, 5, 10], [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=_a, time_step=_a ) def __lowerCAmelCase ( self ) -> Any: __SCREAMING_SNAKE_CASE = self.full_loop() __SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_a ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
693
1
import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging _snake_case : Optional[int] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE__ ="""linear""" SCREAMING_SNAKE_CASE__ ="""cosine""" SCREAMING_SNAKE_CASE__ ="""cosine_with_restarts""" SCREAMING_SNAKE_CASE__ ="""polynomial""" SCREAMING_SNAKE_CASE__ ="""constant""" SCREAMING_SNAKE_CASE__ ="""constant_with_warmup""" SCREAMING_SNAKE_CASE__ ="""piecewise_constant""" def _A ( __snake_case :Optimizer , __snake_case :int = -1 ) -> Optional[int]: """simple docstring""" return LambdaLR(__snake_case , lambda __snake_case : 1 , last_epoch=__snake_case ) def _A ( __snake_case :Optimizer , __snake_case :int , __snake_case :int = -1 ) -> Tuple: """simple docstring""" def lr_lambda(__snake_case :int ): if current_step < num_warmup_steps: return float(__snake_case ) / float(max(1.0 , __snake_case ) ) return 1.0 return LambdaLR(__snake_case , __snake_case , last_epoch=__snake_case ) def _A ( __snake_case :Optimizer , __snake_case :str , __snake_case :int = -1 ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = step_rules.split("," ) for rule_str in rule_list[:-1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rule_str.split(":" ) __SCREAMING_SNAKE_CASE = int(__snake_case ) __SCREAMING_SNAKE_CASE = float(__snake_case ) __SCREAMING_SNAKE_CASE = value __SCREAMING_SNAKE_CASE = float(rule_list[-1] ) def create_rules_function(__snake_case :Optional[int] , __snake_case :int ): def rule_func(__snake_case :int ) -> float: __SCREAMING_SNAKE_CASE = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(__snake_case ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func __SCREAMING_SNAKE_CASE = create_rules_function(__snake_case , __snake_case ) return LambdaLR(__snake_case , __snake_case , last_epoch=__snake_case ) def _A ( __snake_case :Union[str, Any] , __snake_case :Optional[int] , __snake_case :Optional[int] , __snake_case :Tuple=-1 ) -> str: """simple docstring""" def lr_lambda(__snake_case :int ): if current_step < num_warmup_steps: return float(__snake_case ) / float(max(1 , __snake_case ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(__snake_case , __snake_case , __snake_case ) def _A ( __snake_case :Optimizer , __snake_case :int , __snake_case :int , __snake_case :float = 0.5 , __snake_case :int = -1 ) -> int: """simple docstring""" def lr_lambda(__snake_case :Tuple ): if current_step < num_warmup_steps: return float(__snake_case ) / float(max(1 , __snake_case ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(__snake_case ) * 2.0 * progress )) ) return LambdaLR(__snake_case , __snake_case , __snake_case ) def _A ( __snake_case :Optimizer , __snake_case :int , __snake_case :int , __snake_case :int = 1 , __snake_case :int = -1 ) -> str: """simple docstring""" def lr_lambda(__snake_case :int ): if current_step < num_warmup_steps: return float(__snake_case ) / float(max(1 , __snake_case ) ) __SCREAMING_SNAKE_CASE = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(__snake_case ) * progress) % 1.0) )) ) return LambdaLR(__snake_case , __snake_case , __snake_case ) def _A ( __snake_case :List[Any] , __snake_case :str , __snake_case :Union[str, Any] , __snake_case :Optional[Any]=1e-7 , __snake_case :Dict=1.0 , __snake_case :List[Any]=-1 ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = optimizer.defaults["lr"] if not (lr_init > lr_end): raise ValueError(f'''lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})''' ) def lr_lambda(__snake_case :int ): if current_step < num_warmup_steps: return float(__snake_case ) / float(max(1 , __snake_case ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: __SCREAMING_SNAKE_CASE = lr_init - lr_end __SCREAMING_SNAKE_CASE = num_training_steps - num_warmup_steps __SCREAMING_SNAKE_CASE = 1 - (current_step - num_warmup_steps) / decay_steps __SCREAMING_SNAKE_CASE = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(__snake_case , __snake_case , __snake_case ) _snake_case : Any = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def _A ( __snake_case :Union[str, SchedulerType] , __snake_case :Optimizer , __snake_case :Optional[str] = None , __snake_case :Optional[int] = None , __snake_case :Optional[int] = None , __snake_case :int = 1 , __snake_case :float = 1.0 , __snake_case :int = -1 , ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = SchedulerType(__snake_case ) __SCREAMING_SNAKE_CASE = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(__snake_case , last_epoch=__snake_case ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(__snake_case , step_rules=__snake_case , last_epoch=__snake_case ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f'''{name} requires `num_warmup_steps`, please provide that argument.''' ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(__snake_case , num_warmup_steps=__snake_case , last_epoch=__snake_case ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f'''{name} requires `num_training_steps`, please provide that argument.''' ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( __snake_case , num_warmup_steps=__snake_case , num_training_steps=__snake_case , num_cycles=__snake_case , last_epoch=__snake_case , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( __snake_case , num_warmup_steps=__snake_case , num_training_steps=__snake_case , power=__snake_case , last_epoch=__snake_case , ) return schedule_func( __snake_case , num_warmup_steps=__snake_case , num_training_steps=__snake_case , last_epoch=__snake_case )
693
import random from .binary_exp_mod import bin_exp_mod def _A ( __snake_case :List[Any] , __snake_case :Union[str, Any]=1000 ) -> int: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __SCREAMING_SNAKE_CASE = n - 1 __SCREAMING_SNAKE_CASE = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __SCREAMING_SNAKE_CASE = 0 while count < prec: __SCREAMING_SNAKE_CASE = random.randint(2 , n - 1 ) __SCREAMING_SNAKE_CASE = bin_exp_mod(__snake_case , __snake_case , __snake_case ) if b != 1: __SCREAMING_SNAKE_CASE = True for _ in range(__snake_case ): if b == n - 1: __SCREAMING_SNAKE_CASE = False break __SCREAMING_SNAKE_CASE = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _snake_case : int = 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)))
693
1
import math def _A ( __snake_case :int ) -> list[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 2 __SCREAMING_SNAKE_CASE = int(math.sqrt(__snake_case ) ) # Size of every segment __SCREAMING_SNAKE_CASE = [True] * (end + 1) __SCREAMING_SNAKE_CASE = [] while start <= end: if temp[start] is True: in_prime.append(__snake_case ) for i in range(start * start , end + 1 , __snake_case ): __SCREAMING_SNAKE_CASE = False start += 1 prime += in_prime __SCREAMING_SNAKE_CASE = end + 1 __SCREAMING_SNAKE_CASE = min(2 * end , __snake_case ) while low <= n: __SCREAMING_SNAKE_CASE = [True] * (high - low + 1) for each in in_prime: __SCREAMING_SNAKE_CASE = math.floor(low / each ) * each if t < low: t += each for j in range(__snake_case , high + 1 , __snake_case ): __SCREAMING_SNAKE_CASE = False for j in range(len(__snake_case ) ): if temp[j] is True: prime.append(j + low ) __SCREAMING_SNAKE_CASE = high + 1 __SCREAMING_SNAKE_CASE = min(high + end , __snake_case ) return prime print(sieve(10**6))
693
import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _A ( __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int , __snake_case :int ) -> np.ndarray: """simple docstring""" if (ksize % 2) == 0: __SCREAMING_SNAKE_CASE = ksize + 1 __SCREAMING_SNAKE_CASE = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(__snake_case ): for x in range(__snake_case ): # distance from center __SCREAMING_SNAKE_CASE = x - ksize // 2 __SCREAMING_SNAKE_CASE = y - ksize // 2 # degree to radiant __SCREAMING_SNAKE_CASE = theta / 180 * np.pi __SCREAMING_SNAKE_CASE = np.cos(_theta ) __SCREAMING_SNAKE_CASE = np.sin(_theta ) # get kernel x __SCREAMING_SNAKE_CASE = cos_theta * px + sin_theta * py # get kernel y __SCREAMING_SNAKE_CASE = -sin_theta * px + cos_theta * py # fill kernel __SCREAMING_SNAKE_CASE = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _snake_case : Union[str, Any] = imread('../image_data/lena.jpg') # turn image in gray scale value _snake_case : List[str] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _snake_case : int = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 1_20, 1_50]: _snake_case : List[str] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _snake_case : Optional[Any] = out / out.max() * 2_55 _snake_case : Union[str, Any] = out.astype(np.uinta) imshow('Original', gray) imshow('Gabor filter with 20x20 mask and 6 directions', out) waitKey(0)
693
1
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def _A ( ) -> Any: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(__snake_case ): requests.request("GET" , "https://huggingface.co" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("GET" , "https://huggingface.co" , timeout=1.0 ) @pytest.mark.integration def _A ( ) -> List[str]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("GET" , "https://huggingface.co" ) def _A ( ) -> Any: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(__snake_case ): http_head("https://huggingface.co" )
693
def _A ( __snake_case :int ) -> int: """simple docstring""" assert isinstance(__snake_case , __snake_case ), f'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: __SCREAMING_SNAKE_CASE = f'''The input value of [n={number}] has to be > 0''' raise ValueError(__snake_case ) else: __SCREAMING_SNAKE_CASE = sylvester(number - 1 ) __SCREAMING_SNAKE_CASE = num - 1 __SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
693
1