code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _lowercase : def __init__( self: List[Any] , UpperCamelCase__: Optional[Any] , UpperCamelCase__: int=3 , UpperCamelCase__: Union[str, Any]=32 , UpperCamelCase__: Any=3 , UpperCamelCase__: Optional[int]=10 , UpperCamelCase__: List[str]=[10, 20, 30, 40] , UpperCamelCase__: Tuple=[1, 1, 2, 1] , UpperCamelCase__: Union[str, Any]=True , UpperCamelCase__: List[str]=True , UpperCamelCase__: str="relu" , UpperCamelCase__: Any=3 , UpperCamelCase__: Optional[int]=None , ): lowerCamelCase__ : List[str] = parent lowerCamelCase__ : str = batch_size lowerCamelCase__ : str = image_size lowerCamelCase__ : List[str] = num_channels lowerCamelCase__ : List[str] = embeddings_size lowerCamelCase__ : Dict = hidden_sizes lowerCamelCase__ : Optional[Any] = depths lowerCamelCase__ : Dict = is_training lowerCamelCase__ : str = use_labels lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : str = num_labels lowerCamelCase__ : List[str] = scope lowerCamelCase__ : Optional[int] = len(UpperCamelCase__ ) def lowerCamelCase_ ( self: str ): lowerCamelCase__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : int = None if self.use_labels: lowerCamelCase__ : Dict = ids_tensor([self.batch_size] , self.num_labels ) lowerCamelCase__ : str = self.get_config() return config, pixel_values, labels def lowerCamelCase_ ( self: Any ): return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase__: str , UpperCamelCase__: str , UpperCamelCase__: int ): lowerCamelCase__ : Union[str, Any] = TFResNetModel(config=UpperCamelCase__ ) lowerCamelCase__ : int = model(UpperCamelCase__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowerCamelCase_ ( self: Any , UpperCamelCase__: List[Any] , UpperCamelCase__: List[str] , UpperCamelCase__: int ): lowerCamelCase__ : Union[str, Any] = self.num_labels lowerCamelCase__ : Dict = TFResNetForImageClassification(UpperCamelCase__ ) lowerCamelCase__ : int = model(UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase_ ( self: Optional[int] ): lowerCamelCase__ : Union[str, Any] = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] = config_and_inputs lowerCamelCase__ : Union[str, Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _lowercase ( _lowercase , _lowercase , unittest.TestCase ): a = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a = ( {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification} if is_tf_available() else {} ) a = False a = False a = False a = False a = False def lowerCamelCase_ ( self: Any ): lowerCamelCase__ : Union[str, Any] = TFResNetModelTester(self ) lowerCamelCase__ : Any = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ ) def lowerCamelCase_ ( self: Optional[int] ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCamelCase_ ( self: int ): return @unittest.skip(reason="""ResNet does not use inputs_embeds""" ) def lowerCamelCase_ ( self: Optional[int] ): pass @unittest.skip(reason="""ResNet does not support input and output embeddings""" ) def lowerCamelCase_ ( self: Union[str, Any] ): pass def lowerCamelCase_ ( self: Optional[int] ): lowerCamelCase__ , lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Optional[Any] = model_class(UpperCamelCase__ ) lowerCamelCase__ : List[str] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : Union[str, Any] = [*signature.parameters.keys()] lowerCamelCase__ : Dict = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def lowerCamelCase_ ( self: int ): lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def lowerCamelCase_ ( self: Union[str, Any] ): def check_hidden_states_output(UpperCamelCase__: Optional[Any] , UpperCamelCase__: Optional[int] , UpperCamelCase__: int ): lowerCamelCase__ : List[str] = model_class(UpperCamelCase__ ) lowerCamelCase__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) lowerCamelCase__ : Tuple = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCamelCase__ : Optional[int] = self.model_tester.num_stages self.assertEqual(len(UpperCamelCase__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : Tuple = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCamelCase__ : Tuple = layer_type lowerCamelCase__ : Optional[Any] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCamelCase__ : Union[str, Any] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def lowerCamelCase_ ( self: List[Any] ): lowerCamelCase__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ ) @slow def lowerCamelCase_ ( self: List[Any] ): for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : List[str] = TFResNetModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE_ () -> Union[str, Any]: lowerCamelCase__ : List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class _lowercase ( unittest.TestCase ): @cached_property def lowerCamelCase_ ( self: int ): return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowerCamelCase_ ( self: Optional[int] ): lowerCamelCase__ : List[str] = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCamelCase__ : Union[str, Any] = self.default_image_processor lowerCamelCase__ : Optional[Any] = prepare_img() lowerCamelCase__ : Optional[Any] = image_processor(images=UpperCamelCase__ , return_tensors="""tf""" ) # forward pass lowerCamelCase__ : Optional[int] = model(**UpperCamelCase__ ) # verify the logits lowerCamelCase__ : List[Any] = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) lowerCamelCase__ : Tuple = tf.constant([-11.1_069, -9.7_877, -8.3_777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , UpperCamelCase__ , atol=1e-4 ) )
41
'''simple docstring''' import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = 'docs/source/en/_toctree.yml' def lowercase__ ( __UpperCamelCase )-> Optional[Any]: UpperCamelCase = defaultdict(__UpperCamelCase ) UpperCamelCase = [] UpperCamelCase = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__UpperCamelCase ) UpperCamelCase = new_doc_list UpperCamelCase = [key for key, value in counts.items() if value > 1] UpperCamelCase = [] for duplicate_key in duplicates: UpperCamelCase = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__UpperCamelCase ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) UpperCamelCase = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__UpperCamelCase ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__UpperCamelCase ) # Sort return overview_doc def lowercase__ ( __UpperCamelCase=False )-> List[str]: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 UpperCamelCase = api_doc[scheduler_idx]["""sections"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) UpperCamelCase = False if new_scheduler_doc != scheduler_doc: UpperCamelCase = True if overwrite: UpperCamelCase = new_scheduler_doc if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def lowercase__ ( __UpperCamelCase=False )-> Tuple: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 UpperCamelCase = False UpperCamelCase = api_doc[pipeline_idx]["""sections"""] UpperCamelCase = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: UpperCamelCase = pipeline_doc["""section"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if overwrite: UpperCamelCase = new_sub_pipeline_doc new_pipeline_docs.append(__UpperCamelCase ) # sort overall pipeline doc UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if new_pipeline_docs != pipeline_docs: UpperCamelCase = True if overwrite: UpperCamelCase = new_pipeline_docs if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) 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__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
321
0
'''simple docstring''' import os def SCREAMING_SNAKE_CASE__ ( __A = "input.txt" ) -> int: with open(os.path.join(os.path.dirname(__A ) , __A ) ) as input_file: _snake_case = [ [int(__A ) for element in line.split(',' )] for line in input_file.readlines() ] _snake_case = len(__A ) _snake_case = len(matrix[0] ) _snake_case = [[-1 for _ in range(__A )] for _ in range(__A )] for i in range(__A ): _snake_case = matrix[i][0] for j in range(1 , __A ): for i in range(__A ): _snake_case = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , __A ): _snake_case = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 , -1 , -1 ): _snake_case = min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(F'''{solution() = }''')
42
'''simple docstring''' import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) with open(F"{class_data_dir}/caption.txt" , """w""" ) as fa, open(F"{class_data_dir}/urls.txt" , """w""" ) as fa, open( F"{class_data_dir}/images.txt" , """w""" ) as fa: while total < num_class_images: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = Image.open(BytesIO(img.content ) ) with open(F"{class_data_dir}/images/{total}.jpg" , """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(F"{class_data_dir}/images/{total}.jpg" + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
0
import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder __lowercase = logging.get_logger(__name__) # pylint: disable=invalid-name __lowercase = 256 class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : Tuple = ["""melgan"""] def __init__( self , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> None: super().__init__() # From MELGAN __UpperCamelCase :int = math.log(1E-5) # Matches MelGAN training. __UpperCamelCase :int = 4.0 # Largest value for most examples __UpperCamelCase :str = 128 self.register_modules( notes_encoder=__lowercase , continuous_encoder=__lowercase , decoder=__lowercase , scheduler=__lowercase , melgan=__lowercase , ) def UpperCamelCase__ ( self , __lowercase , __lowercase=(-1.0, 1.0) , __lowercase=False) -> Dict: __UpperCamelCase , __UpperCamelCase :str = output_range if clip: __UpperCamelCase :Union[str, Any] = torch.clip(__lowercase , self.min_value , self.max_value) # Scale to [0, 1]. __UpperCamelCase :Union[str, Any] = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def UpperCamelCase__ ( self , __lowercase , __lowercase=(-1.0, 1.0) , __lowercase=False) -> Optional[int]: __UpperCamelCase , __UpperCamelCase :int = input_range __UpperCamelCase :Optional[int] = torch.clip(__lowercase , __lowercase , __lowercase) if clip else outputs # Scale to [0, 1]. __UpperCamelCase :List[str] = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def UpperCamelCase__ ( self , __lowercase , __lowercase , __lowercase) -> List[Any]: __UpperCamelCase :List[str] = input_tokens > 0 __UpperCamelCase , __UpperCamelCase :Union[str, Any] = self.notes_encoder( encoder_input_tokens=__lowercase , encoder_inputs_mask=__lowercase) __UpperCamelCase , __UpperCamelCase :Union[str, Any] = self.continuous_encoder( encoder_inputs=__lowercase , encoder_inputs_mask=__lowercase) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def UpperCamelCase__ ( self , __lowercase , __lowercase , __lowercase) -> str: __UpperCamelCase :Optional[int] = noise_time if not torch.is_tensor(__lowercase): __UpperCamelCase :str = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device) elif torch.is_tensor(__lowercase) and len(timesteps.shape) == 0: __UpperCamelCase :Dict = timesteps[None].to(input_tokens.device) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __UpperCamelCase :List[str] = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device) __UpperCamelCase :Tuple = self.decoder( encodings_and_masks=__lowercase , decoder_input_tokens=__lowercase , decoder_noise_time=__lowercase) return logits @torch.no_grad() def __call__( self , __lowercase , __lowercase = None , __lowercase = 100 , __lowercase = True , __lowercase = "numpy" , __lowercase = None , __lowercase = 1 , ) -> Union[AudioPipelineOutput, Tuple]: if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__lowercase , __lowercase) or callback_steps <= 0) ): raise ValueError( f"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" f""" {type(__lowercase)}.""") __UpperCamelCase :Union[str, Any] = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa) __UpperCamelCase :Union[str, Any] = np.zeros([1, 0, self.n_dims] , np.floataa) __UpperCamelCase :Union[str, Any] = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=__lowercase , device=self.device) for i, encoder_input_tokens in enumerate(__lowercase): if i == 0: __UpperCamelCase :int = torch.from_numpy(pred_mel[:1].copy()).to( device=self.device , dtype=self.decoder.dtype) # The first chunk has no previous context. __UpperCamelCase :int = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=__lowercase , device=self.device) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. __UpperCamelCase :Tuple = ones __UpperCamelCase :Optional[Any] = self.scale_features( __lowercase , output_range=[-1.0, 1.0] , clip=__lowercase) __UpperCamelCase :int = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens]).to(device=self.device) , continuous_inputs=__lowercase , continuous_mask=__lowercase , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop __UpperCamelCase :int = randn_tensor( shape=encoder_continuous_inputs.shape , generator=__lowercase , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(__lowercase) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps)): __UpperCamelCase :Optional[int] = self.decode( encodings_and_masks=__lowercase , input_tokens=__lowercase , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 __UpperCamelCase :int = self.scheduler.step(__lowercase , __lowercase , __lowercase , generator=__lowercase).prev_sample __UpperCamelCase :Tuple = self.scale_to_features(__lowercase , input_range=[-1.0, 1.0]) __UpperCamelCase :List[Any] = mel[:1] __UpperCamelCase :Optional[Any] = mel.cpu().float().numpy() __UpperCamelCase :Any = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__lowercase , __lowercase) logger.info('''Generated segment''' , __lowercase) if output_type == "numpy" and not is_onnx_available(): raise ValueError( '''Cannot return output in \'np\' format if ONNX is not available. Make sure to have ONNX installed or set \'output_type\' to \'mel\'.''') elif output_type == "numpy" and self.melgan is None: raise ValueError( '''Cannot return output in \'np\' format if melgan component is not defined. Make sure to define `self.melgan` or set \'output_type\' to \'mel\'.''') if output_type == "numpy": __UpperCamelCase :Optional[Any] = self.melgan(input_features=full_pred_mel.astype(np.floataa)) else: __UpperCamelCase :List[str] = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=__lowercase)
43
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = 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 a_ : lowercase = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The column name of the images in the files."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the training data."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the validation data."""} ) lowercase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class a_ : lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) lowercase = field( default=lowerCamelCase , 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""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) lowercase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class a_ ( lowerCamelCase ): lowercase = field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def lowercase__ ( __UpperCamelCase )-> int: UpperCamelCase = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def lowercase__ ( )-> List[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase ,UpperCamelCase ,UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase ,UpperCamelCase ,UpperCamelCase = 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""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCamelCase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase = split["""train"""] UpperCamelCase = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(F"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(F"New config: {config}" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase = ViTMAEForPreTraining(__UpperCamelCase ) if training_args.do_train: UpperCamelCase = ds["""train"""].column_names else: UpperCamelCase = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = """image""" elif "img" in column_names: UpperCamelCase = """img""" else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size["""shortest_edge"""] else: UpperCamelCase = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase = Compose( [ Lambda(lambda __UpperCamelCase : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(__UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__UpperCamelCase ): UpperCamelCase = [transforms(__UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__UpperCamelCase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def lowercase__ ( __UpperCamelCase )-> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
321
0
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> Optional[Any]: stooge(_lowerCamelCase ,0 ,len(_lowerCamelCase ) - 1 ) return arr def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ,_lowerCamelCase : List[str] ,_lowerCamelCase : Any ) -> str: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: _lowerCAmelCase , _lowerCAmelCase : str = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: _lowerCAmelCase : int = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_lowerCamelCase ,_lowerCamelCase ,(h - t) ) # Recursively sort last 2/3 elements stooge(_lowerCamelCase ,i + t ,(_lowerCamelCase) ) # Recursively sort first 2/3 elements stooge(_lowerCamelCase ,_lowerCamelCase ,(h - t) ) if __name__ == "__main__": _a : List[str] = input('Enter numbers separated by a comma:\n').strip() _a : List[Any] = [int(item) for item in user_input.split(',')] print(stooge_sort(unsorted))
44
'''simple docstring''' import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # pylint: disable=invalid-name SCREAMING_SNAKE_CASE__ = 2_5_6 class a_ ( lowerCamelCase ): lowercase = ["""melgan"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> None: """simple docstring""" super().__init__() # From MELGAN UpperCamelCase = math.log(1e-5 ) # Matches MelGAN training. UpperCamelCase = 4.0 # Largest value for most examples UpperCamelCase = 128 self.register_modules( notes_encoder=_SCREAMING_SNAKE_CASE , continuous_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , melgan=_SCREAMING_SNAKE_CASE , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" UpperCamelCase ,UpperCamelCase = output_range if clip: UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , self.min_value , self.max_value ) # Scale to [0, 1]. UpperCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase ,UpperCamelCase = input_range UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if clip else outputs # Scale to [0, 1]. UpperCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" UpperCamelCase = input_tokens > 0 UpperCamelCase ,UpperCamelCase = self.notes_encoder( encoder_input_tokens=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) UpperCamelCase ,UpperCamelCase = self.continuous_encoder( encoder_inputs=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" UpperCamelCase = noise_time if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML UpperCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) UpperCamelCase = self.decoder( encodings_and_masks=_SCREAMING_SNAKE_CASE , decoder_input_tokens=_SCREAMING_SNAKE_CASE , decoder_noise_time=_SCREAMING_SNAKE_CASE ) return logits @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "numpy" , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , ) -> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_SCREAMING_SNAKE_CASE )}." ) UpperCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) UpperCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) UpperCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) for i, encoder_input_tokens in enumerate(_SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. UpperCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. UpperCamelCase = ones UpperCamelCase = self.scale_features( _SCREAMING_SNAKE_CASE , output_range=[-1.0, 1.0] , clip=_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_SCREAMING_SNAKE_CASE , continuous_mask=_SCREAMING_SNAKE_CASE , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop UpperCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_SCREAMING_SNAKE_CASE , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_SCREAMING_SNAKE_CASE ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCamelCase = self.decode( encodings_and_masks=_SCREAMING_SNAKE_CASE , input_tokens=_SCREAMING_SNAKE_CASE , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 UpperCamelCase = self.scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = self.scale_to_features(_SCREAMING_SNAKE_CASE , input_range=[-1.0, 1.0] ) UpperCamelCase = mel[:1] UpperCamelCase = mel.cpu().float().numpy() UpperCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) logger.info("""Generated segment""" , _SCREAMING_SNAKE_CASE ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": UpperCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: UpperCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" import argparse import logging import os import re import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, DataCollatorForLanguageModeling, PushToHubCallback, TFAutoModelForMaskedLM, create_optimizer, ) lowercase_ = logging.getLogger(__name__) lowercase_ = tf.data.AUTOTUNE def lowercase ( ) -> Optional[int]: __a = argparse.ArgumentParser(description='''Train a masked language model on TPU.''' ) parser.add_argument( '''--pretrained_model_config''' , type=lowerCAmelCase__ , default='''roberta-base''' , help='''The model config to use. Note that we don\'t copy the model\'s weights, only the config!''' , ) parser.add_argument( '''--tokenizer''' , type=lowerCAmelCase__ , default='''unigram-tokenizer-wikitext''' , help='''The name of the tokenizer to load. We use the pretrained tokenizer to initialize the model\'s vocab size.''' , ) parser.add_argument( '''--per_replica_batch_size''' , type=lowerCAmelCase__ , default=8 , help='''Batch size per TPU core.''' , ) parser.add_argument( '''--no_tpu''' , action='''store_true''' , help='''If set, run on CPU and don\'t try to initialize a TPU. Useful for debugging on non-TPU instances.''' , ) parser.add_argument( '''--tpu_name''' , type=lowerCAmelCase__ , help='''Name of TPU resource to initialize. Should be blank on Colab, and \'local\' on TPU VMs.''' , default='''local''' , ) parser.add_argument( '''--tpu_zone''' , type=lowerCAmelCase__ , help='''Google cloud zone that TPU resource is located in. Only used for non-Colab TPU nodes.''' , ) parser.add_argument( '''--gcp_project''' , type=lowerCAmelCase__ , help='''Google cloud project name. Only used for non-Colab TPU nodes.''' ) parser.add_argument( '''--bfloat16''' , action='''store_true''' , help='''Use mixed-precision bfloat16 for training. This is the recommended lower-precision format for TPU.''' , ) parser.add_argument( '''--train_dataset''' , type=lowerCAmelCase__ , help='''Path to training dataset to load. If the path begins with `gs://`''' ''' then the dataset will be loaded from a Google Cloud Storage bucket.''' , ) parser.add_argument( '''--shuffle_buffer_size''' , type=lowerCAmelCase__ , default=2**18 , help='''Size of the shuffle buffer (in samples)''' , ) parser.add_argument( '''--eval_dataset''' , type=lowerCAmelCase__ , help='''Path to evaluation dataset to load. If the path begins with `gs://`''' ''' then the dataset will be loaded from a Google Cloud Storage bucket.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=1 , help='''Number of epochs to train for.''' , ) parser.add_argument( '''--learning_rate''' , type=lowerCAmelCase__ , default=1e-4 , help='''Learning rate to use for training.''' , ) parser.add_argument( '''--weight_decay_rate''' , type=lowerCAmelCase__ , default=1e-3 , help='''Weight decay rate to use for training.''' , ) parser.add_argument( '''--max_length''' , type=lowerCAmelCase__ , default=512 , help='''Maximum length of tokenized sequences. Should match the setting used in prepare_tfrecord_shards.py''' , ) parser.add_argument( '''--mlm_probability''' , type=lowerCAmelCase__ , default=0.15 , help='''Fraction of tokens to mask during training.''' , ) parser.add_argument('''--output_dir''' , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''Path to save model checkpoints to.''' ) parser.add_argument('''--hub_model_id''' , type=lowerCAmelCase__ , help='''Model ID to upload to on the Hugging Face Hub.''' ) __a = parser.parse_args() return args def lowercase ( lowerCAmelCase__ : Optional[int] ) -> int: try: if args.tpu_name: __a = tf.distribute.cluster_resolver.TPUClusterResolver( args.tpu_name , zone=args.tpu_zone , project=args.gcp_project ) else: __a = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: raise RuntimeError( '''Couldn\'t connect to TPU! Most likely you need to specify --tpu_name, --tpu_zone, or ''' '''--gcp_project. When running on a TPU VM, use --tpu_name local.''' ) tf.config.experimental_connect_to_cluster(lowerCAmelCase__ ) tf.tpu.experimental.initialize_tpu_system(lowerCAmelCase__ ) return tpu def lowercase ( lowerCAmelCase__ : Dict ) -> Dict: __a = 0 for file in file_list: __a = file.split('''/''' )[-1] __a = re.search(r'''-\d+-(\d+)\.tfrecord''' , lowerCAmelCase__ ).group(1 ) __a = int(lowerCAmelCase__ ) num_samples += sample_count return num_samples def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[int]=None ) -> Tuple: __a = count_samples(lowerCAmelCase__ ) __a = tf.data.Dataset.from_tensor_slices(lowerCAmelCase__ ) if shuffle: __a = dataset.shuffle(len(lowerCAmelCase__ ) ) __a = tf.data.TFRecordDataset(lowerCAmelCase__ , num_parallel_reads=lowerCAmelCase__ ) # TF can't infer the total sample count because it doesn't read all the records yet, so we assert it here __a = dataset.apply(tf.data.experimental.assert_cardinality(lowerCAmelCase__ ) ) __a = dataset.map(lowerCAmelCase__ , num_parallel_calls=lowerCAmelCase__ ) if shuffle: assert shuffle_buffer_size is not None __a = dataset.shuffle(args.shuffle_buffer_size ) __a = dataset.batch(lowerCAmelCase__ , drop_remainder=lowerCAmelCase__ ) __a = dataset.map(lowerCAmelCase__ , num_parallel_calls=lowerCAmelCase__ ) __a = dataset.prefetch(lowerCAmelCase__ ) return dataset def lowercase ( lowerCAmelCase__ : str ) -> List[str]: if not args.no_tpu: __a = initialize_tpu(lowerCAmelCase__ ) __a = tf.distribute.TPUStrategy(lowerCAmelCase__ ) else: __a = tf.distribute.OneDeviceStrategy(device='''/gpu:0''' ) if args.bfloataa: tf.keras.mixed_precision.set_global_policy('''mixed_bfloat16''' ) __a = AutoTokenizer.from_pretrained(args.tokenizer ) __a = AutoConfig.from_pretrained(args.pretrained_model_config ) __a = tokenizer.vocab_size __a = tf.io.gfile.glob(os.path.join(args.train_dataset , '''*.tfrecord''' ) ) if not training_records: raise ValueError(f'''No .tfrecord files found in {args.train_dataset}.''' ) __a = tf.io.gfile.glob(os.path.join(args.eval_dataset , '''*.tfrecord''' ) ) if not eval_records: raise ValueError(f'''No .tfrecord files found in {args.eval_dataset}.''' ) __a = count_samples(lowerCAmelCase__ ) __a = num_train_samples // (args.per_replica_batch_size * strategy.num_replicas_in_sync) __a = steps_per_epoch * args.num_epochs with strategy.scope(): __a = TFAutoModelForMaskedLM.from_config(lowerCAmelCase__ ) model(model.dummy_inputs ) # Pass some dummy inputs through the model to ensure all the weights are built __a , __a = create_optimizer( num_train_steps=lowerCAmelCase__ , num_warmup_steps=total_train_steps // 20 , init_lr=args.learning_rate , weight_decay_rate=args.weight_decay_rate , ) # Transformers models compute the right loss for their task by default when labels are passed, and will # use this for training unless you specify your own loss function in compile(). model.compile(optimizer=lowerCAmelCase__ , metrics=['''accuracy'''] ) def decode_fn(lowerCAmelCase__ : Tuple ): __a = { '''input_ids''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), '''attention_mask''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), } return tf.io.parse_single_example(lowerCAmelCase__ , lowerCAmelCase__ ) # Many of the data collators in Transformers are TF-compilable when return_tensors == "tf", so we can # use their methods in our data pipeline. __a = DataCollatorForLanguageModeling( tokenizer=lowerCAmelCase__ , mlm_probability=args.mlm_probability , mlm=lowerCAmelCase__ , return_tensors='''tf''' ) def mask_with_collator(lowerCAmelCase__ : List[Any] ): # TF really needs an isin() function __a = ( ~tf.cast(batch['''attention_mask'''] , tf.bool ) | (batch['''input_ids'''] == tokenizer.cls_token_id) | (batch['''input_ids'''] == tokenizer.sep_token_id) ) __a , __a = data_collator.tf_mask_tokens( batch['''input_ids'''] , vocab_size=len(lowerCAmelCase__ ) , mask_token_id=tokenizer.mask_token_id , special_tokens_mask=lowerCAmelCase__ , ) return batch __a = args.per_replica_batch_size * strategy.num_replicas_in_sync __a = prepare_dataset( lowerCAmelCase__ , decode_fn=lowerCAmelCase__ , mask_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ , shuffle=lowerCAmelCase__ , shuffle_buffer_size=args.shuffle_buffer_size , ) __a = prepare_dataset( lowerCAmelCase__ , decode_fn=lowerCAmelCase__ , mask_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ , shuffle=lowerCAmelCase__ , ) __a = [] if args.hub_model_id: callbacks.append( PushToHubCallback(output_dir=args.output_dir , hub_model_id=args.hub_model_id , tokenizer=lowerCAmelCase__ ) ) model.fit( lowerCAmelCase__ , validation_data=lowerCAmelCase__ , epochs=args.num_epochs , callbacks=lowerCAmelCase__ , ) model.save_pretrained(args.output_dir ) if __name__ == "__main__": lowercase_ = parse_args() main(args)
45
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 4000000 )-> int: UpperCamelCase = [] UpperCamelCase ,UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCamelCase ) UpperCamelCase ,UpperCamelCase = b, a + b return sum(__UpperCamelCase ) if __name__ == "__main__": print(f'{solution() = }')
321
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin SCREAMING_SNAKE_CASE__ = False @skip_mps class lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): _SCREAMING_SNAKE_CASE = StableDiffusionAttendAndExcitePipeline _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_BATCH_PARAMS.union({'token_indices'} ) _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def _snake_case ( cls ) -> str: super().setUpClass() torch.use_deterministic_algorithms(lowercase ) @classmethod def _snake_case ( cls ) -> List[Any]: super().tearDownClass() torch.use_deterministic_algorithms(lowercase ) def _snake_case ( self ) -> str: torch.manual_seed(0 ) lowerCAmelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowercase , ) lowerCAmelCase = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=lowercase , set_alpha_to_one=lowercase , ) torch.manual_seed(0 ) lowerCAmelCase = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) lowerCAmelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act="""gelu""" , projection_dim=512 , ) lowerCAmelCase = CLIPTextModel(lowercase ) lowerCAmelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) lowerCAmelCase = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def _snake_case ( self , lowercase , lowercase=0 ) -> Optional[Any]: if str(lowercase ).startswith("""mps""" ): lowerCAmelCase = torch.manual_seed(lowercase ) else: lowerCAmelCase = torch.Generator(device=lowercase ).manual_seed(lowercase ) lowerCAmelCase = lowerCAmelCase = { """prompt""": """a cat and a frog""", """token_indices""": [2, 5], """generator""": generator, """num_inference_steps""": 1, """guidance_scale""": 6.0, """output_type""": """numpy""", """max_iter_to_alter""": 2, """thresholds""": {0: 0.7}, } return inputs def _snake_case ( self ) -> Optional[int]: lowerCAmelCase = """cpu""" lowerCAmelCase = self.get_dummy_components() lowerCAmelCase = self.pipeline_class(**lowercase ) pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) lowerCAmelCase = self.get_dummy_inputs(lowercase ) lowerCAmelCase = pipe(**lowercase ).images lowerCAmelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 64, 64, 3) ) lowerCAmelCase = np.array( [0.63_905_364, 0.62_897_307, 0.48_599_017, 0.5_133_624, 0.5_550_048, 0.45_769_516, 0.50_326_973, 0.5_023_139, 0.45_384_496] ) lowerCAmelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowercase , 1e-3 ) def _snake_case ( self ) -> Union[str, Any]: super().test_cpu_offload_forward_pass(expected_max_diff=5e-4 ) def _snake_case ( self ) -> int: # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def _snake_case ( self ) -> Optional[int]: self._test_inference_batch_single_identical(batch_size=2 , expected_max_diff=7e-4 ) def _snake_case ( self ) -> Optional[int]: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def _snake_case ( self ) -> Optional[int]: super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4 ) def _snake_case ( self ) -> int: super().test_save_load_local(expected_max_difference=5e-4 ) def _snake_case ( self ) -> int: super().test_save_load_optional_components(expected_max_difference=4e-4 ) @require_torch_gpu @slow class lowercase ( unittest.TestCase ): @classmethod def _snake_case ( cls ) -> Dict: super().setUpClass() torch.use_deterministic_algorithms(lowercase ) @classmethod def _snake_case ( cls ) -> Tuple: super().tearDownClass() torch.use_deterministic_algorithms(lowercase ) def _snake_case ( self ) -> List[str]: super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self ) -> List[Any]: lowerCAmelCase = torch.manual_seed(51 ) lowerCAmelCase = StableDiffusionAttendAndExcitePipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""" , safety_checker=lowercase , torch_dtype=torch.floataa ) pipe.to("""cuda""" ) lowerCAmelCase = """a painting of an elephant with glasses""" lowerCAmelCase = [5, 7] lowerCAmelCase = pipe( prompt=lowercase , token_indices=lowercase , guidance_scale=7.5 , generator=lowercase , num_inference_steps=5 , max_iter_to_alter=5 , output_type="""numpy""" , ).images[0] lowerCAmelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy""" ) assert np.abs((expected_image - image).max() ) < 5e-1
46
'''simple docstring''' def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: # Base Case if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] , __UpperCamelCase , __UpperCamelCase ): # Color current vertex UpperCamelCase = i # Validate coloring if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , index + 1 ): return True # Backtrack UpperCamelCase = -1 return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> list[int]: UpperCamelCase = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , 0 ): return colored_vertices return []
321
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase : List[Any] = logging.get_logger(__name__) def _lowerCAmelCase ( _UpperCamelCase : str ) -> YolosConfig: """simple docstring""" _SCREAMING_SNAKE_CASE =YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: _SCREAMING_SNAKE_CASE =1_92 _SCREAMING_SNAKE_CASE =7_68 _SCREAMING_SNAKE_CASE =12 _SCREAMING_SNAKE_CASE =3 _SCREAMING_SNAKE_CASE =[8_00, 13_33] _SCREAMING_SNAKE_CASE =False elif yolos_name == "yolos_s_dWr": _SCREAMING_SNAKE_CASE =3_30 _SCREAMING_SNAKE_CASE =14 _SCREAMING_SNAKE_CASE =6 _SCREAMING_SNAKE_CASE =13_20 elif "yolos_s" in yolos_name: _SCREAMING_SNAKE_CASE =3_84 _SCREAMING_SNAKE_CASE =15_36 _SCREAMING_SNAKE_CASE =12 _SCREAMING_SNAKE_CASE =6 elif "yolos_b" in yolos_name: _SCREAMING_SNAKE_CASE =[8_00, 13_44] _SCREAMING_SNAKE_CASE =91 _SCREAMING_SNAKE_CASE ='huggingface/label-files' _SCREAMING_SNAKE_CASE ='coco-detection-id2label.json' _SCREAMING_SNAKE_CASE =json.load(open(hf_hub_download(_UpperCamelCase , _UpperCamelCase , repo_type='dataset' ) , 'r' ) ) _SCREAMING_SNAKE_CASE ={int(_UpperCamelCase ): 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 _lowerCAmelCase ( _UpperCamelCase : dict , _UpperCamelCase : YolosConfig , _UpperCamelCase : bool = False ) -> Union[str, Any]: """simple docstring""" for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _SCREAMING_SNAKE_CASE =state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) _SCREAMING_SNAKE_CASE =state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict _SCREAMING_SNAKE_CASE =in_proj_weight[: config.hidden_size, :] _SCREAMING_SNAKE_CASE =in_proj_bias[: config.hidden_size] _SCREAMING_SNAKE_CASE =in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _SCREAMING_SNAKE_CASE =in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _SCREAMING_SNAKE_CASE =in_proj_weight[-config.hidden_size :, :] _SCREAMING_SNAKE_CASE =in_proj_bias[-config.hidden_size :] def _lowerCAmelCase ( _UpperCamelCase : str ) -> str: """simple docstring""" if "backbone" in name: _SCREAMING_SNAKE_CASE =name.replace('backbone' , 'vit' ) if "cls_token" in name: _SCREAMING_SNAKE_CASE =name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: _SCREAMING_SNAKE_CASE =name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: _SCREAMING_SNAKE_CASE =name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: _SCREAMING_SNAKE_CASE =name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: _SCREAMING_SNAKE_CASE =name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: _SCREAMING_SNAKE_CASE =name.replace('blocks' , '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 "class_embed" in name: _SCREAMING_SNAKE_CASE =name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: _SCREAMING_SNAKE_CASE =name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: _SCREAMING_SNAKE_CASE =name.replace('vit.norm' , 'vit.layernorm' ) return name def _lowerCAmelCase ( _UpperCamelCase : dict , _UpperCamelCase : YolosForObjectDetection ) -> dict: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE =orig_state_dict.pop(_UpperCamelCase ) if "qkv" in key: _SCREAMING_SNAKE_CASE =key.split('.' ) _SCREAMING_SNAKE_CASE =int(key_split[2] ) _SCREAMING_SNAKE_CASE =model.vit.encoder.layer[layer_num].attention.attention.all_head_size 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[:dim] _SCREAMING_SNAKE_CASE =val[dim : dim * 2] _SCREAMING_SNAKE_CASE =val[-dim:] else: _SCREAMING_SNAKE_CASE =val return orig_state_dict def _lowerCAmelCase ( ) -> torch.Tensor: """simple docstring""" _SCREAMING_SNAKE_CASE ='http://images.cocodataset.org/val2017/000000039769.jpg' _SCREAMING_SNAKE_CASE =Image.open(requests.get(_UpperCamelCase , stream=_UpperCamelCase ).raw ) return im @torch.no_grad() def _lowerCAmelCase ( _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : bool = False ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =get_yolos_config(_UpperCamelCase ) # load original state_dict _SCREAMING_SNAKE_CASE =torch.load(_UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model _SCREAMING_SNAKE_CASE =YolosForObjectDetection(_UpperCamelCase ) model.eval() _SCREAMING_SNAKE_CASE =convert_state_dict(_UpperCamelCase , _UpperCamelCase ) model.load_state_dict(_UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor _SCREAMING_SNAKE_CASE =8_00 if yolos_name != 'yolos_ti' else 5_12 _SCREAMING_SNAKE_CASE =YolosImageProcessor(format='coco_detection' , size=_UpperCamelCase ) _SCREAMING_SNAKE_CASE =image_processor(images=prepare_img() , return_tensors='pt' ) _SCREAMING_SNAKE_CASE =model(**_UpperCamelCase ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =outputs.logits, outputs.pred_boxes _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =None, None if yolos_name == "yolos_ti": _SCREAMING_SNAKE_CASE =torch.tensor( [[-39.50_22, -11.98_20, -17.68_88], [-29.95_74, -9.97_69, -17.76_91], [-42.32_81, -20.72_00, -30.62_94]] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[0.40_21, 0.08_36, 0.79_79], [0.01_84, 0.26_09, 0.03_64], [0.17_81, 0.20_04, 0.20_95]] ) elif yolos_name == "yolos_s_200_pre": _SCREAMING_SNAKE_CASE =torch.tensor( [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]] ) elif yolos_name == "yolos_s_300_pre": _SCREAMING_SNAKE_CASE =torch.tensor( [[-36.22_20, -14.43_85, -23.54_57], [-35.69_70, -14.75_83, -21.39_35], [-31.59_39, -13.60_42, -16.80_49]] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[0.76_14, 0.23_16, 0.47_28], [0.71_68, 0.44_95, 0.38_55], [0.49_96, 0.14_66, 0.99_96]] ) elif yolos_name == "yolos_s_dWr": _SCREAMING_SNAKE_CASE =torch.tensor( [[-42.86_68, -24.10_49, -41.16_90], [-34.74_56, -14.12_74, -24.91_94], [-33.78_98, -12.19_46, -25.64_95]] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[0.55_87, 0.27_73, 0.06_05], [0.50_04, 0.30_14, 0.99_94], [0.49_99, 0.15_48, 0.99_94]] ) elif yolos_name == "yolos_base": _SCREAMING_SNAKE_CASE =torch.tensor( [[-40.60_64, -24.30_84, -32.64_47], [-55.19_90, -30.77_19, -35.58_77], [-51.43_11, -33.35_07, -35.64_62]] ) _SCREAMING_SNAKE_CASE =torch.tensor( [[0.55_55, 0.27_94, 0.06_55], [0.90_49, 0.26_64, 0.18_94], [0.91_83, 0.19_84, 0.16_35]] ) else: raise ValueError(f"Unknown yolos_name: {yolos_name}" ) assert torch.allclose(logits[0, :3, :3] , _UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , _UpperCamelCase , atol=1E-4 ) Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) print(f"Saving model {yolos_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_UpperCamelCase ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(_UpperCamelCase ) if push_to_hub: _SCREAMING_SNAKE_CASE ={ 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) _SCREAMING_SNAKE_CASE =model_mapping[yolos_name] image_processor.push_to_hub(_UpperCamelCase , organization='hustvl' ) model.push_to_hub(_UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--yolos_name", default="yolos_s_200_pre", type=str, help=( "Name of the YOLOS model you'd like to convert. Should be one of 'yolos_ti', 'yolos_s_200_pre'," " 'yolos_s_300_pre', 'yolos_s_dWr', 'yolos_base'." ), ) parser.add_argument( "--checkpoint_path", default=None, type=str, help="Path to the original state dict (.pth file)." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) lowerCamelCase : int = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
47
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 2000000 )-> int: UpperCamelCase = [0 for i in range(n + 1 )] UpperCamelCase = 1 UpperCamelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __UpperCamelCase ): UpperCamelCase = 1 UpperCamelCase = 0 for i in range(__UpperCamelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'{solution() = }')
321
0
class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ ) -> None: lowerCamelCase : int = set_counts lowerCamelCase : Any = max(UpperCamelCase__ ) lowerCamelCase : str = len(UpperCamelCase__ ) lowerCamelCase : List[str] = [1] * num_sets lowerCamelCase : Any = list(range(UpperCamelCase__ ) ) def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__ ) -> bool: lowerCamelCase : Optional[Any] = self.get_parent(UpperCamelCase__ ) lowerCamelCase : Union[str, Any] = self.get_parent(UpperCamelCase__ ) 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] lowerCamelCase : Optional[int] = 0 lowerCamelCase : str = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 lowerCamelCase : Dict = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] lowerCamelCase : int = 0 lowerCamelCase : Dict = src_parent lowerCamelCase : Tuple = self.set_counts[src_parent] lowerCamelCase : int = max(self.max_set , UpperCamelCase__ ) return True def _lowercase ( self , UpperCamelCase__ ) -> int: if self.parents[disj_set] == disj_set: return disj_set lowerCamelCase : Optional[Any] = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
48
'''simple docstring''' from timeit import timeit def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: number &= number - 1 result += 1 return result def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowercase__ ( )-> None: def do_benchmark(__UpperCamelCase ) -> None: UpperCamelCase = """import __main__ as z""" print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) UpperCamelCase = timeit("""z.get_set_bits_count_using_modulo_operator(25)""" , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) UpperCamelCase = timeit( """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""" , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
321
0
import os from collections import namedtuple import pytest from datasets import ClassLabel, Features, Sequence, Value from datasets.commands.test import TestCommand from datasets.info import DatasetInfo, DatasetInfosDict __snake_case :int = namedtuple( '''_TestCommandArgs''', [ '''dataset''', '''name''', '''cache_dir''', '''data_dir''', '''all_configs''', '''save_infos''', '''ignore_verifications''', '''force_redownload''', '''clear_cache''', ], defaults=[None, None, None, False, False, False, False, False], ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return (abs(source - target ) / target) < 0.01 @pytest.mark.integration def __snake_case ( _UpperCAmelCase ): __a = _TestCommandArgs(dataset=_UpperCAmelCase , all_configs=_UpperCAmelCase , save_infos=_UpperCAmelCase ) __a = TestCommand(*_UpperCAmelCase ) test_command.run() __a = os.path.join(_UpperCAmelCase , '''README.md''' ) assert os.path.exists(_UpperCAmelCase ) __a = DatasetInfosDict.from_directory(_UpperCAmelCase ) __a = DatasetInfosDict( { '''default''': DatasetInfo( features=Features( { '''tokens''': Sequence(Value('''string''' ) ), '''ner_tags''': Sequence( ClassLabel(names=['''O''', '''B-PER''', '''I-PER''', '''B-ORG''', '''I-ORG''', '''B-LOC''', '''I-LOC'''] ) ), '''langs''': Sequence(Value('''string''' ) ), '''spans''': Sequence(Value('''string''' ) ), } ) , splits=[ { '''name''': '''train''', '''num_bytes''': 2351563, '''num_examples''': 10000, }, { '''name''': '''validation''', '''num_bytes''': 238418, '''num_examples''': 1000, }, ] , download_size=3940680 , dataset_size=2589981 , ) } ) assert dataset_infos.keys() == expected_dataset_infos.keys() for key in DatasetInfo._INCLUDED_INFO_IN_YAML: __a , __a = getattr(dataset_infos['''default'''] , _UpperCAmelCase ), getattr(expected_dataset_infos['''default'''] , _UpperCAmelCase ) if key == "num_bytes": assert is_apercent_close(_UpperCAmelCase , _UpperCAmelCase ) elif key == "splits": assert list(_UpperCAmelCase ) == list(_UpperCAmelCase ) for split in result: assert result[split].name == expected[split].name assert result[split].num_examples == expected[split].num_examples assert is_apercent_close(result[split].num_bytes , expected[split].num_bytes ) else: result == expected
49
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class lowerCAmelCase ( unittest.TestCase ): def __init__( self : List[Any] , UpperCAmelCase : int , UpperCAmelCase : Any=7 , UpperCAmelCase : Dict=3 , UpperCAmelCase : Optional[int]=10 , UpperCAmelCase : List[Any]=18 , UpperCAmelCase : Dict=30 , UpperCAmelCase : List[str]=400 , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : str=None , UpperCAmelCase : List[Any]=True , UpperCAmelCase : List[str]=[0.5, 0.5, 0.5] , UpperCAmelCase : Optional[int]=[0.5, 0.5, 0.5] , UpperCAmelCase : List[str]=None , ) -> Any: lowerCamelCase__ : Any = size if size is not None else {'shortest_edge': 18} lowerCamelCase__ : List[Any] = crop_size if crop_size is not None else {'height': 18, 'width': 18} lowerCamelCase__ : Tuple = parent lowerCamelCase__ : List[Any] = batch_size lowerCamelCase__ : Dict = num_channels lowerCamelCase__ : Union[str, Any] = num_frames lowerCamelCase__ : Any = image_size lowerCamelCase__ : int = min_resolution lowerCamelCase__ : Tuple = max_resolution lowerCamelCase__ : List[str] = do_resize lowerCamelCase__ : List[str] = size lowerCamelCase__ : Union[str, Any] = do_normalize lowerCamelCase__ : List[str] = image_mean lowerCamelCase__ : Tuple = image_std lowerCamelCase__ : Tuple = crop_size def A_ ( self : Tuple ) -> List[Any]: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = VivitImageProcessor if is_vision_available() else None def A_ ( self : Optional[Any] ) -> Union[str, Any]: lowerCamelCase__ : Optional[int] = VivitImageProcessingTester(self ) @property def A_ ( self : int ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def A_ ( self : Dict ) -> Any: lowerCamelCase__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase , 'image_mean' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'image_std' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_normalize' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_resize' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_center_crop' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'size' ) ) def A_ ( self : Dict ) -> Optional[Any]: lowerCamelCase__ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 18} ) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18} ) lowerCamelCase__ : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def A_ ( self : List[Any] ) -> Optional[int]: # Initialize image_processing lowerCamelCase__ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos lowerCamelCase__ : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase ) for video in video_inputs: self.assertIsInstance(UpperCAmelCase , UpperCAmelCase ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input lowerCamelCase__ : str = image_processing(video_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCamelCase__ : Optional[int] = image_processing(UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def A_ ( self : int ) -> Union[str, Any]: # Initialize image_processing lowerCamelCase__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCamelCase__ : List[str] = prepare_video_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase , numpify=UpperCAmelCase ) for video in video_inputs: self.assertIsInstance(UpperCAmelCase , UpperCAmelCase ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input lowerCamelCase__ : Any = image_processing(video_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCamelCase__ : Tuple = image_processing(UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def A_ ( self : Dict ) -> Optional[int]: # Initialize image_processing lowerCamelCase__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCamelCase__ : Union[str, Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase , torchify=UpperCAmelCase ) for video in video_inputs: self.assertIsInstance(UpperCAmelCase , UpperCAmelCase ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input lowerCamelCase__ : Union[str, Any] = image_processing(video_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCamelCase__ : Tuple = image_processing(UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
50
'''simple docstring''' import math def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
321
0
import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging snake_case_ : str = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( a ): def __init__( self : Any , _snake_case : WhisperForConditionalGeneration , _snake_case : WhisperProcessor , _snake_case : AutoencoderKL , _snake_case : CLIPTextModel , _snake_case : CLIPTokenizer , _snake_case : UNetaDConditionModel , _snake_case : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , _snake_case : StableDiffusionSafetyChecker , _snake_case : CLIPImageProcessor , ): """simple docstring""" super().__init__() if safety_checker is None: logger.warning( F"""You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure""" ''' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered''' ''' results in services or applications open to the public. Both the diffusers team and Hugging Face''' ''' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling''' ''' it only for use-cases that involve analyzing network behavior or auditing its results. For more''' ''' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .''') self.register_modules( speech_model=_snake_case , speech_processor=_snake_case , vae=_snake_case , text_encoder=_snake_case , tokenizer=_snake_case , unet=_snake_case , scheduler=_snake_case , feature_extractor=_snake_case , ) def lowerCamelCase ( self : Optional[int] , _snake_case : Optional[Union[str, int]] = "auto"): """simple docstring""" if slice_size == "auto": UpperCAmelCase_ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_snake_case) def lowerCamelCase ( self : Any): """simple docstring""" self.enable_attention_slicing(_snake_case) @torch.no_grad() def __call__( self : Optional[Any] , _snake_case : str , _snake_case : Optional[Any]=16000 , _snake_case : int = 512 , _snake_case : int = 512 , _snake_case : int = 50 , _snake_case : float = 7.5 , _snake_case : Optional[Union[str, List[str]]] = None , _snake_case : Optional[int] = 1 , _snake_case : float = 0.0 , _snake_case : Optional[torch.Generator] = None , _snake_case : Optional[torch.FloatTensor] = None , _snake_case : Optional[str] = "pil" , _snake_case : bool = True , _snake_case : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _snake_case : int = 1 , **_snake_case : str , ): """simple docstring""" UpperCAmelCase_ = self.speech_processor.feature_extractor( _snake_case , return_tensors='''pt''' , sampling_rate=_snake_case).input_features.to(self.device) UpperCAmelCase_ = self.speech_model.generate(_snake_case , max_length=480000) UpperCAmelCase_ = self.speech_processor.tokenizer.batch_decode(_snake_case , skip_special_tokens=_snake_case , normalize=_snake_case)[ 0 ] if isinstance(_snake_case , _snake_case): UpperCAmelCase_ = 1 elif isinstance(_snake_case , _snake_case): UpperCAmelCase_ = len(_snake_case) else: raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(_snake_case)}""") if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""") if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_snake_case , _snake_case) or callback_steps <= 0) ): raise ValueError( F"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" F""" {type(_snake_case)}.""") # get prompt text embeddings UpperCAmelCase_ = self.tokenizer( _snake_case , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) UpperCAmelCase_ = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase_ = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' F""" {self.tokenizer.model_max_length} tokens: {removed_text}""") UpperCAmelCase_ = text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase_ = self.text_encoder(text_input_ids.to(self.device))[0] # duplicate text embeddings for each generation per prompt, using mps friendly method UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = text_embeddings.shape UpperCAmelCase_ = text_embeddings.repeat(1 , _snake_case , 1) UpperCAmelCase_ = text_embeddings.view(bs_embed * num_images_per_prompt , _snake_case , -1) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. UpperCAmelCase_ = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: UpperCAmelCase_ = 42 if negative_prompt is None: UpperCAmelCase_ = [''''''] * batch_size elif type(_snake_case) is not type(_snake_case): raise TypeError( F"""`negative_prompt` should be the same type to `prompt`, but got {type(_snake_case)} !=""" F""" {type(_snake_case)}.""") elif isinstance(_snake_case , _snake_case): UpperCAmelCase_ = [negative_prompt] elif batch_size != len(_snake_case): raise ValueError( F"""`negative_prompt`: {negative_prompt} has batch size {len(_snake_case)}, but `prompt`:""" F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" ''' the batch size of `prompt`.''') else: UpperCAmelCase_ = negative_prompt UpperCAmelCase_ = text_input_ids.shape[-1] UpperCAmelCase_ = self.tokenizer( _snake_case , padding='''max_length''' , max_length=_snake_case , truncation=_snake_case , return_tensors='''pt''' , ) UpperCAmelCase_ = self.text_encoder(uncond_input.input_ids.to(self.device))[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase_ = uncond_embeddings.shape[1] UpperCAmelCase_ = uncond_embeddings.repeat(1 , _snake_case , 1) UpperCAmelCase_ = uncond_embeddings.view(batch_size * num_images_per_prompt , _snake_case , -1) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase_ = torch.cat([uncond_embeddings, text_embeddings]) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. UpperCAmelCase_ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) UpperCAmelCase_ = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps UpperCAmelCase_ = torch.randn(_snake_case , generator=_snake_case , device='''cpu''' , dtype=_snake_case).to( self.device) else: UpperCAmelCase_ = torch.randn(_snake_case , generator=_snake_case , device=self.device , dtype=_snake_case) else: if latents.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""") UpperCAmelCase_ = latents.to(self.device) # set timesteps self.scheduler.set_timesteps(_snake_case) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand UpperCAmelCase_ = self.scheduler.timesteps.to(self.device) # scale the initial noise by the standard deviation required by the scheduler UpperCAmelCase_ = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] UpperCAmelCase_ = '''eta''' in set(inspect.signature(self.scheduler.step).parameters.keys()) UpperCAmelCase_ = {} if accepts_eta: UpperCAmelCase_ = eta for i, t in enumerate(self.progress_bar(_snake_case)): # expand the latents if we are doing classifier free guidance UpperCAmelCase_ = torch.cat([latents] * 2) if do_classifier_free_guidance else latents UpperCAmelCase_ = self.scheduler.scale_model_input(_snake_case , _snake_case) # predict the noise residual UpperCAmelCase_ = self.unet(_snake_case , _snake_case , encoder_hidden_states=_snake_case).sample # perform guidance if do_classifier_free_guidance: UpperCAmelCase_ , UpperCAmelCase_ = noise_pred.chunk(2) UpperCAmelCase_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase_ = self.scheduler.step(_snake_case , _snake_case , _snake_case , **_snake_case).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_snake_case , _snake_case , _snake_case) UpperCAmelCase_ = 1 / 0.1_8_2_1_5 * latents UpperCAmelCase_ = self.vae.decode(_snake_case).sample UpperCAmelCase_ = (image / 2 + 0.5).clamp(0 , 1) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCAmelCase_ = image.cpu().permute(0 , 2 , 3 , 1).float().numpy() if output_type == "pil": UpperCAmelCase_ = self.numpy_to_pil(_snake_case) if not return_dict: return image return StableDiffusionPipelineOutput(images=_snake_case , nsfw_content_detected=_snake_case)
51
'''simple docstring''' import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE__ = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE__ = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' SCREAMING_SNAKE_CASE__ = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://www.atticusprojectai.org/cuad"""] , reference_urls=["""https://www.atticusprojectai.org/cuad"""] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} UpperCamelCase = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] UpperCamelCase = evaluate(dataset=_SCREAMING_SNAKE_CASE , predictions=_SCREAMING_SNAKE_CASE ) return score
321
0
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def A_ ( _lowerCAmelCase , _lowerCAmelCase ) -> int: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> Dict: UpperCamelCase : Optional[int] = tmp_path / "cache" UpperCamelCase : Any = {"text": "string"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCamelCase : Any = TextDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_text_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"text": "string"}, {"text": "int32"}, {"text": "float32"}, ] , ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> str: UpperCamelCase : int = tmp_path / "cache" UpperCamelCase : int = {"text": "string"} UpperCamelCase : List[str] = features.copy() if features else default_expected_features UpperCamelCase : Optional[Any] = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Union[str, Any] = TextDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_text_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> Union[str, Any]: UpperCamelCase : Union[str, Any] = tmp_path / "cache" UpperCamelCase : Optional[Any] = {"text": "string"} UpperCamelCase : List[str] = TextDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_text_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> List[Any]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): UpperCamelCase : str = text_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): UpperCamelCase : Tuple = [text_path] UpperCamelCase : Union[str, Any] = tmp_path / "cache" UpperCamelCase : Any = {"text": "string"} UpperCamelCase : Optional[Any] = TextDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_text_dataset(_lowerCAmelCase , _lowerCAmelCase ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase=("train",) ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: UpperCamelCase : Optional[int] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> Union[str, Any]: UpperCamelCase : int = tmp_path / "cache" UpperCamelCase : int = {"text": "string"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCamelCase : Union[str, Any] = TextDatasetReader({"train": text_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_text_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"text": "string"}, {"text": "int32"}, {"text": "float32"}, ] , ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> Optional[Any]: UpperCamelCase : Tuple = tmp_path / "cache" # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" UpperCamelCase : Optional[Any] = {"text": "string"} UpperCamelCase : Dict = features.copy() if features else default_expected_features UpperCamelCase : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Optional[int] = TextDatasetReader({"train": text_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_text_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) -> Any: if split: UpperCamelCase : int = {split: text_path} else: UpperCamelCase : Optional[int] = "train" UpperCamelCase : str = {"train": text_path, "test": text_path} UpperCamelCase : int = tmp_path / "cache" UpperCamelCase : int = {"text": "string"} UpperCamelCase : str = TextDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_text_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
52
'''simple docstring''' def lowercase__ ( __UpperCamelCase )-> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase__ ( __UpperCamelCase = 1000000 )-> int: UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__UpperCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ : List[str] ={ '''configuration_bigbird_pegasus''': [ '''BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BigBirdPegasusConfig''', '''BigBirdPegasusOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Any =[ '''BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BigBirdPegasusForCausalLM''', '''BigBirdPegasusForConditionalGeneration''', '''BigBirdPegasusForQuestionAnswering''', '''BigBirdPegasusForSequenceClassification''', '''BigBirdPegasusModel''', '''BigBirdPegasusPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdPegasusConfig, BigBirdPegasusOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST, BigBirdPegasusForCausalLM, BigBirdPegasusForConditionalGeneration, BigBirdPegasusForQuestionAnswering, BigBirdPegasusForSequenceClassification, BigBirdPegasusModel, BigBirdPegasusPreTrainedModel, ) else: import sys a__ : str =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
53
'''simple docstring''' from __future__ import annotations from math import pow, sqrt def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if resistance == 0: return {"resistance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(__UpperCamelCase , 2 ) + pow(__UpperCamelCase , 2 ) )} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
321
0
"""simple docstring""" import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node a__ : Any = 4 a__ : Dict = 3 class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" pass def UpperCAmelCase__ (lowerCAmelCase_ ): '''simple docstring''' for shard in shards: for i in range(lowerCAmelCase_ ): yield {"i": i, "shard": shard} def UpperCAmelCase__ (): '''simple docstring''' __SCREAMING_SNAKE_CASE = int(os.environ["RANK"] ) __SCREAMING_SNAKE_CASE = int(os.environ["WORLD_SIZE"] ) __SCREAMING_SNAKE_CASE = ArgumentParser() parser.add_argument("--streaming" , type=lowerCAmelCase_ ) parser.add_argument("--local_rank" , type=lowerCAmelCase_ ) parser.add_argument("--num_workers" , type=lowerCAmelCase_ , default=0 ) __SCREAMING_SNAKE_CASE = parser.parse_args() __SCREAMING_SNAKE_CASE = args.streaming __SCREAMING_SNAKE_CASE = args.num_workers __SCREAMING_SNAKE_CASE = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowerCAmelCase_ )]} __SCREAMING_SNAKE_CASE = IterableDataset.from_generator(lowerCAmelCase_ , gen_kwargs=lowerCAmelCase_ ) if not streaming: __SCREAMING_SNAKE_CASE = Dataset.from_list(list(lowerCAmelCase_ ) ) __SCREAMING_SNAKE_CASE = split_dataset_by_node(lowerCAmelCase_ , rank=lowerCAmelCase_ , world_size=lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = torch.utils.data.DataLoader(lowerCAmelCase_ , num_workers=lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = NUM_SHARDS * NUM_ITEMS_PER_SHARD __SCREAMING_SNAKE_CASE = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __SCREAMING_SNAKE_CASE = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
54
'''simple docstring''' # Algorithm for the pigeonhole sorting def lowercase__ ( __UpperCamelCase )-> Union[str, Any]: UpperCamelCase = min(__UpperCamelCase ) # min() finds the minimum value UpperCamelCase = max(__UpperCamelCase ) # max() finds the maximum value UpperCamelCase = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size UpperCamelCase = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(__UpperCamelCase , __UpperCamelCase ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. UpperCamelCase = 0 for count in range(__UpperCamelCase ): while holes[count] > 0: holes[count] -= 1 UpperCamelCase = count + min_val i += 1 def lowercase__ ( )-> Any: UpperCamelCase = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(__UpperCamelCase ) print("""Sorted order is:""" , """ """.join(__UpperCamelCase ) ) if __name__ == "__main__": main()
321
0
'''simple docstring''' import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def __snake_case ( UpperCAmelCase_ : Dict ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def __snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] ): lowerCamelCase_ = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue lowerCamelCase_ = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) lowerCamelCase_ = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) lowerCamelCase_ = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) lowerCamelCase_ = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) lowerCamelCase_ = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) lowerCamelCase_ = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) lowerCamelCase_ = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) lowerCamelCase_ = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) lowerCamelCase_ = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) lowerCamelCase_ = key.replace("image_encoder.module" , "flava.image_model" ) lowerCamelCase_ = key.replace("text_encoder.module" , "flava.text_model" ) lowerCamelCase_ = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) lowerCamelCase_ = key.replace("mm_encoder.module" , "flava.multimodal_model" ) lowerCamelCase_ = key.replace("text_projection" , "flava.text_projection" ) lowerCamelCase_ = key.replace("image_projection" , "flava.image_projection" ) lowerCamelCase_ = value.float() for key, value in codebook_state_dict.items(): lowerCamelCase_ = value return upgrade @torch.no_grad() def __snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Dict=None ): if config_path is not None: lowerCamelCase_ = FlavaConfig.from_pretrained(UpperCAmelCase_ ) else: lowerCamelCase_ = FlavaConfig() lowerCamelCase_ = FlavaForPreTraining(UpperCAmelCase_ ).eval() lowerCamelCase_ = convert_dalle_checkpoint(UpperCAmelCase_ , UpperCAmelCase_ , save_checkpoint=UpperCAmelCase_ ) if os.path.exists(UpperCAmelCase_ ): lowerCamelCase_ = torch.load(UpperCAmelCase_ , map_location="cpu" ) else: lowerCamelCase_ = torch.hub.load_state_dict_from_url(UpperCAmelCase_ , map_location="cpu" ) lowerCamelCase_ = upgrade_state_dict(UpperCAmelCase_ , UpperCAmelCase_ ) hf_model.load_state_dict(UpperCAmelCase_ ) lowerCamelCase_ = hf_model.state_dict() lowerCamelCase_ = count_parameters(UpperCAmelCase_ ) lowerCamelCase_ = count_parameters(UpperCAmelCase_ ) + count_parameters(UpperCAmelCase_ ) assert torch.allclose(UpperCAmelCase_ , UpperCAmelCase_ , atol=1E-3 ) hf_model.save_pretrained(UpperCAmelCase_ ) if __name__ == "__main__": a_ : Optional[int] = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to flava checkpoint""") parser.add_argument("""--codebook_path""", default=None, type=str, help="""Path to flava codebook checkpoint""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") a_ : Union[str, Any] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
55
'''simple docstring''' import torch from diffusers import DDPMParallelScheduler from .test_schedulers import SchedulerCommonTest class a_ ( lowerCamelCase ): lowercase = (DDPMParallelScheduler,) def A__ ( self , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = { """num_train_timesteps""": 1000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """variance_type""": """fixed_small""", """clip_sample""": True, } config.update(**_SCREAMING_SNAKE_CASE ) return config def A__ ( self ) -> List[str]: """simple docstring""" for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[int]: """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE , beta_end=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Tuple: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> List[Any]: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> str: """simple docstring""" self.check_over_configs(thresholding=_SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_SCREAMING_SNAKE_CASE , prediction_type=_SCREAMING_SNAKE_CASE , sample_max_value=_SCREAMING_SNAKE_CASE , ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Union[str, Any]: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> int: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def A__ ( self ) -> Optional[int]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = self.dummy_sample_deter + 0.1 UpperCamelCase = self.dummy_sample_deter - 0.1 UpperCamelCase = samplea.shape[0] UpperCamelCase = torch.stack([samplea, samplea, samplea] , dim=0 ) UpperCamelCase = torch.arange(_SCREAMING_SNAKE_CASE )[0:3, None].repeat(1 , _SCREAMING_SNAKE_CASE ) UpperCamelCase = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) UpperCamelCase = scheduler.batch_step_no_noise(_SCREAMING_SNAKE_CASE , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) ) UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 1_1_5_3.1_8_3_3 ) < 1e-2 assert abs(result_mean.item() - 0.5_0_0_5 ) < 1e-3 def A__ ( self ) -> List[str]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config(prediction_type="""v_prediction""" ) UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) UpperCamelCase = scheduler.timesteps for i, timestep in enumerate(_SCREAMING_SNAKE_CASE ): if i == len(_SCREAMING_SNAKE_CASE ) - 1: UpperCamelCase = -1 else: UpperCamelCase = timesteps[i + 1] UpperCamelCase = scheduler.previous_timestep(_SCREAMING_SNAKE_CASE ) UpperCamelCase = prev_t.item() self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 51, 0] with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""`custom_timesteps` must be in descending order.""" ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""Can only pass one of `num_inference_steps` or `custom_timesteps`.""" ): scheduler.set_timesteps(num_inference_steps=_SCREAMING_SNAKE_CASE , timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( _SCREAMING_SNAKE_CASE , msg="""`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}""" , ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE )
321
0
'''simple docstring''' import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase=(), __UpperCAmelCase=None, __UpperCAmelCase="no", __UpperCAmelCase="29500" ) -> Union[str, Any]: '''simple docstring''' snake_case_ = False snake_case_ = False if any(key.startswith('''KAGGLE''' ) for key in os.environ.keys() ): snake_case_ = True elif "IPython" in sys.modules: snake_case_ = '''google.colab''' in str(sys.modules['''IPython'''].get_ipython() ) try: snake_case_ = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F"Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}." ) if (in_colab or in_kaggle) and (os.environ.get('''TPU_NAME''', __UpperCAmelCase ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( '''To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ''' '''your training function. Restart your notebook and make sure no cells initializes an ''' '''`Accelerator`.''' ) if num_processes is None: snake_case_ = 8 snake_case_ = PrepareForLaunch(__UpperCAmelCase, distributed_type='''TPU''' ) print(F"Launching a training on {num_processes} TPU cores." ) xmp.spawn(__UpperCAmelCase, args=__UpperCAmelCase, nprocs=__UpperCAmelCase, start_method='''fork''' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('''Launching training on one GPU.''' ) else: print('''Launching training on one CPU.''' ) function(*__UpperCAmelCase ) else: if num_processes is None: raise ValueError( '''You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.''' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( '''To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ''' '''inside your training function. Restart your notebook and make sure no cells initializes an ''' '''`Accelerator`.''' ) if torch.cuda.is_initialized(): raise ValueError( '''To launch a multi-GPU training from your notebook, you need to avoid running any instruction ''' '''using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ''' '''function.''' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=__UpperCAmelCase, master_addr='''127.0.01''', master_port=__UpperCAmelCase, mixed_precision=__UpperCAmelCase ): snake_case_ = PrepareForLaunch(__UpperCAmelCase, distributed_type='''MULTI_GPU''' ) print(F"Launching training on {num_processes} GPUs." ) try: start_processes(__UpperCAmelCase, args=__UpperCAmelCase, nprocs=__UpperCAmelCase, start_method='''fork''' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( '''CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ''' '''This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ''' '''Please review your imports and test them when running the `notebook_launcher()` to identify ''' '''which one is problematic.''' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): snake_case_ = '''1''' print('''Launching training on MPS.''' ) elif torch.cuda.is_available(): print('''Launching training on one GPU.''' ) else: print('''Launching training on CPU.''' ) function(*__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase=(), __UpperCAmelCase=2 ) -> Any: '''simple docstring''' from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=__UpperCAmelCase, master_addr='''127.0.01''', master_port='''29500''', accelerate_mixed_precision='''no''', accelerate_debug_rdv_file=tmp_file.name, accelerate_use_cpu='''yes''', ): snake_case_ = PrepareForLaunch(__UpperCAmelCase, debug=__UpperCAmelCase ) start_processes(__UpperCAmelCase, args=__UpperCAmelCase, nprocs=__UpperCAmelCase, start_method='''fork''' )
56
'''simple docstring''' from __future__ import annotations import math class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = size # approximate the overall size of segment tree with given value UpperCamelCase = [0 for i in range(0 , 4 * size )] # create array to store lazy update UpperCamelCase = [0 for i in range(0 , 4 * size )] UpperCamelCase = [0 for i in range(0 , 4 * size )] # flag for lazy update def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 + 1 def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" if left_element == right_element: UpperCamelCase = a[left_element - 1] else: UpperCamelCase = (left_element + right_element) // 2 self.build(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.build(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: UpperCamelCase = val if left_element != right_element: UpperCamelCase = val UpperCamelCase = val UpperCamelCase = True UpperCamelCase = True return True UpperCamelCase = (left_element + right_element) // 2 self.update(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.update(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) return True def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | float: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] UpperCamelCase = (left_element + right_element) // 2 UpperCamelCase = self.query(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = self.query(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __str__( self ) -> str: """simple docstring""" return str([self.query(1 , 1 , self.size , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8] SCREAMING_SNAKE_CASE__ = 1_5 SCREAMING_SNAKE_CASE__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 1_1)) print(segt.query(1, 1, size, 7, 1_2)) segt.update(1, 1, size, 1, 3, 1_1_1) print(segt.query(1, 1, size, 1, 1_5)) segt.update(1, 1, size, 7, 8, 2_3_5) print(segt)
321
0
"""simple docstring""" import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors A : Union[str, Any] = logging.getLogger(__name__) class _UpperCamelCase ( lowerCAmelCase__ ): '''simple docstring''' __UpperCAmelCase : Optional[Any] ="""sequence-classification""" def __init__( self , __a ): if type(__a ) == dict: __lowerCAmelCase = Namespace(**__a ) __lowerCAmelCase = glue_output_modes[hparams.task] __lowerCAmelCase = glue_tasks_num_labels[hparams.task] super().__init__(__a , __a , self.mode ) def snake_case ( self , **__a ): return self.model(**__a ) def snake_case ( self , __a , __a ): __lowerCAmelCase = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __lowerCAmelCase = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None __lowerCAmelCase = self(**__a ) __lowerCAmelCase = outputs[0] __lowerCAmelCase = self.trainer.lr_schedulers[0]["scheduler"] __lowerCAmelCase = {"loss": loss, "rate": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def snake_case ( self ): __lowerCAmelCase = self.hparams __lowerCAmelCase = processors[args.task]() __lowerCAmelCase = processor.get_labels() for mode in ["train", "dev"]: __lowerCAmelCase = self._feature_file(__a ) if os.path.exists(__a ) and not args.overwrite_cache: logger.info("Loading features from cached file %s" , __a ) else: logger.info("Creating features from dataset file at %s" , args.data_dir ) __lowerCAmelCase = ( processor.get_dev_examples(args.data_dir ) if mode == "dev" else processor.get_train_examples(args.data_dir ) ) __lowerCAmelCase = convert_examples_to_features( __a , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info("Saving features into cached file %s" , __a ) torch.save(__a , __a ) def snake_case ( self , __a , __a , __a = False ): __lowerCAmelCase = "dev" if mode == "test" else mode __lowerCAmelCase = self._feature_file(__a ) logger.info("Loading features from cached file %s" , __a ) __lowerCAmelCase = torch.load(__a ) __lowerCAmelCase = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) __lowerCAmelCase = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) __lowerCAmelCase = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) if self.hparams.glue_output_mode == "classification": __lowerCAmelCase = torch.tensor([f.label for f in features] , dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": __lowerCAmelCase = torch.tensor([f.label for f in features] , dtype=torch.float ) return DataLoader( TensorDataset(__a , __a , __a , __a ) , batch_size=__a , shuffle=__a , ) def snake_case ( self , __a , __a ): __lowerCAmelCase = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __lowerCAmelCase = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None __lowerCAmelCase = self(**__a ) __lowerCAmelCase , __lowerCAmelCase = outputs[:2] __lowerCAmelCase = logits.detach().cpu().numpy() __lowerCAmelCase = inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def snake_case ( self , __a ): __lowerCAmelCase = torch.stack([x["val_loss"] for x in outputs] ).mean().detach().cpu().item() __lowerCAmelCase = np.concatenate([x["pred"] for x in outputs] , axis=0 ) if self.hparams.glue_output_mode == "classification": __lowerCAmelCase = np.argmax(__a , axis=1 ) elif self.hparams.glue_output_mode == "regression": __lowerCAmelCase = np.squeeze(__a ) __lowerCAmelCase = np.concatenate([x["target"] for x in outputs] , axis=0 ) __lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0] )] __lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0] )] __lowerCAmelCase = {**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task , __a , __a )} __lowerCAmelCase = dict(results.items() ) __lowerCAmelCase = results return ret, preds_list, out_label_list def snake_case ( self , __a ): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = self._eval_end(__a ) __lowerCAmelCase = ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def snake_case ( self , __a ): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = self._eval_end(__a ) __lowerCAmelCase = ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def snake_case ( __a , __a ): BaseTransformer.add_model_specific_args(__a , __a ) parser.add_argument( "--max_seq_length" , default=1_28 , type=__a , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--task" , default="" , type=__a , required=__a , help="The GLUE task to run" , ) parser.add_argument( "--gpus" , default=0 , type=__a , help="The number of GPUs allocated for this, it is by default 0 meaning none" , ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) return parser def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = argparse.ArgumentParser() add_generic_args(_UpperCamelCase , os.getcwd() ) __lowerCAmelCase = GLUETransformer.add_model_specific_args(_UpperCamelCase , os.getcwd() ) __lowerCAmelCase = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: __lowerCAmelCase = os.path.join( "./results" , f"{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}" , ) os.makedirs(args.output_dir ) __lowerCAmelCase = GLUETransformer(_UpperCamelCase ) __lowerCAmelCase = generic_train(_UpperCamelCase , _UpperCamelCase ) # Optionally, predict on dev set and write to output_dir if args.do_predict: __lowerCAmelCase = sorted(glob.glob(os.path.join(args.output_dir , "checkpoint-epoch=*.ckpt" ) , recursive=_UpperCamelCase ) ) __lowerCAmelCase = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(_UpperCamelCase ) if __name__ == "__main__": main()
57
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 1000 )-> int: UpperCamelCase = -1 UpperCamelCase = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c UpperCamelCase = (n * n - 2 * a * n) // (2 * n - 2 * a) UpperCamelCase = n - a - b if c * c == (a * a + b * b): UpperCamelCase = a * b * c if candidate >= product: UpperCamelCase = candidate return product if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' def lowerCamelCase ( __lowerCamelCase : int = 1000 ) ->int: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 1, 1 _SCREAMING_SNAKE_CASE = [] for i in range(1 , n + 1 ): _SCREAMING_SNAKE_CASE = prev_numerator + 2 * prev_denominator _SCREAMING_SNAKE_CASE = prev_numerator + prev_denominator if len(str(__lowerCamelCase ) ) > len(str(__lowerCamelCase ) ): result.append(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = numerator _SCREAMING_SNAKE_CASE = denominator return len(__lowerCamelCase ) if __name__ == "__main__": print(f"""{solution() = }""")
58
'''simple docstring''' import argparse import struct import unittest class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = data # Initialize hash values UpperCamelCase = [ 0x6A_09_E6_67, 0xBB_67_AE_85, 0x3C_6E_F3_72, 0xA5_4F_F5_3A, 0x51_0E_52_7F, 0x9B_05_68_8C, 0x1F_83_D9_AB, 0x5B_E0_CD_19, ] # Initialize round constants UpperCamelCase = [ 0x42_8A_2F_98, 0x71_37_44_91, 0xB5_C0_FB_CF, 0xE9_B5_DB_A5, 0x39_56_C2_5B, 0x59_F1_11_F1, 0x92_3F_82_A4, 0xAB_1C_5E_D5, 0xD8_07_AA_98, 0x12_83_5B_01, 0x24_31_85_BE, 0x55_0C_7D_C3, 0x72_BE_5D_74, 0x80_DE_B1_FE, 0x9B_DC_06_A7, 0xC1_9B_F1_74, 0xE4_9B_69_C1, 0xEF_BE_47_86, 0x0F_C1_9D_C6, 0x24_0C_A1_CC, 0x2D_E9_2C_6F, 0x4A_74_84_AA, 0x5C_B0_A9_DC, 0x76_F9_88_DA, 0x98_3E_51_52, 0xA8_31_C6_6D, 0xB0_03_27_C8, 0xBF_59_7F_C7, 0xC6_E0_0B_F3, 0xD5_A7_91_47, 0x06_CA_63_51, 0x14_29_29_67, 0x27_B7_0A_85, 0x2E_1B_21_38, 0x4D_2C_6D_FC, 0x53_38_0D_13, 0x65_0A_73_54, 0x76_6A_0A_BB, 0x81_C2_C9_2E, 0x92_72_2C_85, 0xA2_BF_E8_A1, 0xA8_1A_66_4B, 0xC2_4B_8B_70, 0xC7_6C_51_A3, 0xD1_92_E8_19, 0xD6_99_06_24, 0xF4_0E_35_85, 0x10_6A_A0_70, 0x19_A4_C1_16, 0x1E_37_6C_08, 0x27_48_77_4C, 0x34_B0_BC_B5, 0x39_1C_0C_B3, 0x4E_D8_AA_4A, 0x5B_9C_CA_4F, 0x68_2E_6F_F3, 0x74_8F_82_EE, 0x78_A5_63_6F, 0x84_C8_78_14, 0x8C_C7_02_08, 0x90_BE_FF_FA, 0xA4_50_6C_EB, 0xBE_F9_A3_F7, 0xC6_71_78_F2, ] UpperCamelCase = self.preprocessing(self.data ) self.final_hash() @staticmethod def A__ ( _SCREAMING_SNAKE_CASE ) -> bytes: """simple docstring""" UpperCamelCase = B"""\x80""" + (B"""\x00""" * (63 - (len(_SCREAMING_SNAKE_CASE ) + 8) % 64)) UpperCamelCase = struct.pack(""">Q""" , (len(_SCREAMING_SNAKE_CASE ) * 8) ) return data + padding + big_endian_integer def A__ ( self ) -> None: """simple docstring""" UpperCamelCase = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers UpperCamelCase = list(struct.unpack(""">16L""" , _SCREAMING_SNAKE_CASE ) ) # add 48 0-ed integers words += [0] * 48 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array UpperCamelCase = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) UpperCamelCase = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) UpperCamelCase = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x1_00_00_00_00 # Compression UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 6 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 11 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 25 ) UpperCamelCase = (e & f) ^ ((~e & 0xFF_FF_FF_FF) & g) UpperCamelCase = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x1_00_00_00_00 UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 2 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 13 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 22 ) UpperCamelCase = (a & b) ^ (a & c) ^ (b & c) UpperCamelCase = (sa + maj) % 0x1_00_00_00_00 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = ( g, f, e, ((d + tempa) % 0x1_00_00_00_00), c, b, a, ((tempa + tempa) % 0x1_00_00_00_00), ) UpperCamelCase = [a, b, c, d, e, f, g, h] # Modify final values UpperCamelCase = [ ((element + mutated_hash_values[index]) % 0x1_00_00_00_00) for index, element in enumerate(self.hashes ) ] UpperCamelCase = """""".join([hex(_SCREAMING_SNAKE_CASE )[2:].zfill(8 ) for value in self.hashes] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return 0xFF_FF_FF_FF & (value << (32 - rotations)) | (value >> rotations) class a_ ( unittest.TestCase ): def A__ ( self ) -> None: """simple docstring""" import hashlib UpperCamelCase = bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(_SCREAMING_SNAKE_CASE ).hash , hashlib.shaaaa(_SCREAMING_SNAKE_CASE ).hexdigest() ) def lowercase__ ( )-> None: import doctest doctest.testmod() UpperCamelCase = argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: UpperCamelCase = f.read() else: UpperCamelCase = bytes(__UpperCamelCase , """utf-8""" ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
321
0
def UpperCamelCase ( __lowerCamelCase : str , __lowerCamelCase : str ): def get_matched_characters(__lowerCamelCase : str , __lowerCamelCase : str ) -> str: snake_case : Tuple = [] snake_case : Union[str, Any] = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): snake_case : Any = int(max(0 , i - limit ) ) snake_case : Any = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__lowerCamelCase ) snake_case : Dict = f"""{_stra[0:_stra.index(__lowerCamelCase )]} {_stra[_stra.index(__lowerCamelCase ) + 1:]}""" return "".join(__lowerCamelCase ) # matching characters snake_case : List[str] = get_matched_characters(__lowerCamelCase , __lowerCamelCase ) snake_case : Union[str, Any] = get_matched_characters(__lowerCamelCase , __lowerCamelCase ) snake_case : List[str] = len(__lowerCamelCase ) # transposition snake_case : List[Any] = ( len([(ca, ca) for ca, ca in zip(__lowerCamelCase , __lowerCamelCase ) if ca != ca] ) // 2 ) if not match_count: snake_case : Dict = 0.0 else: snake_case : int = ( 1 / 3 * ( match_count / len(__lowerCamelCase ) + match_count / len(__lowerCamelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters snake_case : Any = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
59
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) SCREAMING_SNAKE_CASE__ = _symbol_database.Default() SCREAMING_SNAKE_CASE__ = _descriptor_pool.Default().AddSerializedFile( b'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03' ) SCREAMING_SNAKE_CASE__ = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS is False: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = b'H\003' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" SCREAMING_SNAKE_CASE__ = 4_5 SCREAMING_SNAKE_CASE__ = 1_5_8_1 SCREAMING_SNAKE_CASE__ = 1_5_1_7 SCREAMING_SNAKE_CASE__ = 1_5_7_0 SCREAMING_SNAKE_CASE__ = 1_5_8_4 SCREAMING_SNAKE_CASE__ = 1_7_9_3 SCREAMING_SNAKE_CASE__ = 1_7_9_5 SCREAMING_SNAKE_CASE__ = 1_9_1_6 SCREAMING_SNAKE_CASE__ = 1_8_6_4 SCREAMING_SNAKE_CASE__ = 1_9_0_5 SCREAMING_SNAKE_CASE__ = 1_9_1_9 SCREAMING_SNAKE_CASE__ = 2_4_2_9 SCREAMING_SNAKE_CASE__ = 2_2_0_8 SCREAMING_SNAKE_CASE__ = 2_4_1_8 SCREAMING_SNAKE_CASE__ = 2_3_2_3 SCREAMING_SNAKE_CASE__ = 2_4_0_7 # @@protoc_insertion_point(module_scope)
321
0
"""simple docstring""" import argparse import os import torch from transformers.utils import WEIGHTS_NAME snake_case__ : Optional[int] = ['''small''', '''medium''', '''large'''] snake_case__ : Dict = '''lm_head.decoder.weight''' snake_case__ : str = '''lm_head.weight''' def _snake_case ( _snake_case : str , _snake_case : str ): lowerCAmelCase : Optional[int] = torch.load(_snake_case ) lowerCAmelCase : Any = d.pop(_snake_case ) os.makedirs(_snake_case , exist_ok=_snake_case ) torch.save(_snake_case , os.path.join(_snake_case , _snake_case ) ) if __name__ == "__main__": snake_case__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) snake_case__ : Optional[Any] = parser.parse_args() for MODEL in DIALOGPT_MODELS: snake_case__ : str = os.path.join(args.dialogpt_path, f"""{MODEL}_ft.pkl""") snake_case__ : Any = f"""./DialoGPT-{MODEL}""" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
60
'''simple docstring''' SCREAMING_SNAKE_CASE__ = 8.31_44_62 # Unit - J mol-1 K-1 def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
321
0
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _a = TypeVar('KEY') _a = TypeVar('VAL') @dataclass(frozen=lowercase__ ,slots=lowercase__ ) class A_ (Generic[KEY, VAL] ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : KEY SCREAMING_SNAKE_CASE__ : VAL class A_ (_Item ): '''simple docstring''' def __init__( self ): """simple docstring""" super().__init__(lowercase_ , lowercase_ ) def __bool__( self ): """simple docstring""" return False _a = _DeletedItem() class A_ (MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__( self , lowercase_ = 8 , lowercase_ = 0.75 ): """simple docstring""" UpperCAmelCase_ : Tuple = initial_block_size UpperCAmelCase_ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 UpperCAmelCase_ : Optional[Any] = capacity_factor UpperCAmelCase_ : int = 0 def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return hash(lowercase_ ) % len(self._buckets ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return (ind + 1) % len(self._buckets ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self._buckets[ind] if not stored: UpperCAmelCase_ : List[Any] = _Item(lowercase_ , lowercase_ ) self._len += 1 return True elif stored.key == key: UpperCAmelCase_ : Tuple = _Item(lowercase_ , lowercase_ ) return True else: return False def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" if len(self._buckets ) <= self._initial_block_size: return False UpperCAmelCase_ : List[str] = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = self._buckets UpperCAmelCase_ : Any = [None] * new_size UpperCAmelCase_ : Any = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def UpperCamelCase__ ( self ): """simple docstring""" self._resize(len(self._buckets ) * 2 ) def UpperCamelCase__ ( self ): """simple docstring""" self._resize(len(self._buckets ) // 2 ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self._get_bucket_index(lowercase_ ) for _ in range(len(self._buckets ) ): yield ind UpperCAmelCase_ : Tuple = self._get_next_ind(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): if self._try_set(lowercase_ , lowercase_ , lowercase_ ): break def __setitem__( self , lowercase_ , lowercase_ ): """simple docstring""" if self._is_full(): self._size_up() self._add_item(lowercase_ , lowercase_ ) def __delitem__( self , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): UpperCAmelCase_ : Optional[Any] = self._buckets[ind] if item is None: raise KeyError(lowercase_ ) if item is _deleted: continue if item.key == key: UpperCAmelCase_ : Optional[Any] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): UpperCAmelCase_ : Dict = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowercase_ ) def __len__( self ): """simple docstring""" return self._len def __iter__( self ): """simple docstring""" yield from (item.key for item in self._buckets if item) def __repr__( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = " ,".join( F"""{item.key}: {item.val}""" for item in self._buckets if item ) return F"""HashMap({val_string})"""
61
'''simple docstring''' import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem SCREAMING_SNAKE_CASE__ = importlib.util.find_spec('s3fs') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 SCREAMING_SNAKE_CASE__ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def lowercase__ ( __UpperCamelCase )-> str: if "://" in dataset_path: UpperCamelCase = dataset_path.split("""://""" )[1] return dataset_path def lowercase__ ( __UpperCamelCase )-> bool: if fs is not None and fs.protocol != "file": return True else: return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> int: UpperCamelCase = not is_remote_filesystem(__UpperCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(__UpperCamelCase ) , fs._strip_protocol(__UpperCamelCase ) ) else: fs.mv(__UpperCamelCase , __UpperCamelCase , recursive=__UpperCamelCase ) def lowercase__ ( )-> None: if hasattr(fsspec.asyn , """reset_lock""" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: UpperCamelCase = None UpperCamelCase = None UpperCamelCase = threading.Lock()
321
0
import contextlib from multiprocessing import Pool, RLock from tqdm.auto import tqdm from ..utils import experimental, logging _A = logging.get_logger(__name__) class UpperCAmelCase__ : """simple docstring""" UpperCAmelCase__ : Any = None @experimental def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] ): if ParallelBackendConfig.backend_name is None: return _map_with_multiprocessing_pool( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return _map_with_joblib(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any ): __UpperCamelCase =num_proc if num_proc <= len(SCREAMING_SNAKE_CASE__ ) else len(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =[] # We organize the splits ourselve (contiguous splits) for index in range(SCREAMING_SNAKE_CASE__ ): __UpperCamelCase =len(SCREAMING_SNAKE_CASE__ ) // num_proc __UpperCamelCase =len(SCREAMING_SNAKE_CASE__ ) % num_proc __UpperCamelCase =div * index + min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =start + div + (1 if index < mod else 0) split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc) ) if len(SCREAMING_SNAKE_CASE__ ) != sum(len(i[1] ) for i in split_kwds ): raise ValueError( F'Error dividing inputs iterable among processes. ' F'Total number of objects {len(SCREAMING_SNAKE_CASE__ )}, ' F'length: {sum(len(i[1] ) for i in split_kwds )}' ) logger.info( F'Spawning {num_proc} processes for {len(SCREAMING_SNAKE_CASE__ )} objects in slices of {[len(i[1] ) for i in split_kwds]}' ) __UpperCamelCase , __UpperCamelCase =None, None if not disable_tqdm: __UpperCamelCase , __UpperCamelCase =(RLock(),), tqdm.set_lock with Pool(SCREAMING_SNAKE_CASE__ , initargs=SCREAMING_SNAKE_CASE__ , initializer=SCREAMING_SNAKE_CASE__ ) as pool: __UpperCamelCase =pool.map(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) logger.info(F'Finished {num_proc} processes' ) __UpperCamelCase =[obj for proc_res in mapped for obj in proc_res] logger.info(F'Unpacked {len(SCREAMING_SNAKE_CASE__ )} objects' ) return mapped def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[int] ): # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib, # and it requires monkey-patching joblib internal classes which is subject to change import joblib with joblib.parallel_backend(ParallelBackendConfig.backend_name , n_jobs=SCREAMING_SNAKE_CASE__ ): return joblib.Parallel()( joblib.delayed(SCREAMING_SNAKE_CASE__ )((function, obj, types, None, True, None) ) for obj in iterable ) @experimental @contextlib.contextmanager def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : str ): __UpperCamelCase =backend_name if backend_name == "spark": from joblibspark import register_spark register_spark() # TODO: call create_cache_and_write_probe if "download" in steps # TODO: raise NotImplementedError when Dataset.map etc is called try: yield finally: __UpperCamelCase =None
62
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_xlm_roberta_xl': [ 'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaXLConfig', 'XLMRobertaXLOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaXLForCausalLM', 'XLMRobertaXLForMaskedLM', 'XLMRobertaXLForMultipleChoice', 'XLMRobertaXLForQuestionAnswering', 'XLMRobertaXLForSequenceClassification', 'XLMRobertaXLForTokenClassification', 'XLMRobertaXLModel', 'XLMRobertaXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure)
321
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available lowerCAmelCase_ : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : List[Any] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys lowerCAmelCase_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
63
'''simple docstring''' import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = 'docs/source/en/_toctree.yml' def lowercase__ ( __UpperCamelCase )-> Optional[Any]: UpperCamelCase = defaultdict(__UpperCamelCase ) UpperCamelCase = [] UpperCamelCase = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__UpperCamelCase ) UpperCamelCase = new_doc_list UpperCamelCase = [key for key, value in counts.items() if value > 1] UpperCamelCase = [] for duplicate_key in duplicates: UpperCamelCase = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__UpperCamelCase ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) UpperCamelCase = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__UpperCamelCase ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__UpperCamelCase ) # Sort return overview_doc def lowercase__ ( __UpperCamelCase=False )-> List[str]: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 UpperCamelCase = api_doc[scheduler_idx]["""sections"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) UpperCamelCase = False if new_scheduler_doc != scheduler_doc: UpperCamelCase = True if overwrite: UpperCamelCase = new_scheduler_doc if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def lowercase__ ( __UpperCamelCase=False )-> Tuple: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 UpperCamelCase = False UpperCamelCase = api_doc[pipeline_idx]["""sections"""] UpperCamelCase = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: UpperCamelCase = pipeline_doc["""section"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if overwrite: UpperCamelCase = new_sub_pipeline_doc new_pipeline_docs.append(__UpperCamelCase ) # sort overall pipeline doc UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if new_pipeline_docs != pipeline_docs: UpperCamelCase = True if overwrite: UpperCamelCase = new_pipeline_docs if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) 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__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
321
0
"""simple docstring""" def UpperCAmelCase__ (snake_case__ : int , snake_case__ : int ): """simple docstring""" while b: _snake_case , _snake_case : Optional[int] = b, a % b return a def UpperCAmelCase__ (snake_case__ : int , snake_case__ : int ): """simple docstring""" return a if b == 0 else euclidean_gcd_recursive(snake_case__ , a % b ) def UpperCAmelCase__ (): """simple docstring""" print(F"euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}" ) print(F"euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}" ) print(F"euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}" ) print(F"euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}" ) print(F"euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}" ) print(F"euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}" ) print(F"euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}" ) print(F"euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}" ) print(F"euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}" ) print(F"euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}" ) if __name__ == "__main__": main()
64
'''simple docstring''' import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) with open(F"{class_data_dir}/caption.txt" , """w""" ) as fa, open(F"{class_data_dir}/urls.txt" , """w""" ) as fa, open( F"{class_data_dir}/images.txt" , """w""" ) as fa: while total < num_class_images: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = Image.open(BytesIO(img.content ) ) with open(F"{class_data_dir}/images/{total}.jpg" , """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(F"{class_data_dir}/images/{total}.jpg" + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase__ = { 'configuration_jukebox': [ 'JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP', 'JukeboxConfig', 'JukeboxPriorConfig', 'JukeboxVQVAEConfig', ], 'tokenization_jukebox': ['JukeboxTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ 'JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST', 'JukeboxModel', 'JukeboxPreTrainedModel', 'JukeboxVQVAE', 'JukeboxPrior', ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys UpperCamelCase__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
65
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = 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 a_ : lowercase = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The column name of the images in the files."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the training data."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the validation data."""} ) lowercase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class a_ : lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) lowercase = field( default=lowerCamelCase , 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""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) lowercase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class a_ ( lowerCamelCase ): lowercase = field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def lowercase__ ( __UpperCamelCase )-> int: UpperCamelCase = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def lowercase__ ( )-> List[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase ,UpperCamelCase ,UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase ,UpperCamelCase ,UpperCamelCase = 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""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCamelCase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase = split["""train"""] UpperCamelCase = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(F"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(F"New config: {config}" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase = ViTMAEForPreTraining(__UpperCamelCase ) if training_args.do_train: UpperCamelCase = ds["""train"""].column_names else: UpperCamelCase = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = """image""" elif "img" in column_names: UpperCamelCase = """img""" else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size["""shortest_edge"""] else: UpperCamelCase = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase = Compose( [ Lambda(lambda __UpperCamelCase : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(__UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__UpperCamelCase ): UpperCamelCase = [transforms(__UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__UpperCamelCase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def lowercase__ ( __UpperCamelCase )-> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
321
0
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : Tuple = (UniPCMultistepScheduler,) _A : Any = (("""num_inference_steps""", 2_5),) def lowerCAmelCase_ ( self: Any , **snake_case: Optional[Any] ) -> Optional[int]: snake_case_ :Tuple = { """num_train_timesteps""": 1_000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """solver_order""": 2, """solver_type""": """bh2""", } config.update(**snake_case ) return config def lowerCAmelCase_ ( self: List[Any] , snake_case: Dict=0 , **snake_case: List[str] ) -> Union[str, Any]: snake_case_ :Any = dict(self.forward_default_kwargs ) snake_case_ :str = kwargs.pop("""num_inference_steps""" , snake_case ) snake_case_ :Optional[int] = self.dummy_sample snake_case_ :Tuple = 0.1 * sample snake_case_ :str = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: snake_case_ :Union[str, Any] = self.get_scheduler_config(**snake_case ) snake_case_ :Tuple = scheduler_class(**snake_case ) scheduler.set_timesteps(snake_case ) # copy over dummy past residuals snake_case_ :Optional[int] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(snake_case ) snake_case_ :int = scheduler_class.from_pretrained(snake_case ) new_scheduler.set_timesteps(snake_case ) # copy over dummy past residuals snake_case_ :Union[str, Any] = dummy_past_residuals[: new_scheduler.config.solver_order] snake_case_, snake_case_ :Union[str, Any] = sample, sample for t in range(snake_case , time_step + scheduler.config.solver_order + 1 ): snake_case_ :Any = scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample snake_case_ :Dict = new_scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCAmelCase_ ( self: Tuple , snake_case: List[Any]=0 , **snake_case: Union[str, Any] ) -> Union[str, Any]: snake_case_ :Dict = dict(self.forward_default_kwargs ) snake_case_ :List[Any] = kwargs.pop("""num_inference_steps""" , snake_case ) snake_case_ :Optional[int] = self.dummy_sample snake_case_ :Any = 0.1 * sample snake_case_ :Union[str, Any] = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: snake_case_ :Tuple = self.get_scheduler_config() snake_case_ :Dict = scheduler_class(**snake_case ) scheduler.set_timesteps(snake_case ) # copy over dummy past residuals (must be after setting timesteps) snake_case_ :Dict = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(snake_case ) snake_case_ :Tuple = scheduler_class.from_pretrained(snake_case ) # copy over dummy past residuals new_scheduler.set_timesteps(snake_case ) # copy over dummy past residual (must be after setting timesteps) snake_case_ :List[Any] = dummy_past_residuals[: new_scheduler.config.solver_order] snake_case_ :List[Any] = scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample snake_case_ :List[Any] = new_scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCAmelCase_ ( self: Any , snake_case: List[str]=None , **snake_case: int ) -> Any: if scheduler is None: snake_case_ :Optional[int] = self.scheduler_classes[0] snake_case_ :Optional[int] = self.get_scheduler_config(**snake_case ) snake_case_ :int = scheduler_class(**snake_case ) snake_case_ :Optional[int] = self.scheduler_classes[0] snake_case_ :List[str] = self.get_scheduler_config(**snake_case ) snake_case_ :Optional[int] = scheduler_class(**snake_case ) snake_case_ :Dict = 10 snake_case_ :List[str] = self.dummy_model() snake_case_ :str = self.dummy_sample_deter scheduler.set_timesteps(snake_case ) for i, t in enumerate(scheduler.timesteps ): snake_case_ :Dict = model(snake_case , snake_case ) snake_case_ :int = scheduler.step(snake_case , snake_case , snake_case ).prev_sample return sample def lowerCAmelCase_ ( self: Optional[Any] ) -> Optional[int]: snake_case_ :Dict = dict(self.forward_default_kwargs ) snake_case_ :List[Any] = kwargs.pop("""num_inference_steps""" , snake_case ) for scheduler_class in self.scheduler_classes: snake_case_ :Union[str, Any] = self.get_scheduler_config() snake_case_ :List[str] = scheduler_class(**snake_case ) snake_case_ :int = self.dummy_sample snake_case_ :Tuple = 0.1 * sample if num_inference_steps is not None and hasattr(snake_case , """set_timesteps""" ): scheduler.set_timesteps(snake_case ) elif num_inference_steps is not None and not hasattr(snake_case , """set_timesteps""" ): snake_case_ :Optional[int] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) snake_case_ :Optional[int] = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] snake_case_ :int = dummy_past_residuals[: scheduler.config.solver_order] snake_case_ :int = scheduler.timesteps[5] snake_case_ :List[str] = scheduler.timesteps[6] snake_case_ :Dict = scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample snake_case_ :Any = scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowerCAmelCase_ ( self: int ) -> List[Any]: # make sure that iterating over schedulers with same config names gives same results # for defaults snake_case_ :str = UniPCMultistepScheduler(**self.get_scheduler_config() ) snake_case_ :List[Any] = self.full_loop(scheduler=snake_case ) snake_case_ :Optional[int] = torch.mean(torch.abs(snake_case ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 snake_case_ :Optional[int] = DPMSolverSinglestepScheduler.from_config(scheduler.config ) snake_case_ :Union[str, Any] = DEISMultistepScheduler.from_config(scheduler.config ) snake_case_ :int = DPMSolverMultistepScheduler.from_config(scheduler.config ) snake_case_ :Optional[Any] = UniPCMultistepScheduler.from_config(scheduler.config ) snake_case_ :str = self.full_loop(scheduler=snake_case ) snake_case_ :Dict = torch.mean(torch.abs(snake_case ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 def lowerCAmelCase_ ( self: Any ) -> List[str]: for timesteps in [25, 50, 100, 999, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def lowerCAmelCase_ ( self: Tuple ) -> Tuple: self.check_over_configs(thresholding=snake_case ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , solver_order=snake_case , solver_type=snake_case , ) def lowerCAmelCase_ ( self: Dict ) -> List[str]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def lowerCAmelCase_ ( self: str ) -> Optional[int]: for solver_type in ["bh1", "bh2"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=snake_case , solver_type=snake_case , prediction_type=snake_case , ) snake_case_ :str = self.full_loop( solver_order=snake_case , solver_type=snake_case , prediction_type=snake_case , ) assert not torch.isnan(snake_case ).any(), "Samples have nan numbers" def lowerCAmelCase_ ( self: List[Any] ) -> Union[str, Any]: self.check_over_configs(lower_order_final=snake_case ) self.check_over_configs(lower_order_final=snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> int: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]: self.check_over_forward(num_inference_steps=snake_case , time_step=0 ) def lowerCAmelCase_ ( self: str ) -> Optional[int]: snake_case_ :Tuple = self.full_loop() snake_case_ :List[Any] = torch.mean(torch.abs(snake_case ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 def lowerCAmelCase_ ( self: Tuple ) -> Optional[int]: snake_case_ :Dict = self.full_loop(prediction_type="""v_prediction""" ) snake_case_ :Any = torch.mean(torch.abs(snake_case ) ) assert abs(result_mean.item() - 0.1_0_1_4 ) < 1E-3 def lowerCAmelCase_ ( self: Any ) -> List[str]: snake_case_ :Dict = self.scheduler_classes[0] snake_case_ :Tuple = self.get_scheduler_config(thresholding=snake_case , dynamic_thresholding_ratio=0 ) snake_case_ :List[Any] = scheduler_class(**snake_case ) snake_case_ :Any = 10 snake_case_ :int = self.dummy_model() snake_case_ :Union[str, Any] = self.dummy_sample_deter.half() scheduler.set_timesteps(snake_case ) for i, t in enumerate(scheduler.timesteps ): snake_case_ :int = model(snake_case , snake_case ) snake_case_ :List[Any] = scheduler.step(snake_case , snake_case , snake_case ).prev_sample assert sample.dtype == torch.floataa def lowerCAmelCase_ ( self: Tuple , **snake_case: List[Any] ) -> Dict: for scheduler_class in self.scheduler_classes: snake_case_ :int = self.get_scheduler_config(**snake_case ) snake_case_ :Tuple = scheduler_class(**snake_case ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
66
'''simple docstring''' import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # pylint: disable=invalid-name SCREAMING_SNAKE_CASE__ = 2_5_6 class a_ ( lowerCamelCase ): lowercase = ["""melgan"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> None: """simple docstring""" super().__init__() # From MELGAN UpperCamelCase = math.log(1e-5 ) # Matches MelGAN training. UpperCamelCase = 4.0 # Largest value for most examples UpperCamelCase = 128 self.register_modules( notes_encoder=_SCREAMING_SNAKE_CASE , continuous_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , melgan=_SCREAMING_SNAKE_CASE , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" UpperCamelCase ,UpperCamelCase = output_range if clip: UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , self.min_value , self.max_value ) # Scale to [0, 1]. UpperCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase ,UpperCamelCase = input_range UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if clip else outputs # Scale to [0, 1]. UpperCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" UpperCamelCase = input_tokens > 0 UpperCamelCase ,UpperCamelCase = self.notes_encoder( encoder_input_tokens=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) UpperCamelCase ,UpperCamelCase = self.continuous_encoder( encoder_inputs=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" UpperCamelCase = noise_time if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML UpperCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) UpperCamelCase = self.decoder( encodings_and_masks=_SCREAMING_SNAKE_CASE , decoder_input_tokens=_SCREAMING_SNAKE_CASE , decoder_noise_time=_SCREAMING_SNAKE_CASE ) return logits @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "numpy" , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , ) -> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_SCREAMING_SNAKE_CASE )}." ) UpperCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) UpperCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) UpperCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) for i, encoder_input_tokens in enumerate(_SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. UpperCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. UpperCamelCase = ones UpperCamelCase = self.scale_features( _SCREAMING_SNAKE_CASE , output_range=[-1.0, 1.0] , clip=_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_SCREAMING_SNAKE_CASE , continuous_mask=_SCREAMING_SNAKE_CASE , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop UpperCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_SCREAMING_SNAKE_CASE , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_SCREAMING_SNAKE_CASE ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCamelCase = self.decode( encodings_and_masks=_SCREAMING_SNAKE_CASE , input_tokens=_SCREAMING_SNAKE_CASE , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 UpperCamelCase = self.scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = self.scale_to_features(_SCREAMING_SNAKE_CASE , input_range=[-1.0, 1.0] ) UpperCamelCase = mel[:1] UpperCamelCase = mel.cpu().float().numpy() UpperCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) logger.info("""Generated segment""" , _SCREAMING_SNAKE_CASE ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": UpperCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: UpperCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_SCREAMING_SNAKE_CASE )
321
0
'''simple docstring''' import os __UpperCAmelCase ={"I": 1, "V": 5, "X": 1_0, "L": 5_0, "C": 1_0_0, "D": 5_0_0, "M": 1_0_0_0} def __lowerCAmelCase ( UpperCamelCase__ ) -> int: __lowerCamelCase = 0 __lowerCamelCase = 0 while index < len(UpperCamelCase__ ) - 1: __lowerCamelCase = SYMBOLS[numerals[index]] __lowerCamelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = '''''' __lowerCamelCase = num // 10_00 numerals += m_count * "M" num %= 10_00 __lowerCamelCase = num // 1_00 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 1_00 __lowerCamelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def __lowerCAmelCase ( UpperCamelCase__ = "/p089_roman.txt" ) -> int: __lowerCamelCase = 0 with open(os.path.dirname(UpperCamelCase__ ) + roman_numerals_filename ) as filea: __lowerCamelCase = filea.readlines() for line in lines: __lowerCamelCase = line.strip() __lowerCamelCase = parse_roman_numerals(UpperCamelCase__ ) __lowerCamelCase = generate_roman_numerals(UpperCamelCase__ ) savings += len(UpperCamelCase__ ) - len(UpperCamelCase__ ) return savings if __name__ == "__main__": print(f'{solution() = }')
67
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 4000000 )-> int: UpperCamelCase = [] UpperCamelCase ,UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCamelCase ) UpperCamelCase ,UpperCamelCase = b, a + b return sum(__UpperCamelCase ) if __name__ == "__main__": print(f'{solution() = }')
321
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: list[list[int]] , SCREAMING_SNAKE_CASE_: int , SCREAMING_SNAKE_CASE_: int , SCREAMING_SNAKE_CASE_: set ) -> int: '''simple docstring''' A__ , A__ = len(SCREAMING_SNAKE_CASE_ ), len(grid[0] ) if ( min(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(SCREAMING_SNAKE_CASE_ , row + 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , row - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , col + 1 , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , col - 1 , SCREAMING_SNAKE_CASE_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
68
'''simple docstring''' def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: # Base Case if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] , __UpperCamelCase , __UpperCamelCase ): # Color current vertex UpperCamelCase = i # Validate coloring if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , index + 1 ): return True # Backtrack UpperCamelCase = -1 return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> list[int]: UpperCamelCase = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , 0 ): return colored_vertices return []
321
0
"""simple docstring""" from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
69
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 2000000 )-> int: UpperCamelCase = [0 for i in range(n + 1 )] UpperCamelCase = 1 UpperCamelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __UpperCamelCase ): UpperCamelCase = 1 UpperCamelCase = 0 for i in range(__UpperCamelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging A__ : Optional[Any] =logging.get_logger(__name__) A__ : int ={ '''EleutherAI/gpt-j-6B''': '''https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json''', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class UpperCAmelCase ( snake_case_ ): _lowercase: List[str] = '''gptj''' _lowercase: List[Any] = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : List[str] , __snake_case : Optional[Any]=5_04_00 , __snake_case : Tuple=20_48 , __snake_case : Optional[int]=40_96 , __snake_case : Tuple=28 , __snake_case : Optional[int]=16 , __snake_case : Optional[int]=64 , __snake_case : Optional[int]=None , __snake_case : Optional[Any]="gelu_new" , __snake_case : Union[str, Any]=0.0 , __snake_case : Any=0.0 , __snake_case : List[str]=0.0 , __snake_case : str=1E-5 , __snake_case : Tuple=0.02 , __snake_case : Optional[Any]=True , __snake_case : Union[str, Any]=5_02_56 , __snake_case : int=5_02_56 , __snake_case : Any=False , **__snake_case : Optional[int] , ) -> Any: _lowerCAmelCase = vocab_size _lowerCAmelCase = n_positions _lowerCAmelCase = n_embd _lowerCAmelCase = n_layer _lowerCAmelCase = n_head _lowerCAmelCase = n_inner _lowerCAmelCase = rotary_dim _lowerCAmelCase = activation_function _lowerCAmelCase = resid_pdrop _lowerCAmelCase = embd_pdrop _lowerCAmelCase = attn_pdrop _lowerCAmelCase = layer_norm_epsilon _lowerCAmelCase = initializer_range _lowerCAmelCase = use_cache _lowerCAmelCase = bos_token_id _lowerCAmelCase = eos_token_id super().__init__( bos_token_id=__snake_case , eos_token_id=__snake_case , tie_word_embeddings=__snake_case , **__snake_case ) class UpperCAmelCase ( snake_case_ ): def __init__( self : List[Any] , __snake_case : PretrainedConfig , __snake_case : str = "default" , __snake_case : List[PatchingSpec] = None , __snake_case : bool = False , ) -> Optional[int]: super().__init__(__snake_case , task=__snake_case , patching_specs=__snake_case , use_past=__snake_case ) if not getattr(self._config , """pad_token_id""" , __snake_case ): # TODO: how to do that better? _lowerCAmelCase = 0 @property def lowercase__ ( self : Dict ) -> Mapping[str, Mapping[int, str]]: _lowerCAmelCase = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(__snake_case , direction="""inputs""" ) _lowerCAmelCase = {0: """batch""", 1: """past_sequence + sequence"""} else: _lowerCAmelCase = {0: """batch""", 1: """sequence"""} return common_inputs @property def lowercase__ ( self : Union[str, Any] ) -> int: return self._config.n_layer @property def lowercase__ ( self : Dict ) -> int: return self._config.n_head def lowercase__ ( self : Dict , __snake_case : PreTrainedTokenizer , __snake_case : int = -1 , __snake_case : int = -1 , __snake_case : bool = False , __snake_case : Optional[TensorType] = None , ) -> Mapping[str, Any]: _lowerCAmelCase = super(__snake_case , self ).generate_dummy_inputs( __snake_case , batch_size=__snake_case , seq_length=__snake_case , is_pair=__snake_case , framework=__snake_case ) # We need to order the input in the way they appears in the forward() _lowerCAmelCase = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch _lowerCAmelCase , _lowerCAmelCase = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values _lowerCAmelCase = seqlen + 2 _lowerCAmelCase = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) _lowerCAmelCase = [ (torch.zeros(__snake_case ), torch.zeros(__snake_case )) for _ in range(self.num_layers ) ] _lowerCAmelCase = common_inputs["""attention_mask"""] if self.use_past: _lowerCAmelCase = ordered_inputs["""attention_mask"""].dtype _lowerCAmelCase = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(__snake_case , __snake_case , dtype=__snake_case )] , dim=1 ) return ordered_inputs @property def lowercase__ ( self : Optional[int] ) -> int: return 13
70
'''simple docstring''' from timeit import timeit def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: number &= number - 1 result += 1 return result def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowercase__ ( )-> None: def do_benchmark(__UpperCamelCase ) -> None: UpperCamelCase = """import __main__ as z""" print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) UpperCamelCase = timeit("""z.get_set_bits_count_using_modulo_operator(25)""" , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) UpperCamelCase = timeit( """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""" , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
321
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available A_ :Optional[int] = { '''configuration_longt5''': ['''LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LongT5Config''', '''LongT5OnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :str = [ '''LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LongT5EncoderModel''', '''LongT5ForConditionalGeneration''', '''LongT5Model''', '''LongT5PreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :str = [ '''FlaxLongT5ForConditionalGeneration''', '''FlaxLongT5Model''', '''FlaxLongT5PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys A_ :Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
0
"""simple docstring""" import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def snake_case_ ( A_ : Tuple, A_ : int, A_ : Dict ): '''simple docstring''' _lowerCamelCase : Union[str, Any] = LxmertConfig.from_json_file(A_ ) print(F'''Building PyTorch model from configuration: {config}''' ) _lowerCamelCase : List[str] = LxmertForPreTraining(A_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(A_, A_, A_ ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict(), A_ ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCAmelCase__ = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
72
'''simple docstring''' import math def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
321
0
def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> float: if discount_rate < 0: raise ValueError('Discount rate cannot be negative' ) if not cash_flows: raise ValueError('Cash flows list cannot be empty' ) __lowerCamelCase : int = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(lowerCamelCase__ ) ) return round(lowerCamelCase__ , ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
73
'''simple docstring''' import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE__ = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE__ = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' SCREAMING_SNAKE_CASE__ = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://www.atticusprojectai.org/cuad"""] , reference_urls=["""https://www.atticusprojectai.org/cuad"""] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} UpperCamelCase = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] UpperCamelCase = evaluate(dataset=_SCREAMING_SNAKE_CASE , predictions=_SCREAMING_SNAKE_CASE ) return score
321
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _lowercase = logging.get_logger(__name__) _lowercase = { '''microsoft/swinv2-tiny-patch4-window8-256''': ( '''https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json''' ), } class lowerCAmelCase_ ( _lowercase ): '''simple docstring''' _lowerCamelCase: Tuple = '''swinv2''' _lowerCamelCase: Optional[int] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Optional[Any] ,A_ : str=224 ,A_ : Union[str, Any]=4 ,A_ : Union[str, Any]=3 ,A_ : Union[str, Any]=96 ,A_ : Optional[int]=[2, 2, 6, 2] ,A_ : Any=[3, 6, 12, 24] ,A_ : Union[str, Any]=7 ,A_ : str=4.0 ,A_ : int=True ,A_ : str=0.0 ,A_ : List[str]=0.0 ,A_ : str=0.1 ,A_ : List[Any]="gelu" ,A_ : Tuple=False ,A_ : Dict=0.02 ,A_ : Union[str, Any]=1e-5 ,A_ : int=32 ,**A_ : int ,) -> Any: super().__init__(**A_ ) A = image_size A = patch_size A = num_channels A = embed_dim A = depths A = len(A_ ) A = num_heads A = window_size A = mlp_ratio A = qkv_bias A = hidden_dropout_prob A = attention_probs_dropout_prob A = drop_path_rate A = hidden_act A = use_absolute_embeddings A = layer_norm_eps A = initializer_range A = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A = int(embed_dim * 2 ** (len(A_ ) - 1) ) A = (0, 0, 0, 0)
74
'''simple docstring''' def lowercase__ ( __UpperCamelCase )-> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase__ ( __UpperCamelCase = 1000000 )-> int: UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__UpperCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument a_ : Optional[Any] = { """/attention/""": """/0/SelfAttention/""", """/self_attention/""": """/0/SelfAttention/""", """/encoder_decoder_attention/""": """/1/EncDecAttention/""", """value""": """v""", """query""": """q""", """key""": """k""", """out""": """o""", """pre_self_attention_layer_norm""": """0/layer_norm""", """pre_cross_attention_layer_norm""": """1/layer_norm""", """pre_attention_layer_norm""": """0/layer_norm""", # previously 1, but seems wrong """token_embedder""": """shared""", """encoder_norm""": """final_layer_norm""", """decoder_norm""": """final_layer_norm""", """relpos_bias/rel_embedding""": """block/0/layer/0/SelfAttention/relative_attention_bias/weight""", """router/router_weights/w/""": """router/classifier/""", """roer/roer_weights/w/""": """router/classifier/""", """logits_dense""": """lm_head""", } def a_ ( __snake_case : List[Any] ) -> Tuple: """simple docstring""" # 1. in HF T5, we have block.{x}.layer.{y}. which corresponds to layer.{x} in # the original model lowerCamelCase_ =list(s_dict.keys() ) for key in keys: lowerCamelCase_ =r'''.*/layers_(\d+)''' lowerCamelCase_ =key if re.match(__snake_case , __snake_case ): lowerCamelCase_ =re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __snake_case ) lowerCamelCase_ =r'''(encoder|decoder)\/''' if re.match(__snake_case , __snake_case ): lowerCamelCase_ =re.match(__snake_case , __snake_case ).groups() if groups[0] == "encoder": lowerCamelCase_ =re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __snake_case ) lowerCamelCase_ =re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __snake_case ) elif groups[0] == "decoder": lowerCamelCase_ =re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __snake_case ) lowerCamelCase_ =re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __snake_case ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: lowerCamelCase_ =new_key.replace(__snake_case , __snake_case ) print(F'''{key} -> {new_key}''' ) lowerCamelCase_ =s_dict.pop(__snake_case ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: lowerCamelCase_ =s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: lowerCamelCase_ =s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: lowerCamelCase_ =s_dict[key].shape[0] lowerCamelCase_ =s_dict[key] for idx in range(__snake_case ): lowerCamelCase_ =expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring' )}''' ) s_dict.pop(__snake_case ) return s_dict a_ : Tuple = { """NUM_ENCODER_LAYERS""": """num_layers""", """NUM_DECODER_LAYERS""": """num_decoder_layers""", """NUM_HEADS""": """num_heads""", """HEAD_DIM""": """d_kv""", """EMBED_DIM""": """d_model""", """MLP_DIM""": """d_ff""", """NUM_SELECTED_EXPERTS""": """num_selected_experts""", """NUM_ENCODER_SPARSE_LAYERS""": """num_sparse_encoder_layers""", """NUM_DECODER_SPARSE_LAYERS""": """num_sparse_decoder_layers""", """dense.MlpBlock.activations""": """feed_forward_proj""", } def a_ ( __snake_case : List[Any] , __snake_case : Tuple ) -> Optional[Any]: """simple docstring""" # Convert a google style config to the hugging face fromat import regex as re with open(__snake_case , '''r''' ) as f: lowerCamelCase_ =f.read() lowerCamelCase_ =re.findall(r'''(.*) = ([0-9.]*)''' , __snake_case ) lowerCamelCase_ ={} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": lowerCamelCase_ =float(__snake_case ) if '''.''' in value else int(__snake_case ) lowerCamelCase_ =re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __snake_case )[0] lowerCamelCase_ =str(activation[1] ) lowerCamelCase_ =num_experts lowerCamelCase_ =SwitchTransformersConfig(**__snake_case ) return config def a_ ( __snake_case : Dict , __snake_case : Any , __snake_case : List[str]=None , __snake_case : Any="./" , __snake_case : int=8 ) -> Optional[Any]: """simple docstring""" # Initialise PyTorch model print(F'''Loading flax weights from : {flax_checkpoint_path}''' ) lowerCamelCase_ =checkpoints.load_tax_checkpoint(__snake_case ) if gin_file is not None: lowerCamelCase_ =convert_gin_to_config(__snake_case , __snake_case ) else: lowerCamelCase_ =SwitchTransformersConfig.from_pretrained(__snake_case ) lowerCamelCase_ =SwitchTransformersForConditionalGeneration(__snake_case ) lowerCamelCase_ =flax_params['''target'''] lowerCamelCase_ =flatten_dict(__snake_case , sep='''/''' ) lowerCamelCase_ =rename_keys(__snake_case ) lowerCamelCase_ =unflatten_dict(__snake_case , sep='''/''' ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__snake_case , __snake_case ) print(F'''Save PyTorch model to {pytorch_dump_path}''' ) pt_model.save_pretrained(__snake_case ) if __name__ == "__main__": a_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--switch_t5x_checkpoint_path""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the""" """ model architecture. If not provided, a `gin_file` has to be provided.""" ), ) parser.add_argument( """--gin_file""", default=None, type=str, required=False, help="""Path to the gin config file. If not provided, a `config_file` has to be passed """, ) parser.add_argument( """--config_name""", default=None, type=str, required=False, help="""Config name of SwitchTransformers model.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output pytorch model.""" ) parser.add_argument("""--num_experts""", default=8, type=int, required=False, help="""Number of experts""") a_ : int = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
75
'''simple docstring''' from __future__ import annotations from math import pow, sqrt def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if resistance == 0: return {"resistance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(__UpperCamelCase , 2 ) + pow(__UpperCamelCase , 2 ) )} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
321
0
from functools import lru_cache def lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : str = 2 SCREAMING_SNAKE_CASE : Optional[Any] = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(_a) if n > 1: factors.add(_a) return factors @lru_cache def lowerCamelCase__ ( _a): return len(unique_prime_factors(_a)) def lowerCamelCase__ ( _a): return len(set(_a)) in (0, 1) def lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : Any = 2 while True: # Increment each value of a generated range SCREAMING_SNAKE_CASE : Union[str, Any] = [base + i for i in range(_a)] # Run elements through out unique_prime_factors function # Append our target number to the end. SCREAMING_SNAKE_CASE : List[str] = [upf_len(_a) for x in group] checker.append(_a) # If all numbers in the list are equal, return the group variable. if equality(_a): return group # Increment our base variable by 1 base += 1 def lowerCamelCase__ ( _a = 4): SCREAMING_SNAKE_CASE : Tuple = run(_a) return results[0] if len(_a) else None if __name__ == "__main__": print(solution())
76
'''simple docstring''' # Algorithm for the pigeonhole sorting def lowercase__ ( __UpperCamelCase )-> Union[str, Any]: UpperCamelCase = min(__UpperCamelCase ) # min() finds the minimum value UpperCamelCase = max(__UpperCamelCase ) # max() finds the maximum value UpperCamelCase = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size UpperCamelCase = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(__UpperCamelCase , __UpperCamelCase ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. UpperCamelCase = 0 for count in range(__UpperCamelCase ): while holes[count] > 0: holes[count] -= 1 UpperCamelCase = count + min_val i += 1 def lowercase__ ( )-> Any: UpperCamelCase = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(__UpperCamelCase ) print("""Sorted order is:""" , """ """.join(__UpperCamelCase ) ) if __name__ == "__main__": main()
321
0
"""simple docstring""" def a_ ( _lowerCAmelCase : str , _lowerCAmelCase : str ): '''simple docstring''' lowercase__ : List[str] = len(_lowerCAmelCase ) + 1 lowercase__ : Any = len(_lowerCAmelCase ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. lowercase__ : List[str] = [[0 for i in range(_lowerCAmelCase )] for j in range(_lowerCAmelCase )] # since string of zero length match pattern of zero length lowercase__ : Any = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , _lowerCAmelCase ): lowercase__ : Tuple = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , _lowerCAmelCase ): lowercase__ : Union[str, Any] = dp[0][j - 2] if pattern[j - 1] == '*' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , _lowerCAmelCase ): for j in range(1 , _lowerCAmelCase ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": lowercase__ : List[Any] = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: lowercase__ : Union[str, Any] = 1 elif pattern[j - 2] in (input_string[i - 1], "."): lowercase__ : Tuple = dp[i - 1][j] else: lowercase__ : Tuple = 0 else: lowercase__ : List[Any] = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") _UpperCamelCase : Any = "aab" _UpperCamelCase : int = "c*a*b" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
77
'''simple docstring''' import torch from diffusers import DDPMParallelScheduler from .test_schedulers import SchedulerCommonTest class a_ ( lowerCamelCase ): lowercase = (DDPMParallelScheduler,) def A__ ( self , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = { """num_train_timesteps""": 1000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """variance_type""": """fixed_small""", """clip_sample""": True, } config.update(**_SCREAMING_SNAKE_CASE ) return config def A__ ( self ) -> List[str]: """simple docstring""" for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[int]: """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE , beta_end=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Tuple: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> List[Any]: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> str: """simple docstring""" self.check_over_configs(thresholding=_SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_SCREAMING_SNAKE_CASE , prediction_type=_SCREAMING_SNAKE_CASE , sample_max_value=_SCREAMING_SNAKE_CASE , ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Union[str, Any]: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> int: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def A__ ( self ) -> Optional[int]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = self.dummy_sample_deter + 0.1 UpperCamelCase = self.dummy_sample_deter - 0.1 UpperCamelCase = samplea.shape[0] UpperCamelCase = torch.stack([samplea, samplea, samplea] , dim=0 ) UpperCamelCase = torch.arange(_SCREAMING_SNAKE_CASE )[0:3, None].repeat(1 , _SCREAMING_SNAKE_CASE ) UpperCamelCase = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) UpperCamelCase = scheduler.batch_step_no_noise(_SCREAMING_SNAKE_CASE , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) ) UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 1_1_5_3.1_8_3_3 ) < 1e-2 assert abs(result_mean.item() - 0.5_0_0_5 ) < 1e-3 def A__ ( self ) -> List[str]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config(prediction_type="""v_prediction""" ) UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) UpperCamelCase = scheduler.timesteps for i, timestep in enumerate(_SCREAMING_SNAKE_CASE ): if i == len(_SCREAMING_SNAKE_CASE ) - 1: UpperCamelCase = -1 else: UpperCamelCase = timesteps[i + 1] UpperCamelCase = scheduler.previous_timestep(_SCREAMING_SNAKE_CASE ) UpperCamelCase = prev_t.item() self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 51, 0] with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""`custom_timesteps` must be in descending order.""" ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""Can only pass one of `num_inference_steps` or `custom_timesteps`.""" ): scheduler.set_timesteps(num_inference_steps=_SCREAMING_SNAKE_CASE , timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( _SCREAMING_SNAKE_CASE , msg="""`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}""" , ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available snake_case_ = { """configuration_xlm""": ["""XLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMConfig""", """XLMOnnxConfig"""], """tokenization_xlm""": ["""XLMTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = [ """XLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """XLMForMultipleChoice""", """XLMForQuestionAnswering""", """XLMForQuestionAnsweringSimple""", """XLMForSequenceClassification""", """XLMForTokenClassification""", """XLMModel""", """XLMPreTrainedModel""", """XLMWithLMHeadModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = [ """TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFXLMForMultipleChoice""", """TFXLMForQuestionAnsweringSimple""", """TFXLMForSequenceClassification""", """TFXLMForTokenClassification""", """TFXLMMainLayer""", """TFXLMModel""", """TFXLMPreTrainedModel""", """TFXLMWithLMHeadModel""", ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys snake_case_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
78
'''simple docstring''' from __future__ import annotations import math class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = size # approximate the overall size of segment tree with given value UpperCamelCase = [0 for i in range(0 , 4 * size )] # create array to store lazy update UpperCamelCase = [0 for i in range(0 , 4 * size )] UpperCamelCase = [0 for i in range(0 , 4 * size )] # flag for lazy update def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 + 1 def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" if left_element == right_element: UpperCamelCase = a[left_element - 1] else: UpperCamelCase = (left_element + right_element) // 2 self.build(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.build(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: UpperCamelCase = val if left_element != right_element: UpperCamelCase = val UpperCamelCase = val UpperCamelCase = True UpperCamelCase = True return True UpperCamelCase = (left_element + right_element) // 2 self.update(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.update(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) return True def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | float: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] UpperCamelCase = (left_element + right_element) // 2 UpperCamelCase = self.query(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = self.query(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __str__( self ) -> str: """simple docstring""" return str([self.query(1 , 1 , self.size , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8] SCREAMING_SNAKE_CASE__ = 1_5 SCREAMING_SNAKE_CASE__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 1_1)) print(segt.query(1, 1, size, 7, 1_2)) segt.update(1, 1, size, 1, 3, 1_1_1) print(segt.query(1, 1, size, 1, 1_5)) segt.update(1, 1, size, 7, 8, 2_3_5) print(segt)
321
0
'''simple docstring''' import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = {'''vocab_file''': '''spiece.model'''} lowerCamelCase_ = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', } } lowerCamelCase_ = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } # Segments (not really needed) lowerCamelCase_ = 0 lowerCamelCase_ = 1 lowerCamelCase_ = 2 lowerCamelCase_ = 3 lowerCamelCase_ = 4 class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = VOCAB_FILES_NAMES snake_case = PRETRAINED_VOCAB_FILES_MAP snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case = '''left''' def __init__( self : Any , __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple=False , __UpperCAmelCase : Optional[Any]=True , __UpperCAmelCase : List[Any]=False , __UpperCAmelCase : List[Any]="<s>" , __UpperCAmelCase : Optional[Any]="</s>" , __UpperCAmelCase : Dict="<unk>" , __UpperCAmelCase : Tuple="<sep>" , __UpperCAmelCase : List[str]="<pad>" , __UpperCAmelCase : int="<cls>" , __UpperCAmelCase : Dict="<mask>" , __UpperCAmelCase : Optional[Any]=["<eop>", "<eod>"] , __UpperCAmelCase : Optional[Dict[str, Any]] = None , **__UpperCAmelCase : List[Any] , ): '''simple docstring''' _A = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token _A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__UpperCAmelCase , remove_space=__UpperCAmelCase , keep_accents=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) _A = 3 _A = do_lower_case _A = remove_space _A = keep_accents _A = vocab_file _A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__UpperCAmelCase ) @property def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' return len(self.sp_model ) def lowerCAmelCase ( self : int ): '''simple docstring''' _A = {self.convert_ids_to_tokens(__UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ): '''simple docstring''' _A = self.__dict__.copy() _A = None return state def __setstate__( self : Any , __UpperCAmelCase : str ): '''simple docstring''' _A = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): _A = {} _A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[int] ): '''simple docstring''' if self.remove_space: _A = " ".join(inputs.strip().split() ) else: _A = inputs _A = outputs.replace("``" , "\"" ).replace("''" , "\"" ) if not self.keep_accents: _A = unicodedata.normalize("NFKD" , __UpperCAmelCase ) _A = "".join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase )] ) if self.do_lower_case: _A = outputs.lower() return outputs def lowerCAmelCase ( self : str , __UpperCAmelCase : str ): '''simple docstring''' _A = self.preprocess_text(__UpperCAmelCase ) _A = self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) _A = [] for piece in pieces: if len(__UpperCAmelCase ) > 1 and piece[-1] == str("," ) and piece[-2].isdigit(): _A = self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase , "" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _A = cur_pieces[1:] else: _A = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__UpperCAmelCase ) else: new_pieces.append(__UpperCAmelCase ) return new_pieces def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[int] ): '''simple docstring''' return self.sp_model.PieceToId(__UpperCAmelCase ) def lowerCAmelCase ( self : List[str] , __UpperCAmelCase : List[Any] ): '''simple docstring''' return self.sp_model.IdToPiece(__UpperCAmelCase ) def lowerCAmelCase ( self : str , __UpperCAmelCase : Dict ): '''simple docstring''' _A = "".join(__UpperCAmelCase ).replace(__UpperCAmelCase , " " ).strip() return out_string def lowerCAmelCase ( self : int , __UpperCAmelCase : List[int] , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = None , __UpperCAmelCase : bool = True , **__UpperCAmelCase : List[Any] , ): '''simple docstring''' _A = kwargs.pop("use_source_tokenizer" , __UpperCAmelCase ) _A = self.convert_ids_to_tokens(__UpperCAmelCase , skip_special_tokens=__UpperCAmelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 _A = [] _A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__UpperCAmelCase ) ) _A = [] sub_texts.append(__UpperCAmelCase ) else: current_sub_text.append(__UpperCAmelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__UpperCAmelCase ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens _A = "".join(__UpperCAmelCase ) _A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: _A = self.clean_up_tokenization(__UpperCAmelCase ) return clean_text else: return text def lowerCAmelCase ( self : Dict , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ): '''simple docstring''' _A = [self.sep_token_id] _A = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def lowerCAmelCase ( self : Any , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) if token_ids_a is not None: return ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1, 1] return ([0] * len(__UpperCAmelCase )) + [1, 1] def lowerCAmelCase ( self : Any , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ): '''simple docstring''' _A = [self.sep_token_id] _A = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(__UpperCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _A = os.path.join( __UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCAmelCase , "wb" ) as fi: _A = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (out_vocab_file,)
79
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 1000 )-> int: UpperCamelCase = -1 UpperCamelCase = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c UpperCamelCase = (n * n - 2 * a * n) // (2 * n - 2 * a) UpperCamelCase = n - a - b if c * c == (a * a + b * b): UpperCamelCase = a * b * c if candidate >= product: UpperCamelCase = candidate return product if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' 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 a__ : Any = logging.get_logger(__name__) a__ : Union[str, Any] = '▁' a__ : Tuple = {'vocab_file': 'spiece.model'} a__ : Optional[Any] = { 'vocab_file': { 'google/reformer-crime-and-punishment': ( 'https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model' ) } } a__ : List[Any] = { 'google/reformer-crime-and-punishment': 5_2_4_2_8_8, } class lowercase_ ( a__ ): __UpperCAmelCase = VOCAB_FILES_NAMES __UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase = ['input_ids', 'attention_mask'] def __init__( self , a , a="</s>" , a="<unk>" , a=[] , a = None , **a , ): UpperCamelCase__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=a , unk_token=a , additional_special_tokens=a , sp_model_kwargs=self.sp_model_kwargs , **a , ) UpperCamelCase__ = vocab_file UpperCamelCase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(a ) @property def __a ( self ): return self.sp_model.get_piece_size() def __a ( self ): UpperCamelCase__ = {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 ): UpperCamelCase__ = self.__dict__.copy() UpperCamelCase__ = None return state def __setstate__( self , a ): UpperCamelCase__ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): UpperCamelCase__ = {} UpperCamelCase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __a ( self , a ): return self.sp_model.encode(a , out_type=a ) def __a ( self , a ): return self.sp_model.piece_to_id(a ) def __a ( self , a ): if index < self.sp_model.get_piece_size(): UpperCamelCase__ = self.sp_model.IdToPiece(a ) return token def __a ( self , a ): UpperCamelCase__ = [] UpperCamelCase__ = "" 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 UpperCamelCase__ = [] else: current_sub_tokens.append(a ) out_string += self.sp_model.decode(a ) return out_string.strip() def __a ( self , a , a = None ): if not os.path.isdir(a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase__ = 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: UpperCamelCase__ = self.sp_model.serialized_model_proto() fi.write(a ) return (out_vocab_file,)
80
'''simple docstring''' import argparse import struct import unittest class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = data # Initialize hash values UpperCamelCase = [ 0x6A_09_E6_67, 0xBB_67_AE_85, 0x3C_6E_F3_72, 0xA5_4F_F5_3A, 0x51_0E_52_7F, 0x9B_05_68_8C, 0x1F_83_D9_AB, 0x5B_E0_CD_19, ] # Initialize round constants UpperCamelCase = [ 0x42_8A_2F_98, 0x71_37_44_91, 0xB5_C0_FB_CF, 0xE9_B5_DB_A5, 0x39_56_C2_5B, 0x59_F1_11_F1, 0x92_3F_82_A4, 0xAB_1C_5E_D5, 0xD8_07_AA_98, 0x12_83_5B_01, 0x24_31_85_BE, 0x55_0C_7D_C3, 0x72_BE_5D_74, 0x80_DE_B1_FE, 0x9B_DC_06_A7, 0xC1_9B_F1_74, 0xE4_9B_69_C1, 0xEF_BE_47_86, 0x0F_C1_9D_C6, 0x24_0C_A1_CC, 0x2D_E9_2C_6F, 0x4A_74_84_AA, 0x5C_B0_A9_DC, 0x76_F9_88_DA, 0x98_3E_51_52, 0xA8_31_C6_6D, 0xB0_03_27_C8, 0xBF_59_7F_C7, 0xC6_E0_0B_F3, 0xD5_A7_91_47, 0x06_CA_63_51, 0x14_29_29_67, 0x27_B7_0A_85, 0x2E_1B_21_38, 0x4D_2C_6D_FC, 0x53_38_0D_13, 0x65_0A_73_54, 0x76_6A_0A_BB, 0x81_C2_C9_2E, 0x92_72_2C_85, 0xA2_BF_E8_A1, 0xA8_1A_66_4B, 0xC2_4B_8B_70, 0xC7_6C_51_A3, 0xD1_92_E8_19, 0xD6_99_06_24, 0xF4_0E_35_85, 0x10_6A_A0_70, 0x19_A4_C1_16, 0x1E_37_6C_08, 0x27_48_77_4C, 0x34_B0_BC_B5, 0x39_1C_0C_B3, 0x4E_D8_AA_4A, 0x5B_9C_CA_4F, 0x68_2E_6F_F3, 0x74_8F_82_EE, 0x78_A5_63_6F, 0x84_C8_78_14, 0x8C_C7_02_08, 0x90_BE_FF_FA, 0xA4_50_6C_EB, 0xBE_F9_A3_F7, 0xC6_71_78_F2, ] UpperCamelCase = self.preprocessing(self.data ) self.final_hash() @staticmethod def A__ ( _SCREAMING_SNAKE_CASE ) -> bytes: """simple docstring""" UpperCamelCase = B"""\x80""" + (B"""\x00""" * (63 - (len(_SCREAMING_SNAKE_CASE ) + 8) % 64)) UpperCamelCase = struct.pack(""">Q""" , (len(_SCREAMING_SNAKE_CASE ) * 8) ) return data + padding + big_endian_integer def A__ ( self ) -> None: """simple docstring""" UpperCamelCase = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers UpperCamelCase = list(struct.unpack(""">16L""" , _SCREAMING_SNAKE_CASE ) ) # add 48 0-ed integers words += [0] * 48 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array UpperCamelCase = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) UpperCamelCase = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) UpperCamelCase = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x1_00_00_00_00 # Compression UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 6 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 11 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 25 ) UpperCamelCase = (e & f) ^ ((~e & 0xFF_FF_FF_FF) & g) UpperCamelCase = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x1_00_00_00_00 UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 2 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 13 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 22 ) UpperCamelCase = (a & b) ^ (a & c) ^ (b & c) UpperCamelCase = (sa + maj) % 0x1_00_00_00_00 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = ( g, f, e, ((d + tempa) % 0x1_00_00_00_00), c, b, a, ((tempa + tempa) % 0x1_00_00_00_00), ) UpperCamelCase = [a, b, c, d, e, f, g, h] # Modify final values UpperCamelCase = [ ((element + mutated_hash_values[index]) % 0x1_00_00_00_00) for index, element in enumerate(self.hashes ) ] UpperCamelCase = """""".join([hex(_SCREAMING_SNAKE_CASE )[2:].zfill(8 ) for value in self.hashes] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return 0xFF_FF_FF_FF & (value << (32 - rotations)) | (value >> rotations) class a_ ( unittest.TestCase ): def A__ ( self ) -> None: """simple docstring""" import hashlib UpperCamelCase = bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(_SCREAMING_SNAKE_CASE ).hash , hashlib.shaaaa(_SCREAMING_SNAKE_CASE ).hexdigest() ) def lowercase__ ( )-> None: import doctest doctest.testmod() UpperCamelCase = argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: UpperCamelCase = f.read() else: UpperCamelCase = bytes(__UpperCamelCase , """utf-8""" ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
321
0
"""simple docstring""" import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore lowerCamelCase_ : Union[str, Any] = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" lowerCamelCase_ : Dict = [file for file in filepaths if file != file.lower()] if upper_files: print(F'{len(upper_files)} files contain uppercase characters:') print("""\n""".join(upper_files) + """\n""") lowerCamelCase_ : Any = [file for file in filepaths if """ """ in file] if space_files: print(F'{len(space_files)} files contain space characters:') print("""\n""".join(space_files) + """\n""") lowerCamelCase_ : Dict = [file for file in filepaths if """-""" in file] if hyphen_files: print(F'{len(hyphen_files)} files contain hyphen characters:') print("""\n""".join(hyphen_files) + """\n""") lowerCamelCase_ : Union[str, Any] = [file for file in filepaths if os.sep not in file] if nodir_files: print(F'{len(nodir_files)} files are not in a directory:') print("""\n""".join(nodir_files) + """\n""") lowerCamelCase_ : Optional[int] = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
81
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) SCREAMING_SNAKE_CASE__ = _symbol_database.Default() SCREAMING_SNAKE_CASE__ = _descriptor_pool.Default().AddSerializedFile( b'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03' ) SCREAMING_SNAKE_CASE__ = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS is False: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = b'H\003' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" SCREAMING_SNAKE_CASE__ = 4_5 SCREAMING_SNAKE_CASE__ = 1_5_8_1 SCREAMING_SNAKE_CASE__ = 1_5_1_7 SCREAMING_SNAKE_CASE__ = 1_5_7_0 SCREAMING_SNAKE_CASE__ = 1_5_8_4 SCREAMING_SNAKE_CASE__ = 1_7_9_3 SCREAMING_SNAKE_CASE__ = 1_7_9_5 SCREAMING_SNAKE_CASE__ = 1_9_1_6 SCREAMING_SNAKE_CASE__ = 1_8_6_4 SCREAMING_SNAKE_CASE__ = 1_9_0_5 SCREAMING_SNAKE_CASE__ = 1_9_1_9 SCREAMING_SNAKE_CASE__ = 2_4_2_9 SCREAMING_SNAKE_CASE__ = 2_2_0_8 SCREAMING_SNAKE_CASE__ = 2_4_1_8 SCREAMING_SNAKE_CASE__ = 2_3_2_3 SCREAMING_SNAKE_CASE__ = 2_4_0_7 # @@protoc_insertion_point(module_scope)
321
0
import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __lowerCAmelCase ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase = '''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline''' def snake_case ( self , _snake_case=0 ): """simple docstring""" _lowerCAmelCase = floats_tensor((1, 3, 128, 128) , rng=random.Random(_snake_case ) ) _lowerCAmelCase = np.random.RandomState(_snake_case ) _lowerCAmelCase = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """strength""": 0.75, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.6_9643, 0.5_8484, 0.5_0314, 0.5_8760, 0.5_5368, 0.5_9643, 0.5_1529, 0.4_1217, 0.4_9087] ) assert np.abs(image_slice - expected_slice ).max() < 1e-1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) _lowerCAmelCase = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.6_1737, 0.5_4642, 0.5_3183, 0.5_4465, 0.5_2742, 0.6_0525, 0.4_9969, 0.4_0655, 0.4_8154] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) _lowerCAmelCase = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) # warmup pass to apply optimizations _lowerCAmelCase = pipe(**self.get_dummy_inputs() ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.5_2761, 0.5_9977, 0.4_9033, 0.4_9619, 0.5_4282, 0.5_0311, 0.4_7600, 0.4_0918, 0.4_5203] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) _lowerCAmelCase = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) _lowerCAmelCase = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) _lowerCAmelCase = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = self.get_dummy_inputs() _lowerCAmelCase = pipe(**_snake_case ).images _lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) _lowerCAmelCase = np.array([0.6_5331, 0.5_8277, 0.4_8204, 0.5_6059, 0.5_3665, 0.5_6235, 0.5_0969, 0.4_0009, 0.4_6552] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 @nightly @require_onnxruntime @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): @property def snake_case ( self ): """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def snake_case ( self ): """simple docstring""" _lowerCAmelCase = ort.SessionOptions() _lowerCAmelCase = False return options def snake_case ( self ): """simple docstring""" _lowerCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) _lowerCAmelCase = init_image.resize((768, 512) ) # using the PNDM scheduler by default _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""" , revision="""onnx""" , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = """A fantasy landscape, trending on artstation""" _lowerCAmelCase = np.random.RandomState(0 ) _lowerCAmelCase = pipe( prompt=_snake_case , image=_snake_case , strength=0.75 , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type="""np""" , ) _lowerCAmelCase = output.images _lowerCAmelCase = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) _lowerCAmelCase = np.array([0.4909, 0.5059, 0.5372, 0.4623, 0.4876, 0.5049, 0.4820, 0.4956, 0.5019] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def snake_case ( self ): """simple docstring""" _lowerCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) _lowerCAmelCase = init_image.resize((768, 512) ) _lowerCAmelCase = LMSDiscreteScheduler.from_pretrained( """runwayml/stable-diffusion-v1-5""" , subfolder="""scheduler""" , revision="""onnx""" ) _lowerCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" , revision="""onnx""" , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) _lowerCAmelCase = """A fantasy landscape, trending on artstation""" _lowerCAmelCase = np.random.RandomState(0 ) _lowerCAmelCase = pipe( prompt=_snake_case , image=_snake_case , strength=0.75 , guidance_scale=7.5 , num_inference_steps=20 , generator=_snake_case , output_type="""np""" , ) _lowerCAmelCase = output.images _lowerCAmelCase = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) _lowerCAmelCase = np.array([0.8043, 0.926, 0.9581, 0.8119, 0.8954, 0.913, 0.7209, 0.7463, 0.7431] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2
82
'''simple docstring''' SCREAMING_SNAKE_CASE__ = 8.31_44_62 # Unit - J mol-1 K-1 def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
321
0
'''simple docstring''' import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase__ ( lowercase , unittest.TestCase ): lowercase__ = MgpstrTokenizer lowercase__ = False lowercase__ = {} lowercase__ = False def UpperCamelCase_ ( self : List[str] ): '''simple docstring''' super().setUp() # fmt: off _UpperCamelCase : Optional[Any] = ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # fmt: on _UpperCamelCase : str = dict(zip(lowerCamelCase__ ,range(len(lowerCamelCase__ ) ) ) ) _UpperCamelCase : List[str] = 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(lowerCamelCase__ ) + '\n' ) def UpperCamelCase_ ( self : int ,**lowerCamelCase__ : str ): '''simple docstring''' return MgpstrTokenizer.from_pretrained(self.tmpdirname ,**lowerCamelCase__ ) def UpperCamelCase_ ( self : str ,lowerCamelCase__ : Any ): '''simple docstring''' _UpperCamelCase : Any = 'tester' _UpperCamelCase : Optional[int] = 'tester' return input_text, output_text @unittest.skip('MGP-STR always lower cases letters.' ) def UpperCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' pass def UpperCamelCase_ ( self : Optional[int] ): '''simple docstring''' _UpperCamelCase : List[Any] = self.get_tokenizers(do_lower_case=lowerCamelCase__ ) for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): _UpperCamelCase : Union[str, Any] = '[SPECIAL_TOKEN]' tokenizer.add_special_tokens({'cls_token': special_token} ) _UpperCamelCase : List[Any] = tokenizer.encode([special_token] ,add_special_tokens=lowerCamelCase__ ) self.assertEqual(len(lowerCamelCase__ ) ,1 ) _UpperCamelCase : Any = tokenizer.decode(lowerCamelCase__ ,skip_special_tokens=lowerCamelCase__ ) self.assertTrue(special_token not in decoded ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' _UpperCamelCase : Optional[int] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): _UpperCamelCase , _UpperCamelCase : List[Any] = self.get_input_output_texts(lowerCamelCase__ ) _UpperCamelCase : List[str] = tokenizer.tokenize(lowerCamelCase__ ) _UpperCamelCase : str = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) _UpperCamelCase : List[str] = tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) _UpperCamelCase : Optional[Any] = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertNotEqual(len(lowerCamelCase__ ) ,0 ) _UpperCamelCase : Dict = tokenizer.decode(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ ,lowerCamelCase__ ) self.assertEqual(text_a.replace(' ' ,'' ) ,lowerCamelCase__ ) @unittest.skip('MGP-STR tokenizer only handles one sequence.' ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' pass @unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer' ) def UpperCamelCase_ ( self : Any ): '''simple docstring''' pass
83
'''simple docstring''' import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem SCREAMING_SNAKE_CASE__ = importlib.util.find_spec('s3fs') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 SCREAMING_SNAKE_CASE__ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def lowercase__ ( __UpperCamelCase )-> str: if "://" in dataset_path: UpperCamelCase = dataset_path.split("""://""" )[1] return dataset_path def lowercase__ ( __UpperCamelCase )-> bool: if fs is not None and fs.protocol != "file": return True else: return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> int: UpperCamelCase = not is_remote_filesystem(__UpperCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(__UpperCamelCase ) , fs._strip_protocol(__UpperCamelCase ) ) else: fs.mv(__UpperCamelCase , __UpperCamelCase , recursive=__UpperCamelCase ) def lowercase__ ( )-> None: if hasattr(fsspec.asyn , """reset_lock""" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: UpperCamelCase = None UpperCamelCase = None UpperCamelCase = threading.Lock()
321
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __UpperCAmelCase = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( A__ ): def __init__( self , *__A , **__A ) -> None: warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , __A , ) super().__init__(*__A , **__A )
84
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_xlm_roberta_xl': [ 'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaXLConfig', 'XLMRobertaXLOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaXLForCausalLM', 'XLMRobertaXLForMaskedLM', 'XLMRobertaXLForMultipleChoice', 'XLMRobertaXLForQuestionAnswering', 'XLMRobertaXLForSequenceClassification', 'XLMRobertaXLForTokenClassification', 'XLMRobertaXLModel', 'XLMRobertaXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure)
321
0
'''simple docstring''' from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("socket.socket" ) @patch("builtins.open" ) def UpperCamelCase_( snake_case : Tuple , snake_case : Dict ): '''simple docstring''' snake_case_ = Mock() snake_case_ = conn, Mock() snake_case_ = iter([1, None] ) snake_case_ = lambda snake_case : next(snake_case ) # ===== invoke ===== send_file(filename="mytext.txt" , testing=snake_case ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
85
'''simple docstring''' import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = 'docs/source/en/_toctree.yml' def lowercase__ ( __UpperCamelCase )-> Optional[Any]: UpperCamelCase = defaultdict(__UpperCamelCase ) UpperCamelCase = [] UpperCamelCase = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__UpperCamelCase ) UpperCamelCase = new_doc_list UpperCamelCase = [key for key, value in counts.items() if value > 1] UpperCamelCase = [] for duplicate_key in duplicates: UpperCamelCase = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__UpperCamelCase ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) UpperCamelCase = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__UpperCamelCase ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__UpperCamelCase ) # Sort return overview_doc def lowercase__ ( __UpperCamelCase=False )-> List[str]: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 UpperCamelCase = api_doc[scheduler_idx]["""sections"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) UpperCamelCase = False if new_scheduler_doc != scheduler_doc: UpperCamelCase = True if overwrite: UpperCamelCase = new_scheduler_doc if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def lowercase__ ( __UpperCamelCase=False )-> Tuple: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 UpperCamelCase = False UpperCamelCase = api_doc[pipeline_idx]["""sections"""] UpperCamelCase = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: UpperCamelCase = pipeline_doc["""section"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if overwrite: UpperCamelCase = new_sub_pipeline_doc new_pipeline_docs.append(__UpperCamelCase ) # sort overall pipeline doc UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if new_pipeline_docs != pipeline_docs: UpperCamelCase = True if overwrite: UpperCamelCase = new_pipeline_docs if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) 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__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
321
0
"""simple docstring""" import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = torch.device("""cpu""") def __lowerCAmelCase (): __lowerCAmelCase : List[Any] = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCAmelCase : Dict = Image.open(requests.get(_UpperCamelCase , stream=_UpperCamelCase ).raw ) return im def __lowerCAmelCase (_UpperCamelCase ): if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0, 8.8_6_8_5e-0_1, 2.4_3_6_0e-0_1] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_6_3_6e-0_1, 2.3_4_7_8e-0_1, -1.6_9_6_3e0_0, -1.7_3_8_1e0_0, -8.6_3_3_7e-0_1] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_7_6_8e-0_1, -4.7_4_2_9e-0_1, -1.0_8_9_7e0_0, -1.0_2_4_8e0_0, 3.5_5_2_3e-0_2] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_3_3_0e-0_1, 2.4_2_1_1e-0_1, -6.0_1_8_5e-0_1, -8.2_7_8_9e-0_1, -6.0_4_4_6e-0_2] ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : Tuple = dct.pop(_UpperCamelCase ) __lowerCAmelCase : Dict = val def __lowerCAmelCase (_UpperCamelCase ): __lowerCAmelCase : Optional[Any] = [] for k in state_dict.keys(): __lowerCAmelCase : str = k if ".pwconv" in k: __lowerCAmelCase : List[str] = k_new.replace('.pwconv' , '.point_wise_conv' ) if ".dwconv" in k: __lowerCAmelCase : Tuple = k_new.replace('.dwconv' , '.depth_wise_conv' ) if ".Proj." in k: __lowerCAmelCase : Optional[int] = k_new.replace('.Proj.' , '.proj.' ) if "patch_embed" in k_new: __lowerCAmelCase : List[Any] = k_new.replace('patch_embed' , 'swiftformer.patch_embed.patch_embedding' ) if "network" in k_new: __lowerCAmelCase : Optional[Any] = k_new.split('.' ) if ls[2].isdigit(): __lowerCAmelCase : Union[str, Any] = 'swiftformer.encoder.network.' + ls[1] + '.blocks.' + ls[2] + '.' + '.'.join(ls[3:] ) else: __lowerCAmelCase : int = k_new.replace('network' , 'swiftformer.encoder.network' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : Tuple = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __lowerCAmelCase : Tuple = 1000 __lowerCAmelCase : Tuple = 'huggingface/label-files' __lowerCAmelCase : Any = 'imagenet-1k-id2label.json' __lowerCAmelCase : Dict = json.load(open(hf_hub_download(_UpperCamelCase , _UpperCamelCase , repo_type='dataset' ) , 'r' ) ) __lowerCAmelCase : Optional[Any] = {int(_UpperCamelCase ): v for k, v in idalabel.items()} __lowerCAmelCase : Optional[Any] = idalabel __lowerCAmelCase : Any = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __lowerCAmelCase : Optional[int] = [3, 3, 6, 4] __lowerCAmelCase : Union[str, Any] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __lowerCAmelCase : str = [3, 3, 9, 6] __lowerCAmelCase : Tuple = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __lowerCAmelCase : int = [4, 3, 10, 5] __lowerCAmelCase : List[Any] = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __lowerCAmelCase : Union[str, Any] = [4, 4, 12, 6] __lowerCAmelCase : List[str] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('https' ): __lowerCAmelCase : List[Any] = torch.hub.load_state_dict_from_url(_UpperCamelCase , map_location='cpu' , check_hash=_UpperCamelCase ) else: __lowerCAmelCase : Optional[int] = torch.load(_UpperCamelCase , map_location='cpu' ) __lowerCAmelCase : List[Any] = checkpoint __lowerCAmelCase : Optional[int] = create_rename_keys(_UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # load HuggingFace model __lowerCAmelCase : Optional[Any] = SwiftFormerForImageClassification(_UpperCamelCase ).eval() hf_model.load_state_dict(_UpperCamelCase ) # prepare test inputs __lowerCAmelCase : Optional[Any] = prepare_img() __lowerCAmelCase : Optional[Any] = ViTImageProcessor.from_pretrained('preprocessor_config' ) __lowerCAmelCase : Dict = processor(images=_UpperCamelCase , return_tensors='pt' ) # compare outputs from both models __lowerCAmelCase : int = get_expected_output(_UpperCamelCase ) __lowerCAmelCase : Tuple = hf_model(inputs['pixel_values'] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , _UpperCamelCase , atol=1e-3 ) Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) print(F"Saving model {swiftformer_name} to {pytorch_dump_folder_path}" ) hf_model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swiftformer_name""", default="""swiftformer_xs""", choices=["""swiftformer_xs""", """swiftformer_s""", """swiftformer_l1""", """swiftformer_l3"""], type=str, help="""Name of the SwiftFormer model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""./converted_outputs/""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--original_ckpt""", default=None, type=str, help="""Path to the original model checkpoint.""") lowerCamelCase__ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
86
'''simple docstring''' import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) with open(F"{class_data_dir}/caption.txt" , """w""" ) as fa, open(F"{class_data_dir}/urls.txt" , """w""" ) as fa, open( F"{class_data_dir}/images.txt" , """w""" ) as fa: while total < num_class_images: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = Image.open(BytesIO(img.content ) ) with open(F"{class_data_dir}/images/{total}.jpg" , """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(F"{class_data_dir}/images/{total}.jpg" + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
0
import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int UpperCamelCase = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class snake_case_ ( datasets.BuilderConfig ): __A : Optional[datasets.Features] = None def lowercase_ ( _lowerCamelCase : "pyspark.sql.DataFrame" , _lowerCamelCase : List[int] , ): import pyspark def generate_fn(): lowercase__ : int = df.select("*" , pyspark.sql.functions.spark_partition_id().alias("part_id")) for partition_id in partition_order: lowercase__ : str = df_with_partition_id.select("*").where(f'''part_id = {partition_id}''').drop("part_id") lowercase__ : Dict = partition_df.collect() lowercase__ : Any = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class snake_case_ ( _BaseExamplesIterable ): def __init__( self : Any , lowercase_ : "pyspark.sql.DataFrame" , lowercase_ : str=None , ) -> int: lowercase__ : Optional[int] = df lowercase__ : int = partition_order or range(self.df.rdd.getNumPartitions() ) lowercase__ : Any = _generate_iterable_examples(self.df , self.partition_order ) def __iter__( self : Any ) -> List[str]: yield from self.generate_examples_fn() def __UpperCamelCase ( self : Dict , lowercase_ : np.random.Generator ) -> "SparkExamplesIterable": lowercase__ : Optional[Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowercase_ ) return SparkExamplesIterable(self.df , partition_order=lowercase_ ) def __UpperCamelCase ( self : Any , lowercase_ : int , lowercase_ : int ) -> "SparkExamplesIterable": lowercase__ : Any = self.split_shard_indices_by_worker(lowercase_ , lowercase_ ) return SparkExamplesIterable(self.df , partition_order=lowercase_ ) @property def __UpperCamelCase ( self : Tuple ) -> int: return len(self.partition_order ) class snake_case_ ( datasets.DatasetBuilder ): __A : Any = SparkConfig def __init__( self : List[Any] , lowercase_ : "pyspark.sql.DataFrame" , lowercase_ : str = None , lowercase_ : str = None , **lowercase_ : List[str] , ) -> Union[str, Any]: import pyspark lowercase__ : List[str] = pyspark.sql.SparkSession.builder.getOrCreate() lowercase__ : Optional[int] = df lowercase__ : int = working_dir super().__init__( cache_dir=lowercase_ , config_name=str(self.df.semanticHash() ) , **lowercase_ , ) def __UpperCamelCase ( self : str ) -> Optional[Any]: # Returns the path of the created file. def create_cache_and_write_probe(lowercase_ : Optional[Any] ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir , exist_ok=lowercase_ ) lowercase__ : Union[str, Any] = os.path.join(self._cache_dir , "fs_test" + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowercase_ , "a" ) return [probe_file] if self._spark.conf.get("spark.master" , "" ).startswith("local" ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowercase__ : Dict = ( self._spark.sparkContext.parallelize(range(1 ) , 1 ).mapPartitions(lowercase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( "When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir" ) def __UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: return datasets.DatasetInfo(features=self.config.features ) def __UpperCamelCase ( self : Optional[int] , lowercase_ : datasets.download.download_manager.DownloadManager ) -> Tuple: return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def __UpperCamelCase ( self : Optional[int] , lowercase_ : Optional[Any] ) -> str: import pyspark def get_arrow_batch_size(lowercase_ : List[str] ): for batch in it: yield pa.RecordBatch.from_pydict({"batch_bytes": [batch.nbytes]} ) lowercase__ : Tuple = self.df.count() lowercase__ : Optional[Any] = df_num_rows if df_num_rows <= 1_00 else 1_00 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowercase__ : Optional[Any] = ( self.df.limit(lowercase_ ) .repartition(1 ) .mapInArrow(lowercase_ , "batch_bytes: long" ) .agg(pyspark.sql.functions.sum("batch_bytes" ).alias("sample_bytes" ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowercase__ : Optional[Any] = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowercase__ : int = min(lowercase_ , int(approx_total_size / max_shard_size ) ) lowercase__ : Union[str, Any] = self.df.repartition(lowercase_ ) def __UpperCamelCase ( self : List[str] , lowercase_ : str , lowercase_ : str , lowercase_ : int , ) -> Iterable[Tuple[int, bool, Union[int, tuple]]]: import pyspark lowercase__ : List[str] = ParquetWriter if file_format == "parquet" else ArrowWriter lowercase__ : str = os.path.join(self._working_dir , os.path.basename(lowercase_ ) ) if self._working_dir else fpath lowercase__ : Optional[Any] = file_format == "parquet" # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowercase__ : Union[str, Any] = self.config.features lowercase__ : Any = self._writer_batch_size lowercase__ : Dict = self._fs.storage_options def write_arrow(lowercase_ : List[str] ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowercase__ : str = pyspark.TaskContext().taskAttemptId() lowercase__ : str = next(lowercase_ , lowercase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] , names=["task_id", "num_examples", "num_bytes"] , ) lowercase__ : Any = 0 lowercase__ : Optional[int] = writer_class( features=lowercase_ , path=working_fpath.replace("SSSSS" , F'''{shard_id:05d}''' ).replace("TTTTT" , F'''{task_id:05d}''' ) , writer_batch_size=lowercase_ , storage_options=lowercase_ , embed_local_files=lowercase_ , ) lowercase__ : str = pa.Table.from_batches([first_batch] ) writer.write_table(lowercase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowercase__ , lowercase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=["task_id", "num_examples", "num_bytes"] , ) shard_id += 1 lowercase__ : Dict = writer_class( features=writer._features , path=working_fpath.replace("SSSSS" , F'''{shard_id:05d}''' ).replace("TTTTT" , F'''{task_id:05d}''' ) , writer_batch_size=lowercase_ , storage_options=lowercase_ , embed_local_files=lowercase_ , ) lowercase__ : List[str] = pa.Table.from_batches([batch] ) writer.write_table(lowercase_ ) if writer._num_bytes > 0: lowercase__ , lowercase__ : Dict = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=["task_id", "num_examples", "num_bytes"] , ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowercase_ ) ): lowercase__ : Optional[Any] = os.path.join(os.path.dirname(lowercase_ ) , os.path.basename(lowercase_ ) ) shutil.move(lowercase_ , lowercase_ ) lowercase__ : int = ( self.df.mapInArrow(lowercase_ , "task_id: long, num_examples: long, num_bytes: long" ) .groupBy("task_id" ) .agg( pyspark.sql.functions.sum("num_examples" ).alias("total_num_examples" ) , pyspark.sql.functions.sum("num_bytes" ).alias("total_num_bytes" ) , pyspark.sql.functions.count("num_bytes" ).alias("num_shards" ) , pyspark.sql.functions.collect_list("num_examples" ).alias("shard_lengths" ) , ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def __UpperCamelCase ( self : Optional[int] , lowercase_ : "datasets.SplitGenerator" , lowercase_ : str = "arrow" , lowercase_ : Optional[Union[str, int]] = None , lowercase_ : Optional[int] = None , **lowercase_ : int , ) -> Optional[Any]: self._validate_cache_dir() lowercase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowercase_ ) lowercase__ : Tuple = not is_remote_filesystem(self._fs ) lowercase__ : Optional[Any] = os.path.join if is_local else posixpath.join lowercase__ : List[Any] = "-TTTTT-SSSSS-of-NNNNN" lowercase__ : Any = F'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowercase__ : str = path_join(self._output_dir , lowercase_ ) lowercase__ : Any = 0 lowercase__ : List[Any] = 0 lowercase__ : Dict = 0 lowercase__ : List[str] = [] lowercase__ : List[Any] = [] for task_id, content in self._prepare_split_single(lowercase_ , lowercase_ , lowercase_ ): ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) : Optional[int] = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowercase_ ) lowercase__ : List[Any] = total_num_examples lowercase__ : List[str] = total_num_bytes # should rename everything at the end logger.debug(F'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowercase__ : Tuple = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowercase__ : Any = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowercase_ : int , lowercase_ : int , lowercase_ : int , ): rename( lowercase_ , fpath.replace("SSSSS" , F'''{shard_id:05d}''' ).replace("TTTTT" , F'''{task_id:05d}''' ) , fpath.replace("TTTTT-SSSSS" , F'''{global_shard_id:05d}''' ).replace("NNNNN" , F'''{total_shards:05d}''' ) , ) lowercase__ : Optional[Any] = [] lowercase__ : int = 0 for i in range(len(lowercase_ ) ): lowercase__ , lowercase__ : Optional[int] = task_id_and_num_shards[i] for shard_id in range(lowercase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowercase_ , len(lowercase_ ) ).map(lambda lowercase_ : _rename_shard(*lowercase_ ) ).collect() else: # don't use any pattern lowercase__ : str = 0 lowercase__ : str = task_id_and_num_shards[0][0] self._rename( fpath.replace("SSSSS" , F'''{shard_id:05d}''' ).replace("TTTTT" , F'''{task_id:05d}''' ) , fpath.replace(lowercase_ , "" ) , ) def __UpperCamelCase ( self : Optional[int] , lowercase_ : "datasets.SplitGenerator" , ) -> SparkExamplesIterable: return SparkExamplesIterable(self.df )
87
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = 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 a_ : lowercase = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The column name of the images in the files."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the training data."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the validation data."""} ) lowercase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class a_ : lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) lowercase = field( default=lowerCamelCase , 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""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) lowercase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class a_ ( lowerCamelCase ): lowercase = field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def lowercase__ ( __UpperCamelCase )-> int: UpperCamelCase = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def lowercase__ ( )-> List[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase ,UpperCamelCase ,UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase ,UpperCamelCase ,UpperCamelCase = 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""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCamelCase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase = split["""train"""] UpperCamelCase = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(F"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(F"New config: {config}" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase = ViTMAEForPreTraining(__UpperCamelCase ) if training_args.do_train: UpperCamelCase = ds["""train"""].column_names else: UpperCamelCase = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = """image""" elif "img" in column_names: UpperCamelCase = """img""" else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size["""shortest_edge"""] else: UpperCamelCase = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase = Compose( [ Lambda(lambda __UpperCamelCase : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(__UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__UpperCamelCase ): UpperCamelCase = [transforms(__UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__UpperCamelCase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def lowercase__ ( __UpperCamelCase )-> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
321
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : int = { 'configuration_clipseg': [ 'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CLIPSegConfig', 'CLIPSegTextConfig', 'CLIPSegVisionConfig', ], 'processing_clipseg': ['CLIPSegProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : str = [ 'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST', 'CLIPSegModel', 'CLIPSegPreTrainedModel', 'CLIPSegTextModel', 'CLIPSegVisionModel', 'CLIPSegForImageSegmentation', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys __lowerCAmelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
88
'''simple docstring''' import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # pylint: disable=invalid-name SCREAMING_SNAKE_CASE__ = 2_5_6 class a_ ( lowerCamelCase ): lowercase = ["""melgan"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> None: """simple docstring""" super().__init__() # From MELGAN UpperCamelCase = math.log(1e-5 ) # Matches MelGAN training. UpperCamelCase = 4.0 # Largest value for most examples UpperCamelCase = 128 self.register_modules( notes_encoder=_SCREAMING_SNAKE_CASE , continuous_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , melgan=_SCREAMING_SNAKE_CASE , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" UpperCamelCase ,UpperCamelCase = output_range if clip: UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , self.min_value , self.max_value ) # Scale to [0, 1]. UpperCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase ,UpperCamelCase = input_range UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if clip else outputs # Scale to [0, 1]. UpperCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" UpperCamelCase = input_tokens > 0 UpperCamelCase ,UpperCamelCase = self.notes_encoder( encoder_input_tokens=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) UpperCamelCase ,UpperCamelCase = self.continuous_encoder( encoder_inputs=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" UpperCamelCase = noise_time if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML UpperCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) UpperCamelCase = self.decoder( encodings_and_masks=_SCREAMING_SNAKE_CASE , decoder_input_tokens=_SCREAMING_SNAKE_CASE , decoder_noise_time=_SCREAMING_SNAKE_CASE ) return logits @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "numpy" , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , ) -> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_SCREAMING_SNAKE_CASE )}." ) UpperCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) UpperCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) UpperCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) for i, encoder_input_tokens in enumerate(_SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. UpperCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. UpperCamelCase = ones UpperCamelCase = self.scale_features( _SCREAMING_SNAKE_CASE , output_range=[-1.0, 1.0] , clip=_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_SCREAMING_SNAKE_CASE , continuous_mask=_SCREAMING_SNAKE_CASE , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop UpperCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_SCREAMING_SNAKE_CASE , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_SCREAMING_SNAKE_CASE ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCamelCase = self.decode( encodings_and_masks=_SCREAMING_SNAKE_CASE , input_tokens=_SCREAMING_SNAKE_CASE , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 UpperCamelCase = self.scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = self.scale_to_features(_SCREAMING_SNAKE_CASE , input_range=[-1.0, 1.0] ) UpperCamelCase = mel[:1] UpperCamelCase = mel.cpu().float().numpy() UpperCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) logger.info("""Generated segment""" , _SCREAMING_SNAKE_CASE ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": UpperCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: UpperCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_SCREAMING_SNAKE_CASE )
321
0
'''simple docstring''' import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def __lowerCamelCase ( lowerCAmelCase_ ) -> Optional[int]: if hor == 128: _a : Tuple = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') _a : Union[str, Any] = (32, 128, 256) _a : Any = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: _a : Optional[Any] = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') _a : List[str] = (32, 64, 128, 256) _a : Optional[int] = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') _a : Any = torch.load(f"""/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch""" ) _a : Optional[int] = model.state_dict() _a : Optional[Any] = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } _a : List[str] = UNetaDModel(**lowerCAmelCase_ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) _a : str = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): _a : int = state_dict.pop(lowerCAmelCase_ ) hf_value_function.load_state_dict(lowerCAmelCase_ ) torch.save(hf_value_function.state_dict() , f"""hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin""" ) with open(f"""hub/hopper-medium-v2/unet/hor{hor}/config.json""" , 'w' ) as f: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCamelCase ( ) -> Union[str, Any]: _a : int = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } _a : Optional[int] = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) _a : List[str] = model _a : Optional[Any] = UNetaDModel(**lowerCAmelCase_ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) _a : List[Any] = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): _a : Union[str, Any] = state_dict.pop(lowerCAmelCase_ ) hf_value_function.load_state_dict(lowerCAmelCase_ ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) if __name__ == "__main__": unet(32) # unet(128) value_function()
89
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 4000000 )-> int: UpperCamelCase = [] UpperCamelCase ,UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCamelCase ) UpperCamelCase ,UpperCamelCase = b, a + b return sum(__UpperCamelCase ) if __name__ == "__main__": print(f'{solution() = }')
321
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() __A = logging.get_logger(__name__) def lowerCamelCase_ ( UpperCamelCase__ : str , UpperCamelCase__ : int=False ) -> Optional[int]: """simple docstring""" __lowerCamelCase = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""deit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""deit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""deit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""deit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""deit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""deit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""deit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""deit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""deit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""deit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'deit.embeddings.cls_token'), ('dist_token', 'deit.embeddings.distillation_token'), ('patch_embed.proj.weight', 'deit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'deit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'deit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" __lowerCamelCase = [(pair[0], pair[1][4:]) if pair[1].startswith('deit' ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ('norm.weight', 'deit.layernorm.weight'), ('norm.bias', 'deit.layernorm.bias'), ('head.weight', 'cls_classifier.weight'), ('head.bias', 'cls_classifier.bias'), ('head_dist.weight', 'distillation_classifier.weight'), ('head_dist.bias', 'distillation_classifier.bias'), ] ) return rename_keys def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Tuple=False ) -> Dict: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: __lowerCamelCase = '' else: __lowerCamelCase = 'deit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __lowerCamelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) __lowerCamelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict __lowerCamelCase = in_proj_weight[ : config.hidden_size, : ] __lowerCamelCase = in_proj_bias[: config.hidden_size] __lowerCamelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __lowerCamelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] __lowerCamelCase = in_proj_weight[ -config.hidden_size :, : ] __lowerCamelCase = in_proj_bias[-config.hidden_size :] def lowerCamelCase_ ( UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : Tuple ) -> Dict: """simple docstring""" __lowerCamelCase = dct.pop(UpperCamelCase__ ) __lowerCamelCase = val def lowerCamelCase_ ( ) -> List[str]: """simple docstring""" __lowerCamelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCamelCase = Image.open(requests.get(UpperCamelCase__ , stream=UpperCamelCase__ ).raw ) return im @torch.no_grad() def lowerCamelCase_ ( UpperCamelCase__ : int , UpperCamelCase__ : Optional[int] ) -> Any: """simple docstring""" __lowerCamelCase = DeiTConfig() # all deit models have fine-tuned heads __lowerCamelCase = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size __lowerCamelCase = 1000 __lowerCamelCase = 'huggingface/label-files' __lowerCamelCase = 'imagenet-1k-id2label.json' __lowerCamelCase = json.load(open(hf_hub_download(UpperCamelCase__ , UpperCamelCase__ , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase = {int(UpperCamelCase__ ): v for k, v in idalabel.items()} __lowerCamelCase = idalabel __lowerCamelCase = {v: k for k, v in idalabel.items()} __lowerCamelCase = int(deit_name[-6:-4] ) __lowerCamelCase = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('tiny' ): __lowerCamelCase = 192 __lowerCamelCase = 768 __lowerCamelCase = 12 __lowerCamelCase = 3 elif deit_name[9:].startswith('small' ): __lowerCamelCase = 384 __lowerCamelCase = 1536 __lowerCamelCase = 12 __lowerCamelCase = 6 if deit_name[9:].startswith('base' ): pass elif deit_name[4:].startswith('large' ): __lowerCamelCase = 1024 __lowerCamelCase = 4096 __lowerCamelCase = 24 __lowerCamelCase = 16 # load original model from timm __lowerCamelCase = timm.create_model(UpperCamelCase__ , pretrained=UpperCamelCase__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys __lowerCamelCase = timm_model.state_dict() __lowerCamelCase = create_rename_keys(UpperCamelCase__ , UpperCamelCase__ ) for src, dest in rename_keys: rename_key(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) read_in_q_k_v(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # load HuggingFace model __lowerCamelCase = DeiTForImageClassificationWithTeacher(UpperCamelCase__ ).eval() model.load_state_dict(UpperCamelCase__ ) # Check outputs on an image, prepared by DeiTImageProcessor __lowerCamelCase = int( (256 / 224) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 __lowerCamelCase = DeiTImageProcessor(size=UpperCamelCase__ , crop_size=config.image_size ) __lowerCamelCase = image_processor(images=prepare_img() , return_tensors='pt' ) __lowerCamelCase = encoding['pixel_values'] __lowerCamelCase = model(UpperCamelCase__ ) __lowerCamelCase = timm_model(UpperCamelCase__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(UpperCamelCase__ , outputs.logits , atol=1E-3 ) Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ ) print(F"""Saving model {deit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCamelCase__ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": __A = argparse.ArgumentParser() # Required parameters parser.add_argument( "--deit_name", default="vit_deit_base_distilled_patch16_224", type=str, help="Name of the DeiT timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) __A = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
90
'''simple docstring''' def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: # Base Case if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] , __UpperCamelCase , __UpperCamelCase ): # Color current vertex UpperCamelCase = i # Validate coloring if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , index + 1 ): return True # Backtrack UpperCamelCase = -1 return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> list[int]: UpperCamelCase = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , 0 ): return colored_vertices return []
321
0
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase_ : Tuple = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class lowerCAmelCase__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = XLNetTokenizer __UpperCamelCase = XLNetTokenizerFast __UpperCamelCase = True __UpperCamelCase = True def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_ : str = XLNetTokenizer(lowercase_ , keep_accents=lowercase_) tokenizer.sanitize_special_tokens() tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = '''<s>''' SCREAMING_SNAKE_CASE_ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase_) , lowercase_) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase_) , lowercase_) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , '''<unk>''') self.assertEqual(vocab_keys[1] , '''<s>''') self.assertEqual(vocab_keys[-1] , '''<eod>''') self.assertEqual(len(lowercase_) , 1006) def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = XLNetTokenizer(lowercase_ , keep_accents=lowercase_) SCREAMING_SNAKE_CASE_ : int = tokenizer.tokenize('''This is a test''') self.assertListEqual(lowercase_ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_) , [285, 46, 10, 170, 382]) SCREAMING_SNAKE_CASE_ : int = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( lowercase_ , [ 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_ : Optional[int] = tokenizer.convert_tokens_to_ids(lowercase_) self.assertListEqual(lowercase_ , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4]) SCREAMING_SNAKE_CASE_ : List[str] = tokenizer.convert_ids_to_tokens(lowercase_) self.assertListEqual( lowercase_ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = XLNetTokenizer(lowercase_ , do_lower_case=lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( lowercase_ , [ 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''', '''se''', '''.''', ] , ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''') , ['''▁he''', '''ll''', '''o''']) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = XLNetTokenizer(lowercase_ , do_lower_case=lowercase_) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( lowercase_ , [ 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''', '''se''', '''.''', ] , ) @slow def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = XLNetTokenizer.from_pretrained('''xlnet-base-cased''') SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer.build_inputs_with_special_tokens(lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(lowercase_ , lowercase_) assert encoded_sentence == text + [4, 3] assert encoded_pair == text + [4] + text_a + [4, 3] @slow def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = {'''input_ids''': [[17, 21442, 270, 17, 10, 14645, 318, 34, 17, 4546, 3145, 787, 13, 7752, 22018, 23, 21, 17, 4546, 3145, 787, 13, 3352, 14431, 13, 5500, 11, 1176, 580, 13, 16819, 4797, 23, 17, 10, 17135, 658, 19, 457, 7932, 13, 184, 19, 3154, 17135, 6468, 19, 1404, 12269, 19, 4229, 5356, 16264, 46, 19, 17, 20545, 10395, 9, 9, 9, 11, 28, 6421, 9531, 20729, 17, 10, 353, 17022, 11, 21, 6421, 9531, 16949, 17, 10, 11509, 753, 11, 33, 95, 2421, 7385, 956, 14431, 2626, 25, 842, 7385, 4836, 21, 1429, 2272, 9855, 3120, 161, 24738, 19, 13203, 658, 218, 787, 21, 430, 18482, 847, 2637, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 322, 22178, 27, 1064, 22, 956, 13, 11101, 1429, 5854, 24313, 18953, 40, 422, 24366, 68, 1758, 37, 10483, 14257, 31, 207, 263, 21, 203, 3773, 25, 71, 9735, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 32, 2049, 3442, 17, 13894, 3380, 23, 95, 18, 17634, 2288, 9, 4, 3]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]], '''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], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowercase_ , model_name='''xlnet-base-cased''' , revision='''c841166438c31ec7ca9a106dee7bb312b73ae511''' , )
91
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 2000000 )-> int: UpperCamelCase = [0 for i in range(n + 1 )] UpperCamelCase = 1 UpperCamelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __UpperCamelCase ): UpperCamelCase = 1 UpperCamelCase = 0 for i in range(__UpperCamelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'{solution() = }')
321
0
from ..utils import DummyObject, requires_backends class a__ ( metaclass=snake_case__ ): _a : Any = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : int = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : Union[str, Any] = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : int = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : int = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : Tuple = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : List[Any] = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : int = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : Tuple = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : Optional[int] = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : str = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : str = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) class a__ ( metaclass=snake_case__ ): _a : List[str] = ["""flax"""] def __init__( self , *_A , **_A ): """simple docstring""" requires_backends(self , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] ) @classmethod def __SCREAMING_SNAKE_CASE( cls , *_A , **_A ): """simple docstring""" requires_backends(cls , ["flax"] )
92
'''simple docstring''' from timeit import timeit def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: number &= number - 1 result += 1 return result def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowercase__ ( )-> None: def do_benchmark(__UpperCamelCase ) -> None: UpperCamelCase = """import __main__ as z""" print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) UpperCamelCase = timeit("""z.get_set_bits_count_using_modulo_operator(25)""" , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) UpperCamelCase = timeit( """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""" , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
321
0
'''simple docstring''' import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py _lowercase : int = "src/diffusers" _lowercase : Any = "." # This is to make sure the diffusers module imported is the one in the repo. _lowercase : List[str] = importlib.util.spec_from_file_location( "diffusers", os.path.join(DIFFUSERS_PATH, "__init__.py"), submodule_search_locations=[DIFFUSERS_PATH], ) _lowercase : str = spec.loader.load_module() def snake_case_ ( __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" return line.startswith(__SCREAMING_SNAKE_CASE ) or len(__SCREAMING_SNAKE_CASE ) <= 1 or re.search(R'''^\s*\)(\s*->.*:|:)\s*$''' , __SCREAMING_SNAKE_CASE ) is not None def snake_case_ ( __SCREAMING_SNAKE_CASE : str ): """simple docstring""" lowercase_ : List[str] = object_name.split('''.''' ) lowercase_ : Optional[int] = 0 # First let's find the module where our object lives. lowercase_ : Optional[Any] = parts[i] while i < len(__SCREAMING_SNAKE_CASE ) and not os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE , F'''{module}.py''' ) ): i += 1 if i < len(__SCREAMING_SNAKE_CASE ): lowercase_ : Optional[Any] = os.path.join(__SCREAMING_SNAKE_CASE , parts[i] ) if i >= len(__SCREAMING_SNAKE_CASE ): raise ValueError(F'''`object_name` should begin with the name of a module of diffusers but got {object_name}.''' ) with open(os.path.join(__SCREAMING_SNAKE_CASE , F'''{module}.py''' ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowercase_ : Dict = f.readlines() # Now let's find the class / func in the code! lowercase_ : Tuple = '''''' lowercase_ : Union[str, Any] = 0 for name in parts[i + 1 :]: while ( line_index < len(__SCREAMING_SNAKE_CASE ) and re.search(RF'''^{indent}(class|def)\s+{name}(\(|\:)''' , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(__SCREAMING_SNAKE_CASE ): raise ValueError(F''' {object_name} does not match any function or class in {module}.''' ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). lowercase_ : Optional[Any] = line_index while line_index < len(__SCREAMING_SNAKE_CASE ) and _should_continue(lines[line_index] , __SCREAMING_SNAKE_CASE ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowercase_ : List[Any] = lines[start_index:line_index] return "".join(__SCREAMING_SNAKE_CASE ) _lowercase : str = re.compile(r"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)") _lowercase : Optional[int] = re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)") _lowercase : List[str] = re.compile(r"<FILL\s+[^>]*>") def snake_case_ ( __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : Any = code.split('''\n''' ) lowercase_ : Tuple = 0 while idx < len(__SCREAMING_SNAKE_CASE ) and len(lines[idx] ) == 0: idx += 1 if idx < len(__SCREAMING_SNAKE_CASE ): return re.search(R'''^(\s*)\S''' , lines[idx] ).groups()[0] return "" def snake_case_ ( __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : Union[str, Any] = len(get_indent(__SCREAMING_SNAKE_CASE ) ) > 0 if has_indent: lowercase_ : str = F'''class Bla:\n{code}''' lowercase_ : Any = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=__SCREAMING_SNAKE_CASE ) lowercase_ : Any = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) lowercase_ , lowercase_ : int = style_docstrings_in_code(__SCREAMING_SNAKE_CASE ) return result[len('''class Bla:\n''' ) :] if has_indent else result def snake_case_ ( __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any]=False ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowercase_ : Dict = f.readlines() lowercase_ : Dict = [] lowercase_ : Optional[Any] = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(__SCREAMING_SNAKE_CASE ): lowercase_ : int = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. lowercase_ , lowercase_ , lowercase_ : int = search.groups() lowercase_ : Any = find_code_in_diffusers(__SCREAMING_SNAKE_CASE ) lowercase_ : List[str] = get_indent(__SCREAMING_SNAKE_CASE ) lowercase_ : Optional[Any] = line_index + 1 if indent == theoretical_indent else line_index + 2 lowercase_ : int = theoretical_indent lowercase_ : Any = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. lowercase_ : Union[str, Any] = True while line_index < len(__SCREAMING_SNAKE_CASE ) and should_continue: line_index += 1 if line_index >= len(__SCREAMING_SNAKE_CASE ): break lowercase_ : Tuple = lines[line_index] lowercase_ : List[Any] = _should_continue(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and re.search(F'''^{indent}# End copy''' , __SCREAMING_SNAKE_CASE ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowercase_ : List[str] = lines[start_index:line_index] lowercase_ : List[str] = ''''''.join(__SCREAMING_SNAKE_CASE ) # Remove any nested `Copied from` comments to avoid circular copies lowercase_ : Tuple = [line for line in theoretical_code.split('''\n''' ) if _re_copy_warning.search(__SCREAMING_SNAKE_CASE ) is None] lowercase_ : Any = '''\n'''.join(__SCREAMING_SNAKE_CASE ) # Before comparing, use the `replace_pattern` on the original code. if len(__SCREAMING_SNAKE_CASE ) > 0: lowercase_ : Any = replace_pattern.replace('''with''' , '''''' ).split(''',''' ) lowercase_ : Union[str, Any] = [_re_replace_pattern.search(__SCREAMING_SNAKE_CASE ) for p in patterns] for pattern in patterns: if pattern is None: continue lowercase_ , lowercase_ , lowercase_ : List[str] = pattern.groups() lowercase_ : Dict = re.sub(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if option.strip() == "all-casing": lowercase_ : int = re.sub(obja.lower() , obja.lower() , __SCREAMING_SNAKE_CASE ) lowercase_ : str = re.sub(obja.upper() , obja.upper() , __SCREAMING_SNAKE_CASE ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line lowercase_ : Optional[int] = blackify(lines[start_index - 1] + theoretical_code ) lowercase_ : Any = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: lowercase_ : int = lines[:start_index] + [theoretical_code] + lines[line_index:] lowercase_ : Optional[Any] = start_index + 1 if overwrite and len(__SCREAMING_SNAKE_CASE ) > 0: # Warn the user a file has been modified. print(F'''Detected changes, rewriting {filename}.''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(__SCREAMING_SNAKE_CASE ) return diffs def snake_case_ ( __SCREAMING_SNAKE_CASE : bool = False ): """simple docstring""" lowercase_ : Dict = glob.glob(os.path.join(__SCREAMING_SNAKE_CASE , '''**/*.py''' ) , recursive=__SCREAMING_SNAKE_CASE ) lowercase_ : Any = [] for filename in all_files: lowercase_ : List[Any] = is_copy_consistent(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) diffs += [F'''- {filename}: copy does not match {d[0]} at line {d[1]}''' for d in new_diffs] if not overwrite and len(__SCREAMING_SNAKE_CASE ) > 0: lowercase_ : Union[str, Any] = '''\n'''.join(__SCREAMING_SNAKE_CASE ) raise Exception( '''Found the following copy inconsistencies:\n''' + diff + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.''' ) if __name__ == "__main__": _lowercase : Tuple = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") _lowercase : Optional[int] = parser.parse_args() check_copies(args.fix_and_overwrite)
93
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
0
import os import re import shutil import sys import tempfile import unittest import black snake_case : Any = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. snake_case : int = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class _snake_case ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self ): a :Optional[Any] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) a :List[str] = self.transformer_dir shutil.copy( os.path.join(_lowerCamelCase , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def SCREAMING_SNAKE_CASE__ ( self ): a :Tuple = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None ): a :Dict = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: a :str = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result a :Dict = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) a :Any = black.format_str(_lowerCamelCase , mode=_lowerCamelCase ) a :List[str] = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(_lowerCamelCase , '''w''' , newline='''\n''' ) as f: f.write(_lowerCamelCase ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_lowerCamelCase ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_lowerCamelCase ) with open(_lowerCamelCase , '''r''' ) as f: self.assertTrue(f.read() , _lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): a :Any = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(_lowerCamelCase , _lowerCamelCase ) def SCREAMING_SNAKE_CASE__ ( self ): # Base copy consistency self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , _lowerCamelCase , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , _lowerCamelCase ) , ) # Copy consistency with a really long name a :str = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub('''Bert''' , _lowerCamelCase , _lowerCamelCase ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , _lowerCamelCase , overwrite_result=re.sub('''Bert''' , '''TestModel''' , _lowerCamelCase ) , ) def SCREAMING_SNAKE_CASE__ ( self ): a :Any = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] a :List[str] = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) a :Union[str, Any] = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) a :List[str] = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) a , a :Optional[Any] = check_copies.convert_to_localized_md( _lowerCamelCase , _lowerCamelCase , localized_readme['''format_model_list'''] ) self.assertFalse(_lowerCamelCase ) self.assertEqual(_lowerCamelCase , _lowerCamelCase ) a , a :Optional[int] = check_copies.convert_to_localized_md( _lowerCamelCase , _lowerCamelCase , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_lowerCamelCase ) a :str = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) a :Dict = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) a :str = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) a , a :int = check_copies.convert_to_localized_md( _lowerCamelCase , _lowerCamelCase , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(_lowerCamelCase , _lowerCamelCase )
94
'''simple docstring''' import math def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
321
0
def _A ( SCREAMING_SNAKE_CASE : list ): """simple docstring""" a__ : Tuple =len(SCREAMING_SNAKE_CASE ) for _ in range(SCREAMING_SNAKE_CASE ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: a__ , a__ : Tuple =arr[i + 1], arr[i] return arr if __name__ == "__main__": UpperCAmelCase : Dict = list(range(10, 0, -1)) print(F"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
95
'''simple docstring''' import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE__ = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE__ = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' SCREAMING_SNAKE_CASE__ = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://www.atticusprojectai.org/cuad"""] , reference_urls=["""https://www.atticusprojectai.org/cuad"""] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} UpperCamelCase = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] UpperCamelCase = evaluate(dataset=_SCREAMING_SNAKE_CASE , predictions=_SCREAMING_SNAKE_CASE ) return score
321
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase__ = {"""configuration_unispeech""": ["""UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP""", """UniSpeechConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ """UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST""", """UniSpeechForCTC""", """UniSpeechForPreTraining""", """UniSpeechForSequenceClassification""", """UniSpeechModel""", """UniSpeechPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
96
'''simple docstring''' def lowercase__ ( __UpperCamelCase )-> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase__ ( __UpperCamelCase = 1000000 )-> int: UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__UpperCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING __snake_case = logging.get_logger(__name__) @add_end_docstrings(A__ ) class lowercase ( A__ ): """simple docstring""" def __init__( self , **UpperCamelCase_ ): '''simple docstring''' super().__init__(**UpperCamelCase_ ) if self.framework == "tf": raise ValueError(F'''The {self.__class__} is only available in PyTorch.''' ) requires_backends(self , '''vision''' ) self.check_model_type(UpperCamelCase_ ) def __call__( self , UpperCamelCase_ , UpperCamelCase_ = None , **UpperCamelCase_ , ): '''simple docstring''' if "text_queries" in kwargs: UpperCamelCase__ :str = kwargs.pop('''text_queries''' ) if isinstance(UpperCamelCase_ , (str, Image.Image) ): UpperCamelCase__ :List[Any] = {'''image''': image, '''candidate_labels''': candidate_labels} else: UpperCamelCase__ :Any = image UpperCamelCase__ :Optional[int] = super().__call__(UpperCamelCase_ , **UpperCamelCase_ ) return results def lowerCAmelCase__ ( self , **UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Any = {} if "threshold" in kwargs: UpperCamelCase__ :List[Any] = kwargs['''threshold'''] if "top_k" in kwargs: UpperCamelCase__ :Optional[Any] = kwargs['''top_k'''] return {}, {}, postprocess_params def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :str = load_image(inputs['''image'''] ) UpperCamelCase__ :Dict = inputs['''candidate_labels'''] if isinstance(UpperCamelCase_ , UpperCamelCase_ ): UpperCamelCase__ :Optional[Any] = candidate_labels.split(''',''' ) UpperCamelCase__ :Union[str, Any] = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(UpperCamelCase_ ): UpperCamelCase__ :List[str] = self.tokenizer(UpperCamelCase_ , return_tensors=self.framework ) UpperCamelCase__ :Optional[int] = self.image_processor(UpperCamelCase_ , return_tensors=self.framework ) yield { "is_last": i == len(UpperCamelCase_ ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :int = model_inputs.pop('''target_size''' ) UpperCamelCase__ :Optional[int] = model_inputs.pop('''candidate_label''' ) UpperCamelCase__ :List[Any] = model_inputs.pop('''is_last''' ) UpperCamelCase__ :str = self.model(**UpperCamelCase_ ) UpperCamelCase__ :str = {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs} return model_outputs def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_=0.1 , UpperCamelCase_=None ): '''simple docstring''' UpperCamelCase__ :str = [] for model_output in model_outputs: UpperCamelCase__ :Optional[int] = model_output['''candidate_label'''] UpperCamelCase__ :Union[str, Any] = BaseModelOutput(UpperCamelCase_ ) UpperCamelCase__ :Tuple = self.image_processor.post_process_object_detection( outputs=UpperCamelCase_ , threshold=UpperCamelCase_ , target_sizes=model_output['''target_size'''] )[0] for index in outputs["scores"].nonzero(): UpperCamelCase__ :Dict = outputs['''scores'''][index].item() UpperCamelCase__ :int = self._get_bounding_box(outputs['''boxes'''][index][0] ) UpperCamelCase__ :Optional[int] = {'''score''': score, '''label''': label, '''box''': box} results.append(UpperCamelCase_ ) UpperCamelCase__ :Optional[int] = sorted(UpperCamelCase_ , key=lambda UpperCamelCase_ : x["score"] , reverse=UpperCamelCase_ ) if top_k: UpperCamelCase__ :Tuple = results[:top_k] return results def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' if self.framework != "pt": raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.''' ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = box.int().tolist() UpperCamelCase__ :Union[str, Any] = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
97
'''simple docstring''' from __future__ import annotations from math import pow, sqrt def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if resistance == 0: return {"resistance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(__UpperCamelCase , 2 ) + pow(__UpperCamelCase , 2 ) )} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
321
0
"""simple docstring""" 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 lowerCAmelCase__ : Optional[int] = logging.get_logger(__name__) lowerCAmelCase__ : Optional[int] = { 'google/mobilenet_v1_1.0_224': 'https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json', 'google/mobilenet_v1_0.75_192': 'https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = "mobilenet_v1" def __init__( self : Optional[Any] ,lowerCamelCase__ : List[Any]=3 ,lowerCamelCase__ : Optional[int]=224 ,lowerCamelCase__ : int=1.0 ,lowerCamelCase__ : Union[str, Any]=8 ,lowerCamelCase__ : str="relu6" ,lowerCamelCase__ : Tuple=True ,lowerCamelCase__ : Optional[Any]=0.9_9_9 ,lowerCamelCase__ : Optional[int]=0.0_2 ,lowerCamelCase__ : str=0.0_0_1 ,**lowerCamelCase__ : List[str] ,): super().__init__(**lowerCamelCase__ ) if depth_multiplier <= 0: raise ValueError('depth_multiplier must be greater than zero.' ) UpperCAmelCase__ = num_channels UpperCAmelCase__ = image_size UpperCAmelCase__ = depth_multiplier UpperCAmelCase__ = min_depth UpperCAmelCase__ = hidden_act UpperCAmelCase__ = tf_padding UpperCAmelCase__ = classifier_dropout_prob UpperCAmelCase__ = initializer_range UpperCAmelCase__ = layer_norm_eps class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = version.parse("1.11" ) @property def __lowerCAmelCase ( self : Union[str, Any] ): return OrderedDict([('pixel_values', {0: 'batch'})] ) @property def __lowerCAmelCase ( self : Optional[Any] ): if self.task == "image-classification": return OrderedDict([('logits', {0: 'batch'})] ) else: return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})] ) @property def __lowerCAmelCase ( self : str ): return 1e-4
98
'''simple docstring''' # Algorithm for the pigeonhole sorting def lowercase__ ( __UpperCamelCase )-> Union[str, Any]: UpperCamelCase = min(__UpperCamelCase ) # min() finds the minimum value UpperCamelCase = max(__UpperCamelCase ) # max() finds the maximum value UpperCamelCase = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size UpperCamelCase = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(__UpperCamelCase , __UpperCamelCase ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. UpperCamelCase = 0 for count in range(__UpperCamelCase ): while holes[count] > 0: holes[count] -= 1 UpperCamelCase = count + min_val i += 1 def lowercase__ ( )-> Any: UpperCamelCase = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(__UpperCamelCase ) print("""Sorted order is:""" , """ """.join(__UpperCamelCase ) ) if __name__ == "__main__": main()
321
0
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( """The `image_to_image.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionImg2ImgPipeline` instead.""" )
99
'''simple docstring''' import torch from diffusers import DDPMParallelScheduler from .test_schedulers import SchedulerCommonTest class a_ ( lowerCamelCase ): lowercase = (DDPMParallelScheduler,) def A__ ( self , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = { """num_train_timesteps""": 1000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """variance_type""": """fixed_small""", """clip_sample""": True, } config.update(**_SCREAMING_SNAKE_CASE ) return config def A__ ( self ) -> List[str]: """simple docstring""" for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[int]: """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE , beta_end=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Tuple: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> List[Any]: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> str: """simple docstring""" self.check_over_configs(thresholding=_SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_SCREAMING_SNAKE_CASE , prediction_type=_SCREAMING_SNAKE_CASE , sample_max_value=_SCREAMING_SNAKE_CASE , ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Union[str, Any]: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> int: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def A__ ( self ) -> Optional[int]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = self.dummy_sample_deter + 0.1 UpperCamelCase = self.dummy_sample_deter - 0.1 UpperCamelCase = samplea.shape[0] UpperCamelCase = torch.stack([samplea, samplea, samplea] , dim=0 ) UpperCamelCase = torch.arange(_SCREAMING_SNAKE_CASE )[0:3, None].repeat(1 , _SCREAMING_SNAKE_CASE ) UpperCamelCase = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) UpperCamelCase = scheduler.batch_step_no_noise(_SCREAMING_SNAKE_CASE , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) ) UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 1_1_5_3.1_8_3_3 ) < 1e-2 assert abs(result_mean.item() - 0.5_0_0_5 ) < 1e-3 def A__ ( self ) -> List[str]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config(prediction_type="""v_prediction""" ) UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) UpperCamelCase = scheduler.timesteps for i, timestep in enumerate(_SCREAMING_SNAKE_CASE ): if i == len(_SCREAMING_SNAKE_CASE ) - 1: UpperCamelCase = -1 else: UpperCamelCase = timesteps[i + 1] UpperCamelCase = scheduler.previous_timestep(_SCREAMING_SNAKE_CASE ) UpperCamelCase = prev_t.item() self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 51, 0] with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""`custom_timesteps` must be in descending order.""" ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""Can only pass one of `num_inference_steps` or `custom_timesteps`.""" ): scheduler.set_timesteps(num_inference_steps=_SCREAMING_SNAKE_CASE , timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( _SCREAMING_SNAKE_CASE , msg="""`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}""" , ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor __magic_name__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" def __init__( self , *lowerCAmelCase__ , **lowerCAmelCase__): warnings.warn( """The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use YolosImageProcessor instead.""" , lowerCAmelCase__ , ) super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__)
100
'''simple docstring''' from __future__ import annotations import math class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = size # approximate the overall size of segment tree with given value UpperCamelCase = [0 for i in range(0 , 4 * size )] # create array to store lazy update UpperCamelCase = [0 for i in range(0 , 4 * size )] UpperCamelCase = [0 for i in range(0 , 4 * size )] # flag for lazy update def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 + 1 def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" if left_element == right_element: UpperCamelCase = a[left_element - 1] else: UpperCamelCase = (left_element + right_element) // 2 self.build(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.build(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: UpperCamelCase = val if left_element != right_element: UpperCamelCase = val UpperCamelCase = val UpperCamelCase = True UpperCamelCase = True return True UpperCamelCase = (left_element + right_element) // 2 self.update(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.update(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) return True def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | float: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] UpperCamelCase = (left_element + right_element) // 2 UpperCamelCase = self.query(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = self.query(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __str__( self ) -> str: """simple docstring""" return str([self.query(1 , 1 , self.size , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8] SCREAMING_SNAKE_CASE__ = 1_5 SCREAMING_SNAKE_CASE__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 1_1)) print(segt.query(1, 1, size, 7, 1_2)) segt.update(1, 1, size, 1, 3, 1_1_1) print(segt.query(1, 1, size, 1, 1_5)) segt.update(1, 1, size, 7, 8, 2_3_5) print(segt)
321
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class lowercase ( unittest.TestCase ): def A__ ( self): lowercase = tempfile.mkdtemp() # fmt: off lowercase = ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>'''] # fmt: on lowercase = dict(zip(A__ ,range(len(A__)))) lowercase = ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', ''''''] lowercase = {'''unk_token''': '''<unk>'''} lowercase = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['''vocab_file''']) lowercase = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['''merges_file''']) with open(self.vocab_file ,'''w''' ,encoding='''utf-8''') as fp: fp.write(json.dumps(A__) + '''\n''') with open(self.merges_file ,'''w''' ,encoding='''utf-8''') as fp: fp.write('''\n'''.join(A__)) lowercase = { '''do_resize''': True, '''size''': 2_0, '''do_center_crop''': True, '''crop_size''': 1_8, '''do_normalize''': True, '''image_mean''': [0.48145466, 0.4578275, 0.40821073], '''image_std''': [0.26862954, 0.26130258, 0.27577711], } lowercase = os.path.join(self.tmpdirname ,A__) with open(self.image_processor_file ,'''w''' ,encoding='''utf-8''') as fp: json.dump(A__ ,A__) def A__ ( self ,**A__): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**A__) def A__ ( self ,**A__): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**A__) def A__ ( self ,**A__): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**A__) def A__ ( self): shutil.rmtree(self.tmpdirname) def A__ ( self): lowercase = [np.random.randint(2_5_5 ,size=(3, 3_0, 4_0_0) ,dtype=np.uinta)] lowercase = [Image.fromarray(np.moveaxis(A__ ,0 ,-1)) for x in image_inputs] return image_inputs def A__ ( self): lowercase = self.get_tokenizer() lowercase = self.get_rust_tokenizer() lowercase = self.get_image_processor() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) processor_slow.save_pretrained(self.tmpdirname) lowercase = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=A__) lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) processor_fast.save_pretrained(self.tmpdirname) lowercase = CLIPProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab() ,tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab() ,tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab() ,tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer ,A__) self.assertIsInstance(processor_fast.tokenizer ,A__) self.assertEqual(processor_slow.image_processor.to_json_string() ,image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string() ,image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor ,A__) self.assertIsInstance(processor_fast.image_processor ,A__) def A__ ( self): lowercase = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) lowercase = self.get_tokenizer(bos_token='''(BOS)''' ,eos_token='''(EOS)''') lowercase = self.get_image_processor(do_normalize=A__ ,padding_value=1.0) lowercase = CLIPProcessor.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 A__ ( self): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) lowercase = self.prepare_image_inputs() lowercase = image_processor(A__ ,return_tensors='''np''') lowercase = processor(images=A__ ,return_tensors='''np''') for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() ,input_processor[key].sum() ,delta=1E-2) def A__ ( self): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) lowercase = '''lower newer''' lowercase = processor(text=A__) lowercase = tokenizer(A__) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key]) def A__ ( self): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) lowercase = '''lower newer''' lowercase = self.prepare_image_inputs() lowercase = processor(text=A__ ,images=A__) self.assertListEqual(list(inputs.keys()) ,['''input_ids''', '''attention_mask''', '''pixel_values''']) # test if it raises when no input is passed with pytest.raises(A__): processor() def A__ ( self): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) lowercase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowercase = processor.batch_decode(A__) lowercase = tokenizer.batch_decode(A__) self.assertListEqual(A__ ,A__) def A__ ( self): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = CLIPProcessor(tokenizer=A__ ,image_processor=A__) lowercase = '''lower newer''' lowercase = self.prepare_image_inputs() lowercase = processor(text=A__ ,images=A__) self.assertListEqual(list(inputs.keys()) ,processor.model_input_names)
101
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 1000 )-> int: UpperCamelCase = -1 UpperCamelCase = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c UpperCamelCase = (n * n - 2 * a * n) // (2 * n - 2 * a) UpperCamelCase = n - a - b if c * c == (a * a + b * b): UpperCamelCase = a * b * c if candidate >= product: UpperCamelCase = candidate return product if __name__ == "__main__": print(f'{solution() = }')
321
0
"""simple docstring""" def lowercase ( ) ->Optional[Any]: """simple docstring""" __snake_case : Dict = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] __snake_case : Optional[Any] = 6 __snake_case : Tuple = 1 __snake_case : Tuple = 1_901 __snake_case : Tuple = 0 while year < 2_001: day += 7 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): if day > days_per_month[month - 1] and month != 2: month += 1 __snake_case : str = day - days_per_month[month - 2] elif day > 29 and month == 2: month += 1 __snake_case : Optional[Any] = day - 29 else: if day > days_per_month[month - 1]: month += 1 __snake_case : Tuple = day - days_per_month[month - 2] if month > 12: year += 1 __snake_case : Tuple = 1 if year < 2_001 and day == 1: sundays += 1 return sundays if __name__ == "__main__": print(solution())
102
'''simple docstring''' import argparse import struct import unittest class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = data # Initialize hash values UpperCamelCase = [ 0x6A_09_E6_67, 0xBB_67_AE_85, 0x3C_6E_F3_72, 0xA5_4F_F5_3A, 0x51_0E_52_7F, 0x9B_05_68_8C, 0x1F_83_D9_AB, 0x5B_E0_CD_19, ] # Initialize round constants UpperCamelCase = [ 0x42_8A_2F_98, 0x71_37_44_91, 0xB5_C0_FB_CF, 0xE9_B5_DB_A5, 0x39_56_C2_5B, 0x59_F1_11_F1, 0x92_3F_82_A4, 0xAB_1C_5E_D5, 0xD8_07_AA_98, 0x12_83_5B_01, 0x24_31_85_BE, 0x55_0C_7D_C3, 0x72_BE_5D_74, 0x80_DE_B1_FE, 0x9B_DC_06_A7, 0xC1_9B_F1_74, 0xE4_9B_69_C1, 0xEF_BE_47_86, 0x0F_C1_9D_C6, 0x24_0C_A1_CC, 0x2D_E9_2C_6F, 0x4A_74_84_AA, 0x5C_B0_A9_DC, 0x76_F9_88_DA, 0x98_3E_51_52, 0xA8_31_C6_6D, 0xB0_03_27_C8, 0xBF_59_7F_C7, 0xC6_E0_0B_F3, 0xD5_A7_91_47, 0x06_CA_63_51, 0x14_29_29_67, 0x27_B7_0A_85, 0x2E_1B_21_38, 0x4D_2C_6D_FC, 0x53_38_0D_13, 0x65_0A_73_54, 0x76_6A_0A_BB, 0x81_C2_C9_2E, 0x92_72_2C_85, 0xA2_BF_E8_A1, 0xA8_1A_66_4B, 0xC2_4B_8B_70, 0xC7_6C_51_A3, 0xD1_92_E8_19, 0xD6_99_06_24, 0xF4_0E_35_85, 0x10_6A_A0_70, 0x19_A4_C1_16, 0x1E_37_6C_08, 0x27_48_77_4C, 0x34_B0_BC_B5, 0x39_1C_0C_B3, 0x4E_D8_AA_4A, 0x5B_9C_CA_4F, 0x68_2E_6F_F3, 0x74_8F_82_EE, 0x78_A5_63_6F, 0x84_C8_78_14, 0x8C_C7_02_08, 0x90_BE_FF_FA, 0xA4_50_6C_EB, 0xBE_F9_A3_F7, 0xC6_71_78_F2, ] UpperCamelCase = self.preprocessing(self.data ) self.final_hash() @staticmethod def A__ ( _SCREAMING_SNAKE_CASE ) -> bytes: """simple docstring""" UpperCamelCase = B"""\x80""" + (B"""\x00""" * (63 - (len(_SCREAMING_SNAKE_CASE ) + 8) % 64)) UpperCamelCase = struct.pack(""">Q""" , (len(_SCREAMING_SNAKE_CASE ) * 8) ) return data + padding + big_endian_integer def A__ ( self ) -> None: """simple docstring""" UpperCamelCase = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers UpperCamelCase = list(struct.unpack(""">16L""" , _SCREAMING_SNAKE_CASE ) ) # add 48 0-ed integers words += [0] * 48 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array UpperCamelCase = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) UpperCamelCase = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) UpperCamelCase = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x1_00_00_00_00 # Compression UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 6 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 11 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 25 ) UpperCamelCase = (e & f) ^ ((~e & 0xFF_FF_FF_FF) & g) UpperCamelCase = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x1_00_00_00_00 UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 2 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 13 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 22 ) UpperCamelCase = (a & b) ^ (a & c) ^ (b & c) UpperCamelCase = (sa + maj) % 0x1_00_00_00_00 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = ( g, f, e, ((d + tempa) % 0x1_00_00_00_00), c, b, a, ((tempa + tempa) % 0x1_00_00_00_00), ) UpperCamelCase = [a, b, c, d, e, f, g, h] # Modify final values UpperCamelCase = [ ((element + mutated_hash_values[index]) % 0x1_00_00_00_00) for index, element in enumerate(self.hashes ) ] UpperCamelCase = """""".join([hex(_SCREAMING_SNAKE_CASE )[2:].zfill(8 ) for value in self.hashes] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return 0xFF_FF_FF_FF & (value << (32 - rotations)) | (value >> rotations) class a_ ( unittest.TestCase ): def A__ ( self ) -> None: """simple docstring""" import hashlib UpperCamelCase = bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(_SCREAMING_SNAKE_CASE ).hash , hashlib.shaaaa(_SCREAMING_SNAKE_CASE ).hexdigest() ) def lowercase__ ( )-> None: import doctest doctest.testmod() UpperCamelCase = argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: UpperCamelCase = f.read() else: UpperCamelCase = bytes(__UpperCamelCase , """utf-8""" ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
321
0
import os from tempfile import TemporaryDirectory from unittest import TestCase import pytest from absl.testing import parameterized from datasets import config from datasets.arrow_reader import HF_GCP_BASE_URL from datasets.builder import DatasetBuilder from datasets.dataset_dict import IterableDatasetDict from datasets.iterable_dataset import IterableDataset from datasets.load import dataset_module_factory, import_main_class from datasets.utils.file_utils import cached_path A__ : Any = [ {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.de'''}, {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.en'''}, {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.fr'''}, {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.frr'''}, {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.it'''}, {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.simple'''}, {'''dataset''': '''snli''', '''config_name''': '''plain_text'''}, {'''dataset''': '''eli5''', '''config_name''': '''LFQA_reddit'''}, {'''dataset''': '''wiki40b''', '''config_name''': '''en'''}, {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.nq.compressed'''}, {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.nq.no_index'''}, {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.multiset.no_index'''}, {'''dataset''': '''natural_questions''', '''config_name''': '''default'''}, ] def UpperCamelCase( __UpperCamelCase : Dict=True ): if with_config: return [ { "testcase_name": d["dataset"] + "/" + d["config_name"], "dataset": d["dataset"], "config_name": d["config_name"], } for d in DATASETS_ON_HF_GCP ] else: return [ {"testcase_name": dataset, "dataset": dataset} for dataset in {d["dataset"] for d in DATASETS_ON_HF_GCP} ] @parameterized.named_parameters(list_datasets_on_hf_gcp_parameters(with_config=UpperCamelCase_ ) ) class __snake_case ( UpperCamelCase_ ): _a = None _a = None def UpperCAmelCase__ ( self : List[str] , A_ : Tuple , A_ : Dict): with TemporaryDirectory() as tmp_dir: lowerCAmelCase_ : Dict = dataset_module_factory(A_ , cache_dir=A_) lowerCAmelCase_ : Union[str, Any] = import_main_class(dataset_module.module_path , dataset=A_) lowerCAmelCase_ : DatasetBuilder = builder_cls( cache_dir=A_ , config_name=A_ , hash=dataset_module.hash , ) lowerCAmelCase_ : Optional[Any] = '''/'''.join( [ HF_GCP_BASE_URL, builder_instance._relative_data_dir(with_hash=A_).replace(os.sep , '''/'''), config.DATASET_INFO_FILENAME, ]) lowerCAmelCase_ : Optional[Any] = cached_path(A_ , cache_dir=A_) self.assertTrue(os.path.exists(A_)) @pytest.mark.integration def UpperCamelCase( __UpperCamelCase : Any ): lowerCAmelCase_ : Any = tmp_path_factory.mktemp('''test_hf_gcp''' ) / '''test_wikipedia_simple''' lowerCAmelCase_ : List[Any] = dataset_module_factory('''wikipedia''' ,cache_dir=__UpperCamelCase ) lowerCAmelCase_ : List[Any] = import_main_class(dataset_module.module_path ) lowerCAmelCase_ : DatasetBuilder = builder_cls( cache_dir=__UpperCamelCase ,config_name='''20220301.frr''' ,hash=dataset_module.hash ,) # use the HF cloud storage, not the original download_and_prepare that uses apache-beam lowerCAmelCase_ : Optional[Any] = None builder_instance.download_and_prepare() lowerCAmelCase_ : Tuple = builder_instance.as_dataset() assert ds @pytest.mark.integration def UpperCamelCase( __UpperCamelCase : int ): lowerCAmelCase_ : Union[str, Any] = dataset_module_factory('''wikipedia''' ,cache_dir=__UpperCamelCase ) lowerCAmelCase_ : List[Any] = import_main_class(dataset_module.module_path ,dataset=__UpperCamelCase ) lowerCAmelCase_ : DatasetBuilder = builder_cls( cache_dir=__UpperCamelCase ,config_name='''20220301.frr''' ,hash=dataset_module.hash ,) lowerCAmelCase_ : List[Any] = builder_instance.as_streaming_dataset() assert ds assert isinstance(__UpperCamelCase ,__UpperCamelCase ) assert "train" in ds assert isinstance(ds['''train'''] ,__UpperCamelCase ) assert next(iter(ds['''train'''] ) )
103
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) SCREAMING_SNAKE_CASE__ = _symbol_database.Default() SCREAMING_SNAKE_CASE__ = _descriptor_pool.Default().AddSerializedFile( b'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03' ) SCREAMING_SNAKE_CASE__ = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS is False: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = b'H\003' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" SCREAMING_SNAKE_CASE__ = 4_5 SCREAMING_SNAKE_CASE__ = 1_5_8_1 SCREAMING_SNAKE_CASE__ = 1_5_1_7 SCREAMING_SNAKE_CASE__ = 1_5_7_0 SCREAMING_SNAKE_CASE__ = 1_5_8_4 SCREAMING_SNAKE_CASE__ = 1_7_9_3 SCREAMING_SNAKE_CASE__ = 1_7_9_5 SCREAMING_SNAKE_CASE__ = 1_9_1_6 SCREAMING_SNAKE_CASE__ = 1_8_6_4 SCREAMING_SNAKE_CASE__ = 1_9_0_5 SCREAMING_SNAKE_CASE__ = 1_9_1_9 SCREAMING_SNAKE_CASE__ = 2_4_2_9 SCREAMING_SNAKE_CASE__ = 2_2_0_8 SCREAMING_SNAKE_CASE__ = 2_4_1_8 SCREAMING_SNAKE_CASE__ = 2_3_2_3 SCREAMING_SNAKE_CASE__ = 2_4_0_7 # @@protoc_insertion_point(module_scope)
321
0
'''simple docstring''' from __future__ import annotations class lowercase_ : """simple docstring""" def __init__( self : Optional[int] ,lowercase__ : str ,lowercase__ : str ): __lowercase , __lowercase = text, pattern __lowercase , __lowercase = len(lowercase__ ), len(lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Any ,lowercase__ : str ): for i in range(self.patLen - 1 ,-1 ,-1 ): if char == self.pattern[i]: return i return -1 def SCREAMING_SNAKE_CASE ( self : int ,lowercase__ : int ): for i in range(self.patLen - 1 ,-1 ,-1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def SCREAMING_SNAKE_CASE ( self : int ): # searches pattern in text and returns index positions __lowercase = [] for i in range(self.textLen - self.patLen + 1 ): __lowercase = self.mismatch_in_text(lowercase__ ) if mismatch_index == -1: positions.append(lowercase__ ) else: __lowercase = self.match_in_pattern(self.text[mismatch_index] ) __lowercase = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions lowerCAmelCase__ = '''ABAABA''' lowerCAmelCase__ = '''AB''' lowerCAmelCase__ = BoyerMooreSearch(text, pattern) lowerCAmelCase__ = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
104
'''simple docstring''' SCREAMING_SNAKE_CASE__ = 8.31_44_62 # Unit - J mol-1 K-1 def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
321
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( a__ , a__ , unittest.TestCase ): lowerCamelCase : Union[str, Any] =StableDiffusionSAGPipeline lowerCamelCase : Optional[int] =TEXT_TO_IMAGE_PARAMS lowerCamelCase : int =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : Optional[int] =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Any =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Tuple =False def __a ( self ) -> List[str]: torch.manual_seed(0 ) a : List[Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) a : Tuple = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="scaled_linear" , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , ) torch.manual_seed(0 ) a : Tuple = 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 ) a : Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) a : Any = CLIPTextModel(lowerCAmelCase__ ) a : int = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) a : Optional[Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def __a ( self , lowerCAmelCase__ , lowerCAmelCase__=0 ) -> Optional[int]: if str(lowerCAmelCase__ ).startswith("mps" ): a : Optional[int] = torch.manual_seed(lowerCAmelCase__ ) else: a : Tuple = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) a : Optional[Any] = { "prompt": ".", "generator": generator, "num_inference_steps": 2, "guidance_scale": 1.0, "sag_scale": 1.0, "output_type": "numpy", } return inputs def __a ( self ) -> List[str]: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def __a ( self ) -> List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self ) -> Dict: a : Any = StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" ) a : Any = sag_pipe.to(lowerCAmelCase__ ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) a : str = "." a : int = torch.manual_seed(0 ) a : int = sag_pipe( [prompt] , generator=lowerCAmelCase__ , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="np" ) a : Optional[Any] = output.images a : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) a : Optional[Any] = np.array([0.1_568, 0.1_738, 0.1_695, 0.1_693, 0.1_507, 0.1_705, 0.1_547, 0.1_751, 0.1_949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def __a ( self ) -> Optional[int]: a : Optional[Any] = StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) a : Tuple = sag_pipe.to(lowerCAmelCase__ ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) a : int = "." a : str = torch.manual_seed(0 ) a : Optional[Any] = sag_pipe( [prompt] , generator=lowerCAmelCase__ , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="np" ) a : Optional[int] = output.images a : int = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) a : str = np.array([0.3_459, 0.2_876, 0.2_537, 0.3_002, 0.2_671, 0.2_160, 0.3_026, 0.2_262, 0.2_371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def __a ( self ) -> Dict: a : Tuple = StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) a : Optional[int] = sag_pipe.to(lowerCAmelCase__ ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) a : Optional[int] = "." a : Tuple = torch.manual_seed(0 ) a : Union[str, Any] = sag_pipe( [prompt] , width=768 , height=512 , generator=lowerCAmelCase__ , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="np" , ) a : List[Any] = output.images assert image.shape == (1, 512, 768, 3)
105
'''simple docstring''' import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem SCREAMING_SNAKE_CASE__ = importlib.util.find_spec('s3fs') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 SCREAMING_SNAKE_CASE__ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def lowercase__ ( __UpperCamelCase )-> str: if "://" in dataset_path: UpperCamelCase = dataset_path.split("""://""" )[1] return dataset_path def lowercase__ ( __UpperCamelCase )-> bool: if fs is not None and fs.protocol != "file": return True else: return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> int: UpperCamelCase = not is_remote_filesystem(__UpperCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(__UpperCamelCase ) , fs._strip_protocol(__UpperCamelCase ) ) else: fs.mv(__UpperCamelCase , __UpperCamelCase , recursive=__UpperCamelCase ) def lowercase__ ( )-> None: if hasattr(fsspec.asyn , """reset_lock""" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: UpperCamelCase = None UpperCamelCase = None UpperCamelCase = threading.Lock()
321
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class SCREAMING_SNAKE_CASE ( a_ , unittest.TestCase ): """simple docstring""" lowercase__ = KandinskyInpaintPipeline lowercase__ = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] lowercase__ = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] lowercase__ = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] lowercase__ = False @property def __lowerCAmelCase ( self : int ): return 3_2 @property def __lowerCAmelCase ( self : str ): return 3_2 @property def __lowerCAmelCase ( self : str ): return self.time_input_dim @property def __lowerCAmelCase ( self : int ): return self.time_input_dim * 4 @property def __lowerCAmelCase ( self : Optional[Any] ): return 1_0_0 @property def __lowerCAmelCase ( self : Tuple ): lowerCAmelCase__ : Optional[int] = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __lowerCAmelCase ( self : Dict ): torch.manual_seed(0 ) lowerCAmelCase__ : List[str] = MCLIPConfig( numDims=self.cross_attention_dim ,transformerDimensions=self.text_embedder_hidden_size ,hidden_size=self.text_embedder_hidden_size ,intermediate_size=3_7 ,num_attention_heads=4 ,num_hidden_layers=5 ,vocab_size=1_0_0_5 ,) lowerCAmelCase__ : Any = MultilingualCLIP(lowercase_ ) lowerCAmelCase__ : Optional[int] = text_encoder.eval() return text_encoder @property def __lowerCAmelCase ( self : List[Any] ): torch.manual_seed(0 ) lowerCAmelCase__ : Optional[Any] = { '''in_channels''': 9, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } lowerCAmelCase__ : str = UNetaDConditionModel(**lowercase_ ) return model @property def __lowerCAmelCase ( self : List[Any] ): return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowerCAmelCase ( self : Any ): torch.manual_seed(0 ) lowerCAmelCase__ : Optional[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowerCAmelCase ( self : Any ): lowerCAmelCase__ : Union[str, Any] = self.dummy_text_encoder lowerCAmelCase__ : List[str] = self.dummy_tokenizer lowerCAmelCase__ : List[str] = self.dummy_unet lowerCAmelCase__ : int = self.dummy_movq lowerCAmelCase__ : Tuple = DDIMScheduler( num_train_timesteps=1_0_0_0 ,beta_schedule='''linear''' ,beta_start=0.0_0085 ,beta_end=0.012 ,clip_sample=lowercase_ ,set_alpha_to_one=lowercase_ ,steps_offset=1 ,prediction_type='''epsilon''' ,thresholding=lowercase_ ,) lowerCAmelCase__ : Optional[int] = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowerCAmelCase ( self : List[Any] ,lowercase_ : Dict ,lowercase_ : int=0 ): lowerCAmelCase__ : Dict = floats_tensor((1, self.cross_attention_dim) ,rng=random.Random(lowercase_ ) ).to(lowercase_ ) lowerCAmelCase__ : Optional[Any] = floats_tensor((1, self.cross_attention_dim) ,rng=random.Random(seed + 1 ) ).to(lowercase_ ) # create init_image lowerCAmelCase__ : str = floats_tensor((1, 3, 6_4, 6_4) ,rng=random.Random(lowercase_ ) ).to(lowercase_ ) lowerCAmelCase__ : Dict = image.cpu().permute(0 ,2 ,3 ,1 )[0] lowerCAmelCase__ : Optional[Any] = Image.fromarray(np.uinta(lowercase_ ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask lowerCAmelCase__ : Tuple = np.ones((6_4, 6_4) ,dtype=np.floataa ) lowerCAmelCase__ : List[Any] = 0 if str(lowercase_ ).startswith('''mps''' ): lowerCAmelCase__ : Union[str, Any] = torch.manual_seed(lowercase_ ) else: lowerCAmelCase__ : Dict = torch.Generator(device=lowercase_ ).manual_seed(lowercase_ ) lowerCAmelCase__ : List[str] = { '''prompt''': '''horse''', '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowerCAmelCase ( self : Any ): lowerCAmelCase__ : List[Any] = '''cpu''' lowerCAmelCase__ : Optional[Any] = self.get_dummy_components() lowerCAmelCase__ : List[Any] = self.pipeline_class(**lowercase_ ) lowerCAmelCase__ : Optional[Any] = pipe.to(lowercase_ ) pipe.set_progress_bar_config(disable=lowercase_ ) lowerCAmelCase__ : str = pipe(**self.get_dummy_inputs(lowercase_ ) ) lowerCAmelCase__ : Optional[int] = output.images lowerCAmelCase__ : str = pipe( **self.get_dummy_inputs(lowercase_ ) ,return_dict=lowercase_ ,)[0] lowerCAmelCase__ : List[Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : str = image_from_tuple[0, -3:, -3:, -1] print(F'image.shape {image.shape}' ) assert image.shape == (1, 6_4, 6_4, 3) lowerCAmelCase__ : List[Any] = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' def __lowerCAmelCase ( self : Any ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def __lowerCAmelCase ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self : Union[str, Any] ): lowerCAmelCase__ : Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy''' ) lowerCAmelCase__ : Any = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) lowerCAmelCase__ : Optional[Any] = np.ones((7_6_8, 7_6_8) ,dtype=np.floataa ) lowerCAmelCase__ : List[str] = 0 lowerCAmelCase__ : List[str] = '''a hat''' lowerCAmelCase__ : List[str] = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' ,torch_dtype=torch.floataa ) pipe_prior.to(lowercase_ ) lowerCAmelCase__ : int = KandinskyInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-inpaint''' ,torch_dtype=torch.floataa ) lowerCAmelCase__ : List[Any] = pipeline.to(lowercase_ ) pipeline.set_progress_bar_config(disable=lowercase_ ) lowerCAmelCase__ : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) lowerCAmelCase__ ,lowerCAmelCase__ : Optional[Any] = pipe_prior( lowercase_ ,generator=lowercase_ ,num_inference_steps=5 ,negative_prompt='''''' ,).to_tuple() lowerCAmelCase__ : Union[str, Any] = pipeline( lowercase_ ,image=lowercase_ ,mask_image=lowercase_ ,image_embeds=lowercase_ ,negative_image_embeds=lowercase_ ,generator=lowercase_ ,num_inference_steps=1_0_0 ,height=7_6_8 ,width=7_6_8 ,output_type='''np''' ,) lowerCAmelCase__ : Any = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowercase_ ,lowercase_ )
106
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_xlm_roberta_xl': [ 'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaXLConfig', 'XLMRobertaXLOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaXLForCausalLM', 'XLMRobertaXLForMaskedLM', 'XLMRobertaXLForMultipleChoice', 'XLMRobertaXLForQuestionAnswering', 'XLMRobertaXLForSequenceClassification', 'XLMRobertaXLForTokenClassification', 'XLMRobertaXLModel', 'XLMRobertaXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure)
321
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase : Optional[Any] = { 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[int] = ['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Tuple = [ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : str = [ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : str = [ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys __lowerCAmelCase : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
107
'''simple docstring''' import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = 'docs/source/en/_toctree.yml' def lowercase__ ( __UpperCamelCase )-> Optional[Any]: UpperCamelCase = defaultdict(__UpperCamelCase ) UpperCamelCase = [] UpperCamelCase = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__UpperCamelCase ) UpperCamelCase = new_doc_list UpperCamelCase = [key for key, value in counts.items() if value > 1] UpperCamelCase = [] for duplicate_key in duplicates: UpperCamelCase = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__UpperCamelCase ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) UpperCamelCase = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__UpperCamelCase ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__UpperCamelCase ) # Sort return overview_doc def lowercase__ ( __UpperCamelCase=False )-> List[str]: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 UpperCamelCase = api_doc[scheduler_idx]["""sections"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) UpperCamelCase = False if new_scheduler_doc != scheduler_doc: UpperCamelCase = True if overwrite: UpperCamelCase = new_scheduler_doc if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def lowercase__ ( __UpperCamelCase=False )-> Tuple: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 UpperCamelCase = False UpperCamelCase = api_doc[pipeline_idx]["""sections"""] UpperCamelCase = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: UpperCamelCase = pipeline_doc["""section"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if overwrite: UpperCamelCase = new_sub_pipeline_doc new_pipeline_docs.append(__UpperCamelCase ) # sort overall pipeline doc UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if new_pipeline_docs != pipeline_docs: UpperCamelCase = True if overwrite: UpperCamelCase = new_pipeline_docs if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) 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__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
321
0
"""simple docstring""" from string import ascii_uppercase lowerCAmelCase__ = {str(ord(c) - 55): c for c in ascii_uppercase} def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): raise TypeError("int() can't convert non-string with explicit base" ) if num < 0: raise ValueError("parameter must be positive int" ) if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): raise TypeError("'str' object cannot be interpreted as an integer" ) if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): raise TypeError("'float' object cannot be interpreted as an integer" ) if base in (0, 1): raise ValueError("base must be >= 2" ) if base > 3_6: raise ValueError("base must be <= 36" ) lowerCAmelCase : str = "" lowerCAmelCase : Optional[Any] = 0 lowerCAmelCase : List[Any] = 0 while div != 1: lowerCAmelCase , lowerCAmelCase : Optional[Any] = divmod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if base >= 1_1 and 9 < mod < 3_6: lowerCAmelCase : int = ALPHABET_VALUES[str(SCREAMING_SNAKE_CASE )] else: lowerCAmelCase : List[str] = str(SCREAMING_SNAKE_CASE ) new_value += actual_value lowerCAmelCase : Dict = num // base lowerCAmelCase : Tuple = div if div == 0: return str(new_value[::-1] ) elif div == 1: new_value += str(SCREAMING_SNAKE_CASE ) return str(new_value[::-1] ) return new_value[::-1] if __name__ == "__main__": import doctest doctest.testmod() for base in range(2, 37): for num in range(1_000): assert int(decimal_to_any(num, base), base) == num, ( num, base, decimal_to_any(num, base), int(decimal_to_any(num, base), base), )
108
'''simple docstring''' import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) with open(F"{class_data_dir}/caption.txt" , """w""" ) as fa, open(F"{class_data_dir}/urls.txt" , """w""" ) as fa, open( F"{class_data_dir}/images.txt" , """w""" ) as fa: while total < num_class_images: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = Image.open(BytesIO(img.content ) ) with open(F"{class_data_dir}/images/{total}.jpg" , """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(F"{class_data_dir}/images/{total}.jpg" + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
0
"""simple docstring""" from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
109
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = 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 a_ : lowercase = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The column name of the images in the files."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the training data."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the validation data."""} ) lowercase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class a_ : lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) lowercase = field( default=lowerCamelCase , 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""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) lowercase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class a_ ( lowerCamelCase ): lowercase = field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def lowercase__ ( __UpperCamelCase )-> int: UpperCamelCase = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def lowercase__ ( )-> List[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase ,UpperCamelCase ,UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase ,UpperCamelCase ,UpperCamelCase = 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""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCamelCase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase = split["""train"""] UpperCamelCase = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(F"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(F"New config: {config}" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase = ViTMAEForPreTraining(__UpperCamelCase ) if training_args.do_train: UpperCamelCase = ds["""train"""].column_names else: UpperCamelCase = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = """image""" elif "img" in column_names: UpperCamelCase = """img""" else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size["""shortest_edge"""] else: UpperCamelCase = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase = Compose( [ Lambda(lambda __UpperCamelCase : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(__UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__UpperCamelCase ): UpperCamelCase = [transforms(__UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__UpperCamelCase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def lowercase__ ( __UpperCamelCase )-> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
321
0
"""simple docstring""" import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase_ = 16 lowerCAmelCase_ = 32 def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase = 16 ) -> Tuple: lowercase__ : Tuple = AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowercase__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__lowerCamelCase ): # max_length=None => use the model max length (it's actually the default) lowercase__ : Tuple = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): lowercase__ : Any = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowercase__ : str = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__lowerCamelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. lowercase__ : Tuple = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowercase__ : Optional[Any] = 16 elif accelerator.mixed_precision != "no": lowercase__ : Any = 8 else: lowercase__ : Optional[Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. lowercase__ : Union[str, Any] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase , drop_last=__UpperCamelCase ) lowercase__ : List[str] = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase , drop_last=(accelerator.mixed_precision == '''fp8''') , ) return train_dataloader, eval_dataloader def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> Dict: # Initialize accelerator lowercase__ : Dict = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowercase__ : int = config['''lr'''] lowercase__ : str = int(config['''num_epochs'''] ) lowercase__ : Any = int(config['''seed'''] ) lowercase__ : Dict = int(config['''batch_size'''] ) lowercase__ : str = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation lowercase__ : Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: lowercase__ : Union[str, Any] = batch_size // MAX_GPU_BATCH_SIZE lowercase__ : str = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) lowercase__ , lowercase__ : Union[str, Any] = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowercase__ : int = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowercase__ : int = model.to(accelerator.device ) # Instantiate optimizer lowercase__ : Dict = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler lowercase__ : List[str] = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ : str = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowercase__ : Optional[Any] = model(**__UpperCamelCase ) lowercase__ : Tuple = outputs.loss lowercase__ : Any = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowercase__ : Optional[int] = model(**__UpperCamelCase ) lowercase__ : str = outputs.logits.argmax(dim=-1 ) lowercase__ , lowercase__ : Any = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) lowercase__ : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}:""" , __UpperCamelCase ) def __UpperCAmelCase ( ) -> Union[str, Any]: lowercase__ : Optional[Any] = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) lowercase__ : Optional[Any] = parser.parse_args() lowercase__ : Union[str, Any] = {'''lr''': 2E-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
16
'''simple docstring''' import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # pylint: disable=invalid-name SCREAMING_SNAKE_CASE__ = 2_5_6 class a_ ( lowerCamelCase ): lowercase = ["""melgan"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> None: """simple docstring""" super().__init__() # From MELGAN UpperCamelCase = math.log(1e-5 ) # Matches MelGAN training. UpperCamelCase = 4.0 # Largest value for most examples UpperCamelCase = 128 self.register_modules( notes_encoder=_SCREAMING_SNAKE_CASE , continuous_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , melgan=_SCREAMING_SNAKE_CASE , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" UpperCamelCase ,UpperCamelCase = output_range if clip: UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , self.min_value , self.max_value ) # Scale to [0, 1]. UpperCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase ,UpperCamelCase = input_range UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if clip else outputs # Scale to [0, 1]. UpperCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" UpperCamelCase = input_tokens > 0 UpperCamelCase ,UpperCamelCase = self.notes_encoder( encoder_input_tokens=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) UpperCamelCase ,UpperCamelCase = self.continuous_encoder( encoder_inputs=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" UpperCamelCase = noise_time if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML UpperCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) UpperCamelCase = self.decoder( encodings_and_masks=_SCREAMING_SNAKE_CASE , decoder_input_tokens=_SCREAMING_SNAKE_CASE , decoder_noise_time=_SCREAMING_SNAKE_CASE ) return logits @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "numpy" , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , ) -> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_SCREAMING_SNAKE_CASE )}." ) UpperCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) UpperCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) UpperCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) for i, encoder_input_tokens in enumerate(_SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. UpperCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. UpperCamelCase = ones UpperCamelCase = self.scale_features( _SCREAMING_SNAKE_CASE , output_range=[-1.0, 1.0] , clip=_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_SCREAMING_SNAKE_CASE , continuous_mask=_SCREAMING_SNAKE_CASE , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop UpperCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_SCREAMING_SNAKE_CASE , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_SCREAMING_SNAKE_CASE ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCamelCase = self.decode( encodings_and_masks=_SCREAMING_SNAKE_CASE , input_tokens=_SCREAMING_SNAKE_CASE , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 UpperCamelCase = self.scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = self.scale_to_features(_SCREAMING_SNAKE_CASE , input_range=[-1.0, 1.0] ) UpperCamelCase = mel[:1] UpperCamelCase = mel.cpu().float().numpy() UpperCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) logger.info("""Generated segment""" , _SCREAMING_SNAKE_CASE ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": UpperCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: UpperCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" import argparse import os from . import ( ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BART_PRETRAINED_MODEL_ARCHIVE_LIST, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, BartConfig, BertConfig, CamembertConfig, CTRLConfig, DistilBertConfig, DPRConfig, ElectraConfig, FlaubertConfig, GPTaConfig, LayoutLMConfig, LxmertConfig, OpenAIGPTConfig, RobertaConfig, TaConfig, TFAlbertForPreTraining, TFBartForConditionalGeneration, TFBartForSequenceClassification, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFCamembertForMaskedLM, TFCTRLLMHeadModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, TFElectraForPreTraining, TFFlaubertWithLMHeadModel, TFGPTaLMHeadModel, TFLayoutLMForMaskedLM, TFLxmertForPreTraining, TFLxmertVisualFeatureEncoder, TFOpenAIGPTLMHeadModel, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForSequenceClassification, TFTaForConditionalGeneration, TFTransfoXLLMHeadModel, TFWavaVecaModel, TFXLMRobertaForMaskedLM, TFXLMWithLMHeadModel, TFXLNetLMHeadModel, TransfoXLConfig, WavaVecaConfig, WavaVecaModel, XLMConfig, XLMRobertaConfig, XLNetConfig, is_torch_available, load_pytorch_checkpoint_in_tfa_model, ) from .utils import CONFIG_NAME, WEIGHTS_NAME, cached_file, logging if is_torch_available(): import numpy as np import torch from . import ( AlbertForPreTraining, BartForConditionalGeneration, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, CamembertForMaskedLM, CTRLLMHeadModel, DistilBertForMaskedLM, DistilBertForQuestionAnswering, DPRContextEncoder, DPRQuestionEncoder, DPRReader, ElectraForPreTraining, FlaubertWithLMHeadModel, GPTaLMHeadModel, LayoutLMForMaskedLM, LxmertForPreTraining, LxmertVisualFeatureEncoder, OpenAIGPTLMHeadModel, RobertaForMaskedLM, RobertaForSequenceClassification, TaForConditionalGeneration, TransfoXLLMHeadModel, XLMRobertaForMaskedLM, XLMWithLMHeadModel, XLNetLMHeadModel, ) logging.set_verbosity_info() _a = { 'bart': ( BartConfig, TFBartForConditionalGeneration, TFBartForSequenceClassification, BartForConditionalGeneration, BART_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'bert': ( BertConfig, TFBertForPreTraining, BertForPreTraining, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-base-cased-finetuned-mrpc': ( BertConfig, TFBertForSequenceClassification, BertForSequenceClassification, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'dpr': ( DPRConfig, TFDPRQuestionEncoder, TFDPRContextEncoder, TFDPRReader, DPRQuestionEncoder, DPRContextEncoder, DPRReader, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'gpt2': ( GPTaConfig, TFGPTaLMHeadModel, GPTaLMHeadModel, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlnet': ( XLNetConfig, TFXLNetLMHeadModel, XLNetLMHeadModel, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm': ( XLMConfig, TFXLMWithLMHeadModel, XLMWithLMHeadModel, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm-roberta': ( XLMRobertaConfig, TFXLMRobertaForMaskedLM, XLMRobertaForMaskedLM, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'transfo-xl': ( TransfoXLConfig, TFTransfoXLLMHeadModel, TransfoXLLMHeadModel, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'openai-gpt': ( OpenAIGPTConfig, TFOpenAIGPTLMHeadModel, OpenAIGPTLMHeadModel, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'roberta': ( RobertaConfig, TFRobertaForCausalLM, TFRobertaForMaskedLM, RobertaForMaskedLM, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'layoutlm': ( LayoutLMConfig, TFLayoutLMForMaskedLM, LayoutLMForMaskedLM, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'roberta-large-mnli': ( RobertaConfig, TFRobertaForSequenceClassification, RobertaForSequenceClassification, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'camembert': ( CamembertConfig, TFCamembertForMaskedLM, CamembertForMaskedLM, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'flaubert': ( FlaubertConfig, TFFlaubertWithLMHeadModel, FlaubertWithLMHeadModel, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert': ( DistilBertConfig, TFDistilBertForMaskedLM, DistilBertForMaskedLM, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert-base-distilled-squad': ( DistilBertConfig, TFDistilBertForQuestionAnswering, DistilBertForQuestionAnswering, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert': ( LxmertConfig, TFLxmertForPreTraining, LxmertForPreTraining, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert-visual-feature-encoder': ( LxmertConfig, TFLxmertVisualFeatureEncoder, LxmertVisualFeatureEncoder, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'ctrl': ( CTRLConfig, TFCTRLLMHeadModel, CTRLLMHeadModel, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'albert': ( AlbertConfig, TFAlbertForPreTraining, AlbertForPreTraining, ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 't5': ( TaConfig, TFTaForConditionalGeneration, TaForConditionalGeneration, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'electra': ( ElectraConfig, TFElectraForPreTraining, ElectraForPreTraining, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'wav2vec2': ( WavaVecaConfig, TFWavaVecaModel, WavaVecaModel, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), } def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase=False, __lowerCamelCase=True ): if model_type not in MODEL_CLASSES: raise ValueError(f"""Unrecognized model type, should be one of {list(MODEL_CLASSES.keys() )}.""" ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = MODEL_CLASSES[model_type] # Initialise TF model if config_file in aws_config_map: UpperCAmelCase_ : str = cached_file(__UpperCamelCase, __UpperCamelCase, force_download=not use_cached_models ) UpperCAmelCase_ : Optional[int] = config_class.from_json_file(__UpperCamelCase ) UpperCAmelCase_ : Union[str, Any] = True UpperCAmelCase_ : Any = True print(f"""Building TensorFlow model from configuration: {config}""" ) UpperCAmelCase_ : Dict = model_class(__UpperCamelCase ) # Load weights from tf checkpoint if pytorch_checkpoint_path in aws_config_map.keys(): UpperCAmelCase_ : int = cached_file( __UpperCamelCase, __UpperCamelCase, force_download=not use_cached_models ) # Load PyTorch checkpoint in tf2 model: UpperCAmelCase_ : Dict = load_pytorch_checkpoint_in_tfa_model(__UpperCamelCase, __UpperCamelCase ) if compare_with_pt_model: UpperCAmelCase_ : Dict = tf_model(tf_model.dummy_inputs, training=__UpperCamelCase ) # build the network UpperCAmelCase_ : Optional[Any] = torch.load(__UpperCamelCase, map_location="cpu" ) UpperCAmelCase_ : List[Any] = pt_model_class.from_pretrained( pretrained_model_name_or_path=__UpperCamelCase, config=__UpperCamelCase, state_dict=__UpperCamelCase ) with torch.no_grad(): UpperCAmelCase_ : Any = pt_model(**pt_model.dummy_inputs ) UpperCAmelCase_ : List[Any] = pto[0].numpy() UpperCAmelCase_ : List[Any] = tfo[0].numpy() UpperCAmelCase_ : Optional[int] = np.amax(np.abs(np_pt - np_tf ) ) print(f"""Max absolute difference between models outputs {diff}""" ) assert diff <= 2E-2, f"""Error, model absolute difference is >2e-2: {diff}""" # Save pytorch-model print(f"""Save TensorFlow model to {tf_dump_path}""" ) tf_model.save_weights(__UpperCamelCase, save_format="h5" ) def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase=None, __lowerCamelCase=None, __lowerCamelCase=False, __lowerCamelCase=False, __lowerCamelCase=False, __lowerCamelCase=False, ): if args_model_type is None: UpperCAmelCase_ : List[str] = list(MODEL_CLASSES.keys() ) else: UpperCAmelCase_ : int = [args_model_type] for j, model_type in enumerate(__UpperCamelCase, start=1 ): print("=" * 100 ) print(f""" Converting model type {j}/{len(__UpperCamelCase )}: {model_type}""" ) print("=" * 100 ) if model_type not in MODEL_CLASSES: raise ValueError(f"""Unrecognized model type {model_type}, should be one of {list(MODEL_CLASSES.keys() )}.""" ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = MODEL_CLASSES[model_type] if model_shortcut_names_or_path is None: UpperCAmelCase_ : List[str] = list(aws_model_maps.keys() ) if config_shortcut_names_or_path is None: UpperCAmelCase_ : Optional[int] = model_shortcut_names_or_path for i, (model_shortcut_name, config_shortcut_name) in enumerate( zip(__UpperCamelCase, __UpperCamelCase ), start=1 ): print("-" * 100 ) if "-squad" in model_shortcut_name or "-mrpc" in model_shortcut_name or "-mnli" in model_shortcut_name: if not only_convert_finetuned_models: print(f""" Skipping finetuned checkpoint {model_shortcut_name}""" ) continue UpperCAmelCase_ : str = model_shortcut_name elif only_convert_finetuned_models: print(f""" Skipping not finetuned checkpoint {model_shortcut_name}""" ) continue print( f""" Converting checkpoint {i}/{len(__UpperCamelCase )}: {model_shortcut_name} - model_type {model_type}""" ) print("-" * 100 ) if config_shortcut_name in aws_config_map: UpperCAmelCase_ : Union[str, Any] = cached_file(__UpperCamelCase, __UpperCamelCase, force_download=not use_cached_models ) else: UpperCAmelCase_ : Any = config_shortcut_name if model_shortcut_name in aws_model_maps: UpperCAmelCase_ : Any = cached_file(__UpperCamelCase, __UpperCamelCase, force_download=not use_cached_models ) else: UpperCAmelCase_ : Optional[int] = model_shortcut_name if os.path.isfile(__UpperCamelCase ): UpperCAmelCase_ : Dict = "converted_model" convert_pt_checkpoint_to_tf( model_type=__UpperCamelCase, pytorch_checkpoint_path=__UpperCamelCase, config_file=__UpperCamelCase, tf_dump_path=os.path.join(__UpperCamelCase, model_shortcut_name + "-tf_model.h5" ), compare_with_pt_model=__UpperCamelCase, ) if remove_cached_files: os.remove(__UpperCamelCase ) os.remove(__UpperCamelCase ) if __name__ == "__main__": _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_dump_path', default=None, type=str, required=True, help='Path to the output Tensorflow dump file.' ) parser.add_argument( '--model_type', default=None, type=str, help=( f"""Model type selected in the list of {list(MODEL_CLASSES.keys())}. If not given, will download and """ 'convert all the models from AWS.' ), ) parser.add_argument( '--pytorch_checkpoint_path', default=None, type=str, help=( 'Path to the PyTorch checkpoint path or shortcut name to download from AWS. ' 'If not given, will download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--config_file', default=None, type=str, help=( 'The config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture. If not given and ' '--pytorch_checkpoint_path is not given or is a shortcut name ' 'use the configuration associated to the shortcut name on the AWS' ), ) parser.add_argument( '--compare_with_pt_model', action='store_true', help='Compare Tensorflow and PyTorch model predictions.' ) parser.add_argument( '--use_cached_models', action='store_true', help='Use cached models if possible instead of updating to latest checkpoint versions.', ) parser.add_argument( '--remove_cached_files', action='store_true', help='Remove pytorch models after conversion (save memory when converting in batches).', ) parser.add_argument('--only_convert_finetuned_models', action='store_true', help='Only convert finetuned models.') _a = parser.parse_args() # if args.pytorch_checkpoint_path is not None: # convert_pt_checkpoint_to_tf(args.model_type.lower(), # args.pytorch_checkpoint_path, # args.config_file if args.config_file is not None else args.pytorch_checkpoint_path, # args.tf_dump_path, # compare_with_pt_model=args.compare_with_pt_model, # use_cached_models=args.use_cached_models) # else: convert_all_pt_checkpoints_to_tf( args.model_type.lower() if args.model_type is not None else None, args.tf_dump_path, model_shortcut_names_or_path=[args.pytorch_checkpoint_path] if args.pytorch_checkpoint_path is not None else None, config_shortcut_names_or_path=[args.config_file] if args.config_file is not None else None, compare_with_pt_model=args.compare_with_pt_model, use_cached_models=args.use_cached_models, remove_cached_files=args.remove_cached_files, only_convert_finetuned_models=args.only_convert_finetuned_models, )
61
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 4000000 )-> int: UpperCamelCase = [] UpperCamelCase ,UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCamelCase ) UpperCamelCase ,UpperCamelCase = b, a + b return sum(__UpperCamelCase ) if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' import argparse import os import transformers from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS from .utils import logging logging.set_verbosity_info() lowerCamelCase : Optional[Any] = logging.get_logger(__name__) lowerCamelCase : str = {name: getattr(transformers, name + "Fast") for name in SLOW_TO_FAST_CONVERTERS} def _lowerCAmelCase ( _UpperCamelCase : Dict , _UpperCamelCase : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str ) -> str: """simple docstring""" if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES: raise ValueError(f"Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys() )}." ) if tokenizer_name is None: _SCREAMING_SNAKE_CASE =TOKENIZER_CLASSES else: _SCREAMING_SNAKE_CASE ={tokenizer_name: getattr(__UpperCamelCase , tokenizer_name + 'Fast' )} logger.info(f"Loading tokenizer classes: {tokenizer_names}" ) for tokenizer_name in tokenizer_names: _SCREAMING_SNAKE_CASE =TOKENIZER_CLASSES[tokenizer_name] _SCREAMING_SNAKE_CASE =True if checkpoint_name is None: _SCREAMING_SNAKE_CASE =list(tokenizer_class.max_model_input_sizes.keys() ) else: _SCREAMING_SNAKE_CASE =[checkpoint_name] logger.info(f"For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}" ) for checkpoint in checkpoint_names: logger.info(f"Loading {tokenizer_class.__class__.__name__} {checkpoint}" ) # Load tokenizer _SCREAMING_SNAKE_CASE =tokenizer_class.from_pretrained(__UpperCamelCase , force_download=__UpperCamelCase ) # Save fast tokenizer logger.info(f"Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}" ) # For organization names we create sub-directories if "/" in checkpoint: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =checkpoint.split('/' ) _SCREAMING_SNAKE_CASE =os.path.join(__UpperCamelCase , __UpperCamelCase ) elif add_prefix: _SCREAMING_SNAKE_CASE =checkpoint _SCREAMING_SNAKE_CASE =dump_path else: _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =dump_path logger.info(f"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}" ) if checkpoint in list(tokenizer.pretrained_vocab_files_map.values() )[0]: _SCREAMING_SNAKE_CASE =list(tokenizer.pretrained_vocab_files_map.values() )[0][checkpoint] _SCREAMING_SNAKE_CASE =file_path.split(__UpperCamelCase )[-1][0] if next_char == "/": _SCREAMING_SNAKE_CASE =os.path.join(__UpperCamelCase , __UpperCamelCase ) _SCREAMING_SNAKE_CASE =None logger.info(f"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}" ) _SCREAMING_SNAKE_CASE =tokenizer.save_pretrained( __UpperCamelCase , legacy_format=__UpperCamelCase , filename_prefix=__UpperCamelCase ) logger.info(f"=> File names {file_names}" ) for file_name in file_names: if not file_name.endswith('tokenizer.json' ): os.remove(__UpperCamelCase ) logger.info(f"=> removing {file_name}" ) if __name__ == "__main__": lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--dump_path", default=None, type=str, required=True, help="Path to output generated fast tokenizer files." ) parser.add_argument( "--tokenizer_name", default=None, type=str, help=( f'''Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will ''' "download and convert all the checkpoints from AWS." ), ) parser.add_argument( "--checkpoint_name", default=None, type=str, help="Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.", ) parser.add_argument( "--force_download", action="store_true", help="Re-download checkpoints.", ) lowerCamelCase : Union[str, Any] = parser.parse_args() convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)
47
'''simple docstring''' def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: # Base Case if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] , __UpperCamelCase , __UpperCamelCase ): # Color current vertex UpperCamelCase = i # Validate coloring if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , index + 1 ): return True # Backtrack UpperCamelCase = -1 return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> list[int]: UpperCamelCase = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , 0 ): return colored_vertices return []
321
0
"""simple docstring""" from PIL import Image def __UpperCAmelCase ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str ) -> Image: '''simple docstring''' __snake_case : Union[str, Any] = (2_59 * (level + 2_55)) / (2_55 * (2_59 - level)) def contrast(UpperCAmelCase_ : Tuple ) -> int: return int(1_28 + factor * (c - 1_28) ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open("image_data/lena.jpg") as img: # Change contrast to 170 _a : int= change_contrast(img, 170) cont_img.save("image_data/lena_high_contrast.png", format="png")
172
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 2000000 )-> int: UpperCamelCase = [0 for i in range(n + 1 )] UpperCamelCase = 1 UpperCamelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __UpperCamelCase ): UpperCamelCase = 1 UpperCamelCase = 0 for i in range(__UpperCamelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'{solution() = }')
321
0
import math def __UpperCAmelCase ( __a : List[str] ) -> bool: """simple docstring""" _a : Tuple = math.loga(math.sqrt(4 * positive_integer + 1 ) / 2 + 1 / 2 ) return exponent == int(__UpperCamelCase ) def __UpperCAmelCase ( __a : Optional[int] = 1 / 12_345 ) -> int: """simple docstring""" _a : Optional[int] = 0 _a : List[str] = 0 _a : Optional[Any] = 3 while True: _a : Tuple = (integer**2 - 1) / 4 # if candidate is an integer, then there is a partition for k if partition_candidate == int(__UpperCamelCase ): _a : Dict = int(__UpperCamelCase ) total_partitions += 1 if check_partition_perfect(__UpperCamelCase ): perfect_partitions += 1 if perfect_partitions > 0: if perfect_partitions / total_partitions < max_proportion: return int(__UpperCamelCase ) integer += 1 if __name__ == "__main__": print(f'''{solution() = }''')
235
'''simple docstring''' from timeit import timeit def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: number &= number - 1 result += 1 return result def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowercase__ ( )-> None: def do_benchmark(__UpperCamelCase ) -> None: UpperCamelCase = """import __main__ as z""" print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) UpperCamelCase = timeit("""z.get_set_bits_count_using_modulo_operator(25)""" , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) UpperCamelCase = timeit( """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""" , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
321
0
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def _lowercase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Any: # Initialise PyTorch model SCREAMING_SNAKE_CASE__ : Tuple = RemBertConfig.from_json_file(__UpperCamelCase ) print("""Building PyTorch model from configuration: {}""".format(str(__UpperCamelCase ) ) ) SCREAMING_SNAKE_CASE__ : Dict = RemBertModel(__UpperCamelCase ) # Load weights from tf checkpoint load_tf_weights_in_rembert(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Save pytorch-model print("""Save PyTorch model to {}""".format(__UpperCamelCase ) ) torch.save(model.state_dict() , __UpperCamelCase ) if __name__ == "__main__": a :str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--rembert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained RemBERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) a :str = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
132
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCamelCase : str = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : List[Any] = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys __UpperCamelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
182
'''simple docstring''' import math def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
321
0
'''simple docstring''' from __future__ import annotations def a_ ( _lowerCAmelCase ,_lowerCAmelCase ) -> list[str]: if nth_term == "": return [""] __lowerCamelCase : Any = int(__UpperCamelCase ) __lowerCamelCase : Dict = int(__UpperCamelCase ) __lowerCamelCase : Any = [] for temp in range(int(__UpperCamelCase ) ): series.append(F'1 / {pow(temp + 1 ,int(__UpperCamelCase ) )}' if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = int(input('Enter the last number (nth term) of the P-Series')) _UpperCamelCase = int(input('Enter the power for P-Series')) print('Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p') print(p_series(nth_term, power))
208
'''simple docstring''' import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE__ = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE__ = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' SCREAMING_SNAKE_CASE__ = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://www.atticusprojectai.org/cuad"""] , reference_urls=["""https://www.atticusprojectai.org/cuad"""] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} UpperCamelCase = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] UpperCamelCase = evaluate(dataset=_SCREAMING_SNAKE_CASE , predictions=_SCREAMING_SNAKE_CASE ) return score
321
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase__ ( unittest.TestCase): def __init__( self :List[Any] , _A :Any , _A :Optional[Any]=13 , _A :Tuple=3 , _A :Tuple=224 , _A :Optional[int]=30 , _A :Tuple=400 , _A :str=True , _A :Tuple=None , _A :List[Any]=True , _A :Tuple=[0.5, 0.5, 0.5] , _A :Tuple=[0.5, 0.5, 0.5] , ) -> Any: '''simple docstring''' __A = size if size is not None else {'height': 18, 'width': 18} __A = parent __A = batch_size __A = num_channels __A = image_size __A = min_resolution __A = max_resolution __A = do_resize __A = size __A = do_normalize __A = image_mean __A = image_std def lowercase_ ( self :Dict ) -> Dict: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class UpperCamelCase__ ( SCREAMING_SNAKE_CASE , unittest.TestCase): UpperCAmelCase__ : str = ViTImageProcessor if is_vision_available() else None def lowercase_ ( self :int ) -> Tuple: '''simple docstring''' __A = EfficientFormerImageProcessorTester(self ) @property def lowercase_ ( self :Union[str, Any] ) -> int: '''simple docstring''' return self.image_proc_tester.prepare_image_processor_dict() def lowercase_ ( self :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' __A = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , 'image_mean' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , 'image_std' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , 'do_normalize' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , 'do_resize' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , 'size' ) ) def lowercase_ ( self :int ) -> Optional[int]: '''simple docstring''' pass def lowercase_ ( self :Any ) -> Tuple: '''simple docstring''' __A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __A = prepare_image_inputs(self.image_proc_tester , equal_resolution=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __A = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched __A = image_processor(_SCREAMING_SNAKE_CASE , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) def lowercase_ ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' __A = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __A = prepare_image_inputs(self.image_proc_tester , equal_resolution=_SCREAMING_SNAKE_CASE , numpify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __A = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched __A = image_processor(_SCREAMING_SNAKE_CASE , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) def lowercase_ ( self :List[Any] ) -> Union[str, Any]: '''simple docstring''' __A = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __A = prepare_image_inputs(self.image_proc_tester , equal_resolution=_SCREAMING_SNAKE_CASE , torchify=_SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __A = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched __A = image_processor(_SCREAMING_SNAKE_CASE , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , )
161
'''simple docstring''' def lowercase__ ( __UpperCamelCase )-> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase__ ( __UpperCamelCase = 1000000 )-> int: UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__UpperCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_convbert import ConvBertTokenizer __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''vocab.txt'''} __snake_case = { '''vocab_file''': { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt''', } } __snake_case = { '''YituTech/conv-bert-base''': 512, '''YituTech/conv-bert-medium-small''': 512, '''YituTech/conv-bert-small''': 512, } __snake_case = { '''YituTech/conv-bert-base''': {'''do_lower_case''': True}, '''YituTech/conv-bert-medium-small''': {'''do_lower_case''': True}, '''YituTech/conv-bert-small''': {'''do_lower_case''': True}, } class lowercase ( A__ ): """simple docstring""" _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_INIT_CONFIGURATION _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ConvBertTokenizer def __init__( self , UpperCamelCase_=None , UpperCamelCase_=None , UpperCamelCase_=True , UpperCamelCase_="[UNK]" , UpperCamelCase_="[SEP]" , UpperCamelCase_="[PAD]" , UpperCamelCase_="[CLS]" , UpperCamelCase_="[MASK]" , UpperCamelCase_=True , UpperCamelCase_=None , **UpperCamelCase_ , ): '''simple docstring''' super().__init__( _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , do_lower_case=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , tokenize_chinese_chars=_SCREAMING_SNAKE_CASE , strip_accents=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) UpperCamelCase__ :Union[str, Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get('''strip_accents''' , _SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): UpperCamelCase__ :Union[str, Any] = getattr(_SCREAMING_SNAKE_CASE , normalizer_state.pop('''type''' ) ) UpperCamelCase__ :Tuple = do_lower_case UpperCamelCase__ :str = strip_accents UpperCamelCase__ :Tuple = tokenize_chinese_chars UpperCamelCase__ :Dict = normalizer_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase__ :Dict = do_lower_case def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_=None ): '''simple docstring''' UpperCamelCase__ :Dict = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None ): '''simple docstring''' UpperCamelCase__ :Optional[int] = [self.sep_token_id] UpperCamelCase__ :Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None ): '''simple docstring''' UpperCamelCase__ :Union[str, Any] = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
97
'''simple docstring''' from __future__ import annotations from math import pow, sqrt def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if resistance == 0: return {"resistance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(__UpperCamelCase , 2 ) + pow(__UpperCamelCase , 2 ) )} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
321
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision class A__ ( unittest.TestCase): def UpperCamelCase__ ( self ): lowerCamelCase : Any = tempfile.mkdtemp() # fmt: off lowerCamelCase : Any = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on lowerCamelCase : Optional[int] = dict(zip(_SCREAMING_SNAKE_CASE , range(len(_SCREAMING_SNAKE_CASE ) ) ) ) lowerCamelCase : str = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] lowerCamelCase : List[str] = {"""unk_token""": """<unk>"""} lowerCamelCase : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_SCREAMING_SNAKE_CASE ) ) lowerCamelCase : Tuple = { """do_resize""": True, """size""": 2_0, """do_center_crop""": True, """crop_size""": 1_8, """do_normalize""": True, """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073], """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711], } lowerCamelCase : List[Any] = os.path.join(self.tmpdirname , _SCREAMING_SNAKE_CASE ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , **__magic_name__ ): return CLIPTokenizer.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , **__magic_name__ ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , **__magic_name__ ): return ViTImageProcessor.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): lowerCamelCase : Dict = [np.random.randint(2_5_5 , size=(3, 3_0, 4_0_0) , dtype=np.uinta )] lowerCamelCase : Dict = [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): lowerCamelCase : List[str] = self.get_tokenizer() lowerCamelCase : Optional[Any] = self.get_rust_tokenizer() lowerCamelCase : List[Any] = self.get_image_processor() lowerCamelCase : List[Any] = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) processor_slow.save_pretrained(self.tmpdirname ) lowerCamelCase : str = CLIPSegProcessor.from_pretrained(self.tmpdirname , use_fast=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Union[str, Any] = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) processor_fast.save_pretrained(self.tmpdirname ) lowerCamelCase : Union[str, Any] = CLIPSegProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.image_processor , _SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self ): lowerCamelCase : List[Any] = CLIPSegProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase : Dict = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase : Union[str, Any] = self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE , padding_value=1.0 ) lowerCamelCase : Optional[Any] = CLIPSegProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self ): lowerCamelCase : List[str] = self.get_image_processor() lowerCamelCase : Any = self.get_tokenizer() lowerCamelCase : List[Any] = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Any = self.prepare_image_inputs() lowerCamelCase : Optional[int] = image_processor(_SCREAMING_SNAKE_CASE , return_tensors="""np""" ) lowerCamelCase : Union[str, Any] = processor(images=_SCREAMING_SNAKE_CASE , 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 UpperCamelCase__ ( self ): lowerCamelCase : Dict = self.get_image_processor() lowerCamelCase : int = self.get_tokenizer() lowerCamelCase : Any = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Dict = """lower newer""" lowerCamelCase : Optional[Any] = processor(text=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Any = tokenizer(_SCREAMING_SNAKE_CASE ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase__ ( self ): lowerCamelCase : Optional[Any] = self.get_image_processor() lowerCamelCase : str = self.get_tokenizer() lowerCamelCase : Union[str, Any] = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Dict = """lower newer""" lowerCamelCase : Tuple = self.prepare_image_inputs() lowerCamelCase : Any = processor(text=_SCREAMING_SNAKE_CASE , images=_SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(_SCREAMING_SNAKE_CASE ): processor() def UpperCamelCase__ ( self ): lowerCamelCase : List[str] = self.get_image_processor() lowerCamelCase : int = self.get_tokenizer() lowerCamelCase : int = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Dict = self.prepare_image_inputs() lowerCamelCase : Optional[int] = self.prepare_image_inputs() lowerCamelCase : Dict = processor(images=_SCREAMING_SNAKE_CASE , visual_prompt=_SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """conditional_pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(_SCREAMING_SNAKE_CASE ): processor() def UpperCamelCase__ ( self ): lowerCamelCase : Dict = self.get_image_processor() lowerCamelCase : Union[str, Any] = self.get_tokenizer() lowerCamelCase : List[str] = CLIPSegProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Optional[int] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase : int = processor.batch_decode(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Union[str, Any] = tokenizer.batch_decode(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
287
'''simple docstring''' # Algorithm for the pigeonhole sorting def lowercase__ ( __UpperCamelCase )-> Union[str, Any]: UpperCamelCase = min(__UpperCamelCase ) # min() finds the minimum value UpperCamelCase = max(__UpperCamelCase ) # max() finds the maximum value UpperCamelCase = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size UpperCamelCase = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(__UpperCamelCase , __UpperCamelCase ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. UpperCamelCase = 0 for count in range(__UpperCamelCase ): while holes[count] > 0: holes[count] -= 1 UpperCamelCase = count + min_val i += 1 def lowercase__ ( )-> Any: UpperCamelCase = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(__UpperCamelCase ) print("""Sorted order is:""" , """ """.join(__UpperCamelCase ) ) if __name__ == "__main__": main()
321
0
"""simple docstring""" import datasets lowerCAmelCase_ = '\\n@InProceedings{conneau2018xnli,\n author = "Conneau, Alexis\n and Rinott, Ruty\n and Lample, Guillaume\n and Williams, Adina\n and Bowman, Samuel R.\n and Schwenk, Holger\n and Stoyanov, Veselin",\n title = "XNLI: Evaluating Cross-lingual Sentence Representations",\n booktitle = "Proceedings of the 2018 Conference on Empirical Methods\n in Natural Language Processing",\n year = "2018",\n publisher = "Association for Computational Linguistics",\n location = "Brussels, Belgium",\n}\n' lowerCAmelCase_ = '\\nXNLI is a subset of a few thousand examples from MNLI which has been translated\ninto a 14 different languages (some low-ish resource). As with MNLI, the goal is\nto predict textual entailment (does sentence A imply/contradict/neither sentence\nB) and is a classification task (given two sentences, predict one of three\nlabels).\n' lowerCAmelCase_ = '\nComputes XNLI score which is just simple accuracy.\nArgs:\n predictions: Predicted labels.\n references: Ground truth labels.\nReturns:\n \'accuracy\': accuracy\nExamples:\n\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> xnli_metric = datasets.load_metric("xnli")\n >>> results = xnli_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n' def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> List[Any]: return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def UpperCAmelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), } ) ,codebase_urls=[] ,reference_urls=[] ,format='''numpy''' ,) def UpperCAmelCase ( self : int ,_snake_case : Optional[int] ,_snake_case : Dict ) -> Optional[int]: """simple docstring""" return {"accuracy": simple_accuracy(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE )}
16
'''simple docstring''' import torch from diffusers import DDPMParallelScheduler from .test_schedulers import SchedulerCommonTest class a_ ( lowerCamelCase ): lowercase = (DDPMParallelScheduler,) def A__ ( self , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = { """num_train_timesteps""": 1000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """variance_type""": """fixed_small""", """clip_sample""": True, } config.update(**_SCREAMING_SNAKE_CASE ) return config def A__ ( self ) -> List[str]: """simple docstring""" for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[int]: """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE , beta_end=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Tuple: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> List[Any]: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> str: """simple docstring""" self.check_over_configs(thresholding=_SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_SCREAMING_SNAKE_CASE , prediction_type=_SCREAMING_SNAKE_CASE , sample_max_value=_SCREAMING_SNAKE_CASE , ) def A__ ( self ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Union[str, Any]: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> int: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def A__ ( self ) -> Optional[int]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = self.dummy_sample_deter + 0.1 UpperCamelCase = self.dummy_sample_deter - 0.1 UpperCamelCase = samplea.shape[0] UpperCamelCase = torch.stack([samplea, samplea, samplea] , dim=0 ) UpperCamelCase = torch.arange(_SCREAMING_SNAKE_CASE )[0:3, None].repeat(1 , _SCREAMING_SNAKE_CASE ) UpperCamelCase = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) UpperCamelCase = scheduler.batch_step_no_noise(_SCREAMING_SNAKE_CASE , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) ) UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 1_1_5_3.1_8_3_3 ) < 1e-2 assert abs(result_mean.item() - 0.5_0_0_5 ) < 1e-3 def A__ ( self ) -> List[str]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config(prediction_type="""v_prediction""" ) UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(_SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = torch.mean(torch.abs(_SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) UpperCamelCase = scheduler.timesteps for i, timestep in enumerate(_SCREAMING_SNAKE_CASE ): if i == len(_SCREAMING_SNAKE_CASE ) - 1: UpperCamelCase = -1 else: UpperCamelCase = timesteps[i + 1] UpperCamelCase = scheduler.previous_timestep(_SCREAMING_SNAKE_CASE ) UpperCamelCase = prev_t.item() self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 51, 0] with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""`custom_timesteps` must be in descending order.""" ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Optional[Any]: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [100, 87, 50, 1, 0] UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) with self.assertRaises(_SCREAMING_SNAKE_CASE , msg="""Can only pass one of `num_inference_steps` or `custom_timesteps`.""" ): scheduler.set_timesteps(num_inference_steps=_SCREAMING_SNAKE_CASE , timesteps=_SCREAMING_SNAKE_CASE ) def A__ ( self ) -> Any: """simple docstring""" UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( _SCREAMING_SNAKE_CASE , msg="""`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}""" , ): scheduler.set_timesteps(timesteps=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging _a = logging.get_logger(__name__) def __a ( __lowerCamelCase, __lowerCamelCase ): try: with open(__UpperCamelCase, "rb" ) as flax_state_f: UpperCAmelCase_ : Optional[int] = from_bytes(__UpperCamelCase, flax_state_f.read() ) except UnpicklingError as e: try: with open(__UpperCamelCase ) as f: if f.read().startswith("version" ): raise OSError( "You seem to have cloned a repository without having git-lfs installed. Please" " install git-lfs and run `git lfs install` followed by `git lfs pull` in the" " folder you cloned." ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(f"""Unable to convert {model_file} to Flax deserializable object. """ ) return load_flax_weights_in_pytorch_model(__UpperCamelCase, __UpperCamelCase ) def __a ( __lowerCamelCase, __lowerCamelCase ): try: import torch # noqa: F401 except ImportError: logger.error( "Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights UpperCAmelCase_ : Union[str, Any] = flatten_dict(jax.tree_util.tree_map(lambda __lowerCamelCase : x.dtype == jnp.bfloataa, __UpperCamelCase ) ).values() if any(__UpperCamelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) UpperCAmelCase_ : Tuple = jax.tree_util.tree_map( lambda __lowerCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params, __UpperCamelCase ) UpperCAmelCase_ : Optional[Any] = "" UpperCAmelCase_ : Dict = flatten_dict(__UpperCamelCase, sep="." ) UpperCAmelCase_ : List[Any] = pt_model.state_dict() # keep track of unexpected & missing keys UpperCAmelCase_ : Any = [] UpperCAmelCase_ : Tuple = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): UpperCAmelCase_ : Dict = flax_key_tuple.split("." ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: UpperCAmelCase_ : str = flax_key_tuple_array[:-1] + ["weight"] UpperCAmelCase_ : List[str] = jnp.transpose(__UpperCamelCase, (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": UpperCAmelCase_ : List[Any] = flax_key_tuple_array[:-1] + ["weight"] UpperCAmelCase_ : Optional[int] = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": UpperCAmelCase_ : str = flax_key_tuple_array[:-1] + ["weight"] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(__UpperCamelCase ): UpperCAmelCase_ : Dict = ( flax_key_tuple_string.replace("_0", ".0" ) .replace("_1", ".1" ) .replace("_2", ".2" ) .replace("_3", ".3" ) .replace("_4", ".4" ) .replace("_5", ".5" ) .replace("_6", ".6" ) .replace("_7", ".7" ) .replace("_8", ".8" ) .replace("_9", ".9" ) ) UpperCAmelCase_ : Any = ".".join(__UpperCamelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( f"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """ f"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) else: # add weight to pytorch dict UpperCAmelCase_ : Any = np.asarray(__UpperCamelCase ) if not isinstance(__UpperCamelCase, np.ndarray ) else flax_tensor UpperCAmelCase_ : Any = torch.from_numpy(__UpperCamelCase ) # remove from missing keys missing_keys.remove(__UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(__UpperCamelCase ) pt_model.load_state_dict(__UpperCamelCase ) # re-transform missing_keys to list UpperCAmelCase_ : Optional[Any] = list(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" f""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing""" f""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture""" " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" f""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect""" " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) if len(__UpperCamelCase ) > 0: logger.warning( f"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly""" f""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to""" " use it for predictions and inference." ) return pt_model
61
'''simple docstring''' from __future__ import annotations import math class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = size # approximate the overall size of segment tree with given value UpperCamelCase = [0 for i in range(0 , 4 * size )] # create array to store lazy update UpperCamelCase = [0 for i in range(0 , 4 * size )] UpperCamelCase = [0 for i in range(0 , 4 * size )] # flag for lazy update def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return idx * 2 + 1 def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" if left_element == right_element: UpperCamelCase = a[left_element - 1] else: UpperCamelCase = (left_element + right_element) // 2 self.build(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.build(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: UpperCamelCase = val if left_element != right_element: UpperCamelCase = val UpperCamelCase = val UpperCamelCase = True UpperCamelCase = True return True UpperCamelCase = (left_element + right_element) // 2 self.update(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.update(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = max( self.segment_tree[self.left(_SCREAMING_SNAKE_CASE )] , self.segment_tree[self.right(_SCREAMING_SNAKE_CASE )] ) return True def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | float: """simple docstring""" if self.flag[idx] is True: UpperCamelCase = self.lazy[idx] UpperCamelCase = False if left_element != right_element: UpperCamelCase = self.lazy[idx] UpperCamelCase = self.lazy[idx] UpperCamelCase = True UpperCamelCase = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] UpperCamelCase = (left_element + right_element) // 2 UpperCamelCase = self.query(self.left(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = self.query(self.right(_SCREAMING_SNAKE_CASE ) , mid + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __str__( self ) -> str: """simple docstring""" return str([self.query(1 , 1 , self.size , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8] SCREAMING_SNAKE_CASE__ = 1_5 SCREAMING_SNAKE_CASE__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 1_1)) print(segt.query(1, 1, size, 7, 1_2)) segt.update(1, 1, size, 1, 3, 1_1_1) print(segt.query(1, 1, size, 1, 1_5)) segt.update(1, 1, size, 7, 8, 2_3_5) print(segt)
321
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCamelCase : Optional[int] = logging.get_logger(__name__) class A__ ( A__ ): A__ = ['pixel_values'] def __init__( self : Any , _a : Optional[Any] = True , _a : Dict = None , _a : Optional[int] = PILImageResampling.BICUBIC , _a : Tuple = True , _a : Tuple = 1 / 255 , _a : Tuple = True , _a : List[Any] = None , _a : Optional[Any] = None , _a : List[Any] = True , **_a : Tuple , ) -> None: '''simple docstring''' super().__init__(**_SCREAMING_SNAKE_CASE ) _SCREAMING_SNAKE_CASE =size if size is not None else {'height': 384, 'width': 384} _SCREAMING_SNAKE_CASE =get_size_dict(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) _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 A ( self : Any , _a : Tuple , _a : Union[str, Any] , _a : Tuple = PILImageResampling.BICUBIC , _a : Optional[int] = None , **_a : Tuple , ) -> np.ndarray: '''simple docstring''' _SCREAMING_SNAKE_CASE =get_size_dict(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) 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(_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE , resample=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def A ( self : Optional[Any] , _a : int , _a : Optional[Any] , _a : List[Any] = None , **_a : Dict , ) -> str: '''simple docstring''' return rescale(_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def A ( self : Optional[int] , _a : Any , _a : Optional[int] , _a : Union[str, Any] , _a : Any = None , **_a : List[str] , ) -> np.ndarray: '''simple docstring''' return normalize(_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def A ( self : List[Any] , _a : str , _a : int = None , _a : Optional[Any] = None , _a : Tuple = None , _a : Union[str, Any] = None , _a : List[str] = None , _a : List[str] = None , _a : Union[str, Any] = None , _a : int = None , _a : int = None , _a : Optional[int] = None , _a : int = ChannelDimension.FIRST , **_a : Optional[int] , ) -> PIL.Image.Image: '''simple docstring''' _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(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) _SCREAMING_SNAKE_CASE =make_list_of_images(_SCREAMING_SNAKE_CASE ) if not valid_images(_SCREAMING_SNAKE_CASE ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None 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(_SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. _SCREAMING_SNAKE_CASE =[to_numpy_array(_SCREAMING_SNAKE_CASE ) for image in images] if do_resize: _SCREAMING_SNAKE_CASE =[self.resize(image=_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE , resample=_SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: _SCREAMING_SNAKE_CASE =[self.rescale(image=_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: _SCREAMING_SNAKE_CASE =[self.normalize(image=_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE ) for image in images] _SCREAMING_SNAKE_CASE =[to_channel_dimension_format(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for image in images] _SCREAMING_SNAKE_CASE =BatchFeature(data={'pixel_values': images} , tensor_type=_SCREAMING_SNAKE_CASE ) return encoded_outputs
47
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 1000 )-> int: UpperCamelCase = -1 UpperCamelCase = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c UpperCamelCase = (n * n - 2 * a * n) // (2 * n - 2 * a) UpperCamelCase = n - a - b if c * c == (a * a + b * b): UpperCamelCase = a * b * c if candidate >= product: UpperCamelCase = candidate return product if __name__ == "__main__": print(f'{solution() = }')
321
0
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging _a : Tuple= logging.get_logger(__name__) _a : str= "▁" _a : List[str]= {"vocab_file": "sentencepiece.bpe.model"} _a : List[str]= { "vocab_file": { "facebook/mbart-large-50-one-to-many-mmt": ( "https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model" ), } } _a : str= { "facebook/mbart-large-50-one-to-many-mmt": 1_024, } # fmt: off _a : List[Any]= ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN", "af_ZA", "az_AZ", "bn_IN", "fa_IR", "he_IL", "hr_HR", "id_ID", "ka_GE", "km_KH", "mk_MK", "ml_IN", "mn_MN", "mr_IN", "pl_PL", "ps_AF", "pt_XX", "sv_SE", "sw_KE", "ta_IN", "te_IN", "th_TH", "tl_XX", "uk_UA", "ur_PK", "xh_ZA", "gl_ES", "sl_SI"] class UpperCamelCase ( lowercase ): UpperCAmelCase : Optional[int] = VOCAB_FILES_NAMES UpperCAmelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase : Optional[Any] = ["""input_ids""", """attention_mask"""] UpperCAmelCase : Union[str, Any] = [] UpperCAmelCase : Tuple = [] def __init__(self : Optional[int] , _A : Optional[Any] , _A : Dict=None , _A : Optional[int]=None , _A : Tuple="</s>" , _A : Tuple="</s>" , _A : Optional[int]="<s>" , _A : Optional[int]="<unk>" , _A : Union[str, Any]="<pad>" , _A : Union[str, Any]="<mask>" , _A : Any = None , **_A : Dict , ) -> None: __snake_case : Dict = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) else mask_token __snake_case : Union[str, Any] = {} if sp_model_kwargs is None else sp_model_kwargs __snake_case : Any = kwargs.get('additional_special_tokens' , []) kwargs["additional_special_tokens"] += [ code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=_SCREAMING_SNAKE_CASE , tgt_lang=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **_SCREAMING_SNAKE_CASE , ) __snake_case : str = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(str(_SCREAMING_SNAKE_CASE)) __snake_case : Tuple = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token __snake_case : Optional[Any] = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab __snake_case : Optional[Any] = 1 __snake_case : int = len(self.sp_model) __snake_case : Optional[int] = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(_SCREAMING_SNAKE_CASE) } __snake_case : Any = {v: k for k, v in self.lang_code_to_id.items()} __snake_case : str = len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id) __snake_case : List[str] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} __snake_case : Optional[Any] = src_lang if src_lang is not None else 'en_XX' __snake_case : List[str] = self.lang_code_to_id[self._src_lang] __snake_case : Optional[Any] = tgt_lang self.set_src_lang_special_tokens(self._src_lang) @property def _lowercase (self : Tuple) -> int: return len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def _lowercase (self : Any) -> str: return self._src_lang @src_lang.setter def _lowercase (self : Dict , _A : Any) -> None: __snake_case : Optional[int] = new_src_lang self.set_src_lang_special_tokens(self._src_lang) def __getstate__(self : Optional[int]) -> Dict: __snake_case : Tuple = self.__dict__.copy() __snake_case : Tuple = None return state def __setstate__(self : str , _A : Tuple) -> None: __snake_case : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs'): __snake_case : Dict = {} __snake_case : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) def _lowercase (self : str) -> Dict: __snake_case : Optional[int] = {self.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def _lowercase (self : Optional[Any] , _A : Dict) -> List[str]: return self.sp_model.encode(_SCREAMING_SNAKE_CASE , out_type=_SCREAMING_SNAKE_CASE) def _lowercase (self : Optional[Any] , _A : Optional[int]) -> int: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __snake_case : Union[str, Any] = self.sp_model.PieceToId(_SCREAMING_SNAKE_CASE) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def _lowercase (self : int , _A : List[Any]) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset) def _lowercase (self : Any , _A : List[str]) -> Any: __snake_case : Union[str, Any] = [] __snake_case : int = '' __snake_case : List[str] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_SCREAMING_SNAKE_CASE) + token __snake_case : str = True __snake_case : str = [] else: current_sub_tokens.append(_SCREAMING_SNAKE_CASE) __snake_case : Tuple = False out_string += self.sp_model.decode(_SCREAMING_SNAKE_CASE) return out_string.strip() def _lowercase (self : List[str] , _A : List[Any] , _A : Optional[Any] = None) -> Tuple[str]: if not os.path.isdir(_SCREAMING_SNAKE_CASE): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return __snake_case : Optional[int] = os.path.join( _SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) if os.path.abspath(self.vocab_file) != os.path.abspath(_SCREAMING_SNAKE_CASE) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , _SCREAMING_SNAKE_CASE) elif not os.path.isfile(self.vocab_file): with open(_SCREAMING_SNAKE_CASE , 'wb') as fi: __snake_case : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(_SCREAMING_SNAKE_CASE) return (out_vocab_file,) def _lowercase (self : List[Any] , _A : Any , _A : str = None , _A : Optional[Any] = False) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_SCREAMING_SNAKE_CASE , token_ids_a=_SCREAMING_SNAKE_CASE , already_has_special_tokens=_SCREAMING_SNAKE_CASE) __snake_case : List[str] = [1] * len(self.prefix_tokens) __snake_case : List[str] = [1] * len(self.suffix_tokens) if token_ids_a is None: return prefix_ones + ([0] * len(_SCREAMING_SNAKE_CASE)) + suffix_ones return prefix_ones + ([0] * len(_SCREAMING_SNAKE_CASE)) + ([0] * len(_SCREAMING_SNAKE_CASE)) + suffix_ones def _lowercase (self : Optional[int] , _A : Union[str, Any] , _A : List[str] = None) -> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase (self : Union[str, Any] , _A : List[Any] , _A : int , _A : Tuple , _A : int , **_A : Tuple) -> Optional[Any]: if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model') __snake_case : Dict = src_lang __snake_case : Optional[int] = self(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) __snake_case : int = self.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE) __snake_case : Union[str, Any] = tgt_lang_id return inputs def _lowercase (self : str , _A : List[str] , _A : str = "en_XX" , _A : Tuple = None , _A : str = "ro_RO" , **_A : int , ) -> BatchEncoding: __snake_case : List[str] = src_lang __snake_case : int = tgt_lang return super().prepare_seqaseq_batch(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) def _lowercase (self : Dict) -> Optional[int]: return self.set_src_lang_special_tokens(self.src_lang) def _lowercase (self : int) -> Tuple: return self.set_tgt_lang_special_tokens(self.tgt_lang) def _lowercase (self : Any , _A : str) -> None: __snake_case : Dict = self.lang_code_to_id[src_lang] __snake_case : Tuple = [self.cur_lang_code_id] __snake_case : List[Any] = [self.eos_token_id] def _lowercase (self : List[Any] , _A : Optional[int]) -> None: __snake_case : Union[str, Any] = self.lang_code_to_id[tgt_lang] __snake_case : Dict = [self.cur_lang_code_id] __snake_case : Optional[int] = [self.eos_token_id]
172
'''simple docstring''' import argparse import struct import unittest class a_ : def __init__( self , _SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" UpperCamelCase = data # Initialize hash values UpperCamelCase = [ 0x6A_09_E6_67, 0xBB_67_AE_85, 0x3C_6E_F3_72, 0xA5_4F_F5_3A, 0x51_0E_52_7F, 0x9B_05_68_8C, 0x1F_83_D9_AB, 0x5B_E0_CD_19, ] # Initialize round constants UpperCamelCase = [ 0x42_8A_2F_98, 0x71_37_44_91, 0xB5_C0_FB_CF, 0xE9_B5_DB_A5, 0x39_56_C2_5B, 0x59_F1_11_F1, 0x92_3F_82_A4, 0xAB_1C_5E_D5, 0xD8_07_AA_98, 0x12_83_5B_01, 0x24_31_85_BE, 0x55_0C_7D_C3, 0x72_BE_5D_74, 0x80_DE_B1_FE, 0x9B_DC_06_A7, 0xC1_9B_F1_74, 0xE4_9B_69_C1, 0xEF_BE_47_86, 0x0F_C1_9D_C6, 0x24_0C_A1_CC, 0x2D_E9_2C_6F, 0x4A_74_84_AA, 0x5C_B0_A9_DC, 0x76_F9_88_DA, 0x98_3E_51_52, 0xA8_31_C6_6D, 0xB0_03_27_C8, 0xBF_59_7F_C7, 0xC6_E0_0B_F3, 0xD5_A7_91_47, 0x06_CA_63_51, 0x14_29_29_67, 0x27_B7_0A_85, 0x2E_1B_21_38, 0x4D_2C_6D_FC, 0x53_38_0D_13, 0x65_0A_73_54, 0x76_6A_0A_BB, 0x81_C2_C9_2E, 0x92_72_2C_85, 0xA2_BF_E8_A1, 0xA8_1A_66_4B, 0xC2_4B_8B_70, 0xC7_6C_51_A3, 0xD1_92_E8_19, 0xD6_99_06_24, 0xF4_0E_35_85, 0x10_6A_A0_70, 0x19_A4_C1_16, 0x1E_37_6C_08, 0x27_48_77_4C, 0x34_B0_BC_B5, 0x39_1C_0C_B3, 0x4E_D8_AA_4A, 0x5B_9C_CA_4F, 0x68_2E_6F_F3, 0x74_8F_82_EE, 0x78_A5_63_6F, 0x84_C8_78_14, 0x8C_C7_02_08, 0x90_BE_FF_FA, 0xA4_50_6C_EB, 0xBE_F9_A3_F7, 0xC6_71_78_F2, ] UpperCamelCase = self.preprocessing(self.data ) self.final_hash() @staticmethod def A__ ( _SCREAMING_SNAKE_CASE ) -> bytes: """simple docstring""" UpperCamelCase = B"""\x80""" + (B"""\x00""" * (63 - (len(_SCREAMING_SNAKE_CASE ) + 8) % 64)) UpperCamelCase = struct.pack(""">Q""" , (len(_SCREAMING_SNAKE_CASE ) * 8) ) return data + padding + big_endian_integer def A__ ( self ) -> None: """simple docstring""" UpperCamelCase = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers UpperCamelCase = list(struct.unpack(""">16L""" , _SCREAMING_SNAKE_CASE ) ) # add 48 0-ed integers words += [0] * 48 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array UpperCamelCase = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) UpperCamelCase = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) UpperCamelCase = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x1_00_00_00_00 # Compression UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 6 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 11 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 25 ) UpperCamelCase = (e & f) ^ ((~e & 0xFF_FF_FF_FF) & g) UpperCamelCase = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x1_00_00_00_00 UpperCamelCase = self.ror(_SCREAMING_SNAKE_CASE , 2 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 13 ) ^ self.ror(_SCREAMING_SNAKE_CASE , 22 ) UpperCamelCase = (a & b) ^ (a & c) ^ (b & c) UpperCamelCase = (sa + maj) % 0x1_00_00_00_00 UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase = ( g, f, e, ((d + tempa) % 0x1_00_00_00_00), c, b, a, ((tempa + tempa) % 0x1_00_00_00_00), ) UpperCamelCase = [a, b, c, d, e, f, g, h] # Modify final values UpperCamelCase = [ ((element + mutated_hash_values[index]) % 0x1_00_00_00_00) for index, element in enumerate(self.hashes ) ] UpperCamelCase = """""".join([hex(_SCREAMING_SNAKE_CASE )[2:].zfill(8 ) for value in self.hashes] ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return 0xFF_FF_FF_FF & (value << (32 - rotations)) | (value >> rotations) class a_ ( unittest.TestCase ): def A__ ( self ) -> None: """simple docstring""" import hashlib UpperCamelCase = bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(_SCREAMING_SNAKE_CASE ).hash , hashlib.shaaaa(_SCREAMING_SNAKE_CASE ).hexdigest() ) def lowercase__ ( )-> None: import doctest doctest.testmod() UpperCamelCase = argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: UpperCamelCase = f.read() else: UpperCamelCase = bytes(__UpperCamelCase , """utf-8""" ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
321
0
import inspect import os import torch from transformers import AutoModel from transformers.testing_utils import mockenv_context from transformers.trainer_utils import set_seed import accelerate from accelerate.accelerator import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils.testing import ( AccelerateTestCase, TempDirTestCase, execute_subprocess_async, require_cuda, require_fsdp, require_multi_gpu, slow, ) from accelerate.utils.constants import ( FSDP_AUTO_WRAP_POLICY, FSDP_BACKWARD_PREFETCH, FSDP_SHARDING_STRATEGY, FSDP_STATE_DICT_TYPE, ) from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin from accelerate.utils.other import patch_environment set_seed(42) a__ = '''bert-base-cased''' a__ = '''fp16''' a__ = '''bf16''' a__ = [FPaa, BFaa] @require_fsdp @require_cuda class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> List[str]: super().setUp() _a : List[str] = dict( ACCELERATE_USE_FSDP='''true''' , MASTER_ADDR='''localhost''' , MASTER_PORT='''10999''' , RANK='''0''' , LOCAL_RANK='''0''' , WORLD_SIZE='''1''' , ) def __lowercase ( self ) -> List[str]: from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy for i, strategy in enumerate(_SCREAMING_SNAKE_CASE ): _a : Tuple = self.dist_env.copy() _a : int = F"""{i + 1}""" _a : Optional[int] = strategy with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : List[Any] = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.sharding_strategy , ShardingStrategy(i + 1 ) ) def __lowercase ( self ) -> str: from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch for i, prefetch_policy in enumerate(_SCREAMING_SNAKE_CASE ): _a : Tuple = self.dist_env.copy() _a : List[str] = prefetch_policy with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : int = FullyShardedDataParallelPlugin() if prefetch_policy == "NO_PREFETCH": self.assertIsNone(fsdp_plugin.backward_prefetch ) else: self.assertEqual(fsdp_plugin.backward_prefetch , BackwardPrefetch(i + 1 ) ) def __lowercase ( self ) -> List[Any]: from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType for i, state_dict_type in enumerate(_SCREAMING_SNAKE_CASE ): _a : Tuple = self.dist_env.copy() _a : Optional[Any] = state_dict_type with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : str = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.state_dict_type , StateDictType(i + 1 ) ) if state_dict_type == "FULL_STATE_DICT": self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu ) self.assertTrue(fsdp_plugin.state_dict_config.ranka_only ) def __lowercase ( self ) -> List[Any]: _a : Any = AutoModel.from_pretrained(_SCREAMING_SNAKE_CASE ) for policy in FSDP_AUTO_WRAP_POLICY: _a : Tuple = self.dist_env.copy() _a : Dict = policy if policy == "TRANSFORMER_BASED_WRAP": _a : Union[str, Any] = '''BertLayer''' elif policy == "SIZE_BASED_WRAP": _a : List[Any] = '''2000''' with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : Dict = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(_SCREAMING_SNAKE_CASE ) if policy == "NO_WRAP": self.assertIsNone(fsdp_plugin.auto_wrap_policy ) else: self.assertIsNotNone(fsdp_plugin.auto_wrap_policy ) _a : int = self.dist_env.copy() _a : Any = '''TRANSFORMER_BASED_WRAP''' _a : Any = '''T5Layer''' with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : str = FullyShardedDataParallelPlugin() with self.assertRaises(_SCREAMING_SNAKE_CASE ) as cm: fsdp_plugin.set_auto_wrap_policy(_SCREAMING_SNAKE_CASE ) self.assertTrue('''Could not find the transformer layer class to wrap in the model.''' in str(cm.exception ) ) _a : int = self.dist_env.copy() _a : Optional[int] = '''SIZE_BASED_WRAP''' _a : Tuple = '''0''' with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : Tuple = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(_SCREAMING_SNAKE_CASE ) self.assertIsNone(fsdp_plugin.auto_wrap_policy ) def __lowercase ( self ) -> Any: from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler for mp_dtype in dtypes: _a : Union[str, Any] = self.dist_env.copy() _a : List[Any] = mp_dtype with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : Optional[int] = Accelerator() if mp_dtype == "fp16": _a : int = torch.floataa elif mp_dtype == "bf16": _a : Optional[Any] = torch.bfloataa _a : int = MixedPrecision(param_dtype=_SCREAMING_SNAKE_CASE , reduce_dtype=_SCREAMING_SNAKE_CASE , buffer_dtype=_SCREAMING_SNAKE_CASE ) self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy , _SCREAMING_SNAKE_CASE ) if mp_dtype == FPaa: self.assertTrue(isinstance(accelerator.scaler , _SCREAMING_SNAKE_CASE ) ) elif mp_dtype == BFaa: self.assertIsNone(accelerator.scaler ) AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE ) def __lowercase ( self ) -> List[Any]: from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload for flag in [True, False]: _a : Any = self.dist_env.copy() _a : Dict = str(_SCREAMING_SNAKE_CASE ).lower() with mockenv_context(**_SCREAMING_SNAKE_CASE ): _a : int = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.cpu_offload , CPUOffload(offload_params=_SCREAMING_SNAKE_CASE ) ) @require_fsdp @require_multi_gpu @slow class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> Union[str, Any]: super().setUp() _a : List[Any] = 0.82 _a : List[Any] = [ '''fsdp_shard_grad_op_transformer_based_wrap''', '''fsdp_full_shard_transformer_based_wrap''', ] _a : Dict = { '''multi_gpu_fp16''': 3_2_0_0, '''fsdp_shard_grad_op_transformer_based_wrap_fp16''': 2_0_0_0, '''fsdp_full_shard_transformer_based_wrap_fp16''': 1_9_0_0, # Disabling below test as it overwhelms the RAM memory usage # on CI self-hosted runner leading to tests getting killed. # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500, # fp16 was leading to indefinite hang } _a : List[Any] = 1_6_0 _a : int = 1_6_0 _a : List[str] = inspect.getfile(accelerate.test_utils ) _a : List[Any] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps'''] ) def __lowercase ( self ) -> List[str]: _a : Optional[int] = os.path.join(self.test_scripts_folder , '''test_performance.py''' ) _a : str = ['''accelerate''', '''launch''', '''--num_processes=2''', '''--num_machines=1''', '''--machine_rank=0''', '''--use_fsdp'''] for config in self.performance_configs: _a : List[str] = cmd.copy() for i, strategy in enumerate(_SCREAMING_SNAKE_CASE ): if strategy.lower() in config: cmd_config.append(F"""--fsdp_sharding_strategy={i+1}""" ) break if "fp32" in config: cmd_config.append('''--mixed_precision=no''' ) else: cmd_config.append('''--mixed_precision=fp16''' ) if "cpu_offload" in config: cmd_config.append('''--fsdp_offload_params=True''' ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in config: cmd_config.append(F"""--fsdp_auto_wrap_policy={policy}""" ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append('''--fsdp_transformer_layer_cls_to_wrap=BertLayer''' ) elif policy == "SIZE_BASED_WRAP": cmd_config.append('''--fsdp_min_num_params=2000''' ) cmd_config.extend( [ self.test_file_path, F"""--output_dir={self.tmpdir}""", F"""--performance_lower_bound={self.performance_lower_bound}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_SCREAMING_SNAKE_CASE , env=os.environ.copy() ) def __lowercase ( self ) -> List[Any]: _a : Optional[Any] = os.path.join(self.test_scripts_folder , '''test_checkpointing.py''' ) _a : str = [ '''accelerate''', '''launch''', '''--num_processes=2''', '''--num_machines=1''', '''--machine_rank=0''', '''--use_fsdp''', '''--mixed_precision=fp16''', '''--fsdp_transformer_layer_cls_to_wrap=BertLayer''', ] for i, strategy in enumerate(_SCREAMING_SNAKE_CASE ): _a : int = cmd.copy() cmd_config.append(F"""--fsdp_sharding_strategy={i+1}""" ) if strategy != "FULL_SHARD": continue _a : Tuple = len(_SCREAMING_SNAKE_CASE ) for state_dict_type in FSDP_STATE_DICT_TYPE: _a : Dict = cmd_config[:state_dict_config_index] cmd_config.append(F"""--fsdp_state_dict_type={state_dict_type}""" ) cmd_config.extend( [ self.test_file_path, F"""--output_dir={self.tmpdir}""", '''--partial_train_epoch=1''', ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_SCREAMING_SNAKE_CASE , env=os.environ.copy() ) _a : List[Any] = cmd_config[:-1] _a : Optional[int] = os.path.join(self.tmpdir , '''epoch_0''' ) cmd_config.extend( [ F"""--resume_from_checkpoint={resume_from_checkpoint}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_SCREAMING_SNAKE_CASE , env=os.environ.copy() ) def __lowercase ( self ) -> List[str]: _a : Union[str, Any] = os.path.join(self.test_scripts_folder , '''test_peak_memory_usage.py''' ) _a : List[Any] = [ '''accelerate''', '''launch''', '''--num_processes=2''', '''--num_machines=1''', '''--machine_rank=0''', ] for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items(): _a : Optional[int] = cmd.copy() if "fp16" in spec: cmd_config.extend(['''--mixed_precision=fp16'''] ) else: cmd_config.extend(['''--mixed_precision=no'''] ) if "multi_gpu" in spec: continue else: cmd_config.extend(['''--use_fsdp'''] ) for i, strategy in enumerate(_SCREAMING_SNAKE_CASE ): if strategy.lower() in spec: cmd_config.append(F"""--fsdp_sharding_strategy={i+1}""" ) break if "cpu_offload" in spec: cmd_config.append('''--fsdp_offload_params=True''' ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in spec: cmd_config.append(F"""--fsdp_auto_wrap_policy={policy}""" ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append('''--fsdp_transformer_layer_cls_to_wrap=BertLayer''' ) elif policy == "SIZE_BASED_WRAP": cmd_config.append('''--fsdp_min_num_params=2000''' ) cmd_config.extend( [ self.test_file_path, F"""--output_dir={self.tmpdir}""", F"""--peak_memory_upper_bound={peak_mem_upper_bound}""", F"""--n_train={self.n_train}""", F"""--n_val={self.n_val}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_SCREAMING_SNAKE_CASE , env=os.environ.copy() )
235
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) SCREAMING_SNAKE_CASE__ = _symbol_database.Default() SCREAMING_SNAKE_CASE__ = _descriptor_pool.Default().AddSerializedFile( b'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03' ) SCREAMING_SNAKE_CASE__ = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS is False: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = b'H\003' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" SCREAMING_SNAKE_CASE__ = 4_5 SCREAMING_SNAKE_CASE__ = 1_5_8_1 SCREAMING_SNAKE_CASE__ = 1_5_1_7 SCREAMING_SNAKE_CASE__ = 1_5_7_0 SCREAMING_SNAKE_CASE__ = 1_5_8_4 SCREAMING_SNAKE_CASE__ = 1_7_9_3 SCREAMING_SNAKE_CASE__ = 1_7_9_5 SCREAMING_SNAKE_CASE__ = 1_9_1_6 SCREAMING_SNAKE_CASE__ = 1_8_6_4 SCREAMING_SNAKE_CASE__ = 1_9_0_5 SCREAMING_SNAKE_CASE__ = 1_9_1_9 SCREAMING_SNAKE_CASE__ = 2_4_2_9 SCREAMING_SNAKE_CASE__ = 2_2_0_8 SCREAMING_SNAKE_CASE__ = 2_4_1_8 SCREAMING_SNAKE_CASE__ = 2_3_2_3 SCREAMING_SNAKE_CASE__ = 2_4_0_7 # @@protoc_insertion_point(module_scope)
321
0
"""simple docstring""" from dataclasses import dataclass from typing import Tuple import numpy as np import torch @dataclass class __a : '''simple docstring''' _SCREAMING_SNAKE_CASE :Optional[Any] = 42 # [batch_size x 3] _SCREAMING_SNAKE_CASE :Tuple = 42 # [batch_size x 3] _SCREAMING_SNAKE_CASE :Optional[int] = 42 # [batch_size x 3] _SCREAMING_SNAKE_CASE :Any = 42 # [batch_size x 3] _SCREAMING_SNAKE_CASE :List[str] = 42 _SCREAMING_SNAKE_CASE :List[Any] = 42 _SCREAMING_SNAKE_CASE :List[Any] = 42 _SCREAMING_SNAKE_CASE :int = 42 _SCREAMING_SNAKE_CASE :Any = 42 def _a ( self ) -> Union[str, Any]: """simple docstring""" assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0] assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3 assert len(self.x.shape ) == len(self.y.shape ) == len(self.z.shape ) == len(self.origin.shape ) == 2 def _a ( self ) -> Optional[Any]: """simple docstring""" return torch.from_numpy(np.array([self.width, self.height] , dtype=np.floataa ) ) def _a ( self ) -> List[Any]: """simple docstring""" return torch.from_numpy(np.array([self.x_fov, self.y_fov] , dtype=np.floataa ) ) def _a ( self ) -> torch.Tensor: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = torch.arange(self.height * self.width ) SCREAMING_SNAKE_CASE__ : str = torch.stack( [ pixel_indices % self.width, torch.div(_SCREAMING_SNAKE_CASE , self.width , rounding_mode="""trunc""" ), ] , axis=1 , ) return coords @property def _a ( self ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ : List[str] = self.shape SCREAMING_SNAKE_CASE__ : Union[str, Any] = int(np.prod(_SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.get_image_coords() SCREAMING_SNAKE_CASE__ : Dict = torch.broadcast_to(coords.unsqueeze(0 ) , [batch_size * inner_batch_size, *coords.shape] ) SCREAMING_SNAKE_CASE__ : List[Any] = self.get_camera_rays(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE__ : Optional[int] = rays.view(_SCREAMING_SNAKE_CASE , inner_batch_size * self.height * self.width , 2 , 3 ) return rays def _a ( self , _a ) -> torch.Tensor: """simple docstring""" SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = coords.shape assert n_coords == 2 assert batch_size == self.origin.shape[0] SCREAMING_SNAKE_CASE__ : Tuple = coords.view(_SCREAMING_SNAKE_CASE , -1 , 2 ) SCREAMING_SNAKE_CASE__ : Optional[int] = self.resolution() SCREAMING_SNAKE_CASE__ : List[str] = self.fov() SCREAMING_SNAKE_CASE__ : Optional[Any] = (flat.float() / (res - 1)) * 2 - 1 SCREAMING_SNAKE_CASE__ : int = fracs * torch.tan(fov / 2 ) SCREAMING_SNAKE_CASE__ : str = fracs.view(_SCREAMING_SNAKE_CASE , -1 , 2 ) SCREAMING_SNAKE_CASE__ : Any = ( self.z.view(_SCREAMING_SNAKE_CASE , 1 , 3 ) + self.x.view(_SCREAMING_SNAKE_CASE , 1 , 3 ) * fracs[:, :, :1] + self.y.view(_SCREAMING_SNAKE_CASE , 1 , 3 ) * fracs[:, :, 1:] ) SCREAMING_SNAKE_CASE__ : str = directions / directions.norm(dim=-1 , keepdim=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE__ : List[str] = torch.stack( [ torch.broadcast_to(self.origin.view(_SCREAMING_SNAKE_CASE , 1 , 3 ) , [batch_size, directions.shape[1], 3] ), directions, ] , dim=2 , ) return rays.view(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , 2 , 3 ) def _a ( self , _a , _a ) -> "DifferentiableProjectiveCamera": """simple docstring""" assert width * self.height == height * self.width, "The aspect ratio should not change." return DifferentiableProjectiveCamera( origin=self.origin , x=self.x , y=self.y , z=self.z , width=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , x_fov=self.x_fov , y_fov=self.y_fov , ) def _lowercase ( __lowerCAmelCase ) -> DifferentiableProjectiveCamera: SCREAMING_SNAKE_CASE__ : int = [] SCREAMING_SNAKE_CASE__ : Any = [] SCREAMING_SNAKE_CASE__ : str = [] SCREAMING_SNAKE_CASE__ : int = [] for theta in np.linspace(0 , 2 * np.pi , num=20 ): SCREAMING_SNAKE_CASE__ : List[Any] = np.array([np.sin(__UpperCamelCase ), np.cos(__UpperCamelCase ), -0.5] ) z /= np.sqrt(np.sum(z**2 ) ) SCREAMING_SNAKE_CASE__ : Optional[int] = -z * 4 SCREAMING_SNAKE_CASE__ : Dict = np.array([np.cos(__UpperCamelCase ), -np.sin(__UpperCamelCase ), 0.0] ) SCREAMING_SNAKE_CASE__ : Tuple = np.cross(__UpperCamelCase , __UpperCamelCase ) origins.append(__UpperCamelCase ) xs.append(__UpperCamelCase ) ys.append(__UpperCamelCase ) zs.append(__UpperCamelCase ) return DifferentiableProjectiveCamera( origin=torch.from_numpy(np.stack(__UpperCamelCase , axis=0 ) ).float() , x=torch.from_numpy(np.stack(__UpperCamelCase , axis=0 ) ).float() , y=torch.from_numpy(np.stack(__UpperCamelCase , axis=0 ) ).float() , z=torch.from_numpy(np.stack(__UpperCamelCase , axis=0 ) ).float() , width=__UpperCamelCase , height=__UpperCamelCase , x_fov=0.7 , y_fov=0.7 , shape=(1, len(__UpperCamelCase )) , )
132
'''simple docstring''' SCREAMING_SNAKE_CASE__ = 8.31_44_62 # Unit - J mol-1 K-1 def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("""Invalid inputs. Enter positive value.""" ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
321
0
from bisect import bisect from itertools import accumulate def A ( _lowercase , _lowercase , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : int = sorted(zip(__UpperCamelCase , __UpperCamelCase ) , key=lambda _lowercase : x[0] / x[1] , reverse=__UpperCamelCase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = [i[0] for i in r], [i[1] for i in r] SCREAMING_SNAKE_CASE : Tuple = list(accumulate(__UpperCamelCase ) ) SCREAMING_SNAKE_CASE : Optional[Any] = bisect(__UpperCamelCase , __UpperCamelCase ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
182
'''simple docstring''' import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem SCREAMING_SNAKE_CASE__ = importlib.util.find_spec('s3fs') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 SCREAMING_SNAKE_CASE__ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def lowercase__ ( __UpperCamelCase )-> str: if "://" in dataset_path: UpperCamelCase = dataset_path.split("""://""" )[1] return dataset_path def lowercase__ ( __UpperCamelCase )-> bool: if fs is not None and fs.protocol != "file": return True else: return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> int: UpperCamelCase = not is_remote_filesystem(__UpperCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(__UpperCamelCase ) , fs._strip_protocol(__UpperCamelCase ) ) else: fs.mv(__UpperCamelCase , __UpperCamelCase , recursive=__UpperCamelCase ) def lowercase__ ( )-> None: if hasattr(fsspec.asyn , """reset_lock""" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: UpperCamelCase = None UpperCamelCase = None UpperCamelCase = threading.Lock()
321
0
'''simple docstring''' def a_ ( _lowerCAmelCase ,_lowerCAmelCase ) -> float: if discount_rate < 0: raise ValueError('Discount rate cannot be negative' ) if not cash_flows: raise ValueError('Cash flows list cannot be empty' ) __lowerCamelCase : int = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(__UpperCamelCase ) ) return round(__UpperCamelCase ,ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
208
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_xlm_roberta_xl': [ 'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaXLConfig', 'XLMRobertaXLOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaXLForCausalLM', 'XLMRobertaXLForMaskedLM', 'XLMRobertaXLForMultipleChoice', 'XLMRobertaXLForQuestionAnswering', 'XLMRobertaXLForSequenceClassification', 'XLMRobertaXLForTokenClassification', 'XLMRobertaXLModel', 'XLMRobertaXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure)
321
0
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import AlignProcessor, EfficientNetImageProcessor @require_vision class UpperCamelCase__ ( unittest.TestCase): def lowercase_ ( self :Dict ) -> Tuple: '''simple docstring''' __A = tempfile.mkdtemp() __A = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __A = 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] ) ) __A = { 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 18, 'do_normalize': True, 'image_mean': [0.48_145_466, 0.4_578_275, 0.40_821_073], 'image_std': [0.26_862_954, 0.26_130_258, 0.27_577_711], } __A = os.path.join(self.tmpdirname , _SCREAMING_SNAKE_CASE ) with open(self.image_processor_file , 'w' , encoding='utf-8' ) as fp: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def lowercase_ ( self :List[Any] , **_A :Dict ) -> Any: '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def lowercase_ ( self :Union[str, Any] , **_A :str ) -> Optional[Any]: '''simple docstring''' return BertTokenizerFast.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def lowercase_ ( self :str , **_A :List[Any] ) -> Dict: '''simple docstring''' return EfficientNetImageProcessor.from_pretrained(self.tmpdirname , **_SCREAMING_SNAKE_CASE ) def lowercase_ ( self :List[str] ) -> str: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowercase_ ( self :List[Any] ) -> Any: '''simple docstring''' __A = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] __A = [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs def lowercase_ ( self :Optional[int] ) -> Any: '''simple docstring''' __A = self.get_tokenizer() __A = self.get_rust_tokenizer() __A = self.get_image_processor() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) processor_slow.save_pretrained(self.tmpdirname ) __A = AlignProcessor.from_pretrained(self.tmpdirname , use_fast=_SCREAMING_SNAKE_CASE ) __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) processor_fast.save_pretrained(self.tmpdirname ) __A = AlignProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.image_processor , _SCREAMING_SNAKE_CASE ) def lowercase_ ( self :List[str] ) -> str: '''simple docstring''' __A = AlignProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __A = self.get_tokenizer(bos_token='(BOS)' , eos_token='(EOS)' ) __A = self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE , padding_value=1.0 ) __A = AlignProcessor.from_pretrained( self.tmpdirname , bos_token='(BOS)' , eos_token='(EOS)' , do_normalize=_SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _SCREAMING_SNAKE_CASE ) def lowercase_ ( self :int ) -> str: '''simple docstring''' __A = self.get_image_processor() __A = self.get_tokenizer() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) __A = self.prepare_image_inputs() __A = image_processor(_SCREAMING_SNAKE_CASE , return_tensors='np' ) __A = processor(images=_SCREAMING_SNAKE_CASE , return_tensors='np' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def lowercase_ ( self :Dict ) -> Union[str, Any]: '''simple docstring''' __A = self.get_image_processor() __A = self.get_tokenizer() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) __A = 'lower newer' __A = processor(text=_SCREAMING_SNAKE_CASE ) __A = tokenizer(_SCREAMING_SNAKE_CASE , padding='max_length' , max_length=64 ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase_ ( self :Union[str, Any] ) -> List[Any]: '''simple docstring''' __A = self.get_image_processor() __A = self.get_tokenizer() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) __A = 'lower newer' __A = self.prepare_image_inputs() __A = processor(text=_SCREAMING_SNAKE_CASE , images=_SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values'] ) # test if it raises when no input is passed with pytest.raises(_SCREAMING_SNAKE_CASE ): processor() def lowercase_ ( self :Any ) -> Optional[Any]: '''simple docstring''' __A = self.get_image_processor() __A = self.get_tokenizer() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) __A = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __A = processor.batch_decode(_SCREAMING_SNAKE_CASE ) __A = tokenizer.batch_decode(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def lowercase_ ( self :int ) -> Tuple: '''simple docstring''' __A = self.get_image_processor() __A = self.get_tokenizer() __A = AlignProcessor(tokenizer=_SCREAMING_SNAKE_CASE , image_processor=_SCREAMING_SNAKE_CASE ) __A = 'lower newer' __A = self.prepare_image_inputs() __A = processor(text=_SCREAMING_SNAKE_CASE , images=_SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
161
'''simple docstring''' import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = 'docs/source/en/_toctree.yml' def lowercase__ ( __UpperCamelCase )-> Optional[Any]: UpperCamelCase = defaultdict(__UpperCamelCase ) UpperCamelCase = [] UpperCamelCase = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__UpperCamelCase ) UpperCamelCase = new_doc_list UpperCamelCase = [key for key, value in counts.items() if value > 1] UpperCamelCase = [] for duplicate_key in duplicates: UpperCamelCase = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__UpperCamelCase ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) UpperCamelCase = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__UpperCamelCase ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__UpperCamelCase ) # Sort return overview_doc def lowercase__ ( __UpperCamelCase=False )-> List[str]: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 UpperCamelCase = api_doc[scheduler_idx]["""sections"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) UpperCamelCase = False if new_scheduler_doc != scheduler_doc: UpperCamelCase = True if overwrite: UpperCamelCase = new_scheduler_doc if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def lowercase__ ( __UpperCamelCase=False )-> Tuple: with open(__UpperCamelCase , encoding="""utf-8""" ) as f: UpperCamelCase = yaml.safe_load(f.read() ) # Get to the API doc UpperCamelCase = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCamelCase = content[api_idx]["""sections"""] # Then to the model doc UpperCamelCase = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 UpperCamelCase = False UpperCamelCase = api_doc[pipeline_idx]["""sections"""] UpperCamelCase = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: UpperCamelCase = pipeline_doc["""section"""] UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if overwrite: UpperCamelCase = new_sub_pipeline_doc new_pipeline_docs.append(__UpperCamelCase ) # sort overall pipeline doc UpperCamelCase = clean_doc_toc(__UpperCamelCase ) if new_pipeline_docs != pipeline_docs: UpperCamelCase = True if overwrite: UpperCamelCase = new_pipeline_docs if diff: if overwrite: UpperCamelCase = api_doc with open(__UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) 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__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
321
0
'''simple docstring''' import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def a ( __a , __a=None ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ :str = None if token is not None: UpperCamelCase__ :Union[str, Any] = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': f'''Bearer {token}'''} UpperCamelCase__ :str = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' UpperCamelCase__ :str = requests.get(__UpperCamelCase , headers=__UpperCamelCase ).json() UpperCamelCase__ :Union[str, Any] = {} try: job_links.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) UpperCamelCase__ :str = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(__UpperCamelCase ): UpperCamelCase__ :int = requests.get(url + f'''&page={i + 2}''' , headers=__UpperCamelCase ).json() job_links.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) return job_links except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} def a ( __a , __a=None ) -> List[str]: '''simple docstring''' UpperCamelCase__ :List[Any] = None if token is not None: UpperCamelCase__ :int = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': f'''Bearer {token}'''} UpperCamelCase__ :int = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100''' UpperCamelCase__ :List[str] = requests.get(__UpperCamelCase , headers=__UpperCamelCase ).json() UpperCamelCase__ :Any = {} try: artifacts.update({artifact['''name''']: artifact['''archive_download_url'''] for artifact in result['''artifacts''']} ) UpperCamelCase__ :str = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(__UpperCamelCase ): UpperCamelCase__ :Optional[Any] = requests.get(url + f'''&page={i + 2}''' , headers=__UpperCamelCase ).json() artifacts.update({artifact['''name''']: artifact['''archive_download_url'''] for artifact in result['''artifacts''']} ) return artifacts except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} def a ( __a , __a , __a , __a ) -> str: '''simple docstring''' UpperCamelCase__ :str = None if token is not None: UpperCamelCase__ :List[str] = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': f'''Bearer {token}'''} UpperCamelCase__ :Union[str, Any] = requests.get(__UpperCamelCase , headers=__UpperCamelCase , allow_redirects=__UpperCamelCase ) UpperCamelCase__ :Any = result.headers['''Location'''] UpperCamelCase__ :str = requests.get(__UpperCamelCase , allow_redirects=__UpperCamelCase ) UpperCamelCase__ :Any = os.path.join(__UpperCamelCase , f'''{artifact_name}.zip''' ) with open(__UpperCamelCase , '''wb''' ) as fp: fp.write(response.content ) def a ( __a , __a=None ) -> str: '''simple docstring''' UpperCamelCase__ :int = [] UpperCamelCase__ :Tuple = [] UpperCamelCase__ :Dict = None with zipfile.ZipFile(__UpperCamelCase ) as z: for filename in z.namelist(): if not os.path.isdir(__UpperCamelCase ): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(__UpperCamelCase ) as f: for line in f: UpperCamelCase__ :Tuple = line.decode('''UTF-8''' ).strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs UpperCamelCase__ :Optional[Any] = line[: line.index(''': ''' )] UpperCamelCase__ :Tuple = line[line.index(''': ''' ) + len(''': ''' ) :] errors.append([error_line, error] ) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith('''FAILED ''' ): # `test` is the test method that failed UpperCamelCase__ :Optional[Any] = line[len('''FAILED ''' ) :] failed_tests.append(__UpperCamelCase ) elif filename == "job_name.txt": UpperCamelCase__ :Tuple = line if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise ValueError( f'''`errors` and `failed_tests` should have the same number of elements. Got {len(__UpperCamelCase )} for `errors` ''' f'''and {len(__UpperCamelCase )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some''' ''' problem.''' ) UpperCamelCase__ :Tuple = None if job_name and job_links: UpperCamelCase__ :Tuple = job_links.get(__UpperCamelCase , __UpperCamelCase ) # A list with elements of the form (line of error, error, failed test) UpperCamelCase__ :str = [x + [y] + [job_link] for x, y in zip(__UpperCamelCase , __UpperCamelCase )] return result def a ( __a , __a=None ) -> int: '''simple docstring''' UpperCamelCase__ :Optional[int] = [] UpperCamelCase__ :Optional[int] = [os.path.join(__UpperCamelCase , __UpperCamelCase ) for p in os.listdir(__UpperCamelCase ) if p.endswith('''.zip''' )] for p in paths: errors.extend(get_errors_from_single_artifact(__UpperCamelCase , job_links=__UpperCamelCase ) ) return errors def a ( __a , __a=None ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ :List[Any] = Counter() counter.update([x[1] for x in logs] ) UpperCamelCase__ :int = counter.most_common() UpperCamelCase__ :Dict = {} for error, count in counts: if error_filter is None or error not in error_filter: UpperCamelCase__ :Optional[Any] = {'''count''': count, '''failed_tests''': [(x[2], x[0]) for x in logs if x[1] == error]} UpperCamelCase__ :Optional[Any] = dict(sorted(r.items() , key=lambda __a : item[1]["count"] , reverse=__UpperCamelCase ) ) return r def a ( __a ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ :int = test.split('''::''' )[0] if test.startswith('''tests/models/''' ): UpperCamelCase__ :Dict = test.split('''/''' )[2] else: UpperCamelCase__ :int = None return test def a ( __a , __a=None ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ :List[Any] = [(x[0], x[1], get_model(x[2] )) for x in logs] UpperCamelCase__ :List[str] = [x for x in logs if x[2] is not None] UpperCamelCase__ :List[Any] = {x[2] for x in logs} UpperCamelCase__ :Union[str, Any] = {} for test in tests: UpperCamelCase__ :Tuple = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test] ) UpperCamelCase__ :Optional[Any] = counter.most_common() UpperCamelCase__ :int = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} UpperCamelCase__ :Optional[Any] = sum(error_counts.values() ) if n_errors > 0: UpperCamelCase__ :int = {'''count''': n_errors, '''errors''': error_counts} UpperCamelCase__ :int = dict(sorted(r.items() , key=lambda __a : item[1]["count"] , reverse=__UpperCamelCase ) ) return r def a ( __a ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ :int = '''| no. | error | status |''' UpperCamelCase__ :Tuple = '''|-:|:-|:-|''' UpperCamelCase__ :Union[str, Any] = [header, sep] for error in reduced_by_error: UpperCamelCase__ :int = reduced_by_error[error]['''count'''] UpperCamelCase__ :str = f'''| {count} | {error[:100]} | |''' lines.append(__UpperCamelCase ) return "\n".join(__UpperCamelCase ) def a ( __a ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ :int = '''| model | no. of errors | major error | count |''' UpperCamelCase__ :Any = '''|-:|-:|-:|-:|''' UpperCamelCase__ :Tuple = [header, sep] for model in reduced_by_model: UpperCamelCase__ :Tuple = reduced_by_model[model]['''count'''] UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = list(reduced_by_model[model]['''errors'''].items() )[0] UpperCamelCase__ :Any = f'''| {model} | {count} | {error[:60]} | {_count} |''' lines.append(__UpperCamelCase ) return "\n".join(__UpperCamelCase ) if __name__ == "__main__": __snake_case = 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.''') __snake_case = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) __snake_case = get_job_links(args.workflow_run_id, token=args.token) __snake_case = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: __snake_case = k.find(''' / ''') __snake_case = k[index + len(''' / ''') :] __snake_case = v with open(os.path.join(args.output_dir, '''job_links.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) __snake_case = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, '''artifacts.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) __snake_case = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error __snake_case = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors __snake_case = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, '''errors.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) __snake_case = reduce_by_error(errors) __snake_case = reduce_by_model(errors) __snake_case = make_github_table(reduced_by_error) __snake_case = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, '''reduced_by_error.txt'''), '''w''', encoding='''UTF-8''') as fp: fp.write(sa) with open(os.path.join(args.output_dir, '''reduced_by_model.txt'''), '''w''', encoding='''UTF-8''') as fp: fp.write(sa)
97
'''simple docstring''' import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) with open(F"{class_data_dir}/caption.txt" , """w""" ) as fa, open(F"{class_data_dir}/urls.txt" , """w""" ) as fa, open( F"{class_data_dir}/images.txt" , """w""" ) as fa: while total < num_class_images: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = Image.open(BytesIO(img.content ) ) with open(F"{class_data_dir}/images/{total}.jpg" , """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(F"{class_data_dir}/images/{total}.jpg" + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
0
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup _lowerCamelCase =logging.get_logger(__name__) class A__ ( __SCREAMING_SNAKE_CASE): def __init__( self , **__magic_name__ ): requires_backends(self , ["""bs4"""] ) super().__init__(**_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , __magic_name__ ): lowerCamelCase : str = [] lowerCamelCase : List[str] = [] lowerCamelCase : Union[str, Any] = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag lowerCamelCase : Any = parent.find_all(child.name , recursive=_SCREAMING_SNAKE_CASE ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_SCREAMING_SNAKE_CASE ) else next(i for i, s in enumerate(_SCREAMING_SNAKE_CASE , 1 ) if s is child ) ) lowerCamelCase : Optional[Any] = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def UpperCamelCase__ ( self , __magic_name__ ): lowerCamelCase : Tuple = BeautifulSoup(_SCREAMING_SNAKE_CASE , """html.parser""" ) lowerCamelCase : List[Any] = [] lowerCamelCase : Union[str, Any] = [] lowerCamelCase : Any = [] for element in html_code.descendants: if type(_SCREAMING_SNAKE_CASE ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue lowerCamelCase : Dict = html.unescape(_SCREAMING_SNAKE_CASE ).strip() if not text_in_this_tag: continue all_doc_strings.append(_SCREAMING_SNAKE_CASE ) lowerCamelCase , lowerCamelCase : Optional[Any] = self.xpath_soup(_SCREAMING_SNAKE_CASE ) stringaxtag_seq.append(_SCREAMING_SNAKE_CASE ) stringaxsubs_seq.append(_SCREAMING_SNAKE_CASE ) if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError("""Number of doc strings and xtags does not correspond""" ) if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError("""Number of doc strings and xsubs does not correspond""" ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ ): lowerCamelCase : Dict = """""" for tagname, subs in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): xpath += F'''/{tagname}''' if subs != 0: xpath += F'''[{subs}]''' return xpath def __call__( self , __magic_name__ ): lowerCamelCase : Optional[Any] = False # Check that strings has a valid type if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): lowerCamelCase : int = True elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ): if len(_SCREAMING_SNAKE_CASE ) == 0 or isinstance(html_strings[0] , _SCREAMING_SNAKE_CASE ): lowerCamelCase : List[Any] = True if not valid_strings: raise ValueError( """HTML strings must of type `str`, `List[str]` (batch of examples), """ F'''but is of type {type(_SCREAMING_SNAKE_CASE )}.''' ) lowerCamelCase : str = bool(isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(html_strings[0] , _SCREAMING_SNAKE_CASE )) ) if not is_batched: lowerCamelCase : List[str] = [html_strings] # Get nodes + xpaths lowerCamelCase : List[str] = [] lowerCamelCase : List[str] = [] for html_string in html_strings: lowerCamelCase , lowerCamelCase , lowerCamelCase : Union[str, Any] = self.get_three_from_single(_SCREAMING_SNAKE_CASE ) nodes.append(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Dict = [] for node, tag_list, sub_list in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): lowerCamelCase : Optional[Any] = self.construct_xpath(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) xpath_strings.append(_SCREAMING_SNAKE_CASE ) xpaths.append(_SCREAMING_SNAKE_CASE ) # return as Dict lowerCamelCase : List[str] = {"""nodes""": nodes, """xpaths""": xpaths} lowerCamelCase : Dict = BatchFeature(data=_SCREAMING_SNAKE_CASE , tensor_type=_SCREAMING_SNAKE_CASE ) return encoded_inputs
287
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = 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 a_ : lowercase = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """The column name of the images in the files."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the training data."""} ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """A folder containing the validation data."""} ) lowercase = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def A__ ( self ) -> Tuple: """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class a_ : lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) lowercase = field( default=lowerCamelCase , 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""" ) } , ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) lowercase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase = field(default=lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase = field( default=lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) lowercase = field( default=lowerCamelCase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class a_ ( lowerCamelCase ): lowercase = field( default=1E-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def lowercase__ ( __UpperCamelCase )-> int: UpperCamelCase = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def lowercase__ ( )-> List[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase ,UpperCamelCase ,UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase ,UpperCamelCase ,UpperCamelCase = 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""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCamelCase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase = split["""train"""] UpperCamelCase = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(F"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(F"New config: {config}" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__UpperCamelCase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__UpperCamelCase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase = ViTMAEForPreTraining(__UpperCamelCase ) if training_args.do_train: UpperCamelCase = ds["""train"""].column_names else: UpperCamelCase = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = """image""" elif "img" in column_names: UpperCamelCase = """img""" else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size["""shortest_edge"""] else: UpperCamelCase = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase = Compose( [ Lambda(lambda __UpperCamelCase : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(__UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__UpperCamelCase ): UpperCamelCase = [transforms(__UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__UpperCamelCase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def lowercase__ ( __UpperCamelCase )-> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
321
0
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __A ( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase ( self : Dict ) -> List[Any]: """simple docstring""" lowercase__ : Dict = [[1, 2, 4], [1, 2, 3, 4]] lowercase__ : Dict = DisjunctiveConstraint(_SCREAMING_SNAKE_CASE ) self.assertTrue(isinstance(dc.token_ids ,_SCREAMING_SNAKE_CASE ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: """simple docstring""" lowercase__ : Dict = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(_SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(_SCREAMING_SNAKE_CASE ) # fails here def UpperCAmelCase ( self : Dict ) -> List[Any]: """simple docstring""" lowercase__ : str = [[1, 2, 3], [1, 2, 4]] lowercase__ : int = DisjunctiveConstraint(_SCREAMING_SNAKE_CASE ) lowercase__ , lowercase__ , lowercase__ : int = dc.update(1 ) lowercase__ : Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(_SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) lowercase__ , lowercase__ , lowercase__ : List[Any] = dc.update(2 ) lowercase__ : Any = stepped is True and completed is False and reset is False self.assertTrue(_SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) lowercase__ , lowercase__ , lowercase__ : Optional[int] = dc.update(3 ) lowercase__ : int = stepped is True and completed is True and reset is False self.assertTrue(_SCREAMING_SNAKE_CASE ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def UpperCAmelCase ( self : str ) -> Optional[int]: """simple docstring""" lowercase__ : Any = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] lowercase__ : Optional[Any] = DisjunctiveConstraint(_SCREAMING_SNAKE_CASE ) lowercase__ , lowercase__ , lowercase__ : Optional[int] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) lowercase__ , lowercase__ , lowercase__ : Any = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) lowercase__ , lowercase__ , lowercase__ : Optional[int] = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) lowercase__ , lowercase__ , lowercase__ : int = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() lowercase__ , lowercase__ , lowercase__ : Optional[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) lowercase__ , lowercase__ , lowercase__ : str = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) lowercase__ , lowercase__ , lowercase__ : List[Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
16
'''simple docstring''' import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # pylint: disable=invalid-name SCREAMING_SNAKE_CASE__ = 2_5_6 class a_ ( lowerCamelCase ): lowercase = ["""melgan"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> None: """simple docstring""" super().__init__() # From MELGAN UpperCamelCase = math.log(1e-5 ) # Matches MelGAN training. UpperCamelCase = 4.0 # Largest value for most examples UpperCamelCase = 128 self.register_modules( notes_encoder=_SCREAMING_SNAKE_CASE , continuous_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , melgan=_SCREAMING_SNAKE_CASE , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Any: """simple docstring""" UpperCamelCase ,UpperCamelCase = output_range if clip: UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , self.min_value , self.max_value ) # Scale to [0, 1]. UpperCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=(-1.0, 1.0) , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase ,UpperCamelCase = input_range UpperCamelCase = torch.clip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if clip else outputs # Scale to [0, 1]. UpperCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" UpperCamelCase = input_tokens > 0 UpperCamelCase ,UpperCamelCase = self.notes_encoder( encoder_input_tokens=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) UpperCamelCase ,UpperCamelCase = self.continuous_encoder( encoder_inputs=_SCREAMING_SNAKE_CASE , encoder_inputs_mask=_SCREAMING_SNAKE_CASE ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" UpperCamelCase = noise_time if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML UpperCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) UpperCamelCase = self.decoder( encodings_and_masks=_SCREAMING_SNAKE_CASE , decoder_input_tokens=_SCREAMING_SNAKE_CASE , decoder_noise_time=_SCREAMING_SNAKE_CASE ) return logits @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "numpy" , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , ) -> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(_SCREAMING_SNAKE_CASE )}." ) UpperCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) UpperCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) UpperCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) for i, encoder_input_tokens in enumerate(_SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. UpperCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_SCREAMING_SNAKE_CASE , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. UpperCamelCase = ones UpperCamelCase = self.scale_features( _SCREAMING_SNAKE_CASE , output_range=[-1.0, 1.0] , clip=_SCREAMING_SNAKE_CASE ) UpperCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_SCREAMING_SNAKE_CASE , continuous_mask=_SCREAMING_SNAKE_CASE , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop UpperCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_SCREAMING_SNAKE_CASE , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_SCREAMING_SNAKE_CASE ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCamelCase = self.decode( encodings_and_masks=_SCREAMING_SNAKE_CASE , input_tokens=_SCREAMING_SNAKE_CASE , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 UpperCamelCase = self.scheduler.step(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase = self.scale_to_features(_SCREAMING_SNAKE_CASE , input_range=[-1.0, 1.0] ) UpperCamelCase = mel[:1] UpperCamelCase = mel.cpu().float().numpy() UpperCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) logger.info("""Generated segment""" , _SCREAMING_SNAKE_CASE ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": UpperCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: UpperCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_SCREAMING_SNAKE_CASE )
321
0
"""simple docstring""" import os def __a ( __lowerCamelCase = "matrix.txt" ): with open(os.path.join(os.path.dirname(__UpperCamelCase ), __UpperCamelCase ) ) as in_file: UpperCAmelCase_ : Optional[int] = in_file.read() UpperCAmelCase_ : List[Any] = [[int(__UpperCamelCase ) for cell in row.split("," )] for row in data.strip().splitlines()] UpperCAmelCase_ : Optional[Any] = [[0 for cell in row] for row in grid] UpperCAmelCase_ : Optional[Any] = len(grid[0] ) UpperCAmelCase_ : Dict = [[0 for i in range(__UpperCamelCase )] for j in range(__UpperCamelCase )] UpperCAmelCase_ : Optional[Any] = grid[0][0] for i in range(1, __UpperCamelCase ): UpperCAmelCase_ : Dict = grid[0][i] + dp[0][i - 1] for i in range(1, __UpperCamelCase ): UpperCAmelCase_ : Any = grid[i][0] + dp[i - 1][0] for i in range(1, __UpperCamelCase ): for j in range(1, __UpperCamelCase ): UpperCAmelCase_ : Tuple = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1] ) return dp[-1][-1] if __name__ == "__main__": print(f"""{solution() = }""")
61
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 4000000 )-> int: UpperCamelCase = [] UpperCamelCase ,UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCamelCase ) UpperCamelCase ,UpperCamelCase = b, a + b return sum(__UpperCamelCase ) if __name__ == "__main__": print(f'{solution() = }')
321
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer lowerCamelCase : List[str] = logging.get_logger(__name__) lowerCamelCase : Dict = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase : Optional[int] = { "vocab_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json" ), }, } lowerCamelCase : str = { "yjernite/retribert-base-uncased": 5_1_2, } lowerCamelCase : int = { "yjernite/retribert-base-uncased": {"do_lower_case": True}, } class A__ ( A__ ): A__ = VOCAB_FILES_NAMES A__ = PRETRAINED_VOCAB_FILES_MAP A__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ = PRETRAINED_INIT_CONFIGURATION A__ = RetriBertTokenizer A__ = ['input_ids', 'attention_mask'] def __init__( self : Tuple , _a : List[Any]=None , _a : List[str]=None , _a : Tuple=True , _a : Optional[int]="[UNK]" , _a : Optional[Any]="[SEP]" , _a : Union[str, Any]="[PAD]" , _a : Dict="[CLS]" , _a : str="[MASK]" , _a : Any=True , _a : Dict=None , **_a : int , ) -> int: '''simple docstring''' super().__init__( _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , do_lower_case=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , tokenize_chinese_chars=_SCREAMING_SNAKE_CASE , strip_accents=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , _SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get('strip_accents' , _SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get('handle_chinese_chars' , _SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): _SCREAMING_SNAKE_CASE =getattr(_SCREAMING_SNAKE_CASE , normalizer_state.pop('type' ) ) _SCREAMING_SNAKE_CASE =do_lower_case _SCREAMING_SNAKE_CASE =strip_accents _SCREAMING_SNAKE_CASE =tokenize_chinese_chars _SCREAMING_SNAKE_CASE =normalizer_class(**_SCREAMING_SNAKE_CASE ) _SCREAMING_SNAKE_CASE =do_lower_case def A ( self : int , _a : Tuple , _a : int=None ) -> int: '''simple docstring''' _SCREAMING_SNAKE_CASE =[self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def A ( self : str , _a : List[str] , _a : Optional[int] = None ) -> List[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def A ( self : Tuple , _a : List[Any] , _a : Union[str, Any] = None ) -> Tuple[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
47
'''simple docstring''' def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> bool: # Base Case if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] , __UpperCamelCase , __UpperCamelCase ): # Color current vertex UpperCamelCase = i # Validate coloring if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , index + 1 ): return True # Backtrack UpperCamelCase = -1 return False def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> list[int]: UpperCamelCase = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , 0 ): return colored_vertices return []
321
0
"""simple docstring""" from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def __UpperCAmelCase ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] = None ) -> str: '''simple docstring''' if version.parse(hfh.__version__ ).release < version.parse('0.11.0' ).release: # old versions of hfh don't url-encode the file path __snake_case : int = quote(__UpperCamelCase ) return hfh.hf_hub_url(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' , revision=__UpperCamelCase )
172
'''simple docstring''' def lowercase__ ( __UpperCamelCase = 2000000 )-> int: UpperCamelCase = [0 for i in range(n + 1 )] UpperCamelCase = 1 UpperCamelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __UpperCamelCase ): UpperCamelCase = 1 UpperCamelCase = 0 for i in range(__UpperCamelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'{solution() = }')
321
0
from ....configuration_utils import PretrainedConfig from ....utils import logging a__ = logging.get_logger(__name__) # TODO: upload to AWS a__ = { '''yjernite/retribert-base-uncased''': ( '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json''' ), } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = "retribert" def __init__( self , _a=3_0_5_2_2 , _a=7_6_8 , _a=8 , _a=1_2 , _a=3_0_7_2 , _a="gelu" , _a=0.1 , _a=0.1 , _a=5_1_2 , _a=2 , _a=0.02 , _a=1e-1_2 , _a=True , _a=1_2_8 , _a=0 , **_a , ) -> Any: super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) _a : Optional[int] = vocab_size _a : Union[str, Any] = hidden_size _a : str = num_hidden_layers _a : int = num_attention_heads _a : Dict = hidden_act _a : List[str] = intermediate_size _a : List[str] = hidden_dropout_prob _a : Optional[int] = attention_probs_dropout_prob _a : Optional[int] = max_position_embeddings _a : Dict = type_vocab_size _a : List[Any] = initializer_range _a : Optional[Any] = layer_norm_eps _a : Any = share_encoders _a : Union[str, Any] = projection_dim
235
'''simple docstring''' from timeit import timeit def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: number &= number - 1 result += 1 return result def lowercase__ ( __UpperCamelCase )-> int: if number < 0: raise ValueError("""the value of input must not be negative""" ) UpperCamelCase = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowercase__ ( )-> None: def do_benchmark(__UpperCamelCase ) -> None: UpperCamelCase = """import __main__ as z""" print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) UpperCamelCase = timeit("""z.get_set_bits_count_using_modulo_operator(25)""" , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) UpperCamelCase = timeit( """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""" , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
321
0
"""simple docstring""" import qiskit def _lowercase ( __lowerCAmelCase , __lowerCAmelCase ) -> qiskit.result.counts.Counts: SCREAMING_SNAKE_CASE__ : int = qiskit.Aer.get_backend("""aer_simulator""" ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE__ : Dict = qiskit.QuantumCircuit(__UpperCamelCase , __UpperCamelCase ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator SCREAMING_SNAKE_CASE__ : Dict = qiskit.execute(__UpperCamelCase , __UpperCamelCase , shots=1000 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(__UpperCamelCase ) if __name__ == "__main__": a :Any = single_qubit_measure(2, 2) print(f'Total count for various states are: {counts}')
132
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
0
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bart import BartTokenizer __UpperCamelCase : str = logging.get_logger(__name__) __UpperCamelCase : str = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} # See all BART models at https://huggingface.co/models?filter=bart __UpperCamelCase : Optional[int] = { 'vocab_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json', }, 'merges_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt', }, 'tokenizer_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json', }, } __UpperCamelCase : Tuple = { 'facebook/bart-base': 1024, 'facebook/bart-large': 1024, 'facebook/bart-large-mnli': 1024, 'facebook/bart-large-cnn': 1024, 'facebook/bart-large-xsum': 1024, 'yjernite/bart_eli5': 1024, } class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = ["""input_ids""", """attention_mask"""] UpperCamelCase_ = BartTokenizer def __init__( self : str , UpperCamelCase__ : int=None , UpperCamelCase__ : Dict=None , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple="replace" , UpperCamelCase__ : str="<s>" , UpperCamelCase__ : Tuple="</s>" , UpperCamelCase__ : List[Any]="</s>" , UpperCamelCase__ : str="<s>" , UpperCamelCase__ : Any="<unk>" , UpperCamelCase__ : Tuple="<pad>" , UpperCamelCase__ : Optional[Any]="<mask>" , UpperCamelCase__ : int=False , UpperCamelCase__ : List[Any]=True , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , errors=_SCREAMING_SNAKE_CASE , bos_token=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , add_prefix_space=_SCREAMING_SNAKE_CASE , trim_offsets=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) SCREAMING_SNAKE_CASE : Dict = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _SCREAMING_SNAKE_CASE ) != add_prefix_space: SCREAMING_SNAKE_CASE : Optional[int] = getattr(_SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Any = add_prefix_space SCREAMING_SNAKE_CASE : Optional[int] = pre_tok_class(**_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE : int = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` SCREAMING_SNAKE_CASE : Dict = '''post_processor''' SCREAMING_SNAKE_CASE : str = getattr(self.backend_tokenizer , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: SCREAMING_SNAKE_CASE : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: SCREAMING_SNAKE_CASE : Optional[int] = tuple(state['''sep'''] ) if "cls" in state: SCREAMING_SNAKE_CASE : List[str] = tuple(state['''cls'''] ) SCREAMING_SNAKE_CASE : Union[str, Any] = False if state.get('''add_prefix_space''' , _SCREAMING_SNAKE_CASE ) != add_prefix_space: SCREAMING_SNAKE_CASE : Tuple = add_prefix_space SCREAMING_SNAKE_CASE : Optional[Any] = True if state.get('''trim_offsets''' , _SCREAMING_SNAKE_CASE ) != trim_offsets: SCREAMING_SNAKE_CASE : int = trim_offsets SCREAMING_SNAKE_CASE : Optional[int] = True if changes_to_apply: SCREAMING_SNAKE_CASE : int = getattr(_SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Optional[int] = component_class(**_SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @property def __A ( self : Tuple ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __A ( self : Optional[Any] , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else value SCREAMING_SNAKE_CASE : Dict = value def __A ( self : str , *UpperCamelCase__ : str , **UpperCamelCase__ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = kwargs.get('''is_split_into_words''' , _SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def __A ( self : Tuple , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = kwargs.get('''is_split_into_words''' , _SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def __A ( self : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE ) def __A ( self : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : Dict=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __A ( self : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : Any = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [self.sep_token_id] SCREAMING_SNAKE_CASE : List[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
182
'''simple docstring''' import math def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
321
0
'''simple docstring''' import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = OrderedDict( [ ('align', 'EfficientNetImageProcessor'), ('beit', 'BeitImageProcessor'), ('bit', 'BitImageProcessor'), ('blip', 'BlipImageProcessor'), ('blip-2', 'BlipImageProcessor'), ('bridgetower', 'BridgeTowerImageProcessor'), ('chinese_clip', 'ChineseCLIPImageProcessor'), ('clip', 'CLIPImageProcessor'), ('clipseg', 'ViTImageProcessor'), ('conditional_detr', 'ConditionalDetrImageProcessor'), ('convnext', 'ConvNextImageProcessor'), ('convnextv2', 'ConvNextImageProcessor'), ('cvt', 'ConvNextImageProcessor'), ('data2vec-vision', 'BeitImageProcessor'), ('deformable_detr', 'DeformableDetrImageProcessor'), ('deit', 'DeiTImageProcessor'), ('deta', 'DetaImageProcessor'), ('detr', 'DetrImageProcessor'), ('dinat', 'ViTImageProcessor'), ('donut-swin', 'DonutImageProcessor'), ('dpt', 'DPTImageProcessor'), ('efficientformer', 'EfficientFormerImageProcessor'), ('efficientnet', 'EfficientNetImageProcessor'), ('flava', 'FlavaImageProcessor'), ('focalnet', 'BitImageProcessor'), ('git', 'CLIPImageProcessor'), ('glpn', 'GLPNImageProcessor'), ('groupvit', 'CLIPImageProcessor'), ('imagegpt', 'ImageGPTImageProcessor'), ('instructblip', 'BlipImageProcessor'), ('layoutlmv2', 'LayoutLMv2ImageProcessor'), ('layoutlmv3', 'LayoutLMv3ImageProcessor'), ('levit', 'LevitImageProcessor'), ('mask2former', 'Mask2FormerImageProcessor'), ('maskformer', 'MaskFormerImageProcessor'), ('mgp-str', 'ViTImageProcessor'), ('mobilenet_v1', 'MobileNetV1ImageProcessor'), ('mobilenet_v2', 'MobileNetV2ImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevitv2', 'MobileViTImageProcessor'), ('nat', 'ViTImageProcessor'), ('oneformer', 'OneFormerImageProcessor'), ('owlvit', 'OwlViTImageProcessor'), ('perceiver', 'PerceiverImageProcessor'), ('pix2struct', 'Pix2StructImageProcessor'), ('poolformer', 'PoolFormerImageProcessor'), ('regnet', 'ConvNextImageProcessor'), ('resnet', 'ConvNextImageProcessor'), ('sam', 'SamImageProcessor'), ('segformer', 'SegformerImageProcessor'), ('swiftformer', 'ViTImageProcessor'), ('swin', 'ViTImageProcessor'), ('swin2sr', 'Swin2SRImageProcessor'), ('swinv2', 'ViTImageProcessor'), ('table-transformer', 'DetrImageProcessor'), ('timesformer', 'VideoMAEImageProcessor'), ('tvlt', 'TvltImageProcessor'), ('upernet', 'SegformerImageProcessor'), ('van', 'ConvNextImageProcessor'), ('videomae', 'VideoMAEImageProcessor'), ('vilt', 'ViltImageProcessor'), ('vit', 'ViTImageProcessor'), ('vit_hybrid', 'ViTHybridImageProcessor'), ('vit_mae', 'ViTImageProcessor'), ('vit_msn', 'ViTImageProcessor'), ('xclip', 'CLIPImageProcessor'), ('yolos', 'YolosImageProcessor'), ] ) _UpperCamelCase = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def a_ ( _lowerCAmelCase ) -> int: for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: __lowerCamelCase : Optional[Any] = model_type_to_module_name(__UpperCamelCase ) __lowerCamelCase : List[str] = importlib.import_module(F'.{module_name}' ,'transformers.models' ) try: return getattr(__UpperCamelCase ,__UpperCamelCase ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(__UpperCamelCase ,'__name__' ,__UpperCamelCase ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. __lowerCamelCase : Optional[Any] = importlib.import_module('transformers' ) if hasattr(__UpperCamelCase ,__UpperCamelCase ): return getattr(__UpperCamelCase ,__UpperCamelCase ) return None def a_ ( _lowerCAmelCase ,_lowerCAmelCase = None ,_lowerCAmelCase = False ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,_lowerCAmelCase = None ,_lowerCAmelCase = None ,_lowerCAmelCase = False ,**_lowerCAmelCase ,) -> Any: __lowerCamelCase : Optional[int] = get_file_from_repo( __UpperCamelCase ,__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,resume_download=__UpperCamelCase ,proxies=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,revision=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) if resolved_config_file is None: logger.info( 'Could not locate the image processor configuration file, will try to use the model config instead.' ) return {} with open(__UpperCamelCase ,encoding='utf-8' ) as reader: return json.load(__UpperCamelCase ) class lowerCamelCase_ : """simple docstring""" def __init__( self : Any ) -> List[str]: raise EnvironmentError( 'AutoImageProcessor is designed to be instantiated ' 'using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.' ) @classmethod @replace_list_option_in_docstrings(_SCREAMING_SNAKE_CASE ) def _lowercase ( cls : Union[str, Any] , _a : int , **_a : Tuple ) -> int: __lowerCamelCase : Union[str, Any] = kwargs.pop('config' , _SCREAMING_SNAKE_CASE ) __lowerCamelCase : Any = kwargs.pop('trust_remote_code' , _SCREAMING_SNAKE_CASE ) __lowerCamelCase : Tuple = True __lowerCamelCase ,__lowerCamelCase : Tuple = ImageProcessingMixin.get_image_processor_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) __lowerCamelCase : List[str] = config_dict.get('image_processor_type' , _SCREAMING_SNAKE_CASE ) __lowerCamelCase : Dict = None if "AutoImageProcessor" in config_dict.get('auto_map' , {} ): __lowerCamelCase : List[str] = config_dict['auto_map']['AutoImageProcessor'] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: __lowerCamelCase : Optional[int] = config_dict.pop('feature_extractor_type' , _SCREAMING_SNAKE_CASE ) if feature_extractor_class is not None: logger.warning( 'Could not find image processor class in the image processor config or the model config. Loading' ' based on pattern matching with the model\'s feature extractor configuration.' ) __lowerCamelCase : List[str] = feature_extractor_class.replace('FeatureExtractor' , 'ImageProcessor' ) if "AutoFeatureExtractor" in config_dict.get('auto_map' , {} ): __lowerCamelCase : str = config_dict['auto_map']['AutoFeatureExtractor'] __lowerCamelCase : List[Any] = feature_extractor_auto_map.replace('FeatureExtractor' , 'ImageProcessor' ) logger.warning( 'Could not find image processor auto map in the image processor config or the model config.' ' Loading based on pattern matching with the model\'s feature extractor configuration.' ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCamelCase : str = AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) # It could be in `config.image_processor_type`` __lowerCamelCase : Dict = getattr(_SCREAMING_SNAKE_CASE , 'image_processor_type' , _SCREAMING_SNAKE_CASE ) if hasattr(_SCREAMING_SNAKE_CASE , 'auto_map' ) and "AutoImageProcessor" in config.auto_map: __lowerCamelCase : List[Any] = config.auto_map['AutoImageProcessor'] if image_processor_class is not None: __lowerCamelCase : Any = image_processor_class_from_name(_SCREAMING_SNAKE_CASE ) __lowerCamelCase : Dict = image_processor_auto_map is not None __lowerCamelCase : Any = image_processor_class is not None or type(_SCREAMING_SNAKE_CASE ) in IMAGE_PROCESSOR_MAPPING __lowerCamelCase : List[str] = resolve_trust_remote_code( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if has_remote_code and trust_remote_code: __lowerCamelCase : List[Any] = get_class_from_dynamic_module( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) __lowerCamelCase : Optional[Any] = kwargs.pop('code_revision' , _SCREAMING_SNAKE_CASE ) if os.path.isdir(_SCREAMING_SNAKE_CASE ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) elif image_processor_class is not None: return image_processor_class.from_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(_SCREAMING_SNAKE_CASE ) in IMAGE_PROCESSOR_MAPPING: __lowerCamelCase : Optional[Any] = IMAGE_PROCESSOR_MAPPING[type(_SCREAMING_SNAKE_CASE )] return image_processor_class.from_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) raise ValueError( f'Unrecognized image processor in {pretrained_model_name_or_path}. Should have a ' f'`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following ' f'`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}' ) @staticmethod def _lowercase ( _a : Any , _a : Any ) -> Any: IMAGE_PROCESSOR_MAPPING.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
208
'''simple docstring''' import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE__ = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE__ = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' SCREAMING_SNAKE_CASE__ = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://www.atticusprojectai.org/cuad"""] , reference_urls=["""https://www.atticusprojectai.org/cuad"""] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} UpperCamelCase = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] UpperCamelCase = evaluate(dataset=_SCREAMING_SNAKE_CASE , predictions=_SCREAMING_SNAKE_CASE ) return score
321
0
'''simple docstring''' import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging a__ : Dict = logging.get_logger(__name__) a__ : Any = "▁" a__ : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} a__ : Optional[Any] = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } a__ : str = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } a__ : Union[str, Any] = { "ernie-m-base": 5_1_4, "ernie-m-large": 5_1_4, } a__ : Union[str, Any] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): UpperCAmelCase__ : Union[str, Any] = ['input_ids'] UpperCAmelCase__ : int = VOCAB_FILES_NAMES UpperCAmelCase__ : Tuple = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : Dict = RESOURCE_FILES_NAMES def __init__( self :Union[str, Any] , _A :Tuple , _A :Union[str, Any]=None , _A :Any=False , _A :int="utf8" , _A :List[str]="[UNK]" , _A :int="[SEP]" , _A :Optional[int]="[PAD]" , _A :Tuple="[CLS]" , _A :List[Any]="[MASK]" , _A :Union[str, Any] = None , **_A :Union[str, Any] , ) -> None: '''simple docstring''' __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , vocab_file=_SCREAMING_SNAKE_CASE , encoding=_SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **_SCREAMING_SNAKE_CASE , ) __A = do_lower_case __A = sentencepiece_model_ckpt __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: __A = self.load_vocab(filepath=_SCREAMING_SNAKE_CASE ) else: __A = {self.sp_model.id_to_piece(_SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} __A = {v: k for k, v in self.vocab.items()} def lowercase_ ( self :int , _A :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' if text is None: return None __A = self.tokenize(_SCREAMING_SNAKE_CASE ) __A , __A = '', [] for i, ch in enumerate(_SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: __A = self.SP_CHAR_MAPPING.get(_SCREAMING_SNAKE_CASE ) else: __A = unicodedata.normalize('NFKC' , _SCREAMING_SNAKE_CASE ) if self.is_whitespace(_SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(_SCREAMING_SNAKE_CASE ) ) __A , __A , __A = normalized_text, [], 0 if self.do_lower_case: __A = text.lower() for token in split_tokens: if token[:1] == "▁": __A = token[1:] __A = text[offset:].index(_SCREAMING_SNAKE_CASE ) + offset __A = start + len(_SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) __A = end return token_mapping @property def lowercase_ ( self :List[str] ) -> Optional[int]: '''simple docstring''' return len(self.vocab ) def lowercase_ ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' __A = self.__dict__.copy() __A = None return state def __setstate__( self :List[Any] , _A :int ) -> str: '''simple docstring''' __A = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def lowercase_ ( self :Optional[Any] , _A :Tuple ) -> Union[str, Any]: '''simple docstring''' return "".join((self.SP_CHAR_MAPPING.get(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for c in text) ) def lowercase_ ( self :int , _A :int , _A :Any=False , _A :str=64 , _A :str=0.1 ) -> List[Any]: '''simple docstring''' if self.sp_model_kwargs.get('enable_sampling' ) is True: __A = True if self.sp_model_kwargs.get('alpha' ) is not None: __A = self.sp_model_kwargs.get('alpha' ) if self.sp_model_kwargs.get('nbest_size' ) is not None: __A = self.sp_model_kwargs.get('nbest_size' ) if not enable_sampling: __A = self.sp_model.EncodeAsPieces(_SCREAMING_SNAKE_CASE ) else: __A = self.sp_model.SampleEncodeAsPieces(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __A = [] for pi, piece in enumerate(_SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(_SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(_SCREAMING_SNAKE_CASE ) continue else: continue __A = 0 for i, chunk in enumerate(_SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(_SCREAMING_SNAKE_CASE ) or self.is_punct(_SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(_SCREAMING_SNAKE_CASE ) __A = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) __A = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) __A = i if len(_SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def lowercase_ ( self :Union[str, Any] , _A :Optional[Any] ) -> int: '''simple docstring''' __A = ''.join(_SCREAMING_SNAKE_CASE ).replace(_SCREAMING_SNAKE_CASE , ' ' ).strip() return out_string def lowercase_ ( self :Union[str, Any] , _A :Union[str, Any] ) -> Any: '''simple docstring''' __A = self.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE ) __A = ''.join(_SCREAMING_SNAKE_CASE ).replace(_SCREAMING_SNAKE_CASE , ' ' ).strip() return out_string def lowercase_ ( self :Optional[Any] , _A :List[Any] ) -> str: '''simple docstring''' return self.vocab.get(_SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def lowercase_ ( self :Optional[Any] , _A :List[str] ) -> Union[str, Any]: '''simple docstring''' return self.reverse_vocab.get(_SCREAMING_SNAKE_CASE , self.unk_token ) def lowercase_ ( self :List[Any] , _A :Optional[Any] , _A :Dict=None ) -> List[str]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def lowercase_ ( self :List[Any] , _A :Tuple , _A :Tuple=None ) -> List[Any]: '''simple docstring''' if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def lowercase_ ( self :Any , _A :Tuple , _A :Optional[int]=None , _A :Union[str, Any]=False ) -> List[Any]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def lowercase_ ( self :List[str] , _A :Optional[Any] , _A :Optional[int] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: # [CLS] X [SEP] return (len(_SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(_SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(_SCREAMING_SNAKE_CASE ) + 3) def lowercase_ ( self :Union[str, Any] , _A :List[str] ) -> Dict: '''simple docstring''' if "\u4e00" <= char <= "\u9fff": return True return False def lowercase_ ( self :Optional[int] , _A :Optional[int] ) -> Any: '''simple docstring''' if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def lowercase_ ( self :Dict , _A :Union[str, Any] ) -> Any: '''simple docstring''' if char in ",;:.?!~,;:。?!《》【】": return True return False def lowercase_ ( self :int , _A :str ) -> Optional[int]: '''simple docstring''' if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(_SCREAMING_SNAKE_CASE ) == 1: __A = unicodedata.category(_SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def lowercase_ ( self :str , _A :str ) -> List[str]: '''simple docstring''' __A = {} with io.open(_SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as f: for index, line in enumerate(_SCREAMING_SNAKE_CASE ): __A = line.rstrip('\n' ) __A = int(_SCREAMING_SNAKE_CASE ) return token_to_idx def lowercase_ ( self :Optional[Any] , _A :List[str] , _A :Optional[int] = None ) -> Tuple[str]: '''simple docstring''' __A = 0 if os.path.isdir(_SCREAMING_SNAKE_CASE ): __A = os.path.join( _SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) else: __A = (filename_prefix + '-' if filename_prefix else '') + save_directory with open(_SCREAMING_SNAKE_CASE , 'w' , encoding='utf-8' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda _A : kv[1] ): if index != token_index: logger.warning( F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.' ' Please check that the vocabulary is not corrupted!' ) __A = token_index writer.write(token + '\n' ) index += 1 __A = os.path.join(_SCREAMING_SNAKE_CASE , 'sentencepiece.bpe.model' ) with open(_SCREAMING_SNAKE_CASE , 'wb' ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(_SCREAMING_SNAKE_CASE ) return (vocab_file,)
161
'''simple docstring''' def lowercase__ ( __UpperCamelCase )-> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase__ ( __UpperCamelCase = 1000000 )-> int: UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__UpperCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'{solution() = }')
321
0