code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: stooge(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return arr def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase__ , lowercase__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) # Recursively sort last 2/3 elements stooge(_SCREAMING_SNAKE_CASE , i + t , (_SCREAMING_SNAKE_CASE) ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} lowercase_ = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json""" ), }, } lowercase_ = { """moussaKam/mbarthez""": 1_024, """moussaKam/barthez""": 1_024, """moussaKam/barthez-orangesum-title""": 1_024, } lowercase_ = """▁""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = VOCAB_FILES_NAMES _UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Optional[Any] = ['input_ids', 'attention_mask'] _UpperCamelCase : int = BarthezTokenizer def __init__( self : List[Any] , a : Union[str, Any]=None , a : Optional[Any]=None , a : Dict="<s>" , a : Union[str, Any]="</s>" , a : List[str]="</s>" , a : Optional[Any]="<s>" , a : int="<unk>" , a : str="<pad>" , a : Optional[int]="<mask>" , **a : Union[str, Any] , )-> Tuple: """simple docstring""" lowercase__ = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) lowercase__ = vocab_file lowercase__ = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__ = [self.cls_token_id] lowercase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" lowercase__ = [self.sep_token_id] lowercase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE_ ( self : Dict , a : str , a : Optional[str] = None )-> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ = os.path.join( a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
45
1
import pprint import requests lowercase_ = """https://zenquotes.io/api""" def __UpperCamelCase () -> list: return requests.get(API_ENDPOINT_URL + '/today' ).json() def __UpperCamelCase () -> list: return requests.get(API_ENDPOINT_URL + '/random' ).json() if __name__ == "__main__": lowercase_ = random_quotes() pprint.pprint(response)
45
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
1
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowercase_ = logging.getLogger(__name__) def __UpperCamelCase () -> str: lowercase__ = argparse.ArgumentParser( description='Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).' ) parser.add_argument('--file_path' , type=_SCREAMING_SNAKE_CASE , default='data/dump.txt' , help='The path to the data.' ) parser.add_argument('--tokenizer_type' , type=_SCREAMING_SNAKE_CASE , default='bert' , choices=['bert', 'roberta', 'gpt2'] ) parser.add_argument('--tokenizer_name' , type=_SCREAMING_SNAKE_CASE , default='bert-base-uncased' , help='The tokenizer to use.' ) parser.add_argument('--dump_file' , type=_SCREAMING_SNAKE_CASE , default='data/dump' , help='The dump file prefix.' ) lowercase__ = parser.parse_args() logger.info(F"""Loading Tokenizer ({args.tokenizer_name})""" ) if args.tokenizer_type == "bert": lowercase__ = BertTokenizer.from_pretrained(args.tokenizer_name ) lowercase__ = tokenizer.special_tokens_map['cls_token'] # `[CLS]` lowercase__ = tokenizer.special_tokens_map['sep_token'] # `[SEP]` elif args.tokenizer_type == "roberta": lowercase__ = RobertaTokenizer.from_pretrained(args.tokenizer_name ) lowercase__ = tokenizer.special_tokens_map['cls_token'] # `<s>` lowercase__ = tokenizer.special_tokens_map['sep_token'] # `</s>` elif args.tokenizer_type == "gpt2": lowercase__ = GPTaTokenizer.from_pretrained(args.tokenizer_name ) lowercase__ = tokenizer.special_tokens_map['bos_token'] # `<|endoftext|>` lowercase__ = tokenizer.special_tokens_map['eos_token'] # `<|endoftext|>` logger.info(F"""Loading text from {args.file_path}""" ) with open(args.file_path , 'r' , encoding='utf8' ) as fp: lowercase__ = fp.readlines() logger.info('Start encoding' ) logger.info(F"""{len(_SCREAMING_SNAKE_CASE )} examples to process.""" ) lowercase__ = [] lowercase__ = 0 lowercase__ = 10000 lowercase__ = time.time() for text in data: lowercase__ = F"""{bos} {text.strip()} {sep}""" lowercase__ = tokenizer.encode(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE ) rslt.append(_SCREAMING_SNAKE_CASE ) iter += 1 if iter % interval == 0: lowercase__ = time.time() logger.info(F"""{iter} examples processed. - {(end-start):.2f}s/{interval}expl""" ) lowercase__ = time.time() logger.info('Finished binarization' ) logger.info(F"""{len(_SCREAMING_SNAKE_CASE )} examples processed.""" ) lowercase__ = F"""{args.dump_file}.{args.tokenizer_name}.pickle""" lowercase__ = tokenizer.vocab_size if vocab_size < (1 << 16): lowercase__ = [np.uintaa(_SCREAMING_SNAKE_CASE ) for d in rslt] else: lowercase__ = [np.intaa(_SCREAMING_SNAKE_CASE ) for d in rslt] random.shuffle(rslt_ ) logger.info(F"""Dump to {dp_file}""" ) with open(_SCREAMING_SNAKE_CASE , 'wb' ) as handle: pickle.dump(rslt_ , _SCREAMING_SNAKE_CASE , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
45
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 lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Any = 'deit' def __init__( self : Any , a : Union[str, Any]=768 , a : Optional[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Optional[int]="gelu" , a : Optional[Any]=0.0 , a : List[Any]=0.0 , a : int=0.02 , a : List[str]=1E-1_2 , a : Optional[int]=224 , a : Tuple=16 , a : List[Any]=3 , a : List[str]=True , a : Any=16 , **a : Union[str, Any] , )-> int: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = qkv_bias lowercase__ = encoder_stride class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : int )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-4
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: return round(float(moles / volume ) * nfactor ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
45
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { """microsoft/resnet-50""": """https://huggingface.co/microsoft/resnet-50/blob/main/config.json""", } class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase ): _UpperCamelCase : List[Any] = 'resnet' _UpperCamelCase : List[str] = ['basic', 'bottleneck'] def __init__( self : List[Any] , a : Union[str, Any]=3 , a : str=64 , a : Optional[int]=[256, 512, 1_024, 2_048] , a : str=[3, 4, 6, 3] , a : Any="bottleneck" , a : List[str]="relu" , a : str=False , a : Optional[Any]=None , a : Union[str, Any]=None , **a : str , )-> str: """simple docstring""" super().__init__(**a ) if layer_type not in self.layer_types: raise ValueError(f"""layer_type={layer_type} is not one of {",".join(self.layer_types )}""" ) lowercase__ = num_channels lowercase__ = embedding_size lowercase__ = hidden_sizes lowercase__ = depths lowercase__ = layer_type lowercase__ = hidden_act lowercase__ = downsample_in_first_stage lowercase__ = ['stem'] + [f"""stage{idx}""" for idx in range(1 , len(a ) + 1 )] lowercase__ , lowercase__ = get_aligned_output_features_output_indices( out_features=a , out_indices=a , stage_names=self.stage_names ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Optional[int] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> float: """simple docstring""" return 1E-3
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { """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: lowercase_ = [ """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 lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
from dataclasses import dataclass, field from typing import Optional @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be trained.'} ) _UpperCamelCase : Optional[str] = field( default='./' , metadata={'help': 'Save dir where model repo is cloned and models updates are saved to.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-train' , metadata={'help': 'Name or path of training dataset.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-valid' , metadata={'help': 'Name or path of validation dataset.'} ) _UpperCamelCase : Optional[int] = field(default=2 , metadata={'help': 'Batch size for training.'} ) _UpperCamelCase : Optional[int] = field(default=2 , metadata={'help': 'Batch size for evaluation.'} ) _UpperCamelCase : Optional[float] = field(default=0.1 , metadata={'help': 'Value of weight decay.'} ) _UpperCamelCase : Optional[int] = field( default=1_00_00 , metadata={'help': 'Size of buffer used to shuffle streaming dataset.'} ) _UpperCamelCase : Optional[float] = field(default=2E-4 , metadata={'help': 'Learning rate fo training.'} ) _UpperCamelCase : Optional[str] = field(default='cosine' , metadata={'help': 'Learning rate.'} ) _UpperCamelCase : Optional[int] = field( default=7_50 , metadata={'help': 'Number of warmup steps in the learning rate schedule.'} ) _UpperCamelCase : Optional[int] = field( default=16 , metadata={'help': 'Number of gradient accumulation steps.'} ) _UpperCamelCase : Optional[bool] = field( default=UpperCAmelCase , metadata={'help': 'Use gradient checkpointing to reduce memory footprint.'} ) _UpperCamelCase : Optional[int] = field(default=5_00_00 , metadata={'help': 'Maximum number of training steps.'} ) _UpperCamelCase : Optional[int] = field( default=-1 , metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'} ) _UpperCamelCase : Optional[int] = field(default=10_24 , metadata={'help': 'Sequence lengths used for training.'} ) _UpperCamelCase : Optional[int] = field(default=1 , metadata={'help': 'Training seed.'} ) _UpperCamelCase : Optional[int] = field( default=10_24 , metadata={'help': 'Interval to save checkpoints. Measured as number of forward passes not training steps.'} , ) _UpperCamelCase : Optional[str] = field( default=UpperCAmelCase , metadata={'help': 'States path if the training should continue from a checkpoint folder.'} ) _UpperCamelCase : Optional[bool] = field(default=UpperCAmelCase , metadata={'help': 'If True the data is pretokenized.'} ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be evaluated.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-valid' , metadata={'help': 'Name or path of validation dataset.'} ) _UpperCamelCase : Optional[int] = field(default=2 , metadata={'help': 'Batch size used for evaluation.'} ) _UpperCamelCase : Optional[int] = field( default=-1 , metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'} ) _UpperCamelCase : Optional[int] = field(default=10_24 , metadata={'help': 'Length of sequences to be evaluated.'} ) _UpperCamelCase : Optional[int] = field(default=1 , metadata={'help': 'Random seed used for evaluation.'} ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be evaluated.'} ) _UpperCamelCase : Optional[int] = field(default=UpperCAmelCase , metadata={'help': 'Number of workers used for code evaluation.'} ) _UpperCamelCase : Optional[int] = field( default=UpperCAmelCase , metadata={'help': 'The number of human-eval tasks to run. If not included all tasks are evaluated.'} , ) _UpperCamelCase : Optional[bool] = field( default=UpperCAmelCase , metadata={'help': 'Sample from the language model\'s output distribution.'} ) _UpperCamelCase : Optional[float] = field(default=0.2 , metadata={'help': 'Sampling temperature used for generation.'} ) _UpperCamelCase : Optional[int] = field(default=2_56 , metadata={'help': 'Maximum number of newly generated tokens.'} ) _UpperCamelCase : Optional[int] = field(default=0 , metadata={'help': 'Top-k parameter used for generation.'} ) _UpperCamelCase : Optional[float] = field(default=0.95 , metadata={'help': 'Top-p parameter used for nucleus sampling.'} ) _UpperCamelCase : Optional[int] = field(default=10 , metadata={'help': 'Number of generations to run in parallel.'} ) _UpperCamelCase : Optional[int] = field( default=2_00 , metadata={'help': 'Number of completions to generate for each sample.'} ) _UpperCamelCase : Optional[int] = field(default=1 , metadata={'help': 'Random seed used for evaluation.'} ) _UpperCamelCase : Optional[str] = field( default='eval_results.json' , metadata={'help': 'Random seed used for evaluation.'} ) _UpperCamelCase : Optional[str] = field( default='0' , metadata={'help': 'Allow `code_eval` to execute Python code on machine'} ) _UpperCamelCase : Optional[int] = field( default=-1 , metadata={ 'help': ( 'Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive' ' number corresponds to which GPU device id to run on.' ) } , ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[int] = field( default=UpperCAmelCase , metadata={ 'help': 'The number of CPU cores to use for parallel preprocessing. Default uses the maximum available.' } , ) _UpperCamelCase : Optional[str] = field( default='transformersbook/codeparrot' , metadata={'help': 'Folder or name of dataset to process.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot-clean' , metadata={'help': 'Folder to save processed processed dataset.'} ) _UpperCamelCase : Optional[int] = field( default=10_00_00 , metadata={'help': 'Number of files to save per JSON output file.'} ) _UpperCamelCase : Optional[str] = field(default='content' , metadata={'help': 'Column containing text data to process.'} ) _UpperCamelCase : Optional[float] = field( default=10_00 , metadata={'help': 'Maximum line length in file, otherwise file is filtered.'} ) _UpperCamelCase : Optional[float] = field( default=1_00 , metadata={'help': 'Maximum mean line length in file, otherwise file is filtered.'} ) _UpperCamelCase : Optional[float] = field( default=0.25 , metadata={'help': 'Maximum fraction of non-alphanumeric characters, otherwise file is filtered.'} ) _UpperCamelCase : Optional[float] = field( default=1.5 , metadata={'help': 'Minimum character token ratio for the file, otherwise file is filtered.'} ) _UpperCamelCase : Optional[float] = field( default=0.7 , metadata={'help': 'Probability for filtering config, test and uncommon files.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Name or path to the tokenizer.'} , ) _UpperCamelCase : Optional[bool] = field( default=UpperCAmelCase , metadata={'help': 'If True, near-duplicate samples are removed.'} ) _UpperCamelCase : Optional[float] = field( default=0.85 , metadata={'help': 'Jaccard threshold for near-duplicate samples.'} ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='gpt2' , metadata={'help': 'Base tokenizer to build new tokenizer from.'} ) _UpperCamelCase : Optional[str] = field( default='transformersbook/codeparrot-train' , metadata={'help': 'Dataset to train tokenizer on.'} ) _UpperCamelCase : Optional[str] = field(default='content' , metadata={'help': 'Column containing text data to process.'} ) _UpperCamelCase : Optional[int] = field(default=20_00_00 , metadata={'help': 'Number of examples to train tokenizer on.'} ) _UpperCamelCase : Optional[int] = field( default=3_27_68 , metadata={'help': 'Number of examples to train the tokenizer on.'} ) _UpperCamelCase : Optional[str] = field(default='codeparrot' , metadata={'help': 'Name of new tokenizer.'} ) _UpperCamelCase : Optional[bool] = field(default=UpperCAmelCase , metadata={'help': 'Push saved tokenizer to the hub.'} ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Name or path to the tokenizer.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot-clean-train' , metadata={'help': 'Name or path to the dataset to pretokenize.'} ) _UpperCamelCase : Optional[str] = field( default='tokenized-codeparrot-train' , metadata={'help': 'Repo name of the pretokenized data.'} ) _UpperCamelCase : Optional[int] = field(default=UpperCAmelCase , metadata={'help': 'Number of workers used for code evaluation.'} ) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : Optional[str] = field( default='gpt2-large' , metadata={'help': 'Configuration to use for model initialization.'} ) _UpperCamelCase : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Tokenizer attached to model.'} ) _UpperCamelCase : Optional[str] = field(default='codeparrot' , metadata={'help': 'Name of the created model.'} ) _UpperCamelCase : Optional[bool] = field(default=UpperCAmelCase , metadata={'help': 'Push saved tokenizer to the hub.'} )
45
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
1
from collections.abc import Sequence def __UpperCamelCase (_SCREAMING_SNAKE_CASE = None ) -> int: if nums is None or not nums: raise ValueError('Input sequence should not be empty' ) lowercase__ = nums[0] for i in range(1 , len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = nums[i] lowercase__ = max(_SCREAMING_SNAKE_CASE , ans + num , _SCREAMING_SNAKE_CASE ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user lowercase_ = int(input("""Enter number of elements : """).strip()) lowercase_ = list(map(int, input("""\nEnter the numbers : """).strip().split()))[:n] print(max_subsequence_sum(array))
45
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
1
import collections import inspect import unittest from transformers import FocalNetConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, ) from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : List[str]=32 , a : int=2 , a : Optional[Any]=3 , a : List[str]=16 , a : Union[str, Any]=[32, 64, 128] , a : Tuple=[1, 2, 1] , a : Optional[int]=[2, 2, 4] , a : Optional[Any]=2 , a : str=2.0 , a : Any=True , a : List[Any]=0.0 , a : Tuple=0.0 , a : str=0.1 , a : Dict="gelu" , a : Tuple=False , a : Optional[int]=True , a : str=0.02 , a : int=1E-5 , a : int=True , a : Union[str, Any]=None , a : List[Any]=True , a : Union[str, Any]=10 , a : Any=8 , a : str=["stage1", "stage2"] , a : Dict=[1, 2] , )-> str: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = embed_dim lowercase__ = hidden_sizes lowercase__ = depths lowercase__ = num_heads lowercase__ = window_size lowercase__ = mlp_ratio lowercase__ = qkv_bias lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = drop_path_rate lowercase__ = hidden_act lowercase__ = use_absolute_embeddings lowercase__ = patch_norm lowercase__ = layer_norm_eps lowercase__ = initializer_range lowercase__ = is_training lowercase__ = scope lowercase__ = use_labels lowercase__ = type_sequence_label_size lowercase__ = encoder_stride lowercase__ = out_features lowercase__ = out_indices def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[str]: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> str: """simple docstring""" return FocalNetConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , hidden_sizes=self.hidden_sizes , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def SCREAMING_SNAKE_CASE_ ( self : Any , a : Optional[int] , a : int , a : int )-> Optional[int]: """simple docstring""" lowercase__ = FocalNetModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) lowercase__ = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) lowercase__ = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : int , a : List[Any] , a : Dict )-> Tuple: """simple docstring""" lowercase__ = FocalNetBackbone(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.image_size, 8, 8] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[:-1] ) # verify backbone works with out_features=None lowercase__ = None lowercase__ = FocalNetBackbone(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.image_size * 2, 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[Any] , a : Optional[Any] , a : int )-> Optional[Any]: """simple docstring""" lowercase__ = FocalNetForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = FocalNetForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : Union[str, Any] , a : Union[str, Any] , a : Dict )-> Union[str, Any]: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = FocalNetForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = FocalNetForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Tuple = ( ( FocalNetModel, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetBackbone, ) if is_torch_available() else () ) _UpperCamelCase : List[str] = ( {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Any = False _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Optional[int] = False _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" lowercase__ = FocalNetModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , embed_dim=37 , has_text_modality=a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" 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 SCREAMING_SNAKE_CASE_ ( self : Dict )-> Tuple: """simple docstring""" return def SCREAMING_SNAKE_CASE_ ( self : Any )-> List[str]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Optional[Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @unittest.skip(reason='FocalNet does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Any: """simple docstring""" pass @unittest.skip(reason='FocalNet does not use feedforward chunking' ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Tuple: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Tuple: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : Optional[Any] , a : Any , a : int , a : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): lowercase__ = model(**self._prepare_for_class(a , a ) ) lowercase__ = outputs.hidden_states lowercase__ = getattr( self.model_tester , 'expected_num_hidden_layers' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(a ) , a ) # FocalNet has a different seq_length lowercase__ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) lowercase__ = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) lowercase__ = outputs.reshaped_hidden_states self.assertEqual(len(a ) , a ) lowercase__ , lowercase__ , lowercase__ , lowercase__ = reshaped_hidden_states[0].shape lowercase__ = ( reshaped_hidden_states[0].view(a , a , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes[:-1]: lowercase__ = True self.check_hidden_states_output(a , a , a , a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowercase__ = True self.check_hidden_states_output(a , a , a , a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> str: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = 3 lowercase__ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) lowercase__ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) lowercase__ = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) lowercase__ = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes[:-1]: lowercase__ = True self.check_hidden_states_output(a , a , a , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowercase__ = True self.check_hidden_states_output(a , a , a , (padded_height, padded_width) ) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[str]: """simple docstring""" for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = FocalNetModel.from_pretrained(a ) self.assertIsNotNone(a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = _config_zero_init(a ) for model_class in self.all_model_classes: lowercase__ = model_class(config=a ) for name, param in model.named_parameters(): if "embeddings" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @require_vision @require_torch class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> str: """simple docstring""" return AutoImageProcessor.from_pretrained('microsoft/focalnet-tiny' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = FocalNetForImageClassification.from_pretrained('microsoft/focalnet-tiny' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([0.2166, -0.4368, 0.2191] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) self.assertTrue(outputs.logits.argmax(dim=-1 ).item() , 281 ) @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = (FocalNetBackbone,) if is_torch_available() else () _UpperCamelCase : Union[str, Any] = FocalNetConfig _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> int: """simple docstring""" lowercase__ = FocalNetModelTester(self )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/convnextv2-tiny-1k-224""": """https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json""", } class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase ): _UpperCamelCase : Dict = 'convnextv2' def __init__( self : str , a : Tuple=3 , a : Optional[Any]=4 , a : Optional[int]=4 , a : str=None , a : Any=None , a : Union[str, Any]="gelu" , a : int=0.02 , a : Any=1E-1_2 , a : List[Any]=0.0 , a : List[Any]=224 , a : Dict=None , a : Tuple=None , **a : List[str] , )-> List[Any]: """simple docstring""" super().__init__(**a ) lowercase__ = num_channels lowercase__ = patch_size lowercase__ = num_stages lowercase__ = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes lowercase__ = [3, 3, 9, 3] if depths is None else depths lowercase__ = hidden_act lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = drop_path_rate lowercase__ = image_size lowercase__ = ['stem'] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] lowercase__ , lowercase__ = get_aligned_output_features_output_indices( out_features=a , out_indices=a , stage_names=self.stage_names )
45
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[Any] , a : UNetaDModel , a : UNetaDModel , a : DDPMScheduler , a : Any , )-> Dict: """simple docstring""" super().__init__() lowercase__ = value_function lowercase__ = unet lowercase__ = scheduler lowercase__ = env lowercase__ = env.get_dataset() lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].mean() except: # noqa: E722 pass lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].std() except: # noqa: E722 pass lowercase__ = env.observation_space.shape[0] lowercase__ = env.action_space.shape[0] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Any , a : int )-> Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : str , a : List[str] )-> str: """simple docstring""" return x_in * self.stds[key] + self.means[key] def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : Tuple )-> Tuple: """simple docstring""" if type(a ) is dict: return {k: self.to_torch(a ) for k, v in x_in.items()} elif torch.is_tensor(a ): return x_in.to(self.unet.device ) return torch.tensor(a , device=self.unet.device ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[int] , a : Dict , a : Optional[Any] )-> List[Any]: """simple docstring""" for key, val in cond.items(): lowercase__ = val.clone() return x_in def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[Any] , a : Any , a : Optional[Any] , a : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = x.shape[0] lowercase__ = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model lowercase__ = torch.full((batch_size,) , a , device=self.unet.device , dtype=torch.long ) for _ in range(a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models lowercase__ = self.value_function(x.permute(0 , 2 , 1 ) , a ).sample lowercase__ = torch.autograd.grad([y.sum()] , [x] )[0] lowercase__ = self.scheduler._get_variance(a ) lowercase__ = torch.exp(0.5 * posterior_variance ) lowercase__ = model_std * grad lowercase__ = 0 lowercase__ = x.detach() lowercase__ = x + scale * grad lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.unet(x.permute(0 , 2 , 1 ) , a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg lowercase__ = self.scheduler.step(a , a , a , predict_epsilon=a )['prev_sample'] # apply conditions to the trajectory (set the initial state) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) return x, y def __call__( self : Any , a : Tuple , a : int=64 , a : Tuple=32 , a : List[Any]=2 , a : List[str]=0.1 )-> List[Any]: """simple docstring""" lowercase__ = self.normalize(a , 'observations' ) lowercase__ = obs[None].repeat(a , axis=0 ) lowercase__ = {0: self.to_torch(a )} lowercase__ = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) lowercase__ = randn_tensor(a , device=self.unet.device ) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) # run the diffusion process lowercase__ , lowercase__ = self.run_diffusion(a , a , a , a ) # sort output trajectories by value lowercase__ = y.argsort(0 , descending=a ).squeeze() lowercase__ = x[sorted_idx] lowercase__ = sorted_values[:, :, : self.action_dim] lowercase__ = actions.detach().cpu().numpy() lowercase__ = self.de_normalize(a , key='actions' ) # select the action with the highest value if y is not None: lowercase__ = 0 else: # if we didn't run value guiding, select a random action lowercase__ = np.random.randint(0 , a ) lowercase__ = denorm_actions[selected_index, 0] return denorm_actions
45
1
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'ClapFeatureExtractor' _UpperCamelCase : Union[str, Any] = ('RobertaTokenizer', 'RobertaTokenizerFast') def __init__( self : List[Any] , a : int , a : str )-> Any: """simple docstring""" super().__init__(a , a ) def __call__( self : Any , a : Tuple=None , a : Optional[int]=None , a : int=None , **a : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = kwargs.pop('sampling_rate' , a ) if text is None and audios is None: raise ValueError('You have to specify either text or audios. Both cannot be none.' ) if text is not None: lowercase__ = self.tokenizer(a , return_tensors=a , **a ) if audios is not None: lowercase__ = self.feature_extractor( a , sampling_rate=a , return_tensors=a , **a ) if text is not None and audios is not None: lowercase__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : str , *a : Dict , **a : int )-> Optional[int]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *a : int , **a : Dict )-> Dict: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer.model_input_names lowercase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
45
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
1
from __future__ import annotations from fractions import Fraction def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[str]: lowercase__ = [] lowercase__ = 11 lowercase__ = int('1' + '0' * digit_len ) for num in range(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): solutions.append(F"""{num}/{den}""" ) den += 1 num += 1 lowercase__ = 10 return solutions def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 2 ) -> int: lowercase__ = 1.0 for fraction in fraction_list(_SCREAMING_SNAKE_CASE ): lowercase__ = Fraction(_SCREAMING_SNAKE_CASE ) result *= frac.denominator / frac.numerator return int(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(solution())
45
import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : Any , a : str , a : List[Any]=7 , a : int=3 , a : int=18 , a : Optional[Any]=30 , a : Optional[int]=400 , a : int=True , a : Tuple=None , a : Optional[Any]=True , a : str=False , a : str=True , a : int=True , a : Tuple=[0.5, 0.5, 0.5] , a : Any=[0.5, 0.5, 0.5] , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size if size is not None else {'height': 18, 'width': 20} lowercase__ = do_thumbnail lowercase__ = do_align_axis lowercase__ = do_pad lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = DonutImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) self.assertTrue(hasattr(a , 'do_thumbnail' ) ) self.assertTrue(hasattr(a , 'do_align_long_axis' ) ) self.assertTrue(hasattr(a , 'do_pad' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" pass @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , numpify=a ) for image in image_inputs: self.assertIsInstance(a , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , torchify=a ) for image in image_inputs: self.assertIsInstance(a , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: lowercase__ = len(_SCREAMING_SNAKE_CASE ) for i in range(length - 1 ): lowercase__ = i for k in range(i + 1 , _SCREAMING_SNAKE_CASE ): if collection[k] < collection[least]: lowercase__ = k if least != i: lowercase__ , lowercase__ = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(selection_sort(unsorted))
45
import math def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(_SCREAMING_SNAKE_CASE ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowercase_ = """Enter the base and the power separated by a comma: """ lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. lowercase_ = res(xa, ya) lowercase_ = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
45
1
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: lowercase__ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(_SCREAMING_SNAKE_CASE )] ) lowercase__ = np.array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , _SCREAMING_SNAKE_CASE ) ) , x.transpose() ) , _SCREAMING_SNAKE_CASE ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: lowercase__ = (1, 2, 1) lowercase__ = (1, 1, 0, 7) lowercase__ = SARIMAX( _SCREAMING_SNAKE_CASE , exog=_SCREAMING_SNAKE_CASE , order=_SCREAMING_SNAKE_CASE , seasonal_order=_SCREAMING_SNAKE_CASE ) lowercase__ = model.fit(disp=_SCREAMING_SNAKE_CASE , maxiter=600 , method='nm' ) lowercase__ = model_fit.predict(1 , len(_SCREAMING_SNAKE_CASE ) , exog=[test_match] ) return result[0] def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: lowercase__ = SVR(kernel='rbf' , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = regressor.predict(_SCREAMING_SNAKE_CASE ) return y_pred[0] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: train_user.sort() lowercase__ = np.percentile(_SCREAMING_SNAKE_CASE , 25 ) lowercase__ = np.percentile(_SCREAMING_SNAKE_CASE , 75 ) lowercase__ = qa - qa lowercase__ = qa - (iqr * 0.1) return low_lim def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: lowercase__ = 0 lowercase__ = 0 for i in list_vote: if i > actual_result: lowercase__ = not_safe + 1 else: if abs(abs(_SCREAMING_SNAKE_CASE ) - abs(_SCREAMING_SNAKE_CASE ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) lowercase_ = [[18_231, 0.0, 1], [22_621, 1.0, 2], [15_675, 0.0, 3], [23_583, 1.0, 4]] lowercase_ = pd.DataFrame( data_input, columns=["""total_user""", """total_even""", """days"""] ) lowercase_ = Normalizer().fit_transform(data_input_df.values) # split data lowercase_ = normalize_df[:, 2].tolist() lowercase_ = normalize_df[:, 0].tolist() lowercase_ = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) lowercase_ = normalize_df[:, [1, 2]].tolist() lowercase_ = x[: len(x) - 1] lowercase_ = x[len(x) - 1 :] # for linear regression & sarimax lowercase_ = total_date[: len(total_date) - 1] lowercase_ = total_user[: len(total_user) - 1] lowercase_ = total_match[: len(total_match) - 1] lowercase_ = total_date[len(total_date) - 1 :] lowercase_ = total_user[len(total_user) - 1 :] lowercase_ = total_match[len(total_match) - 1 :] # voting system with forecasting lowercase_ = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data lowercase_ = """""" if data_safety_checker(res_vote, tst_user) else """not """ print("""Today's data is {not_str}safe.""")
45
class SCREAMING_SNAKE_CASE : # Public class to implement a graph def __init__( self : int , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = row lowercase__ = col lowercase__ = graph def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int , a : list[list[bool]] )-> bool: """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order lowercase__ = [-1, 0, 1, -1, 1, -1, 0, 1] lowercase__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , a ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: # And finally, count all islands. """simple docstring""" lowercase__ = [[False for j in range(self.COL )] for i in range(self.ROW )] lowercase__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(a , a , a ) count += 1 return count
45
1
import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[str] = (PNDMScheduler,) _UpperCamelCase : Optional[int] = (('num_inference_steps', 50),) def SCREAMING_SNAKE_CASE_ ( self : Any , **a : Any )-> Optional[Any]: """simple docstring""" lowercase__ = { 'num_train_timesteps': 1_000, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', } config.update(**a ) return config def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any]=0 , **a : int )-> Union[str, Any]: """simple docstring""" lowercase__ = dict(self.forward_default_kwargs ) lowercase__ = kwargs.pop('num_inference_steps' , a ) lowercase__ = self.dummy_sample lowercase__ = 0.1 * sample lowercase__ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowercase__ = self.get_scheduler_config(**a ) lowercase__ = scheduler_class(**a ) scheduler.set_timesteps(a ) # copy over dummy past residuals lowercase__ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(a ) lowercase__ = scheduler_class.from_pretrained(a ) new_scheduler.set_timesteps(a ) # copy over dummy past residuals lowercase__ = dummy_past_residuals[:] lowercase__ = scheduler.step_prk(a , a , a , **a ).prev_sample lowercase__ = new_scheduler.step_prk(a , a , a , **a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" lowercase__ = scheduler.step_plms(a , a , a , **a ).prev_sample lowercase__ = new_scheduler.step_plms(a , a , a , **a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : str , a : List[Any]=0 , **a : List[Any] )-> str: """simple docstring""" lowercase__ = dict(self.forward_default_kwargs ) lowercase__ = kwargs.pop('num_inference_steps' , a ) lowercase__ = self.dummy_sample lowercase__ = 0.1 * sample lowercase__ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowercase__ = self.get_scheduler_config() lowercase__ = scheduler_class(**a ) scheduler.set_timesteps(a ) # copy over dummy past residuals (must be after setting timesteps) lowercase__ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(a ) lowercase__ = scheduler_class.from_pretrained(a ) # copy over dummy past residuals new_scheduler.set_timesteps(a ) # copy over dummy past residual (must be after setting timesteps) lowercase__ = dummy_past_residuals[:] lowercase__ = scheduler.step_prk(a , a , a , **a ).prev_sample lowercase__ = new_scheduler.step_prk(a , a , a , **a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" lowercase__ = scheduler.step_plms(a , a , a , **a ).prev_sample lowercase__ = new_scheduler.step_plms(a , a , a , **a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , **a : Dict )-> Tuple: """simple docstring""" lowercase__ = self.scheduler_classes[0] lowercase__ = self.get_scheduler_config(**a ) lowercase__ = scheduler_class(**a ) lowercase__ = 10 lowercase__ = self.dummy_model() lowercase__ = self.dummy_sample_deter scheduler.set_timesteps(a ) for i, t in enumerate(scheduler.prk_timesteps ): lowercase__ = model(a , a ) lowercase__ = scheduler.step_prk(a , a , a ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): lowercase__ = model(a , a ) lowercase__ = scheduler.step_plms(a , a , a ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Tuple: """simple docstring""" lowercase__ = dict(self.forward_default_kwargs ) lowercase__ = kwargs.pop('num_inference_steps' , a ) for scheduler_class in self.scheduler_classes: lowercase__ = self.get_scheduler_config() lowercase__ = scheduler_class(**a ) lowercase__ = self.dummy_sample lowercase__ = 0.1 * sample if num_inference_steps is not None and hasattr(a , 'set_timesteps' ): scheduler.set_timesteps(a ) elif num_inference_steps is not None and not hasattr(a , 'set_timesteps' ): lowercase__ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowercase__ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] lowercase__ = dummy_past_residuals[:] lowercase__ = scheduler.step_prk(a , 0 , a , **a ).prev_sample lowercase__ = scheduler.step_prk(a , 1 , a , **a ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) lowercase__ = scheduler.step_plms(a , 0 , a , **a ).prev_sample lowercase__ = scheduler.step_plms(a , 1 , a , **a ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[Any]: """simple docstring""" for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=a ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> int: """simple docstring""" for steps_offset in [0, 1]: self.check_over_configs(steps_offset=a ) lowercase__ = self.scheduler_classes[0] lowercase__ = self.get_scheduler_config(steps_offset=1 ) lowercase__ = scheduler_class(**a ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" for beta_start, beta_end in zip([0.0001, 0.001] , [0.002, 0.02] ): self.check_over_configs(beta_start=a , beta_end=a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Tuple: """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" for t in [1, 5, 10]: self.check_over_forward(time_step=a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Any: """simple docstring""" for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Tuple: """simple docstring""" lowercase__ = 27 for scheduler_class in self.scheduler_classes: lowercase__ = self.dummy_sample lowercase__ = 0.1 * sample lowercase__ = self.get_scheduler_config() lowercase__ = scheduler_class(**a ) scheduler.set_timesteps(a ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): lowercase__ = scheduler.step_prk(a , a , a ).prev_sample def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" with self.assertRaises(a ): lowercase__ = self.scheduler_classes[0] lowercase__ = self.get_scheduler_config() lowercase__ = scheduler_class(**a ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" lowercase__ = self.full_loop() lowercase__ = torch.sum(torch.abs(a ) ) lowercase__ = torch.mean(torch.abs(a ) ) assert abs(result_sum.item() - 198.1318 ) < 1E-2 assert abs(result_mean.item() - 0.2580 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.full_loop(prediction_type='v_prediction' ) lowercase__ = torch.sum(torch.abs(a ) ) lowercase__ = torch.mean(torch.abs(a ) ) assert abs(result_sum.item() - 67.3986 ) < 1E-2 assert abs(result_mean.item() - 0.0878 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Optional[Any]: """simple docstring""" lowercase__ = self.full_loop(set_alpha_to_one=a , beta_start=0.01 ) lowercase__ = torch.sum(torch.abs(a ) ) lowercase__ = torch.mean(torch.abs(a ) ) assert abs(result_sum.item() - 230.0399 ) < 1E-2 assert abs(result_mean.item() - 0.2995 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self : Any )-> Union[str, Any]: """simple docstring""" lowercase__ = self.full_loop(set_alpha_to_one=a , beta_start=0.01 ) lowercase__ = torch.sum(torch.abs(a ) ) lowercase__ = torch.mean(torch.abs(a ) ) assert abs(result_sum.item() - 186.9482 ) < 1E-2 assert abs(result_mean.item() - 0.2434 ) < 1E-3
45
from string import ascii_uppercase lowercase_ = {str(ord(c) - 55): c for c in ascii_uppercase} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: 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 > 36: raise ValueError('base must be <= 36' ) lowercase__ = '' lowercase__ = 0 lowercase__ = 0 while div != 1: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if base >= 11 and 9 < mod < 36: lowercase__ = ALPHABET_VALUES[str(_SCREAMING_SNAKE_CASE )] else: lowercase__ = str(_SCREAMING_SNAKE_CASE ) new_value += actual_value lowercase__ = num // base lowercase__ = 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), )
45
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/data2vec-vision-base-ft""": ( """https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json""" ), } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : int = 'data2vec-vision' def __init__( self : List[str] , a : Optional[int]=768 , a : List[Any]=12 , a : List[Any]=12 , a : Any=3_072 , a : Any="gelu" , a : List[str]=0.0 , a : Optional[int]=0.0 , a : Any=0.02 , a : Optional[int]=1E-1_2 , a : str=224 , a : List[Any]=16 , a : int=3 , a : str=False , a : Tuple=False , a : Any=False , a : Dict=False , a : Union[str, Any]=0.1 , a : int=0.1 , a : Tuple=True , a : int=[3, 5, 7, 11] , a : Optional[int]=[1, 2, 3, 6] , a : Optional[int]=True , a : List[Any]=0.4 , a : Dict=256 , a : Optional[Any]=1 , a : Optional[Any]=False , a : str=255 , **a : str , )-> Any: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = use_mask_token lowercase__ = use_absolute_position_embeddings lowercase__ = use_relative_position_bias lowercase__ = use_shared_relative_position_bias lowercase__ = layer_scale_init_value lowercase__ = drop_path_rate lowercase__ = use_mean_pooling # decode head attributes (semantic segmentation) lowercase__ = out_indices lowercase__ = pool_scales # auxiliary head attributes (semantic segmentation) lowercase__ = use_auxiliary_head lowercase__ = auxiliary_loss_weight lowercase__ = auxiliary_channels lowercase__ = auxiliary_num_convs lowercase__ = auxiliary_concat_input lowercase__ = semantic_loss_ignore_index class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : str )-> float: """simple docstring""" return 1E-4
45
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : Tuple=30 , a : Union[str, Any]=2 , a : List[str]=3 , a : Dict=True , a : List[str]=True , a : List[Any]=32 , a : List[str]=5 , a : Optional[int]=4 , a : List[str]=37 , a : Dict="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : int=10 , a : List[str]=0.02 , a : int=None , a : List[str]=2 , )-> Dict: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = is_training lowercase__ = use_labels lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = scope lowercase__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ = (image_size // patch_size) ** 2 lowercase__ = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any] , a : List[str] , a : Dict )-> Optional[Any]: """simple docstring""" lowercase__ = ViTModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Optional[Any] , a : int , a : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] , a : int , a : List[Any] )-> str: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : int = True _UpperCamelCase : int = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = ViTModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = ViTModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> str: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(a ) lowercase__ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass with torch.no_grad(): lowercase__ = model(a , interpolate_pos_encoding=a ) # verify the logits lowercase__ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , a ) lowercase__ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowercase__ = model(a )
45
1
import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin lowercase_ = get_tests_dir("""fixtures/test_sentencepiece.model""") lowercase_ = get_tests_dir("""fixtures/test_sentencepiece_bpe.model""") lowercase_ = """pt""" if is_torch_available() else """tf""" @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : str = CamembertTokenizer _UpperCamelCase : Any = CamembertTokenizerFast _UpperCamelCase : List[str] = True _UpperCamelCase : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Optional[Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowercase__ = CamembertTokenizer(a ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> str: """simple docstring""" lowercase__ = '<pad>' lowercase__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a ) , a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a ) , a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[str]: """simple docstring""" lowercase__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>NOTUSED' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(a ) , 1_004 ) def SCREAMING_SNAKE_CASE_ ( self : str )-> List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_005 ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = CamembertTokenizer(a ) tokenizer.save_pretrained(self.tmpdirname ) lowercase__ = CamembertTokenizerFast.from_pretrained(self.tmpdirname ) lowercase__ = 'I was born in 92000, and this is falsé.' lowercase__ = tokenizer.encode(a ) lowercase__ = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) lowercase__ = tokenizer.encode(a , add_special_tokens=a ) lowercase__ = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) lowercase__ = tokenizer.convert_ids_to_tokens(a ) lowercase__ = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> List[str]: """simple docstring""" if not self.test_rust_tokenizer: return lowercase__ = self.get_tokenizer() lowercase__ = self.get_rust_tokenizer() lowercase__ = 'I was born in 92000, and this is falsé.' lowercase__ = tokenizer.tokenize(a ) lowercase__ = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) lowercase__ = tokenizer.encode(a , add_special_tokens=a ) lowercase__ = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) lowercase__ = self.get_rust_tokenizer() lowercase__ = tokenizer.encode(a ) lowercase__ = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Optional[Any]: """simple docstring""" lowercase__ = {'input_ids': [[5, 54, 7_196, 297, 30, 23, 776, 18, 11, 3_215, 3_705, 8_252, 22, 3_164, 1_181, 2_116, 29, 16, 813, 25, 791, 3_314, 20, 3_446, 38, 27_575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9_088, 20, 1_517, 8, 22_804, 18_818, 10, 38, 629, 607, 607, 142, 19, 7_196, 867, 56, 10_326, 24, 2_267, 20, 416, 5_072, 15_612, 233, 734, 7, 2_399, 27, 16, 3_015, 1_649, 7, 24, 20, 4_338, 2_399, 27, 13, 3_400, 14, 13, 6_189, 8, 930, 9, 6]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. lowercase__ = [ 'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ' 'utilisé principalement dans le domaine du traitement automatique des langues (TAL).', 'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ' 'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ' 'telles que la traduction et la synthèse de texte.', ] self.tokenizer_integration_test_util( expected_encoding=a , model_name='camembert-base' , revision='3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf' , sequences=a , )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: stooge(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return arr def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase__ , lowercase__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) # Recursively sort last 2/3 elements stooge(_SCREAMING_SNAKE_CASE , i + t , (_SCREAMING_SNAKE_CASE) ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
45
1
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { """microsoft/conditional-detr-resnet-50""": ( """https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json""" ), } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[str] = 'conditional_detr' _UpperCamelCase : Union[str, Any] = ['past_key_values'] _UpperCamelCase : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__( self : List[str] , a : Tuple=True , a : List[Any]=None , a : Tuple=3 , a : Optional[Any]=300 , a : Dict=6 , a : str=2_048 , a : Optional[Any]=8 , a : Any=6 , a : Optional[Any]=2_048 , a : Tuple=8 , a : int=0.0 , a : List[Any]=0.0 , a : Optional[Any]=True , a : Optional[int]="relu" , a : List[str]=256 , a : int=0.1 , a : List[Any]=0.0 , a : str=0.0 , a : Optional[int]=0.02 , a : Dict=1.0 , a : Union[str, Any]=False , a : List[Any]="sine" , a : Any="resnet50" , a : Dict=True , a : List[Any]=False , a : Optional[Any]=2 , a : int=5 , a : Tuple=2 , a : Optional[int]=1 , a : int=1 , a : Dict=2 , a : List[Any]=5 , a : Optional[Any]=2 , a : List[str]=0.25 , **a : Union[str, Any] , )-> Union[str, Any]: """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('You can\'t specify both `backbone_config` and `use_timm_backbone`.' ) if not use_timm_backbone: if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.' ) lowercase__ = CONFIG_MAPPING['resnet'](out_features=['stage4'] ) elif isinstance(a , a ): lowercase__ = backbone_config.get('model_type' ) lowercase__ = CONFIG_MAPPING[backbone_model_type] lowercase__ = config_class.from_dict(a ) lowercase__ = use_timm_backbone lowercase__ = backbone_config lowercase__ = num_channels lowercase__ = num_queries lowercase__ = d_model lowercase__ = encoder_ffn_dim lowercase__ = encoder_layers lowercase__ = encoder_attention_heads lowercase__ = decoder_ffn_dim lowercase__ = decoder_layers lowercase__ = decoder_attention_heads lowercase__ = dropout lowercase__ = attention_dropout lowercase__ = activation_dropout lowercase__ = activation_function lowercase__ = init_std lowercase__ = init_xavier_std lowercase__ = encoder_layerdrop lowercase__ = decoder_layerdrop lowercase__ = encoder_layers lowercase__ = auxiliary_loss lowercase__ = position_embedding_type lowercase__ = backbone lowercase__ = use_pretrained_backbone lowercase__ = dilation # Hungarian matcher lowercase__ = class_cost lowercase__ = bbox_cost lowercase__ = giou_cost # Loss coefficients lowercase__ = mask_loss_coefficient lowercase__ = dice_loss_coefficient lowercase__ = cls_loss_coefficient lowercase__ = bbox_loss_coefficient lowercase__ = giou_loss_coefficient lowercase__ = focal_alpha super().__init__(is_encoder_decoder=a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> int: """simple docstring""" return self.encoder_attention_heads @property def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" return self.d_model def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: lowercase__ = self.backbone_config.to_dict() lowercase__ = self.__class__.model_type return output class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Optional[int] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ('pixel_mask', {0: 'batch'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-5 @property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> int: """simple docstring""" return 12
45
from scipy.stats import spearmanr import datasets lowercase_ = """ The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. """ lowercase_ = """ Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {'spearmanr': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results['spearmanr']) -0.7 >>> print(round(results['spearmanr_pvalue'], 2)) 0.19 """ lowercase_ = R"""\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE (datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'] , ) def SCREAMING_SNAKE_CASE_ ( self : int , a : str , a : Any , a : str=False )-> Optional[int]: """simple docstring""" lowercase__ = spearmanr(a , a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
45
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowercase_ = {"""configuration_speech_encoder_decoder""": ["""SpeechEncoderDecoderConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SpeechEncoderDecoderModel"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""FlaxSpeechEncoderDecoderModel"""] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: if not nums: return 0 lowercase__ = nums[0] lowercase__ = 0 for num in nums[1:]: lowercase__ , lowercase__ = ( max_excluding + num, max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } lowercase_ = { """b0""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 224, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 240, """dropout_rate""": 0.2, """dw_padding""": [16], }, """b2""": { """hidden_dim""": 1_408, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 260, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 16], }, """b3""": { """hidden_dim""": 1_536, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 300, """dropout_rate""": 0.3, """dw_padding""": [5, 18], }, """b4""": { """hidden_dim""": 1_792, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 380, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_048, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 456, """dropout_rate""": 0.4, """dw_padding""": [13, 27], }, """b6""": { """hidden_dim""": 2_304, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 528, """dropout_rate""": 0.5, """dw_padding""": [31], }, """b7""": { """hidden_dim""": 2_560, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 600, """dropout_rate""": 0.5, """dw_padding""": [18], }, } def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = EfficientNetConfig() lowercase__ = CONFIG_MAP[model_name]['hidden_dim'] lowercase__ = CONFIG_MAP[model_name]['width_coef'] lowercase__ = CONFIG_MAP[model_name]['depth_coef'] lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = CONFIG_MAP[model_name]['dropout_rate'] lowercase__ = CONFIG_MAP[model_name]['dw_padding'] lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = 1000 lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} return config def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = EfficientNetImageProcessor( size={'height': size, 'width': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=_SCREAMING_SNAKE_CASE , ) return preprocessor def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = [v.split('_' )[0].split('block' )[1] for v in original_param_names if v.startswith('block' )] lowercase__ = sorted(set(_SCREAMING_SNAKE_CASE ) ) lowercase__ = len(_SCREAMING_SNAKE_CASE ) lowercase__ = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} lowercase__ = [] rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight') ) rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight') ) rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias') ) rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean') ) rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var') ) for b in block_names: lowercase__ = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight') ) rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight') ) rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias') ) rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean') ) rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var') ) lowercase__ = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ = 'efficientnet.' + item[1] lowercase__ = 'classifier.weight' lowercase__ = 'classifier.bias' return key_mapping def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights='imagenet' , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=1000 , classifier_activation='softmax' , ) lowercase__ = original_model.trainable_variables lowercase__ = original_model.non_trainable_variables lowercase__ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ = param.numpy() lowercase__ = list(tf_params.keys() ) # Load HuggingFace model lowercase__ = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) lowercase__ = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() lowercase__ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('Converting parameters...' ) lowercase__ = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowercase__ = convert_image_processor(_SCREAMING_SNAKE_CASE ) lowercase__ = preprocessor(images=prepare_img() , return_tensors='pt' ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ = hf_model(**_SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits.detach().numpy() # Original model inference lowercase__ = False lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ = image.img_to_array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) lowercase__ = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print('Model outputs match!' ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") lowercase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
45
1
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> tuple: return (data["data"], data["target"]) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> XGBClassifier: lowercase__ = XGBClassifier() classifier.fit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return classifier def __UpperCamelCase () -> None: lowercase__ = load_iris() lowercase__ , lowercase__ = data_handling(_SCREAMING_SNAKE_CASE ) lowercase__ , lowercase__ , lowercase__ , lowercase__ = train_test_split( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , test_size=0.2_5 ) lowercase__ = iris['target_names'] # Create an XGBoost Classifier from the training data lowercase__ = xgboost(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , display_labels=_SCREAMING_SNAKE_CASE , cmap='Blues' , normalize='true' , ) plt.title('Normalized Confusion Matrix - IRIS Dataset' ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
45
import argparse import json import subprocess def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = [] lowercase__ = ( F"""curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"""" ' https://api.github.com/repos/huggingface/transformers/actions/runners' ) lowercase__ = subprocess.run(_SCREAMING_SNAKE_CASE , shell=_SCREAMING_SNAKE_CASE , stdout=subprocess.PIPE ) lowercase__ = output.stdout.decode('utf-8' ) lowercase__ = json.loads(_SCREAMING_SNAKE_CASE ) lowercase__ = status['runners'] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_SCREAMING_SNAKE_CASE ) # save the result so we can report them on Slack with open('offline_runners.txt' , 'w' ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) if len(_SCREAMING_SNAKE_CASE ) > 0: lowercase__ = '\n'.join([x['name'] for x in offline_runners] ) raise ValueError(F"""The following runners are offline:\n{failed}""" ) if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return values.split(',' ) lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--target_runners""", default=None, type=list_str, required=True, help="""Comma-separated list of runners to check status.""", ) parser.add_argument( """--token""", default=None, type=str, required=True, help="""A token that has actions:read permission.""" ) lowercase_ = parser.parse_args() get_runner_status(args.target_runners, args.token)
45
1
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : int , a : List[str] , a : int )-> Dict: """simple docstring""" super().__init__() self.register_modules(unet=a , scheduler=a ) @torch.no_grad() def __call__( self : Optional[int] , a : int = 1 , a : int = 100 , a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , a : Optional[float] = None , a : bool = True , )-> Union[AudioPipelineOutput, Tuple]: """simple docstring""" if audio_length_in_s is None: lowercase__ = self.unet.config.sample_size / self.unet.config.sample_rate lowercase__ = audio_length_in_s * self.unet.config.sample_rate lowercase__ = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( f"""{audio_length_in_s} is too small. Make sure it's bigger or equal to""" f""" {3 * down_scale_factor / self.unet.config.sample_rate}.""" ) lowercase__ = int(a ) if sample_size % down_scale_factor != 0: lowercase__ = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( f"""{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled""" f""" by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising""" ' process.' ) lowercase__ = int(a ) lowercase__ = next(iter(self.unet.parameters() ) ).dtype lowercase__ = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(a , a ) and len(a ) != batch_size: raise ValueError( f"""You have passed a list of generators of length {len(a )}, but requested an effective batch""" f""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) lowercase__ = randn_tensor(a , generator=a , device=self.device , dtype=a ) # set step values self.scheduler.set_timesteps(a , device=audio.device ) lowercase__ = self.scheduler.timesteps.to(a ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output lowercase__ = self.unet(a , a ).sample # 2. compute previous image: x_t -> t_t-1 lowercase__ = self.scheduler.step(a , a , a ).prev_sample lowercase__ = audio.clamp(-1 , 1 ).float().cpu().numpy() lowercase__ = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=a )
45
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'ClapFeatureExtractor' _UpperCamelCase : Union[str, Any] = ('RobertaTokenizer', 'RobertaTokenizerFast') def __init__( self : List[Any] , a : int , a : str )-> Any: """simple docstring""" super().__init__(a , a ) def __call__( self : Any , a : Tuple=None , a : Optional[int]=None , a : int=None , **a : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = kwargs.pop('sampling_rate' , a ) if text is None and audios is None: raise ValueError('You have to specify either text or audios. Both cannot be none.' ) if text is not None: lowercase__ = self.tokenizer(a , return_tensors=a , **a ) if audios is not None: lowercase__ = self.feature_extractor( a , sampling_rate=a , return_tensors=a , **a ) if text is not None and audios is not None: lowercase__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : str , *a : Dict , **a : int )-> Optional[int]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *a : int , **a : Dict )-> Dict: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer.model_input_names lowercase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
45
1
from __future__ import annotations import bisect def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = -1 ) -> int: if hi < 0: lowercase__ = len(_SCREAMING_SNAKE_CASE ) while lo < hi: lowercase__ = lo + (hi - lo) // 2 if sorted_collection[mid] < item: lowercase__ = mid + 1 else: lowercase__ = mid return lo def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = -1 ) -> int: if hi < 0: lowercase__ = len(_SCREAMING_SNAKE_CASE ) while lo < hi: lowercase__ = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: lowercase__ = mid + 1 else: lowercase__ = mid return lo def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = -1 ) -> None: sorted_collection.insert(bisect_left(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = -1 ) -> None: sorted_collection.insert(bisect_right(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | None: lowercase__ = 0 lowercase__ = len(_SCREAMING_SNAKE_CASE ) - 1 while left <= right: lowercase__ = left + (right - left) // 2 lowercase__ = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: lowercase__ = midpoint - 1 else: lowercase__ = midpoint + 1 return None def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | None: lowercase__ = bisect.bisect_left(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if index != len(_SCREAMING_SNAKE_CASE ) and sorted_collection[index] == item: return index return None def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int | None: if right < left: return None lowercase__ = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , midpoint - 1 ) else: return binary_search_by_recursion(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , midpoint + 1 , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by comma:\n""").strip() lowercase_ = sorted(int(item) for item in user_input.split(""",""")) lowercase_ = int(input("""Enter a single number to be found in the list:\n""")) lowercase_ = binary_search(collection, target) if result is None: print(f'''{target} was not found in {collection}.''') else: print(f'''{target} was found at position {result} in {collection}.''')
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} lowercase_ = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json""" ), }, } lowercase_ = { """moussaKam/mbarthez""": 1_024, """moussaKam/barthez""": 1_024, """moussaKam/barthez-orangesum-title""": 1_024, } lowercase_ = """▁""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = VOCAB_FILES_NAMES _UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Optional[Any] = ['input_ids', 'attention_mask'] _UpperCamelCase : int = BarthezTokenizer def __init__( self : List[Any] , a : Union[str, Any]=None , a : Optional[Any]=None , a : Dict="<s>" , a : Union[str, Any]="</s>" , a : List[str]="</s>" , a : Optional[Any]="<s>" , a : int="<unk>" , a : str="<pad>" , a : Optional[int]="<mask>" , **a : Union[str, Any] , )-> Tuple: """simple docstring""" lowercase__ = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) lowercase__ = vocab_file lowercase__ = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__ = [self.cls_token_id] lowercase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" lowercase__ = [self.sep_token_id] lowercase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE_ ( self : Dict , a : str , a : Optional[str] = None )-> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ = os.path.join( a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
45
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class SCREAMING_SNAKE_CASE : def __init__( self : str , a : Optional[int] , )-> str: """simple docstring""" lowercase__ = parent lowercase__ = 13 lowercase__ = 7 lowercase__ = True lowercase__ = True lowercase__ = True lowercase__ = True lowercase__ = True lowercase__ = False lowercase__ = False lowercase__ = False lowercase__ = 2 lowercase__ = 99 lowercase__ = 0 lowercase__ = 32 lowercase__ = 2 lowercase__ = 4 lowercase__ = 0.1 lowercase__ = 0.1 lowercase__ = 512 lowercase__ = 16 lowercase__ = 2 lowercase__ = 0.02 lowercase__ = 3 lowercase__ = 4 lowercase__ = 'last' lowercase__ = True lowercase__ = None lowercase__ = 0 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) lowercase__ = None if self.use_input_lengths: lowercase__ = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase__ = None if self.use_token_type_ids: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) lowercase__ = None lowercase__ = None lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase__ = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) lowercase__ = ids_tensor([self.batch_size] , self.num_choices ) lowercase__ = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE_ ( self : int , a : str , a : Tuple , a : Optional[Any] , a : Dict , a : Union[str, Any] , a : Any , a : Union[str, Any] , a : int , a : Optional[int] , )-> Optional[int]: """simple docstring""" lowercase__ = TFFlaubertModel(config=a ) lowercase__ = {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids} lowercase__ = model(a ) lowercase__ = [input_ids, input_mask] lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : str , a : Tuple , a : List[str] , a : List[Any] , a : Optional[Any] , a : Dict , a : Optional[int] , a : Optional[Any] , a : Union[str, Any] , )-> int: """simple docstring""" lowercase__ = TFFlaubertWithLMHeadModel(a ) lowercase__ = {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids} lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : Optional[int] , a : Tuple , a : int , a : Tuple , a : Optional[Any] , a : List[str] , a : List[Any] , a : Optional[int] , a : int , )-> List[str]: """simple docstring""" lowercase__ = TFFlaubertForQuestionAnsweringSimple(a ) lowercase__ = {'input_ids': input_ids, 'lengths': input_lengths} lowercase__ = model(a ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : int , a : Tuple , a : List[Any] , a : List[str] , a : str , a : int , a : str , a : Tuple , a : str , )-> Dict: """simple docstring""" lowercase__ = TFFlaubertForSequenceClassification(a ) lowercase__ = {'input_ids': input_ids, 'lengths': input_lengths} lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : Tuple , a : Dict , a : Any , a : List[str] , a : Union[str, Any] , a : int , a : Optional[Any] , a : Dict , a : Tuple , )-> Optional[Any]: """simple docstring""" lowercase__ = self.num_labels lowercase__ = TFFlaubertForTokenClassification(config=a ) lowercase__ = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : Optional[int] , a : Optional[Any] , a : Tuple , a : Tuple , a : Any , a : Union[str, Any] , a : int , a : Tuple , a : str , )-> List[str]: """simple docstring""" lowercase__ = self.num_choices lowercase__ = TFFlaubertForMultipleChoice(config=a ) lowercase__ = tf.tile(tf.expand_dims(a , 1 ) , (1, self.num_choices, 1) ) lowercase__ = tf.tile(tf.expand_dims(a , 1 ) , (1, self.num_choices, 1) ) lowercase__ = tf.tile(tf.expand_dims(a , 1 ) , (1, self.num_choices, 1) ) lowercase__ = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Dict: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = { 'input_ids': input_ids, 'token_type_ids': token_type_ids, 'langs': token_type_ids, 'lengths': input_lengths, } return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _UpperCamelCase : List[str] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _UpperCamelCase : List[str] = ( { 'feature-extraction': TFFlaubertModel, 'fill-mask': TFFlaubertWithLMHeadModel, 'question-answering': TFFlaubertForQuestionAnsweringSimple, 'text-classification': TFFlaubertForSequenceClassification, 'token-classification': TFFlaubertForTokenClassification, 'zero-shot': TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : int , a : List[str] , a : Dict , a : Optional[int] , a : Union[str, Any] , a : List[Any] )-> Optional[Any]: """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> int: """simple docstring""" lowercase__ = TFFlaubertModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , emb_dim=37 ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Any: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Dict: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Any: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[Any]: """simple docstring""" for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = TFFlaubertModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_tf @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE (unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE_ ( self : Any )-> List[str]: """simple docstring""" lowercase__ = TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased' ) lowercase__ = tf.convert_to_tensor( [[0, 158, 735, 2_592, 1_424, 6_727, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" lowercase__ = model(a )[0] lowercase__ = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape , a ) # compare the actual values for a slice. lowercase__ = tf.convert_to_tensor( [ [ [-1.8768773, -1.566555, 0.27072418], [-1.6920038, -0.5873505, 1.9329599], [-2.9563985, -1.6993835, 1.7972052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowercase_ = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowercase_ = TaTokenizerFast lowercase_ = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowercase_ = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
45
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 lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Any = 'deit' def __init__( self : Any , a : Union[str, Any]=768 , a : Optional[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Optional[int]="gelu" , a : Optional[Any]=0.0 , a : List[Any]=0.0 , a : int=0.02 , a : List[str]=1E-1_2 , a : Optional[int]=224 , a : Tuple=16 , a : List[Any]=3 , a : List[str]=True , a : Any=16 , **a : Union[str, Any] , )-> int: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = qkv_bias lowercase__ = encoder_stride class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : int )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-4
45
1
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowercase_ = get_tests_dir("""fixtures""") lowercase_ = get_tests_dir("""fixtures/dummy_feature_extractor_config.json""") lowercase_ = get_tests_dir("""fixtures/dummy-config.json""") class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : Dict )-> str: """simple docstring""" lowercase__ = 0 def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[Any]: """simple docstring""" lowercase__ = AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h' ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : str )-> List[str]: """simple docstring""" lowercase__ = AutoFeatureExtractor.from_pretrained(a ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmpdirname: lowercase__ = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally lowercase__ = AutoFeatureExtractor.from_pretrained(a ).to_dict() config_dict.pop('feature_extractor_type' ) lowercase__ = WavaVecaFeatureExtractor(**a ) # save in new folder model_config.save_pretrained(a ) config.save_pretrained(a ) lowercase__ = AutoFeatureExtractor.from_pretrained(a ) # make sure private variable is not incorrectly saved lowercase__ = json.loads(config.to_json_string() ) self.assertTrue('_processor_class' not in dict_as_saved ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = AutoFeatureExtractor.from_pretrained(a ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Tuple: """simple docstring""" with self.assertRaisesRegex( a , 'bert-base is not a local folder and is not a valid model identifier' ): lowercase__ = AutoFeatureExtractor.from_pretrained('bert-base' ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Dict: """simple docstring""" with self.assertRaisesRegex( a , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): lowercase__ = AutoFeatureExtractor.from_pretrained(a , revision='aaaaaa' ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Any: """simple docstring""" with self.assertRaisesRegex( a , 'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.' , ): lowercase__ = AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model' ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[str]: """simple docstring""" with self.assertRaises(a ): lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' ) # If remote code is disabled, we can't load this config. with self.assertRaises(a ): lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' , trust_remote_code=a ) lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' , trust_remote_code=a ) self.assertEqual(feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(a ) lowercase__ = AutoFeatureExtractor.from_pretrained(a , trust_remote_code=a ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Dict: """simple docstring""" try: AutoConfig.register('custom' , a ) AutoFeatureExtractor.register(a , a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(a ): AutoFeatureExtractor.register(a , a ) # Now that the config is registered, it can be used as any other config with the auto-API lowercase__ = CustomFeatureExtractor.from_pretrained(a ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(a ) lowercase__ = AutoFeatureExtractor.from_pretrained(a ) self.assertIsInstance(a , a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = True try: AutoConfig.register('custom' , a ) AutoFeatureExtractor.register(a , a ) # If remote code is not set, the default is to use local lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' ) self.assertEqual(feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' , trust_remote_code=a ) self.assertEqual(feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub lowercase__ = AutoFeatureExtractor.from_pretrained( 'hf-internal-testing/test_dynamic_feature_extractor' , trust_remote_code=a ) self.assertEqual(feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) self.assertTrue(not hasattr(a , 'is_local' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
45
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
1
import inspect import unittest import warnings from math import ceil, floor from transformers import LevitConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_MAPPING, LevitForImageClassification, LevitForImageClassificationWithTeacher, LevitModel, ) from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(a , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(a , 'num_attention_heads' ) ) class SCREAMING_SNAKE_CASE : def __init__( self : Any , a : Union[str, Any] , a : Optional[int]=13 , a : int=64 , a : Optional[int]=3 , a : List[Any]=3 , a : str=2 , a : Any=1 , a : str=16 , a : List[Any]=[128, 256, 384] , a : Optional[int]=[4, 6, 8] , a : Any=[2, 3, 4] , a : Any=[16, 16, 16] , a : Any=0 , a : Dict=[2, 2, 2] , a : Optional[int]=[2, 2, 2] , a : str=0.02 , a : Optional[Any]=True , a : List[Any]=True , a : Optional[int]=2 , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = num_channels lowercase__ = kernel_size lowercase__ = stride lowercase__ = padding lowercase__ = hidden_sizes lowercase__ = num_attention_heads lowercase__ = depths lowercase__ = key_dim lowercase__ = drop_path_rate lowercase__ = patch_size lowercase__ = attention_ratio lowercase__ = mlp_ratio lowercase__ = initializer_range lowercase__ = [ ['Subsample', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['Subsample', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] lowercase__ = is_training lowercase__ = use_labels lowercase__ = num_labels lowercase__ = initializer_range def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.num_labels ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" return LevitConfig( image_size=self.image_size , num_channels=self.num_channels , kernel_size=self.kernel_size , stride=self.stride , padding=self.padding , patch_size=self.patch_size , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , depths=self.depths , key_dim=self.key_dim , drop_path_rate=self.drop_path_rate , mlp_ratio=self.mlp_ratio , attention_ratio=self.attention_ratio , initializer_range=self.initializer_range , down_ops=self.down_ops , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : Tuple , a : Dict , a : List[str] )-> List[Any]: """simple docstring""" lowercase__ = LevitModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) lowercase__ = (self.image_size, self.image_size) lowercase__ , lowercase__ = image_size[0], image_size[1] for _ in range(4 ): lowercase__ = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) lowercase__ = floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, ceil(height / 4 ) * ceil(width / 4 ), self.hidden_sizes[-1]) , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[Any] , a : Any , a : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.num_labels lowercase__ = LevitForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Tuple = ( (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( { 'feature-extraction': LevitModel, 'image-classification': (LevitForImageClassification, LevitForImageClassificationWithTeacher), } if is_torch_available() else {} ) _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Optional[int] = False _UpperCamelCase : Dict = False _UpperCamelCase : List[Any] = False _UpperCamelCase : Tuple = False def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = LevitModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Dict: """simple docstring""" 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 SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" return @unittest.skip(reason='Levit does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='Levit does not support input and output embeddings' ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> str: """simple docstring""" pass @unittest.skip(reason='Levit does not output attentions' ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> int: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> str: """simple docstring""" def check_hidden_states_output(a : Tuple , a : Optional[int] , a : Optional[int] ): lowercase__ = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): lowercase__ = model(**self._prepare_for_class(a , a ) ) lowercase__ = outputs.hidden_states lowercase__ = len(self.model_tester.depths ) + 1 self.assertEqual(len(a ) , a ) lowercase__ = (self.model_tester.image_size, self.model_tester.image_size) lowercase__ , lowercase__ = image_size[0], image_size[1] for _ in range(4 ): lowercase__ = floor( ( (height + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) lowercase__ = floor( ( (width + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [ height * width, self.model_tester.hidden_sizes[0], ] , ) lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = True check_hidden_states_output(a , a , a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowercase__ = True check_hidden_states_output(a , a , a ) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Union[str, Any] , a : str , a : List[str]=False )-> List[str]: """simple docstring""" lowercase__ = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "LevitForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" if not self.model_tester.is_training: return lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = True for model_class in self.all_model_classes: # LevitForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(a ) or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue lowercase__ = model_class(a ) model.to(a ) model.train() lowercase__ = self._prepare_for_class(a , a , return_labels=a ) lowercase__ = model(**a ).loss loss.backward() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Any: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return lowercase__ = False lowercase__ = True for model_class in self.all_model_classes: if model_class in get_values(a ) or not model_class.supports_gradient_checkpointing: continue # LevitForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "LevitForImageClassificationWithTeacher": continue lowercase__ = model_class(a ) model.gradient_checkpointing_enable() model.to(a ) model.train() lowercase__ = self._prepare_for_class(a , a , return_labels=a ) lowercase__ = model(**a ).loss loss.backward() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = [ {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float}, {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long}, {'title': 'regression', 'num_labels': 1, 'dtype': torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(a ), ] or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=f"""Testing {model_class} with {problem_type["title"]}""" ): lowercase__ = problem_type['title'] lowercase__ = problem_type['num_labels'] lowercase__ = model_class(a ) model.to(a ) model.train() lowercase__ = self._prepare_for_class(a , a , return_labels=a ) if problem_type["num_labels"] > 1: lowercase__ = inputs['labels'].unsqueeze(1 ).repeat(1 , problem_type['num_labels'] ) lowercase__ = inputs['labels'].to(problem_type['dtype'] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=a ) as warning_list: lowercase__ = model(**a ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( f"""Something is going wrong in the regression problem: intercepted {w.message}""" ) loss.backward() @slow def SCREAMING_SNAKE_CASE_ ( self : Any )-> Optional[int]: """simple docstring""" for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = LevitModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> Optional[int]: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Any: """simple docstring""" return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> str: """simple docstring""" lowercase__ = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([1.0448, -0.3745, -1.8317] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) )
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
1
import unittest from transformers.testing_utils import require_bsa from transformers.utils import is_bsa_available from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin if is_bsa_available(): from transformers import MarkupLMFeatureExtractor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : int , a : int )-> Dict: """simple docstring""" lowercase__ = parent def SCREAMING_SNAKE_CASE_ ( self : Any )-> Optional[Any]: """simple docstring""" return {} def __UpperCamelCase () -> List[Any]: lowercase__ = '<HTML>\n\n <HEAD>\n <TITLE>sample document</TITLE>\n </HEAD>\n\n <BODY BGCOLOR="FFFFFF">\n <HR>\n <a href="http://google.com">Goog</a>\n <H1>This is one header</H1>\n <H2>This is a another Header</H2>\n <P>Travel from\n <P>\n <B>SFO to JFK</B>\n <BR>\n <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>\n <HR>\n <div style="color:#0000FF">\n <h3>Traveler <b> name </b> is\n <p> John Doe </p>\n </div>' lowercase__ = '\n <!DOCTYPE html>\n <html>\n <body>\n\n <h1>My First Heading</h1>\n <p>My first paragraph.</p>\n\n </body>\n </html>\n ' return [html_string_a, html_string_a] @require_bsa class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = MarkupLMFeatureExtractor if is_bsa_available() else None def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = MarkupLMFeatureExtractionTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> str: """simple docstring""" return self.feature_extract_tester.prepare_feat_extract_dict() def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.feature_extraction_class() # Test not batched input lowercase__ = get_html_strings()[0] lowercase__ = feature_extractor(a ) # fmt: off lowercase__ = [['sample document', 'Goog', 'This is one header', 'This is a another Header', 'Travel from', 'SFO to JFK', 'on May 2, 2015 at 2:00 pm. For details go to confirm.com', 'Traveler', 'name', 'is', 'John Doe']] lowercase__ = [['/html/head/title', '/html/body/a', '/html/body/h1', '/html/body/h2', '/html/body/p', '/html/body/p/p/b[1]', '/html/body/p/p/b[2]/i', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/b', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/p']] # fmt: on self.assertEqual(encoding.nodes , a ) self.assertEqual(encoding.xpaths , a ) # Test batched lowercase__ = get_html_strings() lowercase__ = feature_extractor(a ) # fmt: off lowercase__ = expected_nodes + [['My First Heading', 'My first paragraph.']] lowercase__ = expected_xpaths + [['/html/body/h1', '/html/body/p']] self.assertEqual(len(encoding.nodes ) , 2 ) self.assertEqual(len(encoding.xpaths ) , 2 ) self.assertEqual(encoding.nodes , a ) self.assertEqual(encoding.xpaths , a )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
# This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny model through reduction of a normal pre-trained model, but keeping the # full vocab, merges file, and thus also resulting in a larger model due to a large vocab size. # This gives ~3MB in total for all files. # # If you want a 50 times smaller than this see `fsmt-make-super-tiny-model.py`, which is slightly more complicated # # # It will be used then as "stas/tiny-wmt19-en-de" # Build from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration lowercase_ = """facebook/wmt19-en-de""" lowercase_ = FSMTTokenizer.from_pretrained(mname) # get the correct vocab sizes, etc. from the master model lowercase_ = FSMTConfig.from_pretrained(mname) config.update( dict( d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) ) lowercase_ = FSMTForConditionalGeneration(config) print(f'''num of params {tiny_model.num_parameters()}''') # Test lowercase_ = tokenizer(["""Making tiny model"""], return_tensors="""pt""") lowercase_ = tiny_model(**batch) print("""test output:""", len(outputs.logits[0])) # Save lowercase_ = """tiny-wmt19-en-de""" tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(f'''Generated {mname_tiny}''') # Upload # transformers-cli upload tiny-wmt19-en-de
45
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: lowercase__ = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): lowercase__ = n - k # Calculate C(n,k) for i in range(_SCREAMING_SNAKE_CASE ): result *= n - i result //= i + 1 return result def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: return binomial_coefficient(2 * node_count , _SCREAMING_SNAKE_CASE ) // (node_count + 1) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: if n < 0: raise ValueError('factorial() not defined for negative values' ) lowercase__ = 1 for i in range(1 , n + 1 ): result *= i return result def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: return catalan_number(_SCREAMING_SNAKE_CASE ) * factorial(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( f'''Given {node_count} nodes, there are {binary_tree_count(node_count)} ''' f'''binary trees and {catalan_number(node_count)} binary search trees.''' )
45
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
1
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class SCREAMING_SNAKE_CASE (unittest.TestCase ): @parameterized.expand([(None,), ('foo.json',)] ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : List[str] )-> Any: """simple docstring""" lowercase__ = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a , config_name=a ) lowercase__ = GenerationConfig.from_pretrained(a , config_name=a ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , a ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[Any]: """simple docstring""" lowercase__ = AutoConfig.from_pretrained('gpt2' ) lowercase__ = GenerationConfig.from_model_config(a ) lowercase__ = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(a , a ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Any: """simple docstring""" lowercase__ = GenerationConfig() lowercase__ = { 'max_new_tokens': 1_024, 'foo': 'bar', } lowercase__ = copy.deepcopy(a ) lowercase__ = generation_config.update(**a ) # update_kwargs was not modified (no side effects) self.assertEqual(a , a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(a , {'foo': 'bar'} ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Tuple: """simple docstring""" lowercase__ = GenerationConfig() lowercase__ = 'bar' with tempfile.TemporaryDirectory('test-generation-config' ) as tmp_dir: generation_config.save_pretrained(a ) lowercase__ = GenerationConfig.from_pretrained(a ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , 'bar' ) lowercase__ = GenerationConfig.from_model_config(a ) assert not hasattr(a , 'foo' ) # no new kwargs should be initialized if from config def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> List[str]: """simple docstring""" lowercase__ = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , a ) self.assertEqual(default_config.num_beams , 1 ) lowercase__ = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , a ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(a ) lowercase__ = GenerationConfig.from_pretrained(a , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , a ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class SCREAMING_SNAKE_CASE (unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Tuple )-> List[str]: """simple docstring""" lowercase__ = TOKEN HfFolder.save_token(a ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Union[str, Any] )-> Optional[int]: """simple docstring""" try: delete_repo(token=cls._token , repo_id='test-generation-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-generation-config-org' ) except HTTPError: pass def SCREAMING_SNAKE_CASE_ ( self : Any )-> Optional[int]: """simple docstring""" lowercase__ = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('test-generation-config' , use_auth_token=self._token ) lowercase__ = GenerationConfig.from_pretrained(f"""{USER}/test-generation-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='test-generation-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='test-generation-config' , push_to_hub=a , use_auth_token=self._token ) lowercase__ = GenerationConfig.from_pretrained(f"""{USER}/test-generation-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[Any]: """simple docstring""" lowercase__ = GenerationConfig( do_sample=a , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('valid_org/test-generation-config-org' , use_auth_token=self._token ) lowercase__ = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-generation-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( a , repo_id='valid_org/test-generation-config-org' , push_to_hub=a , use_auth_token=self._token ) lowercase__ = GenerationConfig.from_pretrained('valid_org/test-generation-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(a , getattr(a , a ) )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
1
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[Any] , a : UNetaDModel , a : UNetaDModel , a : DDPMScheduler , a : Any , )-> Dict: """simple docstring""" super().__init__() lowercase__ = value_function lowercase__ = unet lowercase__ = scheduler lowercase__ = env lowercase__ = env.get_dataset() lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].mean() except: # noqa: E722 pass lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].std() except: # noqa: E722 pass lowercase__ = env.observation_space.shape[0] lowercase__ = env.action_space.shape[0] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Any , a : int )-> Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : str , a : List[str] )-> str: """simple docstring""" return x_in * self.stds[key] + self.means[key] def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : Tuple )-> Tuple: """simple docstring""" if type(a ) is dict: return {k: self.to_torch(a ) for k, v in x_in.items()} elif torch.is_tensor(a ): return x_in.to(self.unet.device ) return torch.tensor(a , device=self.unet.device ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[int] , a : Dict , a : Optional[Any] )-> List[Any]: """simple docstring""" for key, val in cond.items(): lowercase__ = val.clone() return x_in def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[Any] , a : Any , a : Optional[Any] , a : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = x.shape[0] lowercase__ = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model lowercase__ = torch.full((batch_size,) , a , device=self.unet.device , dtype=torch.long ) for _ in range(a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models lowercase__ = self.value_function(x.permute(0 , 2 , 1 ) , a ).sample lowercase__ = torch.autograd.grad([y.sum()] , [x] )[0] lowercase__ = self.scheduler._get_variance(a ) lowercase__ = torch.exp(0.5 * posterior_variance ) lowercase__ = model_std * grad lowercase__ = 0 lowercase__ = x.detach() lowercase__ = x + scale * grad lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.unet(x.permute(0 , 2 , 1 ) , a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg lowercase__ = self.scheduler.step(a , a , a , predict_epsilon=a )['prev_sample'] # apply conditions to the trajectory (set the initial state) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) return x, y def __call__( self : Any , a : Tuple , a : int=64 , a : Tuple=32 , a : List[Any]=2 , a : List[str]=0.1 )-> List[Any]: """simple docstring""" lowercase__ = self.normalize(a , 'observations' ) lowercase__ = obs[None].repeat(a , axis=0 ) lowercase__ = {0: self.to_torch(a )} lowercase__ = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) lowercase__ = randn_tensor(a , device=self.unet.device ) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) # run the diffusion process lowercase__ , lowercase__ = self.run_diffusion(a , a , a , a ) # sort output trajectories by value lowercase__ = y.argsort(0 , descending=a ).squeeze() lowercase__ = x[sorted_idx] lowercase__ = sorted_values[:, :, : self.action_dim] lowercase__ = actions.detach().cpu().numpy() lowercase__ = self.de_normalize(a , key='actions' ) # select the action with the highest value if y is not None: lowercase__ = 0 else: # if we didn't run value guiding, select a random action lowercase__ = np.random.randint(0 , a ) lowercase__ = denorm_actions[selected_index, 0] return denorm_actions
45
1
import json import sys def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: with open(_SCREAMING_SNAKE_CASE , encoding='utf-8' ) as f: lowercase__ = json.load(_SCREAMING_SNAKE_CASE ) lowercase__ = ['<details>', '<summary>Show updated benchmarks!</summary>', ' '] for benchmark_name in sorted(_SCREAMING_SNAKE_CASE ): lowercase__ = results[benchmark_name] lowercase__ = benchmark_name.split('/' )[-1] output_md.append(F"""### Benchmark: {benchmark_file_name}""" ) lowercase__ = '| metric |' lowercase__ = '|--------|' lowercase__ = '| new / old (diff) |' for metric_name in sorted(_SCREAMING_SNAKE_CASE ): lowercase__ = benchmark_res[metric_name] lowercase__ = metric_vals['new'] lowercase__ = metric_vals.get('old' , _SCREAMING_SNAKE_CASE ) lowercase__ = metric_vals.get('diff' , _SCREAMING_SNAKE_CASE ) lowercase__ = F""" {new_val:f}""" if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else 'None' if old_val is not None: val_str += F""" / {old_val:f}""" if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" if dif_val is not None: val_str += F""" ({dif_val:f})""" if isinstance(_SCREAMING_SNAKE_CASE , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append('</details>' ) with open(_SCREAMING_SNAKE_CASE , 'w' , encoding='utf-8' ) as f: f.writelines('\n'.join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": lowercase_ = sys.argv[1] lowercase_ = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
45
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
1
import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) lowercase_ = logging.getLogger() lowercase_ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] )-> str: """simple docstring""" os.makedirs(a , exist_ok=a ) lowercase__ = {'source': 'What is love ?', 'target': 'life'} lowercase__ = {'train': 12, 'val': 2, 'test': 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: lowercase__ = '\n'.join([contents[field]] * n_lines[split] ) with open(os.path.join(a , f"""{split}.{field}""" ) , 'w' ) as f: f.write(a ) def SCREAMING_SNAKE_CASE_ ( self : Any , a : int , a : str = "pytorch" )-> int: """simple docstring""" lowercase__ = self.get_auto_remove_tmp_dir() lowercase__ = os.path.join(a , 'output' ) lowercase__ = os.path.join(a , 'data' ) self._create_dummy_data(data_dir=a ) lowercase__ = f""" --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ """.split() if gpus > 0: testargs.append(f"""--gpus={gpus}""" ) if is_apex_available(): testargs.append('--fp16' ) else: testargs.append('--gpus=0' ) testargs.append('--distributed_backend=ddp_cpu' ) testargs.append('--num_processes=2' ) lowercase__ = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(a , env=self.get_env() ) lowercase__ = os.path.join(a , 'metrics.json' ) with open(a ) as f: lowercase__ = json.load(a ) return result @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_multi_gpu def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_gpu @require_ray def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = self._run_finetune(gpus=1 , distributed_retriever='ray' ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_multi_gpu @require_ray def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" lowercase__ = self._run_finetune(gpus=1 , distributed_retriever='ray' ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 )
45
import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : Any , a : str , a : List[Any]=7 , a : int=3 , a : int=18 , a : Optional[Any]=30 , a : Optional[int]=400 , a : int=True , a : Tuple=None , a : Optional[Any]=True , a : str=False , a : str=True , a : int=True , a : Tuple=[0.5, 0.5, 0.5] , a : Any=[0.5, 0.5, 0.5] , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size if size is not None else {'height': 18, 'width': 20} lowercase__ = do_thumbnail lowercase__ = do_align_axis lowercase__ = do_pad lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = DonutImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) self.assertTrue(hasattr(a , 'do_thumbnail' ) ) self.assertTrue(hasattr(a , 'do_align_long_axis' ) ) self.assertTrue(hasattr(a , 'do_pad' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" pass @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , numpify=a ) for image in image_inputs: self.assertIsInstance(a , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , torchify=a ) for image in image_inputs: self.assertIsInstance(a , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
45
1
import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor lowercase_ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Dict , *a : Dict , **a : int )-> None: """simple docstring""" warnings.warn( 'The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use OwlViTImageProcessor instead.' , a , ) super().__init__(*a , **a )
45
import math def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(_SCREAMING_SNAKE_CASE ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowercase_ = """Enter the base and the power separated by a comma: """ lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. lowercase_ = res(xa, ya) lowercase_ = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
45
1
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType lowercase_ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Union[str, Any] = 'vision-encoder-decoder' _UpperCamelCase : Union[str, Any] = True def __init__( self : Dict , **a : Union[str, Any] )-> Any: """simple docstring""" super().__init__(**a ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( f"""A configuraton of type {self.model_type} cannot be instantiated because """ f"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) lowercase__ = kwargs.pop('encoder' ) lowercase__ = encoder_config.pop('model_type' ) lowercase__ = kwargs.pop('decoder' ) lowercase__ = decoder_config.pop('model_type' ) lowercase__ = AutoConfig.for_model(a , **a ) lowercase__ = AutoConfig.for_model(a , **a ) lowercase__ = True @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[int] , a : PretrainedConfig , a : PretrainedConfig , **a : Dict )-> PretrainedConfig: """simple docstring""" logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config' ) lowercase__ = True lowercase__ = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = copy.deepcopy(self.__dict__ ) lowercase__ = self.encoder.to_dict() lowercase__ = self.decoder.to_dict() lowercase__ = self.__class__.model_type return output class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : str = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> float: """simple docstring""" return 1E-4 @property def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict({'last_hidden_state': {0: 'batch', 1: 'encoder_sequence'}} ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Mapping[str, Mapping[int, str]]: """simple docstring""" lowercase__ = OrderedDict() lowercase__ = {0: 'batch', 1: 'past_decoder_sequence + sequence'} lowercase__ = {0: 'batch', 1: 'past_decoder_sequence + sequence'} lowercase__ = {0: 'batch', 1: 'encoder_sequence'} return common_inputs def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : "PreTrainedTokenizerBase" , a : int = -1 , a : int = -1 , a : bool = False , a : Optional["TensorType"] = None , )-> Mapping[str, Any]: """simple docstring""" import torch lowercase__ = OrderedDict() lowercase__ = super().generate_dummy_inputs( a , batch_size=a , seq_length=a , is_pair=a , framework=a ) lowercase__ , lowercase__ = dummy_input['input_ids'].shape lowercase__ = (batch, encoder_sequence, self._config.encoder_hidden_size) lowercase__ = dummy_input.pop('input_ids' ) lowercase__ = dummy_input.pop('attention_mask' ) lowercase__ = torch.zeros(a ) return common_inputs class SCREAMING_SNAKE_CASE (UpperCAmelCase ): @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> None: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Dict , a : PretrainedConfig )-> OnnxConfig: """simple docstring""" return VisionEncoderDecoderEncoderOnnxConfig(a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : PretrainedConfig , a : PretrainedConfig , a : str = "default" )-> OnnxConfig: """simple docstring""" lowercase__ = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(a , a )
45
class SCREAMING_SNAKE_CASE : # Public class to implement a graph def __init__( self : int , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = row lowercase__ = col lowercase__ = graph def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int , a : list[list[bool]] )-> bool: """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order lowercase__ = [-1, 0, 1, -1, 1, -1, 0, 1] lowercase__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , a ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: # And finally, count all islands. """simple docstring""" lowercase__ = [[False for j in range(self.COL )] for i in range(self.ROW )] lowercase__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(a , a , a ) count += 1 return count
45
1
lowercase_ = { """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""": """--..""", """1""": """.----""", """2""": """..---""", """3""": """...--""", """4""": """....-""", """5""": """.....""", """6""": """-....""", """7""": """--...""", """8""": """---..""", """9""": """----.""", """0""": """-----""", """&""": """.-...""", """@""": """.--.-.""", """:""": """---...""", """,""": """--..--""", """.""": """.-.-.-""", """'""": """.----.""", """\"""": """.-..-.""", """?""": """..--..""", """/""": """-..-.""", """=""": """-...-""", """+""": """.-.-.""", """-""": """-....-""", """(""": """-.--.""", """)""": """-.--.-""", """!""": """-.-.--""", """ """: """/""" } # Exclamation mark is not in ITU-R recommendation # fmt: on lowercase_ = {value: key for key, value in MORSE_CODE_DICT.items()} def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return "".join(REVERSE_DICT[char] for char in message.split() ) def __UpperCamelCase () -> None: lowercase__ = 'Morse code here!' print(_SCREAMING_SNAKE_CASE ) lowercase__ = encrypt(_SCREAMING_SNAKE_CASE ) print(_SCREAMING_SNAKE_CASE ) lowercase__ = decrypt(_SCREAMING_SNAKE_CASE ) print(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
45
from string import ascii_uppercase lowercase_ = {str(ord(c) - 55): c for c in ascii_uppercase} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: 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 > 36: raise ValueError('base must be <= 36' ) lowercase__ = '' lowercase__ = 0 lowercase__ = 0 while div != 1: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if base >= 11 and 9 < mod < 36: lowercase__ = ALPHABET_VALUES[str(_SCREAMING_SNAKE_CASE )] else: lowercase__ = str(_SCREAMING_SNAKE_CASE ) new_value += actual_value lowercase__ = num // base lowercase__ = 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), )
45
1
from typing import Callable, Dict, Optional, Tuple import torch from torch import nn from torch.distributions import ( AffineTransform, Distribution, Independent, NegativeBinomial, Normal, StudentT, TransformedDistribution, ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Any , a : Distribution , a : Tuple=None , a : Optional[Any]=None , a : List[str]=0 )-> List[Any]: """simple docstring""" lowercase__ = 1.0 if scale is None else scale lowercase__ = 0.0 if loc is None else loc super().__init__(a , [AffineTransform(loc=self.loc , scale=self.scale , event_dim=a )] ) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[str]: """simple docstring""" return self.base_dist.mean * self.scale + self.loc @property def SCREAMING_SNAKE_CASE_ ( self : Dict )-> str: """simple docstring""" return self.base_dist.variance * self.scale**2 @property def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> List[str]: """simple docstring""" return self.variance.sqrt() class SCREAMING_SNAKE_CASE (nn.Module ): def __init__( self : Optional[int] , a : int , a : Dict[str, int] , a : Callable[..., Tuple[torch.Tensor]] , **a : Any )-> None: """simple docstring""" super().__init__(**a ) lowercase__ = args_dim lowercase__ = nn.ModuleList([nn.Linear(a , a ) for dim in args_dim.values()] ) lowercase__ = domain_map def SCREAMING_SNAKE_CASE_ ( self : Dict , a : torch.Tensor )-> Tuple[torch.Tensor]: """simple docstring""" lowercase__ = [proj(a ) for proj in self.proj] return self.domain_map(*a ) class SCREAMING_SNAKE_CASE (nn.Module ): def __init__( self : List[str] , a : Optional[Any] )-> Optional[Any]: """simple docstring""" super().__init__() lowercase__ = function def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : Optional[int] , *a : Optional[int] )-> Dict: """simple docstring""" return self.function(a , *a ) class SCREAMING_SNAKE_CASE : _UpperCamelCase : type _UpperCamelCase : int _UpperCamelCase : Dict[str, int] def __init__( self : List[str] , a : int = 1 )-> None: """simple docstring""" lowercase__ = dim lowercase__ = {k: dim * self.args_dim[k] for k in self.args_dim} def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[str] )-> Dict: """simple docstring""" if self.dim == 1: return self.distribution_class(*a ) else: return Independent(self.distribution_class(*a ) , 1 ) def SCREAMING_SNAKE_CASE_ ( self : str , a : List[Any] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , )-> Distribution: """simple docstring""" lowercase__ = self._base_distribution(a ) if loc is None and scale is None: return distr else: return AffineTransformed(a , loc=a , scale=a , event_dim=self.event_dim ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> Tuple: """simple docstring""" return () if self.dim == 1 else (self.dim,) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> int: """simple docstring""" return len(self.event_shape ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> float: """simple docstring""" return 0.0 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : int )-> nn.Module: """simple docstring""" return ParameterProjection( in_features=a , args_dim=self.args_dim , domain_map=LambdaLayer(self.domain_map ) , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , *a : torch.Tensor )-> Dict: """simple docstring""" raise NotImplementedError() @staticmethod def SCREAMING_SNAKE_CASE_ ( a : torch.Tensor )-> torch.Tensor: """simple docstring""" return (x + torch.sqrt(torch.square(a ) + 4.0 )) / 2.0 class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict[str, int] = {"df": 1, "loc": 1, "scale": 1} _UpperCamelCase : type = StudentT @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , a : torch.Tensor , a : torch.Tensor , a : torch.Tensor )-> Any: """simple docstring""" lowercase__ = cls.squareplus(a ).clamp_min(torch.finfo(scale.dtype ).eps ) lowercase__ = 2.0 + cls.squareplus(a ) return df.squeeze(-1 ), loc.squeeze(-1 ), scale.squeeze(-1 ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict[str, int] = {"loc": 1, "scale": 1} _UpperCamelCase : type = Normal @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Dict , a : torch.Tensor , a : torch.Tensor )-> Union[str, Any]: """simple docstring""" lowercase__ = cls.squareplus(a ).clamp_min(torch.finfo(scale.dtype ).eps ) return loc.squeeze(-1 ), scale.squeeze(-1 ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict[str, int] = {"total_count": 1, "logits": 1} _UpperCamelCase : type = NegativeBinomial @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Dict , a : torch.Tensor , a : torch.Tensor )-> int: """simple docstring""" lowercase__ = cls.squareplus(a ) return total_count.squeeze(-1 ), logits.squeeze(-1 ) def SCREAMING_SNAKE_CASE_ ( self : Any , a : Tuple )-> Distribution: """simple docstring""" lowercase__ , lowercase__ = distr_args if self.dim == 1: return self.distribution_class(total_count=a , logits=a ) else: return Independent(self.distribution_class(total_count=a , logits=a ) , 1 ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Dict , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None )-> Distribution: """simple docstring""" lowercase__ , lowercase__ = distr_args if scale is not None: # See scaling property of Gamma. logits += scale.log() return self._base_distribution((total_count, logits) )
45
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : Tuple=30 , a : Union[str, Any]=2 , a : List[str]=3 , a : Dict=True , a : List[str]=True , a : List[Any]=32 , a : List[str]=5 , a : Optional[int]=4 , a : List[str]=37 , a : Dict="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : int=10 , a : List[str]=0.02 , a : int=None , a : List[str]=2 , )-> Dict: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = is_training lowercase__ = use_labels lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = scope lowercase__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ = (image_size // patch_size) ** 2 lowercase__ = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any] , a : List[str] , a : Dict )-> Optional[Any]: """simple docstring""" lowercase__ = ViTModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Optional[Any] , a : int , a : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] , a : int , a : List[Any] )-> str: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : int = True _UpperCamelCase : int = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = ViTModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = ViTModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> str: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(a ) lowercase__ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass with torch.no_grad(): lowercase__ = model(a , interpolate_pos_encoding=a ) # verify the logits lowercase__ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , a ) lowercase__ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowercase__ = model(a )
45
1
import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class SCREAMING_SNAKE_CASE : def __init__( self : int , a : Tuple )-> Tuple: """simple docstring""" if isinstance(a , a ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden lowercase__ = deepcopy(a ) elif os.path.exists(a ): with io.open(a , 'r' , encoding='utf-8' ) as f: lowercase__ = json.load(a ) else: try: lowercase__ = baseaa.urlsafe_baadecode(a ).decode('utf-8' ) lowercase__ = json.loads(a ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""" ) lowercase__ = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = self.get_value('zero_optimization.stage' , -1 ) # offload lowercase__ = False if self.is_zeroa() or self.is_zeroa(): lowercase__ = set(['cpu', 'nvme'] ) lowercase__ = set( [ self.get_value('zero_optimization.offload_optimizer.device' ), self.get_value('zero_optimization.offload_param.device' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: lowercase__ = True def SCREAMING_SNAKE_CASE_ ( self : Dict , a : Dict )-> List[str]: """simple docstring""" lowercase__ = self.config # find the config node of interest if it exists lowercase__ = ds_key_long.split('.' ) lowercase__ = nodes.pop() for node in nodes: lowercase__ = config.get(a ) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : str , a : Dict , a : List[Any]=None )-> Tuple: """simple docstring""" lowercase__ , lowercase__ = self.find_config_node(a ) if config is None: return default return config.get(a , a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : int , a : Optional[Any]=False )-> int: """simple docstring""" lowercase__ = self.config # find the config node of interest if it exists lowercase__ = ds_key_long.split('.' ) for node in nodes: lowercase__ = config lowercase__ = config.get(a ) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""" ) else: return # if found remove it if parent_config is not None: parent_config.pop(a ) def SCREAMING_SNAKE_CASE_ ( self : Dict , a : Dict )-> List[str]: """simple docstring""" lowercase__ = self.get_value(a ) return False if value is None else bool(a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] )-> List[Any]: """simple docstring""" lowercase__ = self.get_value(a ) return False if value is None else not bool(a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[Any]: """simple docstring""" return self._offload class SCREAMING_SNAKE_CASE : def __init__( self : Tuple , a : Optional[int] )-> str: """simple docstring""" lowercase__ = engine def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : Dict , **a : List[str] )-> List[str]: """simple docstring""" self.engine.backward(a , **a ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[int] , a : str )-> Any: """simple docstring""" super().__init__(a , device_placement=a , scaler=a ) lowercase__ = hasattr(self.optimizer , 'overflow' ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : Optional[Any]=None )-> str: """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Any: """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : str , a : Optional[int] , a : Union[str, Any] )-> Tuple: """simple docstring""" super().__init__(a , a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> str: """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class SCREAMING_SNAKE_CASE : def __init__( self : Tuple , a : List[str] , a : Dict=0.001 , a : int=0 , **a : Any )-> Union[str, Any]: """simple docstring""" lowercase__ = params lowercase__ = lr lowercase__ = weight_decay lowercase__ = kwargs class SCREAMING_SNAKE_CASE : def __init__( self : str , a : List[str] , a : List[Any]=None , a : Dict=0 , **a : Union[str, Any] )-> Optional[Any]: """simple docstring""" lowercase__ = optimizer lowercase__ = total_num_steps lowercase__ = warmup_num_steps lowercase__ = kwargs
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: stooge(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return arr def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase__ , lowercase__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) # Recursively sort last 2/3 elements stooge(_SCREAMING_SNAKE_CASE , i + t , (_SCREAMING_SNAKE_CASE) ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
45
1
from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class SCREAMING_SNAKE_CASE : def __init__( self : Tuple , a : int , )-> List[Any]: """simple docstring""" lowercase__ = parent lowercase__ = 13 lowercase__ = 7 lowercase__ = 30 lowercase__ = self.seq_length + self.mem_len lowercase__ = 15 lowercase__ = True lowercase__ = True lowercase__ = 99 lowercase__ = [10, 50, 80] lowercase__ = 32 lowercase__ = 32 lowercase__ = 4 lowercase__ = 8 lowercase__ = 128 lowercase__ = 2 lowercase__ = 2 lowercase__ = None lowercase__ = 1 lowercase__ = 0 lowercase__ = 3 lowercase__ = self.vocab_size - 1 lowercase__ = 0.01 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> List[Any]: """simple docstring""" lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> str: """simple docstring""" random.seed(self.seed ) tf.random.set_seed(self.seed ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : Union[str, Any] , a : List[str] , a : Union[str, Any] , a : Any )-> Optional[int]: """simple docstring""" lowercase__ = TFTransfoXLModel(a ) lowercase__ , lowercase__ = model(a ).to_tuple() lowercase__ = {'input_ids': input_ids_a, 'mems': mems_a} lowercase__ , lowercase__ = model(a ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Union[str, Any] , a : Tuple , a : int , a : Tuple )-> str: """simple docstring""" lowercase__ = TFTransfoXLLMHeadModel(a ) lowercase__ , lowercase__ = model(a ).to_tuple() lowercase__ = {'input_ids': input_ids_a, 'labels': lm_labels} lowercase__ , lowercase__ = model(a ).to_tuple() lowercase__ , lowercase__ = model([input_ids_a, mems_a] ).to_tuple() lowercase__ = {'input_ids': input_ids_a, 'mems': mems_a, 'labels': lm_labels} lowercase__ , lowercase__ = model(a ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def SCREAMING_SNAKE_CASE_ ( self : str , a : Optional[Any] , a : Tuple , a : int , a : Dict )-> Dict: """simple docstring""" lowercase__ = TFTransfoXLForSequenceClassification(a ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ((lowercase__) , (lowercase__) , (lowercase__) , (lowercase__)) = config_and_inputs lowercase__ = {'input_ids': input_ids_a} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : int = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : List[str] = ( { 'feature-extraction': TFTransfoXLModel, 'text-classification': TFTransfoXLForSequenceClassification, 'text-generation': TFTransfoXLLMHeadModel, 'zero-shot': TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : Optional[int] = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Tuple = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : Any , a : Union[str, Any] , a : List[str] , a : Tuple , a : Dict )-> str: """simple docstring""" if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def SCREAMING_SNAKE_CASE_ ( self : int )-> Any: """simple docstring""" lowercase__ = TFTransfoXLModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , d_embed=37 ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> str: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Any )-> Dict: """simple docstring""" self.model_tester.set_seed() lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> List[str]: """simple docstring""" self.model_tester.set_seed() lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> str: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: lowercase__ = model_class(a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: lowercase__ = model.get_output_embeddings() assert isinstance(a , tf.keras.layers.Layer ) lowercase__ = model.get_bias() assert name is None else: lowercase__ = model.get_output_embeddings() assert x is None lowercase__ = model.get_bias() assert name is None def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Union[str, Any]: """simple docstring""" pass @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Any: """simple docstring""" for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = TFTransfoXLModel.from_pretrained(a ) self.assertIsNotNone(a ) @unittest.skip(reason='This model doesn\'t play well with fit() due to not returning a single loss.' ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> List[str]: """simple docstring""" pass @require_tf class SCREAMING_SNAKE_CASE (unittest.TestCase ): @unittest.skip('Skip test until #12651 is resolved.' ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[str]: """simple docstring""" lowercase__ = TFTransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103' ) # fmt: off lowercase__ = tf.convert_to_tensor([[33,1_297,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,22,1_706,17,20_098,5,3_215,21,37,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,6_224,831,16_002,2,8,603,78_967,29_546,23,803,20,25,416,5,8,232,4,277,6,1_855,4_601,3,29_546,54,8,3_609,5,57_211,49,4,1,277,18,8,1_755,15_691,3,341,25,416,693,42_573,71,17,401,94,31,17_919,2,29_546,7_873,18,1,435,23,11_011,755,5,5_167,3,7_983,98,84,2,29_546,3_267,8,3_609,4,1,4_865,1_075,2,6_087,71,6,346,8,5_854,3,29_546,824,1_400,1_868,2,19,160,2,311,8,5_496,2,20_920,17,25,15_097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off lowercase__ = [33,1_297,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,22,1_706,17,20_098,5,3_215,21,37,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,6_224,831,16_002,2,8,603,78_967,29_546,23,803,20,25,416,5,8,232,4,277,6,1_855,4_601,3,29_546,54,8,3_609,5,57_211,49,4,1,277,18,8,1_755,15_691,3,341,25,416,693,42_573,71,17,401,94,31,17_919,2,29_546,7_873,18,1,435,23,11_011,755,5,5_167,3,7_983,98,84,2,29_546,3_267,8,3_609,4,1,4_865,1_075,2,6_087,71,6,346,8,5_854,3,29_546,824,1_400,1_868,2,19,160,2,311,8,5_496,2,20_920,17,25,15_097,3,24,24,0,33,1,1_857,2,1,1_009,4,1_109,11_739,4_762,358,5,25,245,28,1_110,3,13,1_041,4,24,603,490,2,71_477,20_098,104_447,2,20_961,1,2_604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> lowercase__ = model.generate(a , max_length=200 , do_sample=a ) self.assertListEqual(output_ids[0].numpy().tolist() , a )
45
from scipy.stats import spearmanr import datasets lowercase_ = """ The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. """ lowercase_ = """ Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {'spearmanr': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results['spearmanr']) -0.7 >>> print(round(results['spearmanr_pvalue'], 2)) 0.19 """ lowercase_ = R"""\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE (datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'] , ) def SCREAMING_SNAKE_CASE_ ( self : int , a : str , a : Any , a : str=False )-> Optional[int]: """simple docstring""" lowercase__ = spearmanr(a , a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
45
1
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
45
1
from random import shuffle import tensorflow as tf from numpy import array def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: lowercase__ = int(_SCREAMING_SNAKE_CASE ) assert noofclusters < len(_SCREAMING_SNAKE_CASE ) # Find out the dimensionality lowercase__ = len(vectors[0] ) # Will help select random centroids from among the available vectors lowercase__ = list(range(len(_SCREAMING_SNAKE_CASE ) ) ) shuffle(_SCREAMING_SNAKE_CASE ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. lowercase__ = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION lowercase__ = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points lowercase__ = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_SCREAMING_SNAKE_CASE ) ] ##These nodes will assign the centroid Variables the appropriate ##values lowercase__ = tf.placeholder('float64' , [dim] ) lowercase__ = [] for centroid in centroids: cent_assigns.append(tf.assign(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) lowercase__ = [tf.Variable(0 ) for i in range(len(_SCREAMING_SNAKE_CASE ) )] ##These nodes will assign an assignment Variable the appropriate ##value lowercase__ = tf.placeholder('int32' ) lowercase__ = [] for assignment in assignments: cluster_assigns.append(tf.assign(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input lowercase__ = tf.placeholder('float' , [None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors lowercase__ = tf.reduce_mean(_SCREAMING_SNAKE_CASE , 0 ) ##Node for computing Euclidean distances # Placeholders for input lowercase__ = tf.placeholder('float' , [dim] ) lowercase__ = tf.placeholder('float' , [dim] ) lowercase__ = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , 2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input lowercase__ = tf.placeholder('float' , [noofclusters] ) lowercase__ = tf.argmin(_SCREAMING_SNAKE_CASE , 0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. lowercase__ = tf.initialize_all_variables() # Initialize all variables sess.run(_SCREAMING_SNAKE_CASE ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. lowercase__ = 100 for _ in range(_SCREAMING_SNAKE_CASE ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. lowercase__ = [ sess.run(_SCREAMING_SNAKE_CASE , feed_dict={va: vect, va: sess.run(_SCREAMING_SNAKE_CASE )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input lowercase__ = sess.run( _SCREAMING_SNAKE_CASE , feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] , feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_SCREAMING_SNAKE_CASE ): # Collect all the vectors assigned to this cluster lowercase__ = [ vectors[i] for i in range(len(_SCREAMING_SNAKE_CASE ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location lowercase__ = sess.run( _SCREAMING_SNAKE_CASE , feed_dict={mean_input: array(_SCREAMING_SNAKE_CASE )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] , feed_dict={centroid_value: new_location} ) # Return centroids and assignments lowercase__ = sess.run(_SCREAMING_SNAKE_CASE ) lowercase__ = sess.run(_SCREAMING_SNAKE_CASE ) return centroids, assignments
45
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } lowercase_ = { """b0""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 224, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 240, """dropout_rate""": 0.2, """dw_padding""": [16], }, """b2""": { """hidden_dim""": 1_408, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 260, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 16], }, """b3""": { """hidden_dim""": 1_536, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 300, """dropout_rate""": 0.3, """dw_padding""": [5, 18], }, """b4""": { """hidden_dim""": 1_792, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 380, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_048, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 456, """dropout_rate""": 0.4, """dw_padding""": [13, 27], }, """b6""": { """hidden_dim""": 2_304, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 528, """dropout_rate""": 0.5, """dw_padding""": [31], }, """b7""": { """hidden_dim""": 2_560, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 600, """dropout_rate""": 0.5, """dw_padding""": [18], }, } def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = EfficientNetConfig() lowercase__ = CONFIG_MAP[model_name]['hidden_dim'] lowercase__ = CONFIG_MAP[model_name]['width_coef'] lowercase__ = CONFIG_MAP[model_name]['depth_coef'] lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = CONFIG_MAP[model_name]['dropout_rate'] lowercase__ = CONFIG_MAP[model_name]['dw_padding'] lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = 1000 lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} return config def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = EfficientNetImageProcessor( size={'height': size, 'width': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=_SCREAMING_SNAKE_CASE , ) return preprocessor def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = [v.split('_' )[0].split('block' )[1] for v in original_param_names if v.startswith('block' )] lowercase__ = sorted(set(_SCREAMING_SNAKE_CASE ) ) lowercase__ = len(_SCREAMING_SNAKE_CASE ) lowercase__ = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} lowercase__ = [] rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight') ) rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight') ) rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias') ) rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean') ) rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var') ) for b in block_names: lowercase__ = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight') ) rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight') ) rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias') ) rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean') ) rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var') ) lowercase__ = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ = 'efficientnet.' + item[1] lowercase__ = 'classifier.weight' lowercase__ = 'classifier.bias' return key_mapping def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights='imagenet' , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=1000 , classifier_activation='softmax' , ) lowercase__ = original_model.trainable_variables lowercase__ = original_model.non_trainable_variables lowercase__ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ = param.numpy() lowercase__ = list(tf_params.keys() ) # Load HuggingFace model lowercase__ = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) lowercase__ = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() lowercase__ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('Converting parameters...' ) lowercase__ = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowercase__ = convert_image_processor(_SCREAMING_SNAKE_CASE ) lowercase__ = preprocessor(images=prepare_img() , return_tensors='pt' ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ = hf_model(**_SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits.detach().numpy() # Original model inference lowercase__ = False lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ = image.img_to_array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) lowercase__ = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print('Model outputs match!' ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") lowercase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: return len(set(_SCREAMING_SNAKE_CASE ) ) == len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
import argparse import json import subprocess def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = [] lowercase__ = ( F"""curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"""" ' https://api.github.com/repos/huggingface/transformers/actions/runners' ) lowercase__ = subprocess.run(_SCREAMING_SNAKE_CASE , shell=_SCREAMING_SNAKE_CASE , stdout=subprocess.PIPE ) lowercase__ = output.stdout.decode('utf-8' ) lowercase__ = json.loads(_SCREAMING_SNAKE_CASE ) lowercase__ = status['runners'] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_SCREAMING_SNAKE_CASE ) # save the result so we can report them on Slack with open('offline_runners.txt' , 'w' ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) if len(_SCREAMING_SNAKE_CASE ) > 0: lowercase__ = '\n'.join([x['name'] for x in offline_runners] ) raise ValueError(F"""The following runners are offline:\n{failed}""" ) if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return values.split(',' ) lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--target_runners""", default=None, type=list_str, required=True, help="""Comma-separated list of runners to check status.""", ) parser.add_argument( """--token""", default=None, type=str, required=True, help="""A token that has actions:read permission.""" ) lowercase_ = parser.parse_args() get_runner_status(args.target_runners, args.token)
45
1
import argparse import torch from huggingface_hub import hf_hub_download from transformers import AutoTokenizer, RobertaPreLayerNormConfig, RobertaPreLayerNormForMaskedLM from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: lowercase__ = RobertaPreLayerNormConfig.from_pretrained( _SCREAMING_SNAKE_CASE , architectures=['RobertaPreLayerNormForMaskedLM'] ) # convert state_dict lowercase__ = torch.load(hf_hub_download(repo_id=_SCREAMING_SNAKE_CASE , filename='pytorch_model.bin' ) ) lowercase__ = {} for tensor_key, tensor_value in original_state_dict.items(): # The transformer implementation gives the model a unique name, rather than overwiriting 'roberta' if tensor_key.startswith('roberta.' ): lowercase__ = 'roberta_prelayernorm.' + tensor_key[len('roberta.' ) :] # The original implementation contains weights which are not used, remove them from the state_dict if tensor_key.endswith('.self.LayerNorm.weight' ) or tensor_key.endswith('.self.LayerNorm.bias' ): continue lowercase__ = tensor_value lowercase__ = RobertaPreLayerNormForMaskedLM.from_pretrained( pretrained_model_name_or_path=_SCREAMING_SNAKE_CASE , config=_SCREAMING_SNAKE_CASE , state_dict=_SCREAMING_SNAKE_CASE ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) # convert tokenizer lowercase__ = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint-repo""", default=None, type=str, required=True, help="""Path the official PyTorch dump, e.g. 'andreasmadsen/efficient_mlm_m0.40'.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) lowercase_ = parser.parse_args() convert_roberta_prelayernorm_checkpoint_to_pytorch(args.checkpoint_repo, args.pytorch_dump_folder_path)
45
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'ClapFeatureExtractor' _UpperCamelCase : Union[str, Any] = ('RobertaTokenizer', 'RobertaTokenizerFast') def __init__( self : List[Any] , a : int , a : str )-> Any: """simple docstring""" super().__init__(a , a ) def __call__( self : Any , a : Tuple=None , a : Optional[int]=None , a : int=None , **a : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = kwargs.pop('sampling_rate' , a ) if text is None and audios is None: raise ValueError('You have to specify either text or audios. Both cannot be none.' ) if text is not None: lowercase__ = self.tokenizer(a , return_tensors=a , **a ) if audios is not None: lowercase__ = self.feature_extractor( a , sampling_rate=a , return_tensors=a , **a ) if text is not None and audios is not None: lowercase__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : str , *a : Dict , **a : int )-> Optional[int]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *a : int , **a : Dict )-> Dict: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer.model_input_names lowercase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
45
1
from __future__ import annotations lowercase_ = 8.988E9 # units = N * m^s * C^-2 def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> dict[str, float]: lowercase__ = abs(chargea * chargea ) if (force, chargea, chargea, distance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if distance < 0: raise ValueError('Distance cannot be negative' ) if force == 0: lowercase__ = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: lowercase__ = abs(_SCREAMING_SNAKE_CASE ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: lowercase__ = abs(_SCREAMING_SNAKE_CASE ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: lowercase__ = (COULOMBS_CONSTANT * charge_product / abs(_SCREAMING_SNAKE_CASE )) ** 0.5 return {"distance": distance} raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} lowercase_ = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json""" ), }, } lowercase_ = { """moussaKam/mbarthez""": 1_024, """moussaKam/barthez""": 1_024, """moussaKam/barthez-orangesum-title""": 1_024, } lowercase_ = """▁""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = VOCAB_FILES_NAMES _UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Optional[Any] = ['input_ids', 'attention_mask'] _UpperCamelCase : int = BarthezTokenizer def __init__( self : List[Any] , a : Union[str, Any]=None , a : Optional[Any]=None , a : Dict="<s>" , a : Union[str, Any]="</s>" , a : List[str]="</s>" , a : Optional[Any]="<s>" , a : int="<unk>" , a : str="<pad>" , a : Optional[int]="<mask>" , **a : Union[str, Any] , )-> Tuple: """simple docstring""" lowercase__ = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) lowercase__ = vocab_file lowercase__ = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__ = [self.cls_token_id] lowercase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" lowercase__ = [self.sep_token_id] lowercase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE_ ( self : Dict , a : str , a : Optional[str] = None )-> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ = os.path.join( a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: return str(_SCREAMING_SNAKE_CASE ) == str(_SCREAMING_SNAKE_CASE )[::-1] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: return int(_SCREAMING_SNAKE_CASE ) + int(str(_SCREAMING_SNAKE_CASE )[::-1] ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 10000 ) -> int: lowercase__ = [] for num in range(1 , _SCREAMING_SNAKE_CASE ): lowercase__ = 0 lowercase__ = num while iterations < 50: lowercase__ = sum_reverse(_SCREAMING_SNAKE_CASE ) iterations += 1 if is_palindrome(_SCREAMING_SNAKE_CASE ): break else: lychrel_nums.append(_SCREAMING_SNAKE_CASE ) return len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(f'''{solution() = }''')
45
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available lowercase_ = { """configuration_audio_spectrogram_transformer""": [ """AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ASTConfig""", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """ASTForAudioClassification""", """ASTModel""", """ASTPreTrainedModel""", ] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ASTFeatureExtractor"""] if TYPE_CHECKING: from .configuration_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ASTConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ASTForAudioClassification, ASTModel, ASTPreTrainedModel, ) try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
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 lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Any = 'deit' def __init__( self : Any , a : Union[str, Any]=768 , a : Optional[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Optional[int]="gelu" , a : Optional[Any]=0.0 , a : List[Any]=0.0 , a : int=0.02 , a : List[str]=1E-1_2 , a : Optional[int]=224 , a : Tuple=16 , a : List[Any]=3 , a : List[str]=True , a : Any=16 , **a : Union[str, Any] , )-> int: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = qkv_bias lowercase__ = encoder_stride class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : int )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-4
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: lowercase__ = len(_SCREAMING_SNAKE_CASE ) print('The following activities are selected:' ) # The first activity is always selected lowercase__ = 0 print(_SCREAMING_SNAKE_CASE , end=',' ) # Consider rest of the activities for j in range(_SCREAMING_SNAKE_CASE ): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: print(_SCREAMING_SNAKE_CASE , end=',' ) lowercase__ = j if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = [1, 3, 0, 5, 8, 5] lowercase_ = [2, 4, 6, 7, 9, 9] print_max_activities(start, finish)
45
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"""configuration_mmbt""": ["""MMBTConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""MMBTForClassification""", """MMBTModel""", """ModalEmbeddings"""] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""NllbTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""NllbTokenizerFast"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures lowercase_ = logging.get_logger(__name__) @dataclass class SCREAMING_SNAKE_CASE : _UpperCamelCase : str = field(metadata={'help': 'The name of the task to train on: ' + ', '.join(glue_processors.keys() )} ) _UpperCamelCase : str = field( metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'} ) _UpperCamelCase : int = field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _UpperCamelCase : bool = field( default=UpperCAmelCase , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" lowercase__ = self.task_name.lower() class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'train' _UpperCamelCase : Any = 'dev' _UpperCamelCase : List[str] = 'test' class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : GlueDataTrainingArguments _UpperCamelCase : str _UpperCamelCase : List[InputFeatures] def __init__( self : List[Any] , a : GlueDataTrainingArguments , a : PreTrainedTokenizerBase , a : Optional[int] = None , a : Union[str, Split] = Split.train , a : Optional[str] = None , )-> Union[str, Any]: """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , a , ) lowercase__ = args lowercase__ = glue_processors[args.task_name]() lowercase__ = glue_output_modes[args.task_name] if isinstance(a , a ): try: lowercase__ = Split[mode] except KeyError: raise KeyError('mode is not a valid split name' ) # Load data features from cache or dataset file lowercase__ = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) lowercase__ = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) lowercase__ , lowercase__ = label_list[2], label_list[1] lowercase__ = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowercase__ = cached_features_file + '.lock' with FileLock(a ): if os.path.exists(a ) and not args.overwrite_cache: lowercase__ = time.time() lowercase__ = torch.load(a ) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start ) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""" ) if mode == Split.dev: lowercase__ = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: lowercase__ = self.processor.get_test_examples(args.data_dir ) else: lowercase__ = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: lowercase__ = examples[:limit_length] lowercase__ = glue_convert_examples_to_features( a , a , max_length=args.max_seq_length , label_list=a , output_mode=self.output_mode , ) lowercase__ = time.time() torch.save(self.features , a ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""" ) def __len__( self : Dict )-> Optional[int]: """simple docstring""" return len(self.features ) def __getitem__( self : Union[str, Any] , a : Optional[int] )-> InputFeatures: """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" return self.label_list
45
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
1
from __future__ import annotations import csv import requests from bsa import BeautifulSoup def __UpperCamelCase (_SCREAMING_SNAKE_CASE = "" ) -> dict[str, float]: lowercase__ = url or 'https://www.imdb.com/chart/top/?ref_=nv_mv_250' lowercase__ = BeautifulSoup(requests.get(_SCREAMING_SNAKE_CASE ).text , 'html.parser' ) lowercase__ = soup.find_all('td' , attrs='titleColumn' ) lowercase__ = soup.find_all('td' , class_='ratingColumn imdbRating' ) return { title.a.text: float(rating.strong.text ) for title, rating in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) } def __UpperCamelCase (_SCREAMING_SNAKE_CASE = "IMDb_Top_250_Movies.csv" ) -> None: lowercase__ = get_imdb_top_aaa_movies() with open(_SCREAMING_SNAKE_CASE , 'w' , newline='' ) as out_file: lowercase__ = csv.writer(_SCREAMING_SNAKE_CASE ) writer.writerow(['Movie title', 'IMDb rating'] ) for title, rating in movies.items(): writer.writerow([title, rating] ) if __name__ == "__main__": write_movies()
45
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
1
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if num < 0: return False lowercase__ = num lowercase__ = 0 while num > 0: lowercase__ = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[Any] , a : UNetaDModel , a : UNetaDModel , a : DDPMScheduler , a : Any , )-> Dict: """simple docstring""" super().__init__() lowercase__ = value_function lowercase__ = unet lowercase__ = scheduler lowercase__ = env lowercase__ = env.get_dataset() lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].mean() except: # noqa: E722 pass lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].std() except: # noqa: E722 pass lowercase__ = env.observation_space.shape[0] lowercase__ = env.action_space.shape[0] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Any , a : int )-> Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : str , a : List[str] )-> str: """simple docstring""" return x_in * self.stds[key] + self.means[key] def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : Tuple )-> Tuple: """simple docstring""" if type(a ) is dict: return {k: self.to_torch(a ) for k, v in x_in.items()} elif torch.is_tensor(a ): return x_in.to(self.unet.device ) return torch.tensor(a , device=self.unet.device ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[int] , a : Dict , a : Optional[Any] )-> List[Any]: """simple docstring""" for key, val in cond.items(): lowercase__ = val.clone() return x_in def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[Any] , a : Any , a : Optional[Any] , a : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = x.shape[0] lowercase__ = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model lowercase__ = torch.full((batch_size,) , a , device=self.unet.device , dtype=torch.long ) for _ in range(a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models lowercase__ = self.value_function(x.permute(0 , 2 , 1 ) , a ).sample lowercase__ = torch.autograd.grad([y.sum()] , [x] )[0] lowercase__ = self.scheduler._get_variance(a ) lowercase__ = torch.exp(0.5 * posterior_variance ) lowercase__ = model_std * grad lowercase__ = 0 lowercase__ = x.detach() lowercase__ = x + scale * grad lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.unet(x.permute(0 , 2 , 1 ) , a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg lowercase__ = self.scheduler.step(a , a , a , predict_epsilon=a )['prev_sample'] # apply conditions to the trajectory (set the initial state) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) return x, y def __call__( self : Any , a : Tuple , a : int=64 , a : Tuple=32 , a : List[Any]=2 , a : List[str]=0.1 )-> List[Any]: """simple docstring""" lowercase__ = self.normalize(a , 'observations' ) lowercase__ = obs[None].repeat(a , axis=0 ) lowercase__ = {0: self.to_torch(a )} lowercase__ = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) lowercase__ = randn_tensor(a , device=self.unet.device ) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) # run the diffusion process lowercase__ , lowercase__ = self.run_diffusion(a , a , a , a ) # sort output trajectories by value lowercase__ = y.argsort(0 , descending=a ).squeeze() lowercase__ = x[sorted_idx] lowercase__ = sorted_values[:, :, : self.action_dim] lowercase__ = actions.detach().cpu().numpy() lowercase__ = self.de_normalize(a , key='actions' ) # select the action with the highest value if y is not None: lowercase__ = 0 else: # if we didn't run value guiding, select a random action lowercase__ = np.random.randint(0 , a ) lowercase__ = denorm_actions[selected_index, 0] return denorm_actions
45
1
# Author: OMKAR PATHAK, Nwachukwu Chidiebere # Use a Python dictionary to construct the graph. from __future__ import annotations from pprint import pformat from typing import Generic, TypeVar lowercase_ = TypeVar("""T""") class SCREAMING_SNAKE_CASE (Generic[T] ): def __init__( self : Any , a : bool = True )-> None: """simple docstring""" lowercase__ = {} # dictionary of lists lowercase__ = directed def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : T , a : T )-> GraphAdjacencyList[T]: """simple docstring""" if not self.directed: # For undirected graphs # if both source vertex and destination vertex are both present in the # adjacency list, add destination vertex to source vertex list of adjacent # vertices and add source vertex to destination vertex list of adjacent # vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(a ) self.adj_list[destination_vertex].append(a ) # if only source vertex is present in adjacency list, add destination vertex # to source vertex list of adjacent vertices, then create a new vertex with # destination vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(a ) lowercase__ = [source_vertex] # if only destination vertex is present in adjacency list, add source vertex # to destination vertex list of adjacent vertices, then create a new vertex # with source vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif destination_vertex in self.adj_list: self.adj_list[destination_vertex].append(a ) lowercase__ = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and assign a list # containing the destination vertex as it's first adjacent vertex also # create a new vertex with destination vertex as key and assign a list # containing the source vertex as it's first adjacent vertex. else: lowercase__ = [destination_vertex] lowercase__ = [source_vertex] else: # For directed graphs # if both source vertex and destination vertex are present in adjacency # list, add destination vertex to source vertex list of adjacent vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(a ) # if only source vertex is present in adjacency list, add destination # vertex to source vertex list of adjacent vertices and create a new vertex # with destination vertex as key, which has no adjacent vertex elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(a ) lowercase__ = [] # if only destination vertex is present in adjacency list, create a new # vertex with source vertex as key and assign a list containing destination # vertex as first adjacent vertex elif destination_vertex in self.adj_list: lowercase__ = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and a list containing # destination vertex as it's first adjacent vertex. Then create a new vertex # with destination vertex as key, which has no adjacent vertex else: lowercase__ = [destination_vertex] lowercase__ = [] return self def __repr__( self : List[Any] )-> str: """simple docstring""" return pformat(self.adj_list )
45
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : Any , a : str , a : List[Any]=7 , a : int=3 , a : int=18 , a : Optional[Any]=30 , a : Optional[int]=400 , a : int=True , a : Tuple=None , a : Optional[Any]=True , a : str=False , a : str=True , a : int=True , a : Tuple=[0.5, 0.5, 0.5] , a : Any=[0.5, 0.5, 0.5] , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size if size is not None else {'height': 18, 'width': 20} lowercase__ = do_thumbnail lowercase__ = do_align_axis lowercase__ = do_pad lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = DonutImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) self.assertTrue(hasattr(a , 'do_thumbnail' ) ) self.assertTrue(hasattr(a , 'do_align_long_axis' ) ) self.assertTrue(hasattr(a , 'do_pad' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" pass @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , numpify=a ) for image in image_inputs: self.assertIsInstance(a , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , torchify=a ) for image in image_inputs: self.assertIsInstance(a , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: lowercase__ = 0.0_0 lowercase__ = 0 for resistor in resistors: if resistor <= 0: lowercase__ = F"""Resistor at index {index} has a negative or zero value!""" raise ValueError(_SCREAMING_SNAKE_CASE ) first_sum += 1 / float(_SCREAMING_SNAKE_CASE ) index += 1 return 1 / first_sum def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: lowercase__ = 0.0_0 lowercase__ = 0 for resistor in resistors: sum_r += resistor if resistor < 0: lowercase__ = F"""Resistor at index {index} has a negative value!""" raise ValueError(_SCREAMING_SNAKE_CASE ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
45
import math def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(_SCREAMING_SNAKE_CASE ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowercase_ = """Enter the base and the power separated by a comma: """ lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. lowercase_ = res(xa, ya) lowercase_ = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
45
1
lowercase_ = range(2, 20 + 1) lowercase_ = [10**k for k in range(ks[-1] + 1)] lowercase_ = {} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: lowercase__ = sum(a_i[j] for j in range(_SCREAMING_SNAKE_CASE , len(_SCREAMING_SNAKE_CASE ) ) ) lowercase__ = sum(a_i[j] * base[j] for j in range(min(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) ) ) lowercase__ , lowercase__ = 0, 0 lowercase__ = n - i lowercase__ = memo.get(_SCREAMING_SNAKE_CASE ) if sub_memo is not None: lowercase__ = sub_memo.get(_SCREAMING_SNAKE_CASE ) if jumps is not None and len(_SCREAMING_SNAKE_CASE ) > 0: # find and make the largest jump without going over lowercase__ = -1 for _k in range(len(_SCREAMING_SNAKE_CASE ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: lowercase__ = _k break if max_jump >= 0: lowercase__ , lowercase__ , lowercase__ = jumps[max_jump] # since the difference between jumps is cached, add c lowercase__ = diff + c for j in range(min(_SCREAMING_SNAKE_CASE , len(_SCREAMING_SNAKE_CASE ) ) ): lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , 10 ) if new_c > 0: add(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else: lowercase__ = [] else: lowercase__ = {c: []} lowercase__ = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps lowercase__ , lowercase__ = next_term(_SCREAMING_SNAKE_CASE , k - 1 , i + dn , _SCREAMING_SNAKE_CASE ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead lowercase__ , lowercase__ = compute(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , i + dn , _SCREAMING_SNAKE_CASE ) diff += _diff dn += terms_jumped lowercase__ = sub_memo[c] # keep jumps sorted by # of terms skipped lowercase__ = 0 while j < len(_SCREAMING_SNAKE_CASE ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(_SCREAMING_SNAKE_CASE , (diff, dn, k) ) return (diff, dn) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: if i >= n: return 0, i if k > len(_SCREAMING_SNAKE_CASE ): a_i.extend([0 for _ in range(k - len(_SCREAMING_SNAKE_CASE ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) lowercase__ = i lowercase__ , lowercase__ , lowercase__ = 0, 0, 0 for j in range(len(_SCREAMING_SNAKE_CASE ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 lowercase__ = ds_c + ds_b diff += addend lowercase__ = 0 for j in range(_SCREAMING_SNAKE_CASE ): lowercase__ = a_i[j] + addend lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return diff, i - start_i def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: for j in range(_SCREAMING_SNAKE_CASE , len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = digits[j] + addend if s >= 10: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , 10 ) lowercase__ = addend // 10 + quotient else: lowercase__ = s lowercase__ = addend // 10 if addend == 0: break while addend > 0: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , 10 ) digits.append(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 10**15 ) -> int: lowercase__ = [1] lowercase__ = 1 lowercase__ = 0 while True: lowercase__ , lowercase__ = next_term(_SCREAMING_SNAKE_CASE , 20 , i + dn , _SCREAMING_SNAKE_CASE ) dn += terms_jumped if dn == n - i: break lowercase__ = 0 for j in range(len(_SCREAMING_SNAKE_CASE ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f'''{solution() = }''')
45
class SCREAMING_SNAKE_CASE : # Public class to implement a graph def __init__( self : int , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = row lowercase__ = col lowercase__ = graph def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int , a : list[list[bool]] )-> bool: """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order lowercase__ = [-1, 0, 1, -1, 1, -1, 0, 1] lowercase__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , a ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: # And finally, count all islands. """simple docstring""" lowercase__ = [[False for j in range(self.COL )] for i in range(self.ROW )] lowercase__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(a , a , a ) count += 1 return count
45
1
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
from string import ascii_uppercase lowercase_ = {str(ord(c) - 55): c for c in ascii_uppercase} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: 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 > 36: raise ValueError('base must be <= 36' ) lowercase__ = '' lowercase__ = 0 lowercase__ = 0 while div != 1: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if base >= 11 and 9 < mod < 36: lowercase__ = ALPHABET_VALUES[str(_SCREAMING_SNAKE_CASE )] else: lowercase__ = str(_SCREAMING_SNAKE_CASE ) new_value += actual_value lowercase__ = num // base lowercase__ = 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), )
45
1
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) lowercase_ = """bert-base-cased""" lowercase_ = """fp16""" lowercase_ = """bf16""" lowercase_ = [FPaa, BFaa] @require_fsdp @require_cuda class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Optional[int]: """simple docstring""" super().setUp() lowercase__ = dict( ACCELERATE_USE_FSDP='true' , MASTER_ADDR='localhost' , MASTER_PORT='10999' , RANK='0' , LOCAL_RANK='0' , WORLD_SIZE='1' , ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Dict: """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy for i, strategy in enumerate(a ): lowercase__ = self.dist_env.copy() lowercase__ = f"""{i + 1}""" lowercase__ = strategy with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.sharding_strategy , ShardingStrategy(i + 1 ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> int: """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch for i, prefetch_policy in enumerate(a ): lowercase__ = self.dist_env.copy() lowercase__ = prefetch_policy with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() if prefetch_policy == "NO_PREFETCH": self.assertIsNone(fsdp_plugin.backward_prefetch ) else: self.assertEqual(fsdp_plugin.backward_prefetch , BackwardPrefetch(i + 1 ) ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Union[str, Any]: """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType for i, state_dict_type in enumerate(a ): lowercase__ = self.dist_env.copy() lowercase__ = state_dict_type with mockenv_context(**a ): lowercase__ = 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 SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" lowercase__ = AutoModel.from_pretrained(a ) for policy in FSDP_AUTO_WRAP_POLICY: lowercase__ = self.dist_env.copy() lowercase__ = policy if policy == "TRANSFORMER_BASED_WRAP": lowercase__ = 'BertLayer' elif policy == "SIZE_BASED_WRAP": lowercase__ = '2000' with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(a ) if policy == "NO_WRAP": self.assertIsNone(fsdp_plugin.auto_wrap_policy ) else: self.assertIsNotNone(fsdp_plugin.auto_wrap_policy ) lowercase__ = self.dist_env.copy() lowercase__ = 'TRANSFORMER_BASED_WRAP' lowercase__ = 'T5Layer' with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() with self.assertRaises(a ) as cm: fsdp_plugin.set_auto_wrap_policy(a ) self.assertTrue('Could not find the transformer layer class to wrap in the model.' in str(cm.exception ) ) lowercase__ = self.dist_env.copy() lowercase__ = 'SIZE_BASED_WRAP' lowercase__ = '0' with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(a ) self.assertIsNone(fsdp_plugin.auto_wrap_policy ) def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler for mp_dtype in dtypes: lowercase__ = self.dist_env.copy() lowercase__ = mp_dtype with mockenv_context(**a ): lowercase__ = Accelerator() if mp_dtype == "fp16": lowercase__ = torch.floataa elif mp_dtype == "bf16": lowercase__ = torch.bfloataa lowercase__ = MixedPrecision(param_dtype=a , reduce_dtype=a , buffer_dtype=a ) self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy , a ) if mp_dtype == FPaa: self.assertTrue(isinstance(accelerator.scaler , a ) ) elif mp_dtype == BFaa: self.assertIsNone(accelerator.scaler ) AcceleratorState._reset_state(a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload for flag in [True, False]: lowercase__ = self.dist_env.copy() lowercase__ = str(a ).lower() with mockenv_context(**a ): lowercase__ = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.cpu_offload , CPUOffload(offload_params=a ) ) @require_fsdp @require_multi_gpu @slow class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Optional[Any]: """simple docstring""" super().setUp() lowercase__ = 0.82 lowercase__ = [ 'fsdp_shard_grad_op_transformer_based_wrap', 'fsdp_full_shard_transformer_based_wrap', ] lowercase__ = { 'multi_gpu_fp16': 3_200, 'fsdp_shard_grad_op_transformer_based_wrap_fp16': 2_000, 'fsdp_full_shard_transformer_based_wrap_fp16': 1_900, # 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 } lowercase__ = 160 lowercase__ = 160 lowercase__ = inspect.getfile(accelerate.test_utils ) lowercase__ = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['scripts', 'external_deps'] ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> int: """simple docstring""" lowercase__ = os.path.join(self.test_scripts_folder , 'test_performance.py' ) lowercase__ = ['accelerate', 'launch', '--num_processes=2', '--num_machines=1', '--machine_rank=0', '--use_fsdp'] for config in self.performance_configs: lowercase__ = cmd.copy() for i, strategy in enumerate(a ): 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(a , env=os.environ.copy() ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Dict: """simple docstring""" lowercase__ = os.path.join(self.test_scripts_folder , 'test_checkpointing.py' ) lowercase__ = [ '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(a ): lowercase__ = cmd.copy() cmd_config.append(f"""--fsdp_sharding_strategy={i+1}""" ) if strategy != "FULL_SHARD": continue lowercase__ = len(a ) for state_dict_type in FSDP_STATE_DICT_TYPE: lowercase__ = 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(a , env=os.environ.copy() ) lowercase__ = cmd_config[:-1] lowercase__ = 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(a , env=os.environ.copy() ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> List[str]: """simple docstring""" lowercase__ = os.path.join(self.test_scripts_folder , 'test_peak_memory_usage.py' ) lowercase__ = [ '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(): lowercase__ = 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(a ): 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(a , env=os.environ.copy() )
45
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : Tuple=30 , a : Union[str, Any]=2 , a : List[str]=3 , a : Dict=True , a : List[str]=True , a : List[Any]=32 , a : List[str]=5 , a : Optional[int]=4 , a : List[str]=37 , a : Dict="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : int=10 , a : List[str]=0.02 , a : int=None , a : List[str]=2 , )-> Dict: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = is_training lowercase__ = use_labels lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = scope lowercase__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ = (image_size // patch_size) ** 2 lowercase__ = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any] , a : List[str] , a : Dict )-> Optional[Any]: """simple docstring""" lowercase__ = ViTModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Optional[Any] , a : int , a : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] , a : int , a : List[Any] )-> str: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : int = True _UpperCamelCase : int = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = ViTModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = ViTModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> str: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(a ) lowercase__ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass with torch.no_grad(): lowercase__ = model(a , interpolate_pos_encoding=a ) # verify the logits lowercase__ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , a ) lowercase__ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowercase__ = model(a )
45
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : Tuple=30 , a : Union[str, Any]=2 , a : List[str]=3 , a : Dict=True , a : List[str]=True , a : List[Any]=32 , a : List[str]=5 , a : Optional[int]=4 , a : List[str]=37 , a : Dict="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : int=10 , a : List[str]=0.02 , a : int=None , a : List[str]=2 , )-> Dict: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = is_training lowercase__ = use_labels lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = scope lowercase__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ = (image_size // patch_size) ** 2 lowercase__ = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any] , a : List[str] , a : Dict )-> Optional[Any]: """simple docstring""" lowercase__ = ViTModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Optional[Any] , a : int , a : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] , a : int , a : List[Any] )-> str: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : int = True _UpperCamelCase : int = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = ViTModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = ViTModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> str: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(a ) lowercase__ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass with torch.no_grad(): lowercase__ = model(a , interpolate_pos_encoding=a ) # verify the logits lowercase__ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , a ) lowercase__ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowercase__ = model(a )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: stooge(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return arr def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase__ , lowercase__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) # Recursively sort last 2/3 elements stooge(_SCREAMING_SNAKE_CASE , i + t , (_SCREAMING_SNAKE_CASE) ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
45
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Union[str, Any] = 'sew-d' def __init__( self : Any , a : str=32 , a : Optional[int]=768 , a : List[str]=12 , a : Optional[int]=12 , a : Tuple=3_072 , a : Optional[Any]=2 , a : Any=512 , a : List[str]=256 , a : int=True , a : int=True , a : List[Any]=("p2c", "c2p") , a : Any="layer_norm" , a : str="gelu_python" , a : Optional[Any]=0.1 , a : str=0.1 , a : Any=0.1 , a : Tuple=0.0 , a : List[Any]=0.1 , a : List[str]=0.02 , a : Optional[Any]=1E-7 , a : List[Any]=1E-5 , a : Optional[int]="group" , a : Tuple="gelu" , a : List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , a : Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , a : str=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , a : List[Any]=False , a : Optional[int]=128 , a : Tuple=16 , a : Tuple=True , a : Optional[Any]=0.05 , a : List[Any]=10 , a : Dict=2 , a : Optional[Any]=0.0 , a : Optional[Any]=10 , a : Any=0 , a : Union[str, Any]="mean" , a : List[str]=False , a : str=False , a : Union[str, Any]=256 , a : int=0 , a : Optional[Any]=1 , a : Tuple=2 , **a : List[Any] , )-> Optional[Any]: """simple docstring""" super().__init__(**a , pad_token_id=a , bos_token_id=a , eos_token_id=a ) lowercase__ = hidden_size lowercase__ = feat_extract_norm lowercase__ = feat_extract_activation lowercase__ = list(a ) lowercase__ = list(a ) lowercase__ = list(a ) lowercase__ = conv_bias lowercase__ = num_conv_pos_embeddings lowercase__ = num_conv_pos_embedding_groups lowercase__ = len(self.conv_dim ) lowercase__ = num_hidden_layers lowercase__ = intermediate_size lowercase__ = squeeze_factor lowercase__ = max_position_embeddings lowercase__ = position_buckets lowercase__ = share_att_key lowercase__ = relative_attention lowercase__ = norm_rel_ebd lowercase__ = list(a ) lowercase__ = hidden_act lowercase__ = num_attention_heads lowercase__ = hidden_dropout lowercase__ = attention_dropout lowercase__ = activation_dropout lowercase__ = feat_proj_dropout lowercase__ = final_dropout lowercase__ = layer_norm_eps lowercase__ = feature_layer_norm_eps lowercase__ = initializer_range lowercase__ = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect.' 'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,' f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowercase__ = apply_spec_augment lowercase__ = mask_time_prob lowercase__ = mask_time_length lowercase__ = mask_time_min_masks lowercase__ = mask_feature_prob lowercase__ = mask_feature_length lowercase__ = mask_feature_min_masks # ctc loss lowercase__ = ctc_loss_reduction lowercase__ = ctc_zero_infinity # sequence classification lowercase__ = use_weighted_layer_sum lowercase__ = classifier_proj_size @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[Any]: """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
45
from scipy.stats import spearmanr import datasets lowercase_ = """ The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. """ lowercase_ = """ Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {'spearmanr': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results['spearmanr']) -0.7 >>> print(round(results['spearmanr_pvalue'], 2)) 0.19 """ lowercase_ = R"""\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE (datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'] , ) def SCREAMING_SNAKE_CASE_ ( self : int , a : str , a : Any , a : str=False )-> Optional[int]: """simple docstring""" lowercase__ = spearmanr(a , a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[str]: if nth_term == "": return [""] lowercase__ = int(_SCREAMING_SNAKE_CASE ) lowercase__ = int(_SCREAMING_SNAKE_CASE ) lowercase__ = [] for temp in range(int(_SCREAMING_SNAKE_CASE ) ): series.append(F"""1 / {pow(temp + 1 , int(_SCREAMING_SNAKE_CASE ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = int(input("""Enter the last number (nth term) of the P-Series""")) lowercase_ = 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))
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
45
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } lowercase_ = { """b0""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 224, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 240, """dropout_rate""": 0.2, """dw_padding""": [16], }, """b2""": { """hidden_dim""": 1_408, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 260, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 16], }, """b3""": { """hidden_dim""": 1_536, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 300, """dropout_rate""": 0.3, """dw_padding""": [5, 18], }, """b4""": { """hidden_dim""": 1_792, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 380, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_048, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 456, """dropout_rate""": 0.4, """dw_padding""": [13, 27], }, """b6""": { """hidden_dim""": 2_304, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 528, """dropout_rate""": 0.5, """dw_padding""": [31], }, """b7""": { """hidden_dim""": 2_560, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 600, """dropout_rate""": 0.5, """dw_padding""": [18], }, } def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = EfficientNetConfig() lowercase__ = CONFIG_MAP[model_name]['hidden_dim'] lowercase__ = CONFIG_MAP[model_name]['width_coef'] lowercase__ = CONFIG_MAP[model_name]['depth_coef'] lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = CONFIG_MAP[model_name]['dropout_rate'] lowercase__ = CONFIG_MAP[model_name]['dw_padding'] lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = 1000 lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} return config def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = EfficientNetImageProcessor( size={'height': size, 'width': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=_SCREAMING_SNAKE_CASE , ) return preprocessor def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = [v.split('_' )[0].split('block' )[1] for v in original_param_names if v.startswith('block' )] lowercase__ = sorted(set(_SCREAMING_SNAKE_CASE ) ) lowercase__ = len(_SCREAMING_SNAKE_CASE ) lowercase__ = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} lowercase__ = [] rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight') ) rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight') ) rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias') ) rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean') ) rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var') ) for b in block_names: lowercase__ = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight') ) rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight') ) rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias') ) rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean') ) rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var') ) lowercase__ = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ = 'efficientnet.' + item[1] lowercase__ = 'classifier.weight' lowercase__ = 'classifier.bias' return key_mapping def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights='imagenet' , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=1000 , classifier_activation='softmax' , ) lowercase__ = original_model.trainable_variables lowercase__ = original_model.non_trainable_variables lowercase__ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ = param.numpy() lowercase__ = list(tf_params.keys() ) # Load HuggingFace model lowercase__ = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) lowercase__ = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() lowercase__ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('Converting parameters...' ) lowercase__ = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowercase__ = convert_image_processor(_SCREAMING_SNAKE_CASE ) lowercase__ = preprocessor(images=prepare_img() , return_tensors='pt' ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ = hf_model(**_SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits.detach().numpy() # Original model inference lowercase__ = False lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ = image.img_to_array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) lowercase__ = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print('Model outputs match!' ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") lowercase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
45
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase lowercase_ = logging.get_logger(__name__) lowercase_ = { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json""", """allenai/longformer-large-4096""": """https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json""", """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json""" ), } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = 'longformer' def __init__( self : Optional[int] , a : Union[List[int], int] = 512 , a : int = 2 , a : int = 1 , a : int = 0 , a : int = 2 , a : int = 30_522 , a : int = 768 , a : int = 12 , a : int = 12 , a : int = 3_072 , a : str = "gelu" , a : float = 0.1 , a : float = 0.1 , a : int = 512 , a : int = 2 , a : float = 0.02 , a : float = 1E-1_2 , a : bool = False , **a : Optional[int] , )-> Dict: """simple docstring""" super().__init__(pad_token_id=a , **a ) lowercase__ = attention_window lowercase__ = sep_token_id lowercase__ = bos_token_id lowercase__ = eos_token_id lowercase__ = vocab_size lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = hidden_act lowercase__ = intermediate_size lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = onnx_export class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Tuple , a : "PretrainedConfig" , a : str = "default" , a : "List[PatchingSpec]" = None )-> List[str]: """simple docstring""" super().__init__(a , a , a ) lowercase__ = True @property def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": lowercase__ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: lowercase__ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : str )-> Mapping[str, Mapping[int, str]]: """simple docstring""" lowercase__ = super().outputs if self.task == "default": lowercase__ = {0: 'batch'} return outputs @property def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> float: """simple docstring""" return 1E-4 @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> int: """simple docstring""" return max(super().default_onnx_opset , 14 ) def SCREAMING_SNAKE_CASE_ ( self : Any , a : "PreTrainedTokenizerBase" , a : int = -1 , a : int = -1 , a : bool = False , a : Optional[TensorType] = None , )-> Mapping[str, Any]: """simple docstring""" lowercase__ = super().generate_dummy_inputs( preprocessor=a , batch_size=a , seq_length=a , is_pair=a , framework=a ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly lowercase__ = torch.zeros_like(inputs['input_ids'] ) # make every second token global lowercase__ = 1 return inputs
45
import argparse import json import subprocess def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = [] lowercase__ = ( F"""curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"""" ' https://api.github.com/repos/huggingface/transformers/actions/runners' ) lowercase__ = subprocess.run(_SCREAMING_SNAKE_CASE , shell=_SCREAMING_SNAKE_CASE , stdout=subprocess.PIPE ) lowercase__ = output.stdout.decode('utf-8' ) lowercase__ = json.loads(_SCREAMING_SNAKE_CASE ) lowercase__ = status['runners'] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_SCREAMING_SNAKE_CASE ) # save the result so we can report them on Slack with open('offline_runners.txt' , 'w' ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) if len(_SCREAMING_SNAKE_CASE ) > 0: lowercase__ = '\n'.join([x['name'] for x in offline_runners] ) raise ValueError(F"""The following runners are offline:\n{failed}""" ) if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return values.split(',' ) lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--target_runners""", default=None, type=list_str, required=True, help="""Comma-separated list of runners to check status.""", ) parser.add_argument( """--token""", default=None, type=str, required=True, help="""A token that has actions:read permission.""" ) lowercase_ = parser.parse_args() get_runner_status(args.target_runners, args.token)
45
1
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter lowercase_ = logging.get_logger(__name__) lowercase_ = {} lowercase_ = {} lowercase_ = {} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , ) -> Dict: lowercase__ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( F"""Overwriting format type '{format_type}' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})""" ) lowercase__ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( F"""Overwriting format type alias '{alias}' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})""" ) lowercase__ = format_type def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple: lowercase__ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): lowercase__ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=["""python"""]) _register_formatter(ArrowFormatter, """arrow""", aliases=["""pa""", """pyarrow"""]) _register_formatter(NumpyFormatter, """numpy""", aliases=["""np"""]) _register_formatter(PandasFormatter, """pandas""", aliases=["""pd"""]) _register_formatter(CustomFormatter, """custom""") if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, """torch""", aliases=["""pt""", """pytorch"""]) else: lowercase_ = ValueError("""PyTorch needs to be installed to be able to return PyTorch tensors.""") _register_unavailable_formatter(_torch_error, """torch""", aliases=["""pt""", """pytorch"""]) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, """tensorflow""", aliases=["""tf"""]) else: lowercase_ = ValueError("""Tensorflow needs to be installed to be able to return Tensorflow tensors.""") _register_unavailable_formatter(_tf_error, """tensorflow""", aliases=["""tf"""]) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, """jax""", aliases=[]) else: lowercase_ = ValueError("""JAX needs to be installed to be able to return JAX arrays.""") _register_unavailable_formatter(_jax_error, """jax""", aliases=[]) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[str]: if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __UpperCamelCase (_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Formatter: lowercase__ = get_format_type_from_alias(_SCREAMING_SNAKE_CASE ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**_SCREAMING_SNAKE_CASE ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( F"""Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got '{format_type}'""" )
45
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'ClapFeatureExtractor' _UpperCamelCase : Union[str, Any] = ('RobertaTokenizer', 'RobertaTokenizerFast') def __init__( self : List[Any] , a : int , a : str )-> Any: """simple docstring""" super().__init__(a , a ) def __call__( self : Any , a : Tuple=None , a : Optional[int]=None , a : int=None , **a : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = kwargs.pop('sampling_rate' , a ) if text is None and audios is None: raise ValueError('You have to specify either text or audios. Both cannot be none.' ) if text is not None: lowercase__ = self.tokenizer(a , return_tensors=a , **a ) if audios is not None: lowercase__ = self.feature_extractor( a , sampling_rate=a , return_tensors=a , **a ) if text is not None and audios is not None: lowercase__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : str , *a : Dict , **a : int )-> Optional[int]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *a : int , **a : Dict )-> Dict: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer.model_input_names lowercase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
45
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} lowercase_ = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json""" ), }, } lowercase_ = { """moussaKam/mbarthez""": 1_024, """moussaKam/barthez""": 1_024, """moussaKam/barthez-orangesum-title""": 1_024, } lowercase_ = """▁""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = VOCAB_FILES_NAMES _UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Optional[Any] = ['input_ids', 'attention_mask'] _UpperCamelCase : int = BarthezTokenizer def __init__( self : List[Any] , a : Union[str, Any]=None , a : Optional[Any]=None , a : Dict="<s>" , a : Union[str, Any]="</s>" , a : List[str]="</s>" , a : Optional[Any]="<s>" , a : int="<unk>" , a : str="<pad>" , a : Optional[int]="<mask>" , **a : Union[str, Any] , )-> Tuple: """simple docstring""" lowercase__ = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) lowercase__ = vocab_file lowercase__ = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__ = [self.cls_token_id] lowercase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" lowercase__ = [self.sep_token_id] lowercase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE_ ( self : Dict , a : str , a : Optional[str] = None )-> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ = os.path.join( a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
45
1
import random from typing import Any def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[Any]: for _ in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = random.randint(0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) lowercase__ = random.randint(0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) lowercase__ , lowercase__ = data[b], data[a] return data if __name__ == "__main__": lowercase_ = [0, 1, 2, 3, 4, 5, 6, 7] lowercase_ = ["""python""", """says""", """hello""", """!"""] print("""Fisher-Yates Shuffle:""") print("""List""", integers, strings) print("""FY Shuffle""", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
45
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
1
import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) lowercase_ = """\ Text data. Second line of data.""" lowercase_ = """file""" @pytest.fixture(scope='session' ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = tmp_path_factory.mktemp('data' ) / (FILE_PATH + '.zstd') lowercase__ = bytes(_SCREAMING_SNAKE_CASE , 'utf-8' ) with zstd.open(_SCREAMING_SNAKE_CASE , 'wb' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return path @pytest.fixture def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: with open(os.path.join(tmpfs.local_root_dir , _SCREAMING_SNAKE_CASE ) , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return FILE_PATH @pytest.mark.parametrize('compression_format' , ['gzip', 'xz', 'zstd'] ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: lowercase__ = {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path} lowercase__ = input_paths[compression_format] lowercase__ = tmp_path / 'cache' lowercase__ = DownloadConfig(cache_dir=_SCREAMING_SNAKE_CASE , extract_compressed_file=_SCREAMING_SNAKE_CASE ) lowercase__ = cached_path(_SCREAMING_SNAKE_CASE , download_config=_SCREAMING_SNAKE_CASE ) with open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() with open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize('default_extracted' , [True, False] ) @pytest.mark.parametrize('default_cache_dir' , [True, False] ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = 'custom_cache' lowercase__ = 'custom_extracted_dir' lowercase__ = tmp_path / 'custom_extracted_path' if default_extracted: lowercase__ = ('downloads' if default_cache_dir else custom_cache_dir, 'extracted') else: monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR' , _SCREAMING_SNAKE_CASE ) monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(_SCREAMING_SNAKE_CASE ) ) lowercase__ = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) lowercase__ = xz_file lowercase__ = ( DownloadConfig(extract_compressed_file=_SCREAMING_SNAKE_CASE ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir , extract_compressed_file=_SCREAMING_SNAKE_CASE ) ) lowercase__ = cached_path(_SCREAMING_SNAKE_CASE , download_config=_SCREAMING_SNAKE_CASE ) assert Path(_SCREAMING_SNAKE_CASE ).parent.parts[-2:] == expected def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: # absolute path lowercase__ = str(Path(_SCREAMING_SNAKE_CASE ).resolve() ) assert cached_path(_SCREAMING_SNAKE_CASE ) == text_file # relative path lowercase__ = str(Path(_SCREAMING_SNAKE_CASE ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(_SCREAMING_SNAKE_CASE ) == text_file def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: # absolute path lowercase__ = str(tmp_path.resolve() / '__missing_file__.txt' ) with pytest.raises(_SCREAMING_SNAKE_CASE ): cached_path(_SCREAMING_SNAKE_CASE ) # relative path lowercase__ = './__missing_file__.txt' with pytest.raises(_SCREAMING_SNAKE_CASE ): cached_path(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: lowercase__ = get_from_cache(F"""tmp://{tmpfs_file}""" ) with open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() assert output_file_content == FILE_CONTENT @patch('datasets.config.HF_DATASETS_OFFLINE' , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase () -> List[Any]: with pytest.raises(_SCREAMING_SNAKE_CASE ): cached_path('https://huggingface.co' ) @patch('datasets.config.HF_DATASETS_OFFLINE' , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = tmp_path_factory.mktemp('data' ) / 'file.html' with pytest.raises(_SCREAMING_SNAKE_CASE ): http_get('https://huggingface.co' , temp_file=_SCREAMING_SNAKE_CASE ) with pytest.raises(_SCREAMING_SNAKE_CASE ): http_head('https://huggingface.co' ) @patch('datasets.config.HF_DATASETS_OFFLINE' , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: lowercase__ = tmp_path_factory.mktemp('data' ) / 'file.html' with pytest.raises(_SCREAMING_SNAKE_CASE ): ftp_get('ftp://huggingface.co' , temp_file=_SCREAMING_SNAKE_CASE ) with pytest.raises(_SCREAMING_SNAKE_CASE ): ftp_head('ftp://huggingface.co' ) @patch('datasets.config.HF_DATASETS_OFFLINE' , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: lowercase__ = tmp_path_factory.mktemp('data' ) / 'file.html' with pytest.raises(_SCREAMING_SNAKE_CASE ): fsspec_get('s3://huggingface.co' , temp_file=_SCREAMING_SNAKE_CASE ) with pytest.raises(_SCREAMING_SNAKE_CASE ): fsspec_head('s3://huggingface.co' )
45
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 lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Any = 'deit' def __init__( self : Any , a : Union[str, Any]=768 , a : Optional[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Optional[int]="gelu" , a : Optional[Any]=0.0 , a : List[Any]=0.0 , a : int=0.02 , a : List[str]=1E-1_2 , a : Optional[int]=224 , a : Tuple=16 , a : List[Any]=3 , a : List[str]=True , a : Any=16 , **a : Union[str, Any] , )-> int: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = qkv_bias lowercase__ = encoder_stride class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : int )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-4
45
1
import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""", """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""", """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""", """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""", """self_attn_layer_norm""": """encoder.layers.*.layer_norm""", """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""", """fc2""": """encoder.layers.*.feed_forward.output_dense""", """final_layer_norm""": """encoder.layers.*.final_layer_norm""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } lowercase_ = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: for attribute in key.split('.' ): lowercase__ = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if weight_type is not None: lowercase__ = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).shape else: lowercase__ = hf_pointer.shape assert hf_shape == value.shape, ( F"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": lowercase__ = value elif weight_type == "weight_g": lowercase__ = value elif weight_type == "weight_v": lowercase__ = value elif weight_type == "bias": lowercase__ = value else: lowercase__ = value logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: lowercase__ = [] lowercase__ = fairseq_model.state_dict() lowercase__ = hf_model.feature_extractor lowercase__ = hf_model.adapter for name, value in fairseq_dict.items(): lowercase__ = False if "conv_layers" in name: load_conv_layer( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , hf_model.config.feat_extract_norm == 'group' , ) lowercase__ = True elif any(x in name for x in ['adaptor', 'w2v_encoder.proj.', 'w2v_proj_ln.'] ): load_adapter(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: lowercase__ = True if "*" in mapped_key: lowercase__ = name.split(_SCREAMING_SNAKE_CASE )[0].split('.' )[-2] lowercase__ = mapped_key.replace('*' , _SCREAMING_SNAKE_CASE ) if "weight_g" in name: lowercase__ = 'weight_g' elif "weight_v" in name: lowercase__ = 'weight_v' elif "bias" in name: lowercase__ = 'bias' elif "weight" in name: lowercase__ = 'weight' else: lowercase__ = None set_recursively(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) continue if not is_used: unused_weights.append(_SCREAMING_SNAKE_CASE ) logger.warning(F"""Unused weights: {unused_weights}""" ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: lowercase__ = full_name.split('conv_layers.' )[-1] lowercase__ = name.split('.' ) lowercase__ = int(items[0] ) lowercase__ = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) lowercase__ = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) lowercase__ = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) lowercase__ = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) lowercase__ = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: lowercase__ = full_name.split('adaptor.' )[-1] lowercase__ = name.split('.' ) if items[1].isdigit(): lowercase__ = int(items[1] ) else: lowercase__ = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found.""" lowercase__ = value logger.info(F"""Adapter proj layer norm bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found.""" lowercase__ = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found.""" lowercase__ = value logger.info(F"""Adapter proj layer bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found.""" lowercase__ = value logger.info(F"""Adapter proj layer weight was initialized from {full_name}.""" ) elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found.""" lowercase__ = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found.""" lowercase__ = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) else: unused_weights.append(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ , lowercase__ = emb.weight.shape lowercase__ = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) lowercase__ = emb.weight.data return lin_layer @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> List[str]: lowercase__ = WavaVecaConfig.from_pretrained( _SCREAMING_SNAKE_CASE , add_adapter=_SCREAMING_SNAKE_CASE , adapter_stride=_SCREAMING_SNAKE_CASE , adapter_kernel_size=_SCREAMING_SNAKE_CASE , use_auth_token=_SCREAMING_SNAKE_CASE , output_hidden_size=_SCREAMING_SNAKE_CASE , ) lowercase__ = MBartConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) # load model lowercase__ , lowercase__ , lowercase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ 'config_yaml': config_yaml_path, 'data': '/'.join(dict_path.split('/' )[:-1] ), 'w2v_path': checkpoint_path, 'load_pretrained_decoder_from': None, } , ) lowercase__ = model[0].eval() # load feature extractor lowercase__ = WavaVecaFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE , use_auth_token=_SCREAMING_SNAKE_CASE ) # set weights for wav2vec2 encoder lowercase__ = WavaVecaModel(_SCREAMING_SNAKE_CASE ) recursively_load_weights_wavaveca(model.encoder , _SCREAMING_SNAKE_CASE ) # load decoder weights lowercase__ = MBartForCausalLM(_SCREAMING_SNAKE_CASE ) lowercase__ , lowercase__ = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_SCREAMING_SNAKE_CASE ) logger.warning(F"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(F"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) lowercase__ = SpeechEncoderDecoderModel(encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE ) lowercase__ = False lowercase__ = MBartaaTokenizer(_SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) lowercase__ = hf_wavavec.config.to_dict() lowercase__ = tokenizer.pad_token_id lowercase__ = tokenizer.bos_token_id lowercase__ = tokenizer.eos_token_id lowercase__ = 'mbart50' lowercase__ = 'wav2vec2' lowercase__ = tokenizer.eos_token_id lowercase__ = 250004 lowercase__ = tokenizer.eos_token_id lowercase__ = SpeechEncoderDecoderConfig.from_dict(_SCREAMING_SNAKE_CASE ) hf_wavavec.save_pretrained(_SCREAMING_SNAKE_CASE ) feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_yaml_path""", default=None, type=str, help="""Path to yaml file of fine-tuned model""") parser.add_argument( """--encoder_config_path""", default="""facebook/wav2vec2-xls-r-1b""", type=str, help="""Path to hf encoder wav2vec2 checkpoint config""", ) parser.add_argument( """--decoder_config_path""", default="""facebook/mbart-large-50-one-to-many-mmt""", type=str, help="""Path to hf decoder checkpoint config""", ) parser.add_argument("""--add_adapter""", default=True, type=bool, help="""whethere to add model adapter layers""") parser.add_argument("""--adapter_stride""", default=2, type=int, help="""stride of adapter layers""") parser.add_argument("""--adapter_kernel_size""", default=3, type=int, help="""kernel size of adapter layers""") parser.add_argument("""--encoder_output_dim""", default=1_024, type=int, help="""encoder output dim""") parser.add_argument("""--start_token_id""", default=250_004, type=int, help="""`decoder_start_token_id` of model config""") lowercase_ = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
45
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[int]: lowercase__ = [True] * limit lowercase__ = False lowercase__ = False lowercase__ = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): lowercase__ = i * 2 while index < limit: lowercase__ = False lowercase__ = index + i lowercase__ = [2] for i in range(3 , _SCREAMING_SNAKE_CASE , 2 ): if is_prime[i]: primes.append(_SCREAMING_SNAKE_CASE ) return primes def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 1000000 ) -> int: lowercase__ = prime_sieve(_SCREAMING_SNAKE_CASE ) lowercase__ = 0 lowercase__ = 0 for i in range(len(_SCREAMING_SNAKE_CASE ) ): for j in range(i + length , len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: lowercase__ = j - i lowercase__ = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
1
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCamelCase () -> Optional[int]: lowercase__ = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) lowercase__ = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) DownloadCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) EnvironmentCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) RunCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) ServeCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) UserCommands.register_subcommand(_SCREAMING_SNAKE_CASE ) AddNewModelCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) AddNewModelLikeCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) LfsCommands.register_subcommand(_SCREAMING_SNAKE_CASE ) PTtoTFCommand.register_subcommand(_SCREAMING_SNAKE_CASE ) # Let's go lowercase__ = parser.parse_args() if not hasattr(_SCREAMING_SNAKE_CASE , 'func' ): parser.print_help() exit(1 ) # Run lowercase__ = args.func(_SCREAMING_SNAKE_CASE ) service.run() if __name__ == "__main__": main()
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : str = 'new-model' if is_tf_available(): class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Optional[Any] = NewModelConfig @require_tf class SCREAMING_SNAKE_CASE (unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> int: """simple docstring""" lowercase__ = 'bert-base-cased' lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModel.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : str )-> Dict: """simple docstring""" lowercase__ = 'bert-base-cased' lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForPreTraining.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForCausalLM.from_pretrained(a ) lowercase__ , lowercase__ = TFAutoModelForCausalLM.from_pretrained(a , output_loading_info=a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Optional[Any]: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelWithLMHead.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> str: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForMaskedLM.from_pretrained(a ) lowercase__ , lowercase__ = TFAutoModelForMaskedLM.from_pretrained(a , output_loading_info=a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Dict: """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForSeqaSeqLM.from_pretrained(a ) lowercase__ , lowercase__ = TFAutoModelForSeqaSeqLM.from_pretrained(a , output_loading_info=a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Tuple: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForSequenceClassification.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> Any: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForQuestionAnswering.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) @slow @require_tensorflow_probability def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: lowercase__ = AutoConfig.from_pretrained(a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) lowercase__ = TFAutoModelForTableQuestionAnswering.from_pretrained(a ) lowercase__ , lowercase__ = TFAutoModelForTableQuestionAnswering.from_pretrained( a , output_loading_info=a ) self.assertIsNotNone(a ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> str: """simple docstring""" lowercase__ = TFAutoModelWithLMHead.from_pretrained(a ) self.assertIsInstance(a , a ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=a ) , 14_410 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = TFAutoModelWithLMHead.from_pretrained(a ) self.assertIsInstance(a , a ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=a ) , 14_410 ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Any: """simple docstring""" lowercase__ = TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(a , a ) lowercase__ = copy.deepcopy(model.config ) lowercase__ = ['FunnelBaseModel'] lowercase__ = TFAutoModel.from_config(a ) self.assertIsInstance(a , a ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(a ) lowercase__ = TFAutoModel.from_pretrained(a ) self.assertIsInstance(a , a ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" try: AutoConfig.register('new-model' , a ) lowercase__ = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(a ): auto_class.register(a , a ) auto_class.register(a , a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(a ): auto_class.register(a , a ) # Now that the config is registered, it can be used as any other config with the auto-API lowercase__ = BertModelTester(self ).get_config() lowercase__ = NewModelConfig(**tiny_config.to_dict() ) lowercase__ = auto_class.from_config(a ) self.assertIsInstance(a , a ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(a ) lowercase__ = auto_class.from_pretrained(a ) self.assertIsInstance(a , a ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" with self.assertRaisesRegex( a , 'bert-base is not a local folder and is not a valid model identifier' ): lowercase__ = TFAutoModel.from_pretrained('bert-base' ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Optional[int]: """simple docstring""" with self.assertRaisesRegex( a , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): lowercase__ = TFAutoModel.from_pretrained(a , revision='aaaaaa' ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Tuple: """simple docstring""" with self.assertRaisesRegex( a , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): lowercase__ = TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> int: """simple docstring""" with self.assertRaisesRegex(a , 'Use `from_pt=True` to load this model' ): lowercase__ = TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Union[str, Any]: """simple docstring""" lowercase__ = TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: lowercase__ = TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint lowercase__ = TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: lowercase__ = TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
45
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
1
import argparse import json import os from collections import OrderedDict import numpy as np import tensorflow as tf import torch def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Dict: lowercase__ = os.path.join(args.tf_model_dir , 'parameters.json' ) lowercase__ = json.loads(open(_SCREAMING_SNAKE_CASE ).read() ) if not params: raise ValueError( F"""It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.""" ) if not args.output.endswith('.pt' ): lowercase__ = args.output + '.pt' lowercase__ = OrderedDict() with tf.device('/CPU:0' ): lowercase__ = tf.train.load_checkpoint(args.tf_model_dir ) lowercase__ = reader.get_variable_to_shape_map() for key_name in shapes.keys(): lowercase__ = reader.get_tensor(_SCREAMING_SNAKE_CASE ).astype(np.floataa ) if key_name.endswith('/adam_m' ) or key_name.endswith('/adam_v' ): continue if key_name.startswith('pasts/' ): if key_name.startswith('pasts/mlp' ): lowercase__ = int(key_name[9] ) elif key_name.startswith('pasts/out' ): lowercase__ = 8 lowercase__ = 'model.sqout.%d.weight' % (player * 2) # enter to nn.Sequencial with Tanh, so 2 at a time lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/moe' ): lowercase__ = int(key_name[9:].split('/' )[0] ) if key_name.endswith('/switch_gating/kernel' ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.router.classifier.weight' % player lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/softmlp/kernel' ): lowercase__ = 'model.blocks.%d.feed_forward.soft_bypass_mlp.weight' % player lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/wo/kernel' ) or key_name.endswith('/wi/kernel' ): lowercase__ = key_name[-9:-7] for i in range(16 ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight' % (player, i, nlayer) lowercase__ = ( vnp[i].transpose([1, 0] ).copy() ) # In Mesh-Tensorflow, it is one array, so it is divided lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/mlp' ): lowercase__ = int(key_name[9:].split('/' )[0] ) if key_name.endswith('/p1/kernel' ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.wi.weight' % player lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/p1/bias' ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.wi.bias' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/p2/kernel' ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.wo.weight' % player lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/p2/bias' ): lowercase__ = 'model.blocks.%d.feed_forward.mlp.wo.bias' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/ln' ): lowercase__ = int(key_name[8:].split('/' )[0] ) if key_name.endswith('/b' ): lowercase__ = 'model.blocks.%d.feed_forward.norm.bias' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/g' ): lowercase__ = 'model.blocks.%d.feed_forward.norm.weight' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/att' ): lowercase__ = int(key_name[9:].split('/' )[0] ) if key_name.endswith('/qkv/kernel' ): lowercase__ = vnp.copy() # Compute same dimension as Mesh-tensorflow using einsum lowercase__ = state[:, 0, :, :] lowercase__ = state[:, 1, :, :] lowercase__ = state[:, 2, :, :] lowercase__ = ( state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix lowercase__ = ( state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix lowercase__ = ( state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix lowercase__ = 'model.blocks.%d.self_attn.self_attn.q_proj.weight' % player lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) lowercase__ = 'model.blocks.%d.self_attn.self_attn.k_proj.weight' % player lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) lowercase__ = 'model.blocks.%d.self_attn.self_attn.v_proj.weight' % player lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/o/kernel' ): lowercase__ = 'model.blocks.%d.self_attn.self_attn.out_proj.weight' % player lowercase__ = ( vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]] ).transpose([1, 0] ).copy() ) # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/an' ): lowercase__ = int(key_name[8:].split('/' )[0] ) if key_name.endswith('/b' ): lowercase__ = 'model.blocks.%d.self_attn.norm.bias' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.endswith('/g' ): lowercase__ = 'model.blocks.%d.self_attn.norm.weight' % player lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif ( key_name.startswith('model/wte' ) or key_name.startswith('model/wpe' ) or key_name.startswith('model/ete' ) ): lowercase__ = {'wte': 'embed_tokens', 'wpe': 'position_embeddings', 'ete': 'extra_position_embeddings'}[ key_name[-3:] ] lowercase__ = 'model.%s.weight' % nlayer lowercase__ = vnp.copy() # same in embedded lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) if key_name.startswith('model/wte' ): lowercase__ = 'lm_head.weight' lowercase__ = vnp.copy() # same in embedded lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name.startswith('model/wob' ): lowercase__ = 'final_logits_bias' lowercase__ = vnp.copy() # same in embedded lowercase__ = state.reshape((1, -1) ) lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name == "model/dense/kernel": lowercase__ = 'model.last_project.weight' lowercase__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) elif key_name == "model/dense_1/bias": lowercase__ = 'model.last_project.bias' lowercase__ = vnp.copy() # same because it is one dimensional lowercase__ = torch.tensor(_SCREAMING_SNAKE_CASE ) torch.save(_SCREAMING_SNAKE_CASE , args.output ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser( description="""model converter.""", formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument("""--tf_model_dir""", metavar="""PATH""", type=str, required=True, help="""import model""") parser.add_argument("""--output""", metavar="""PATH""", type=str, required=True, help="""output model""") lowercase_ = parser.parse_args() convert_tf_gptsan_to_pt(args)
45
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
1
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class SCREAMING_SNAKE_CASE : def __init__( self : Optional[Any] , a : Optional[Any] , a : str=13 , a : Optional[int]=7 , a : Dict=True , a : List[str]=True , a : Any=True , a : Optional[int]=True , a : Tuple=99 , a : List[str]=32 , a : Optional[Any]=5 , a : List[Any]=4 , a : Tuple=4 , a : Dict="gelu" , a : str=0.0 , a : int=0.1 , a : str=True , a : List[str]=512 , a : Any=16 , a : Optional[Any]=2 , a : List[Any]=0.02 , a : Tuple=3 , a : Union[str, Any]=4 , a : Optional[int]=None , )-> str: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = seq_length lowercase__ = is_training lowercase__ = use_input_mask lowercase__ = use_token_type_ids lowercase__ = use_labels lowercase__ = vocab_size lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_multiple_size lowercase__ = hidden_act lowercase__ = hidden_dropout lowercase__ = attention_dropout lowercase__ = weight_tying lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = num_labels lowercase__ = num_choices lowercase__ = scope def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = None if self.use_input_mask: lowercase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase__ = self.get_config() return config, input_ids, input_mask, token_labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" return GPTNeoXJapaneseConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> str: """simple docstring""" lowercase__ , lowercase__ , lowercase__ , lowercase__ = self.prepare_config_and_inputs() lowercase__ = True return config, input_ids, input_mask, token_labels def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Union[str, Any] , a : List[str] , a : Tuple )-> Optional[int]: """simple docstring""" lowercase__ = GPTNeoXJapaneseModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a , attention_mask=a ) lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , a : List[str] , a : Any , a : int )-> int: """simple docstring""" lowercase__ = True lowercase__ = GPTNeoXJapaneseModel(a ) model.to(a ) model.eval() lowercase__ = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Tuple , a : Optional[int] , a : Dict , a : str )-> int: """simple docstring""" lowercase__ = GPTNeoXJapaneseForCausalLM(config=a ) model.to(a ) model.eval() lowercase__ = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : str , a : Optional[int] , a : Optional[Any] )-> List[Any]: """simple docstring""" lowercase__ = True lowercase__ = GPTNeoXJapaneseForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass lowercase__ = model(a , attention_mask=a , use_cache=a ) lowercase__ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowercase__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowercase__ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowercase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) lowercase__ = torch.cat([input_mask, next_mask] , dim=-1 ) lowercase__ = model(a , attention_mask=a , output_hidden_states=a ) lowercase__ = output_from_no_past['hidden_states'][0] lowercase__ = model( a , attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice lowercase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowercase__ = output_from_no_past[:, -3:, random_slice_idx].detach() lowercase__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Tuple: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ , lowercase__ = config_and_inputs lowercase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : str = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _UpperCamelCase : Optional[int] = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _UpperCamelCase : str = ( {'feature-extraction': GPTNeoXJapaneseModel, 'text-generation': GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _UpperCamelCase : Optional[int] = False _UpperCamelCase : str = False _UpperCamelCase : Optional[int] = False _UpperCamelCase : str = False def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Tuple: """simple docstring""" lowercase__ = GPTNeoXJapaneseModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Any: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> int: """simple docstring""" lowercase__ , lowercase__ , lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(a , a , a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Dict: """simple docstring""" lowercase__ , lowercase__ , lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(a , a , a ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Tuple: """simple docstring""" lowercase__ , lowercase__ , lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_decoder() lowercase__ = None self.model_tester.create_and_check_model_as_decoder(a , a , a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> str: """simple docstring""" lowercase__ , lowercase__ , lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(a , a , a ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Any: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" lowercase__ = 'abeja/gpt-neox-japanese-2.7b' lowercase__ = ['データサイエンティストとは、', '100年後に必要とされる会社は、', 'フルリモートの環境で働くために必要なことは、', '国境の長いトンネルを抜けると', '美味しい日本食といえば、'] lowercase__ = [ 'データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。', '100年後に必要とされる会社は、「人」が中心の会社です。', 'フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。', '国境の長いトンネルを抜けると、そこは雪国だった。', '美味しい日本食といえば、やっぱりお寿司ですよね。', ] lowercase__ = GPTNeoXJapaneseTokenizer.from_pretrained(a ) lowercase__ = GPTNeoXJapaneseForCausalLM.from_pretrained(a ) lowercase__ = [] for prompt in prompts: lowercase__ = tokenizer(a , return_tensors='pt' ).input_ids lowercase__ = model.generate(a , max_length=50 ) lowercase__ = tokenizer.batch_decode(a , skip_special_tokens=a ) predicted_outputs += generated_string self.assertListEqual(a , a )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
1
class SCREAMING_SNAKE_CASE : # Public class to implement a graph def __init__( self : int , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = row lowercase__ = col lowercase__ = graph def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int , a : list[list[bool]] )-> bool: """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order lowercase__ = [-1, 0, 1, -1, 1, -1, 0, 1] lowercase__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , a ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: # And finally, count all islands. """simple docstring""" lowercase__ = [[False for j in range(self.COL )] for i in range(self.ROW )] lowercase__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(a , a , a ) count += 1 return count
45
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[Any] , a : UNetaDModel , a : UNetaDModel , a : DDPMScheduler , a : Any , )-> Dict: """simple docstring""" super().__init__() lowercase__ = value_function lowercase__ = unet lowercase__ = scheduler lowercase__ = env lowercase__ = env.get_dataset() lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].mean() except: # noqa: E722 pass lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].std() except: # noqa: E722 pass lowercase__ = env.observation_space.shape[0] lowercase__ = env.action_space.shape[0] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Any , a : int )-> Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : str , a : List[str] )-> str: """simple docstring""" return x_in * self.stds[key] + self.means[key] def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : Tuple )-> Tuple: """simple docstring""" if type(a ) is dict: return {k: self.to_torch(a ) for k, v in x_in.items()} elif torch.is_tensor(a ): return x_in.to(self.unet.device ) return torch.tensor(a , device=self.unet.device ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[int] , a : Dict , a : Optional[Any] )-> List[Any]: """simple docstring""" for key, val in cond.items(): lowercase__ = val.clone() return x_in def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[Any] , a : Any , a : Optional[Any] , a : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = x.shape[0] lowercase__ = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model lowercase__ = torch.full((batch_size,) , a , device=self.unet.device , dtype=torch.long ) for _ in range(a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models lowercase__ = self.value_function(x.permute(0 , 2 , 1 ) , a ).sample lowercase__ = torch.autograd.grad([y.sum()] , [x] )[0] lowercase__ = self.scheduler._get_variance(a ) lowercase__ = torch.exp(0.5 * posterior_variance ) lowercase__ = model_std * grad lowercase__ = 0 lowercase__ = x.detach() lowercase__ = x + scale * grad lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.unet(x.permute(0 , 2 , 1 ) , a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg lowercase__ = self.scheduler.step(a , a , a , predict_epsilon=a )['prev_sample'] # apply conditions to the trajectory (set the initial state) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) return x, y def __call__( self : Any , a : Tuple , a : int=64 , a : Tuple=32 , a : List[Any]=2 , a : List[str]=0.1 )-> List[Any]: """simple docstring""" lowercase__ = self.normalize(a , 'observations' ) lowercase__ = obs[None].repeat(a , axis=0 ) lowercase__ = {0: self.to_torch(a )} lowercase__ = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) lowercase__ = randn_tensor(a , device=self.unet.device ) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) # run the diffusion process lowercase__ , lowercase__ = self.run_diffusion(a , a , a , a ) # sort output trajectories by value lowercase__ = y.argsort(0 , descending=a ).squeeze() lowercase__ = x[sorted_idx] lowercase__ = sorted_values[:, :, : self.action_dim] lowercase__ = actions.detach().cpu().numpy() lowercase__ = self.de_normalize(a , key='actions' ) # select the action with the highest value if y is not None: lowercase__ = 0 else: # if we didn't run value guiding, select a random action lowercase__ = np.random.randint(0 , a ) lowercase__ = denorm_actions[selected_index, 0] return denorm_actions
45
1
# Algorithm for the pigeonhole sorting def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: lowercase__ = min(_SCREAMING_SNAKE_CASE ) # min() finds the minimum value lowercase__ = max(_SCREAMING_SNAKE_CASE ) # max() finds the maximum value lowercase__ = 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 lowercase__ = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. lowercase__ = 0 for count in range(_SCREAMING_SNAKE_CASE ): while holes[count] > 0: holes[count] -= 1 lowercase__ = count + min_val i += 1 def __UpperCamelCase () -> str: lowercase__ = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(_SCREAMING_SNAKE_CASE ) print('Sorted order is:' , ' '.join(_SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": main()
45
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : Any , a : str , a : List[Any]=7 , a : int=3 , a : int=18 , a : Optional[Any]=30 , a : Optional[int]=400 , a : int=True , a : Tuple=None , a : Optional[Any]=True , a : str=False , a : str=True , a : int=True , a : Tuple=[0.5, 0.5, 0.5] , a : Any=[0.5, 0.5, 0.5] , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size if size is not None else {'height': 18, 'width': 20} lowercase__ = do_thumbnail lowercase__ = do_align_axis lowercase__ = do_pad lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = DonutImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) self.assertTrue(hasattr(a , 'do_thumbnail' ) ) self.assertTrue(hasattr(a , 'do_align_long_axis' ) ) self.assertTrue(hasattr(a , 'do_pad' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" pass @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , numpify=a ) for image in image_inputs: self.assertIsInstance(a , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , torchify=a ) for image in image_inputs: self.assertIsInstance(a , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
45
1
import pytest import datasets # Import fixture modules as plugins lowercase_ = ["""tests.fixtures.files""", """tests.fixtures.hub""", """tests.fixtures.fsspec"""] def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[str]: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? lowercase__ = tmp_path_factory.getbasetemp() / 'cache' lowercase__ = test_hf_cache_home / 'datasets' lowercase__ = test_hf_cache_home / 'metrics' lowercase__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(_SCREAMING_SNAKE_CASE ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(_SCREAMING_SNAKE_CASE ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(_SCREAMING_SNAKE_CASE ) ) lowercase__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(_SCREAMING_SNAKE_CASE ) ) lowercase__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(_SCREAMING_SNAKE_CASE ) ) @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE , scope='session' ) def __UpperCamelCase () -> Any: datasets.disable_progress_bar() @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , _SCREAMING_SNAKE_CASE ) @pytest.fixture def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , _SCREAMING_SNAKE_CASE )
45
import math def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(_SCREAMING_SNAKE_CASE ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowercase_ = """Enter the base and the power separated by a comma: """ lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. lowercase_ = res(xa, ya) lowercase_ = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
45
1
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } lowercase_ = { """b0""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 224, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 240, """dropout_rate""": 0.2, """dw_padding""": [16], }, """b2""": { """hidden_dim""": 1_408, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 260, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 16], }, """b3""": { """hidden_dim""": 1_536, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 300, """dropout_rate""": 0.3, """dw_padding""": [5, 18], }, """b4""": { """hidden_dim""": 1_792, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 380, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_048, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 456, """dropout_rate""": 0.4, """dw_padding""": [13, 27], }, """b6""": { """hidden_dim""": 2_304, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 528, """dropout_rate""": 0.5, """dw_padding""": [31], }, """b7""": { """hidden_dim""": 2_560, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 600, """dropout_rate""": 0.5, """dw_padding""": [18], }, } def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = EfficientNetConfig() lowercase__ = CONFIG_MAP[model_name]['hidden_dim'] lowercase__ = CONFIG_MAP[model_name]['width_coef'] lowercase__ = CONFIG_MAP[model_name]['depth_coef'] lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = CONFIG_MAP[model_name]['dropout_rate'] lowercase__ = CONFIG_MAP[model_name]['dw_padding'] lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = 1000 lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} return config def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = EfficientNetImageProcessor( size={'height': size, 'width': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=_SCREAMING_SNAKE_CASE , ) return preprocessor def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = [v.split('_' )[0].split('block' )[1] for v in original_param_names if v.startswith('block' )] lowercase__ = sorted(set(_SCREAMING_SNAKE_CASE ) ) lowercase__ = len(_SCREAMING_SNAKE_CASE ) lowercase__ = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} lowercase__ = [] rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight') ) rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight') ) rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias') ) rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean') ) rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var') ) for b in block_names: lowercase__ = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight') ) rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight') ) rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias') ) rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean') ) rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var') ) lowercase__ = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ = 'efficientnet.' + item[1] lowercase__ = 'classifier.weight' lowercase__ = 'classifier.bias' return key_mapping def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights='imagenet' , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=1000 , classifier_activation='softmax' , ) lowercase__ = original_model.trainable_variables lowercase__ = original_model.non_trainable_variables lowercase__ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ = param.numpy() lowercase__ = list(tf_params.keys() ) # Load HuggingFace model lowercase__ = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) lowercase__ = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() lowercase__ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('Converting parameters...' ) lowercase__ = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowercase__ = convert_image_processor(_SCREAMING_SNAKE_CASE ) lowercase__ = preprocessor(images=prepare_img() , return_tensors='pt' ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ = hf_model(**_SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits.detach().numpy() # Original model inference lowercase__ = False lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ = image.img_to_array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) lowercase__ = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print('Model outputs match!' ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") lowercase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
45
class SCREAMING_SNAKE_CASE : # Public class to implement a graph def __init__( self : int , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = row lowercase__ = col lowercase__ = graph def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int , a : list[list[bool]] )-> bool: """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : list[list[bool]] )-> None: """simple docstring""" lowercase__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order lowercase__ = [-1, 0, 1, -1, 1, -1, 0, 1] lowercase__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , a ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> int: # And finally, count all islands. """simple docstring""" lowercase__ = [[False for j in range(self.COL )] for i in range(self.ROW )] lowercase__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(a , a , a ) count += 1 return count
45
1
import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--txt2img_unclip""", default="""kakaobrain/karlo-v1-alpha""", type=str, required=False, help="""The pretrained txt2img unclip.""", ) lowercase_ = parser.parse_args() lowercase_ = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) lowercase_ = CLIPImageProcessor() lowercase_ = CLIPVisionModelWithProjection.from_pretrained("""openai/clip-vit-large-patch14""") lowercase_ = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
45
from string import ascii_uppercase lowercase_ = {str(ord(c) - 55): c for c in ascii_uppercase} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: 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 > 36: raise ValueError('base must be <= 36' ) lowercase__ = '' lowercase__ = 0 lowercase__ = 0 while div != 1: lowercase__ , lowercase__ = divmod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if base >= 11 and 9 < mod < 36: lowercase__ = ALPHABET_VALUES[str(_SCREAMING_SNAKE_CASE )] else: lowercase__ = str(_SCREAMING_SNAKE_CASE ) new_value += actual_value lowercase__ = num // base lowercase__ = 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), )
45
1
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion' ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe( image=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.0441, 0.0469, 0.0507, 0.0575, 0.0632, 0.0650, 0.0865, 0.0909, 0.0945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
45
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , a : Any , a : Optional[int]=13 , a : Tuple=30 , a : Union[str, Any]=2 , a : List[str]=3 , a : Dict=True , a : List[str]=True , a : List[Any]=32 , a : List[str]=5 , a : Optional[int]=4 , a : List[str]=37 , a : Dict="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : int=10 , a : List[str]=0.02 , a : int=None , a : List[str]=2 , )-> Dict: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = is_training lowercase__ = use_labels lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = scope lowercase__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ = (image_size // patch_size) ** 2 lowercase__ = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" lowercase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : List[Any] , a : List[str] , a : Dict )-> Optional[Any]: """simple docstring""" lowercase__ = ViTModel(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self : int , a : Optional[Any] , a : int , a : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForMaskedImageModeling(config=a ) model.to(a ) model.eval() lowercase__ = model(a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForMaskedImageModeling(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : List[str] , a : int , a : List[Any] )-> str: """simple docstring""" lowercase__ = self.type_sequence_label_size lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowercase__ = 1 lowercase__ = ViTForImageClassification(a ) model.to(a ) model.eval() lowercase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowercase__ = model(a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) _UpperCamelCase : int = True _UpperCamelCase : int = False _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Dict = False def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> List[str]: """simple docstring""" lowercase__ = ViTModelTester(self ) lowercase__ = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowercase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[int]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ = model_class(a ) lowercase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ = [*signature.parameters.keys()] lowercase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict )-> List[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = ViTModel.from_pretrained(a ) self.assertIsNotNone(a ) def __UpperCamelCase () -> str: lowercase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE (unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Union[str, Any]: """simple docstring""" lowercase__ = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(a ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ).to(a ) # forward pass with torch.no_grad(): lowercase__ = model(**a ) # verify the logits lowercase__ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a ) lowercase__ = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self : int )-> List[str]: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' ).to(a ) lowercase__ = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass with torch.no_grad(): lowercase__ = model(a , interpolate_pos_encoding=a ) # verify the logits lowercase__ = torch.Size((1, 3_601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , a ) lowercase__ = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : str )-> str: """simple docstring""" lowercase__ = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) lowercase__ = self.default_image_processor lowercase__ = prepare_img() lowercase__ = image_processor(images=a , return_tensors='pt' ) lowercase__ = inputs.pixel_values.to(a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): lowercase__ = model(a )
45
1
import os import unittest from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, BertTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Tuple = BertTokenizer _UpperCamelCase : Tuple = BertTokenizerFast _UpperCamelCase : Dict = True _UpperCamelCase : List[Any] = True _UpperCamelCase : List[Any] = filter_non_english def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Union[str, Any]: """simple docstring""" super().setUp() lowercase__ = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] lowercase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : List[str] )-> Union[str, Any]: """simple docstring""" lowercase__ = 'UNwant\u00E9d,running' lowercase__ = 'unwanted, running' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer_class(self.vocab_file ) lowercase__ = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(a , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(a ) , [9, 6, 7, 12, 10, 11] ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Optional[int]: """simple docstring""" if not self.test_rust_tokenizer: return lowercase__ = self.get_tokenizer() lowercase__ = self.get_rust_tokenizer() lowercase__ = 'UNwant\u00E9d,running' lowercase__ = tokenizer.tokenize(a ) lowercase__ = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) lowercase__ = tokenizer.encode(a , add_special_tokens=a ) lowercase__ = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) lowercase__ = self.get_rust_tokenizer() lowercase__ = tokenizer.encode(a ) lowercase__ = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) # With lower casing lowercase__ = self.get_tokenizer(do_lower_case=a ) lowercase__ = self.get_rust_tokenizer(do_lower_case=a ) lowercase__ = 'UNwant\u00E9d,running' lowercase__ = tokenizer.tokenize(a ) lowercase__ = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) lowercase__ = tokenizer.encode(a , add_special_tokens=a ) lowercase__ = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) lowercase__ = self.get_rust_tokenizer() lowercase__ = tokenizer.encode(a ) lowercase__ = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Optional[int]: """simple docstring""" lowercase__ = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Union[str, Any]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Optional[Any]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a , strip_accents=a ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a , strip_accents=a ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Tuple: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[Any]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> List[Any]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a , strip_accents=a ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> List[str]: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a , strip_accents=a ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def SCREAMING_SNAKE_CASE_ ( self : int )-> Dict: """simple docstring""" lowercase__ = BasicTokenizer(do_lower_case=a , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Optional[Any]: """simple docstring""" lowercase__ = BasicTokenizer() lowercase__ = 'a\n\'ll !!to?\'d of, can\'t.' lowercase__ = ['a', '\'', 'll', '!', '!', 'to', '?', '\'', 'd', 'of', ',', 'can', '\'', 't', '.'] self.assertListEqual(tokenizer.tokenize(a ) , a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" lowercase__ = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] lowercase__ = {} for i, token in enumerate(a ): lowercase__ = i lowercase__ = WordpieceTokenizer(vocab=a , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def SCREAMING_SNAKE_CASE_ ( self : Any )-> Dict: """simple docstring""" self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def SCREAMING_SNAKE_CASE_ ( self : Dict )-> Union[str, Any]: """simple docstring""" self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = self.get_tokenizer() lowercase__ = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(a ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) self.assertListEqual( [rust_tokenizer.tokenize(a ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Optional[Any]: """simple docstring""" lowercase__ = self.tokenizer_class.from_pretrained('bert-base-uncased' ) lowercase__ = tokenizer.encode('sequence builders' , add_special_tokens=a ) lowercase__ = tokenizer.encode('multi-sequence build' , add_special_tokens=a ) lowercase__ = tokenizer.build_inputs_with_special_tokens(a ) lowercase__ = tokenizer.build_inputs_with_special_tokens(a , a ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Any: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowercase__ = self.rust_tokenizer_class.from_pretrained(a , **a ) lowercase__ = f"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence.""" lowercase__ = tokenizer_r.encode_plus( a , return_attention_mask=a , return_token_type_ids=a , return_offsets_mapping=a , add_special_tokens=a , ) lowercase__ = tokenizer_r.do_lower_case if hasattr(a , 'do_lower_case' ) else False lowercase__ = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'A'), ((1, 2), ','), ((3, 5), 'na'), ((5, 6), '##ï'), ((6, 8), '##ve'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'Allen'), ((21, 23), '##NL'), ((23, 24), '##P'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'a'), ((1, 2), ','), ((3, 8), 'naive'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'allen'), ((21, 23), '##nl'), ((23, 24), '##p'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['input_ids'] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['offset_mapping'] ) def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Dict: """simple docstring""" lowercase__ = ['的', '人', '有'] lowercase__ = ''.join(a ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowercase__ = True lowercase__ = self.tokenizer_class.from_pretrained(a , **a ) lowercase__ = self.rust_tokenizer_class.from_pretrained(a , **a ) lowercase__ = tokenizer_p.encode(a , add_special_tokens=a ) lowercase__ = tokenizer_r.encode(a , add_special_tokens=a ) lowercase__ = tokenizer_r.convert_ids_to_tokens(a ) lowercase__ = tokenizer_p.convert_ids_to_tokens(a ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(a , a ) self.assertListEqual(a , a ) lowercase__ = False lowercase__ = self.rust_tokenizer_class.from_pretrained(a , **a ) lowercase__ = self.tokenizer_class.from_pretrained(a , **a ) lowercase__ = tokenizer_r.encode(a , add_special_tokens=a ) lowercase__ = tokenizer_p.encode(a , add_special_tokens=a ) lowercase__ = tokenizer_r.convert_ids_to_tokens(a ) lowercase__ = tokenizer_p.convert_ids_to_tokens(a ) # it is expected that only the first Chinese character is not preceded by "##". lowercase__ = [ f"""##{token}""" if idx != 0 else token for idx, token in enumerate(a ) ] self.assertListEqual(a , a ) self.assertListEqual(a , a )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: stooge(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return arr def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase__ , lowercase__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) # Recursively sort last 2/3 elements stooge(_SCREAMING_SNAKE_CASE , i + t , (_SCREAMING_SNAKE_CASE) ) # Recursively sort first 2/3 elements stooge(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (h - t) ) if __name__ == "__main__": lowercase_ = input("""Enter numbers separated by a comma:\n""").strip() lowercase_ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
45
1
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[str] = ['image_processor', 'tokenizer'] _UpperCamelCase : Union[str, Any] = 'OwlViTImageProcessor' _UpperCamelCase : Dict = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[int] , a : Union[str, Any]=None , a : Dict=None , **a : Union[str, Any] )-> int: """simple docstring""" lowercase__ = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , a , ) lowercase__ = kwargs.pop('feature_extractor' ) lowercase__ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(a , a ) def __call__( self : Tuple , a : Tuple=None , a : Any=None , a : Tuple=None , a : Union[str, Any]="max_length" , a : int="np" , **a : Tuple )-> Union[str, Any]: """simple docstring""" if text is None and query_images is None and images is None: raise ValueError( 'You have to specify at least one text or query image or image. All three cannot be none.' ) if text is not None: if isinstance(a , a ) or (isinstance(a , a ) and not isinstance(text[0] , a )): lowercase__ = [self.tokenizer(a , padding=a , return_tensors=a , **a )] elif isinstance(a , a ) and isinstance(text[0] , a ): lowercase__ = [] # Maximum number of queries across batch lowercase__ = max([len(a ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(a ) != max_num_queries: lowercase__ = t + [' '] * (max_num_queries - len(a )) lowercase__ = self.tokenizer(a , padding=a , return_tensors=a , **a ) encodings.append(a ) else: raise TypeError('Input text should be a string, a list of strings or a nested list of strings' ) if return_tensors == "np": lowercase__ = np.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowercase__ = np.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowercase__ = jnp.concatenate([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowercase__ = jnp.concatenate([encoding['attention_mask'] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowercase__ = torch.cat([encoding['input_ids'] for encoding in encodings] , dim=0 ) lowercase__ = torch.cat([encoding['attention_mask'] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowercase__ = tf.stack([encoding['input_ids'] for encoding in encodings] , axis=0 ) lowercase__ = tf.stack([encoding['attention_mask'] for encoding in encodings] , axis=0 ) else: raise ValueError('Target return tensor type could not be returned' ) lowercase__ = BatchEncoding() lowercase__ = input_ids lowercase__ = attention_mask if query_images is not None: lowercase__ = BatchEncoding() lowercase__ = self.image_processor( a , return_tensors=a , **a ).pixel_values lowercase__ = query_pixel_values if images is not None: lowercase__ = self.image_processor(a , return_tensors=a , **a ) if text is not None and images is not None: lowercase__ = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowercase__ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : Any , *a : Optional[int] , **a : List[Any] )-> List[str]: """simple docstring""" return self.image_processor.post_process(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : Any , *a : Optional[Any] , **a : Tuple )-> Optional[Any]: """simple docstring""" return self.image_processor.post_process_object_detection(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : Any , *a : List[str] , **a : Any )-> int: """simple docstring""" return self.image_processor.post_process_image_guided_detection(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : Dict , *a : List[Any] , **a : Union[str, Any] )-> Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , *a : Optional[int] , **a : Tuple )-> Optional[Any]: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> str: """simple docstring""" warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , a , ) return self.image_processor
45
from scipy.stats import spearmanr import datasets lowercase_ = """ The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. """ lowercase_ = """ Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {'spearmanr': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric(\"spearmanr\") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results['spearmanr']) -0.7 >>> print(round(results['spearmanr_pvalue'], 2)) 0.19 """ lowercase_ = R"""\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE (datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'] , ) def SCREAMING_SNAKE_CASE_ ( self : int , a : str , a : Any , a : str=False )-> Optional[int]: """simple docstring""" lowercase__ = spearmanr(a , a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
45
1
import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow lowercase_ = logging.getLogger() @unittest.skip('Temporarily disable the doc tests.' ) @require_torch @require_tf @slow class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : Path , a : Union[str, None] = None , a : Union[List[str], None] = None , a : Union[str, List[str], None] = None , a : bool = True , )-> Union[str, Any]: """simple docstring""" lowercase__ = [file for file in os.listdir(a ) if os.path.isfile(os.path.join(a , a ) )] if identifier is not None: lowercase__ = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(a , a ): for n_ in n_identifier: lowercase__ = [file for file in files if n_ not in file] else: lowercase__ = [file for file in files if n_identifier not in file] lowercase__ = ignore_files or [] ignore_files.append('__init__.py' ) lowercase__ = [file for file in files if file not in ignore_files] for file in files: # Open all files print('Testing' , a ) if only_modules: lowercase__ = file.split('.' )[0] try: lowercase__ = getattr(a , a ) lowercase__ = doctest.DocTestSuite(a ) lowercase__ = unittest.TextTestRunner().run(a ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(f"""{module_identifier} is not a module.""" ) else: lowercase__ = doctest.testfile(str('..' / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = Path('src/transformers' ) lowercase__ = 'modeling' lowercase__ = [ 'modeling_ctrl.py', 'modeling_tf_ctrl.py', ] self.analyze_directory(a , identifier=a , ignore_files=a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> str: """simple docstring""" lowercase__ = Path('src/transformers' ) lowercase__ = 'tokenization' self.analyze_directory(a , identifier=a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = Path('src/transformers' ) lowercase__ = 'configuration' self.analyze_directory(a , identifier=a ) def SCREAMING_SNAKE_CASE_ ( self : int )-> str: """simple docstring""" lowercase__ = Path('src/transformers' ) lowercase__ = ['configuration', 'modeling', 'tokenization'] self.analyze_directory(a , n_identifier=a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Tuple: """simple docstring""" lowercase__ = Path('docs/source' ) lowercase__ = ['favicon.ico'] self.analyze_directory(a , ignore_files=a , only_modules=a )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
45
1
from math import factorial lowercase_ = {str(d): factorial(d) for d in range(10)} def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> int: return sum(DIGIT_FACTORIAL[d] for d in str(_SCREAMING_SNAKE_CASE ) ) def __UpperCamelCase () -> int: lowercase__ = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , _SCREAMING_SNAKE_CASE ) if sum_of_digit_factorial(_SCREAMING_SNAKE_CASE ) == i ) if __name__ == "__main__": print(f'''{solution() = }''')
45
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } lowercase_ = { """b0""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 224, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_280, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 240, """dropout_rate""": 0.2, """dw_padding""": [16], }, """b2""": { """hidden_dim""": 1_408, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 260, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 16], }, """b3""": { """hidden_dim""": 1_536, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 300, """dropout_rate""": 0.3, """dw_padding""": [5, 18], }, """b4""": { """hidden_dim""": 1_792, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 380, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_048, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 456, """dropout_rate""": 0.4, """dw_padding""": [13, 27], }, """b6""": { """hidden_dim""": 2_304, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 528, """dropout_rate""": 0.5, """dw_padding""": [31], }, """b7""": { """hidden_dim""": 2_560, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 600, """dropout_rate""": 0.5, """dw_padding""": [18], }, } def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = EfficientNetConfig() lowercase__ = CONFIG_MAP[model_name]['hidden_dim'] lowercase__ = CONFIG_MAP[model_name]['width_coef'] lowercase__ = CONFIG_MAP[model_name]['depth_coef'] lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = CONFIG_MAP[model_name]['dropout_rate'] lowercase__ = CONFIG_MAP[model_name]['dw_padding'] lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = 1000 lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} return config def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = EfficientNetImageProcessor( size={'height': size, 'width': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=_SCREAMING_SNAKE_CASE , ) return preprocessor def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = [v.split('_' )[0].split('block' )[1] for v in original_param_names if v.startswith('block' )] lowercase__ = sorted(set(_SCREAMING_SNAKE_CASE ) ) lowercase__ = len(_SCREAMING_SNAKE_CASE ) lowercase__ = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} lowercase__ = [] rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight') ) rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight') ) rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias') ) rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean') ) rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var') ) for b in block_names: lowercase__ = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight') ) rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight') ) rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias') ) rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean') ) rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var') ) lowercase__ = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ = 'efficientnet.' + item[1] lowercase__ = 'classifier.weight' lowercase__ = 'classifier.bias' return key_mapping def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: lowercase__ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights='imagenet' , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=1000 , classifier_activation='softmax' , ) lowercase__ = original_model.trainable_variables lowercase__ = original_model.non_trainable_variables lowercase__ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ = param.numpy() lowercase__ = list(tf_params.keys() ) # Load HuggingFace model lowercase__ = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) lowercase__ = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() lowercase__ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('Converting parameters...' ) lowercase__ = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowercase__ = convert_image_processor(_SCREAMING_SNAKE_CASE ) lowercase__ = preprocessor(images=prepare_img() , return_tensors='pt' ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ = hf_model(**_SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits.detach().numpy() # Original model inference lowercase__ = False lowercase__ = CONFIG_MAP[model_name]['image_size'] lowercase__ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ = image.img_to_array(_SCREAMING_SNAKE_CASE ) lowercase__ = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) lowercase__ = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print('Model outputs match!' ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") lowercase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: return int(input_a == input_a == 0 ) def __UpperCamelCase () -> None: print('Truth Table of NOR Gate:' ) print('| Input 1 | Input 2 | Output |' ) print(F"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(F"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(F"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(F"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
import argparse import json import subprocess def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = [] lowercase__ = ( F"""curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"""" ' https://api.github.com/repos/huggingface/transformers/actions/runners' ) lowercase__ = subprocess.run(_SCREAMING_SNAKE_CASE , shell=_SCREAMING_SNAKE_CASE , stdout=subprocess.PIPE ) lowercase__ = output.stdout.decode('utf-8' ) lowercase__ = json.loads(_SCREAMING_SNAKE_CASE ) lowercase__ = status['runners'] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_SCREAMING_SNAKE_CASE ) # save the result so we can report them on Slack with open('offline_runners.txt' , 'w' ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) if len(_SCREAMING_SNAKE_CASE ) > 0: lowercase__ = '\n'.join([x['name'] for x in offline_runners] ) raise ValueError(F"""The following runners are offline:\n{failed}""" ) if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return values.split(',' ) lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--target_runners""", default=None, type=list_str, required=True, help="""Comma-separated list of runners to check status.""", ) parser.add_argument( """--token""", default=None, type=str, required=True, help="""A token that has actions:read permission.""" ) lowercase_ = parser.parse_args() get_runner_status(args.target_runners, args.token)
45
1
from __future__ import annotations from collections.abc import Sequence from typing import Literal def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str | Literal[False]: lowercase__ = list(_SCREAMING_SNAKE_CASE ) lowercase__ = list(_SCREAMING_SNAKE_CASE ) lowercase__ = 0 for i in range(len(_SCREAMING_SNAKE_CASE ) ): if lista[i] != lista[i]: count += 1 lowercase__ = '_' if count > 1: return False else: return "".join(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[str]: lowercase__ = [] while True: lowercase__ = ['$'] * len(_SCREAMING_SNAKE_CASE ) lowercase__ = [] for i in range(len(_SCREAMING_SNAKE_CASE ) ): for j in range(i + 1 , len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = compare_string(binary[i] , binary[j] ) if k is False: lowercase__ = '*' lowercase__ = '*' temp.append('X' ) for i in range(len(_SCREAMING_SNAKE_CASE ) ): if checka[i] == "$": pi.append(binary[i] ) if len(_SCREAMING_SNAKE_CASE ) == 0: return pi lowercase__ = list(set(_SCREAMING_SNAKE_CASE ) ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[str]: lowercase__ = [] for minterm in minterms: lowercase__ = '' for _ in range(_SCREAMING_SNAKE_CASE ): lowercase__ = str(minterm % 2 ) + string minterm //= 2 temp.append(_SCREAMING_SNAKE_CASE ) return temp def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> bool: lowercase__ = list(_SCREAMING_SNAKE_CASE ) lowercase__ = list(_SCREAMING_SNAKE_CASE ) lowercase__ = 0 for i in range(len(_SCREAMING_SNAKE_CASE ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[str]: lowercase__ = [] lowercase__ = [0] * len(_SCREAMING_SNAKE_CASE ) for i in range(len(chart[0] ) ): lowercase__ = 0 lowercase__ = -1 for j in range(len(_SCREAMING_SNAKE_CASE ) ): if chart[j][i] == 1: count += 1 lowercase__ = j if count == 1: lowercase__ = 1 for i in range(len(_SCREAMING_SNAKE_CASE ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = 0 temp.append(prime_implicants[i] ) while True: lowercase__ = 0 lowercase__ = -1 lowercase__ = 0 for i in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = chart[i].count(1 ) if count_n > max_n: lowercase__ = count_n lowercase__ = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = 0 def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[list[int]]: lowercase__ = [[0 for x in range(len(_SCREAMING_SNAKE_CASE ) )] for x in range(len(_SCREAMING_SNAKE_CASE ) )] for i in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = prime_implicants[i].count('_' ) for j in range(len(_SCREAMING_SNAKE_CASE ) ): if is_for_table(prime_implicants[i] , binary[j] , _SCREAMING_SNAKE_CASE ): lowercase__ = 1 return chart def __UpperCamelCase () -> None: lowercase__ = int(input('Enter the no. of variables\n' ) ) lowercase__ = [ float(_SCREAMING_SNAKE_CASE ) for x in input( 'Enter the decimal representation of Minterms \'Spaces Separated\'\n' ).split() ] lowercase__ = decimal_to_binary(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = check(_SCREAMING_SNAKE_CASE ) print('Prime Implicants are:' ) print(_SCREAMING_SNAKE_CASE ) lowercase__ = prime_implicant_chart(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = selection(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) print('Essential Prime Implicants are:' ) print(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Tuple = 'ClapFeatureExtractor' _UpperCamelCase : Union[str, Any] = ('RobertaTokenizer', 'RobertaTokenizerFast') def __init__( self : List[Any] , a : int , a : str )-> Any: """simple docstring""" super().__init__(a , a ) def __call__( self : Any , a : Tuple=None , a : Optional[int]=None , a : int=None , **a : Optional[int] )-> Union[str, Any]: """simple docstring""" lowercase__ = kwargs.pop('sampling_rate' , a ) if text is None and audios is None: raise ValueError('You have to specify either text or audios. Both cannot be none.' ) if text is not None: lowercase__ = self.tokenizer(a , return_tensors=a , **a ) if audios is not None: lowercase__ = self.feature_extractor( a , sampling_rate=a , return_tensors=a , **a ) if text is not None and audios is not None: lowercase__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a ) , tensor_type=a ) def SCREAMING_SNAKE_CASE_ ( self : str , *a : Dict , **a : int )-> Optional[int]: """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *a : int , **a : Dict )-> Dict: """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" lowercase__ = self.tokenizer.model_input_names lowercase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
45
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase ) class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : str = field(default='image-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _UpperCamelCase : ClassVar[Features] = Features({'image': Image()} ) _UpperCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _UpperCamelCase : str = "image" _UpperCamelCase : str = "labels" def SCREAMING_SNAKE_CASE_ ( self : str , a : Optional[int] )-> str: """simple docstring""" if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , a ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) lowercase__ = copy.deepcopy(self ) lowercase__ = self.label_schema.copy() lowercase__ = features[self.label_column] lowercase__ = label_schema return task_template @property def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Dict[str, str]: """simple docstring""" return { self.image_column: "image", self.label_column: "labels", }
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} lowercase_ = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json""" ), }, } lowercase_ = { """moussaKam/mbarthez""": 1_024, """moussaKam/barthez""": 1_024, """moussaKam/barthez-orangesum-title""": 1_024, } lowercase_ = """▁""" class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Dict = VOCAB_FILES_NAMES _UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Optional[Any] = ['input_ids', 'attention_mask'] _UpperCamelCase : int = BarthezTokenizer def __init__( self : List[Any] , a : Union[str, Any]=None , a : Optional[Any]=None , a : Dict="<s>" , a : Union[str, Any]="</s>" , a : List[str]="</s>" , a : Optional[Any]="<s>" , a : int="<unk>" , a : str="<pad>" , a : Optional[int]="<mask>" , **a : Union[str, Any] , )-> Tuple: """simple docstring""" lowercase__ = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) lowercase__ = vocab_file lowercase__ = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__ = [self.cls_token_id] lowercase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None )-> List[int]: """simple docstring""" lowercase__ = [self.sep_token_id] lowercase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE_ ( self : Dict , a : str , a : Optional[str] = None )-> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ = os.path.join( a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
45
1
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 DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> int: lowercase__ = [] 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"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.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 "vit" from all keys that start with "vit" lowercase__ = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): if base_model: lowercase__ = '' else: lowercase__ = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowercase__ = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) lowercase__ = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict lowercase__ = in_proj_weight[ : config.hidden_size, : ] lowercase__ = in_proj_bias[: config.hidden_size] lowercase__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowercase__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowercase__ = in_proj_weight[ -config.hidden_size :, : ] lowercase__ = in_proj_bias[-config.hidden_size :] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: lowercase__ = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: lowercase__ = dct.pop(_SCREAMING_SNAKE_CASE ) lowercase__ = val def __UpperCamelCase () -> str: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: lowercase__ = ViTConfig() lowercase__ = False # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size if vit_name[-5:] == "in21k": lowercase__ = True lowercase__ = int(vit_name[-12:-10] ) lowercase__ = int(vit_name[-9:-6] ) else: lowercase__ = 1000 lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} lowercase__ = int(vit_name[-6:-4] ) lowercase__ = int(vit_name[-3:] ) # size of the architecture if "deit" in vit_name: if vit_name[9:].startswith('tiny' ): lowercase__ = 192 lowercase__ = 768 lowercase__ = 12 lowercase__ = 3 elif vit_name[9:].startswith('small' ): lowercase__ = 384 lowercase__ = 1536 lowercase__ = 12 lowercase__ = 6 else: pass else: if vit_name[4:].startswith('small' ): lowercase__ = 768 lowercase__ = 2304 lowercase__ = 8 lowercase__ = 8 elif vit_name[4:].startswith('base' ): pass elif vit_name[4:].startswith('large' ): lowercase__ = 1024 lowercase__ = 4096 lowercase__ = 24 lowercase__ = 16 elif vit_name[4:].startswith('huge' ): lowercase__ = 1280 lowercase__ = 5120 lowercase__ = 32 lowercase__ = 16 # load original model from timm lowercase__ = timm.create_model(_SCREAMING_SNAKE_CASE , pretrained=_SCREAMING_SNAKE_CASE ) timm_model.eval() # load state_dict of original model, remove and rename some keys lowercase__ = timm_model.state_dict() if base_model: remove_classification_head_(_SCREAMING_SNAKE_CASE ) lowercase__ = create_rename_keys(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) read_in_q_k_v(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # load HuggingFace model if vit_name[-5:] == "in21k": lowercase__ = ViTModel(_SCREAMING_SNAKE_CASE ).eval() else: lowercase__ = ViTForImageClassification(_SCREAMING_SNAKE_CASE ).eval() model.load_state_dict(_SCREAMING_SNAKE_CASE ) # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor if "deit" in vit_name: lowercase__ = DeiTImageProcessor(size=config.image_size ) else: lowercase__ = ViTImageProcessor(size=config.image_size ) lowercase__ = image_processor(images=prepare_img() , return_tensors='pt' ) lowercase__ = encoding['pixel_values'] lowercase__ = model(_SCREAMING_SNAKE_CASE ) if base_model: lowercase__ = timm_model.forward_features(_SCREAMING_SNAKE_CASE ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(_SCREAMING_SNAKE_CASE , outputs.pooler_output , atol=1E-3 ) else: lowercase__ = timm_model(_SCREAMING_SNAKE_CASE ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_SCREAMING_SNAKE_CASE , outputs.logits , atol=1E-3 ) Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--vit_name""", default="""vit_base_patch16_224""", type=str, help="""Name of the ViT 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.""" ) lowercase_ = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)
45
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 SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : List[Any] = StableDiffusionSAGPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" torch.manual_seed(0 ) lowercase__ = 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 , ) lowercase__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) lowercase__ = 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 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) lowercase__ = CLIPTextModel(a ) lowercase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) lowercase__ = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[int] , a : Any=0 )-> Union[str, Any]: """simple docstring""" if str(a ).startswith('mps' ): lowercase__ = torch.manual_seed(a ) else: lowercase__ = torch.Generator(device=a ).manual_seed(a ) lowercase__ = { 'prompt': '.', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 1.0, 'sag_scale': 1.0, 'output_type': 'numpy', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any )-> Any: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> int: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> Optional[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' ) lowercase__ = output.images lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base' ) lowercase__ = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) lowercase__ = '.' lowercase__ = torch.manual_seed(0 ) lowercase__ = sag_pipe( [prompt] , width=768 , height=512 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='np' , ) lowercase__ = output.images assert image.shape == (1, 512, 768, 3)
45
1
from __future__ import annotations def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[int]: lowercase__ = 2 lowercase__ = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(_SCREAMING_SNAKE_CASE ) if n > 1: factors.append(_SCREAMING_SNAKE_CASE ) return factors if __name__ == "__main__": import doctest doctest.testmod()
45
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 lowercase_ = logging.get_logger(__name__) lowercase_ = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : Any = 'deit' def __init__( self : Any , a : Union[str, Any]=768 , a : Optional[Any]=12 , a : Union[str, Any]=12 , a : Optional[int]=3_072 , a : Optional[int]="gelu" , a : Optional[Any]=0.0 , a : List[Any]=0.0 , a : int=0.02 , a : List[str]=1E-1_2 , a : Optional[int]=224 , a : Tuple=16 , a : List[Any]=3 , a : List[str]=True , a : Any=16 , **a : Union[str, Any] , )-> int: """simple docstring""" super().__init__(**a ) lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = qkv_bias lowercase__ = encoder_stride class SCREAMING_SNAKE_CASE (UpperCAmelCase ): _UpperCamelCase : List[Any] = version.parse('1.11' ) @property def SCREAMING_SNAKE_CASE_ ( self : int )-> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self : Any )-> float: """simple docstring""" return 1E-4
45
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""", # See all Dinat models at https://huggingface.co/models?filter=dinat } class SCREAMING_SNAKE_CASE (UpperCAmelCase , UpperCAmelCase ): _UpperCamelCase : int = 'dinat' _UpperCamelCase : Any = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , a : Optional[int]=4 , a : Tuple=3 , a : List[str]=64 , a : Optional[Any]=[3, 4, 6, 5] , a : Tuple=[2, 4, 8, 16] , a : Union[str, Any]=7 , a : Optional[Any]=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , a : int=3.0 , a : Union[str, Any]=True , a : Any=0.0 , a : Optional[Any]=0.0 , a : int=0.1 , a : Dict="gelu" , a : List[str]=0.02 , a : List[str]=1E-5 , a : Any=0.0 , a : Optional[int]=None , a : Optional[Any]=None , **a : Union[str, Any] , )-> Optional[int]: """simple docstring""" super().__init__(**a ) lowercase__ = patch_size lowercase__ = num_channels lowercase__ = embed_dim lowercase__ = depths lowercase__ = len(a ) lowercase__ = num_heads lowercase__ = kernel_size lowercase__ = dilations lowercase__ = mlp_ratio lowercase__ = qkv_bias lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = drop_path_rate lowercase__ = hidden_act lowercase__ = layer_norm_eps lowercase__ = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowercase__ = int(embed_dim * 2 ** (len(a ) - 1) ) lowercase__ = layer_scale_init_value lowercase__ = ['stem'] + [f"""stage{idx}""" for idx in range(1 , len(a ) + 1 )] lowercase__ , lowercase__ = get_aligned_output_features_output_indices( out_features=a , out_indices=a , stage_names=self.stage_names )
45
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 ) -> List[Any]: lowercase__ = None if token is not None: lowercase__ = {'Accept': 'application/vnd.github+json', 'Authorization': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) lowercase__ = '636036' lowercase__ = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" lowercase__ = requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).json() return result["workflow_runs"] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = get_daily_ci_runs(_SCREAMING_SNAKE_CASE ) lowercase__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": lowercase__ = workflow_run['id'] break return workflow_run_id def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: lowercase__ = get_last_daily_ci_runs(_SCREAMING_SNAKE_CASE ) if workflow_run_id is not None: lowercase__ = get_artifacts_links(worflow_run_id=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) for artifact_name in artifact_names: if artifact_name in artifacts_links: lowercase__ = artifacts_links[artifact_name] download_artifact( artifact_name=_SCREAMING_SNAKE_CASE , artifact_url=_SCREAMING_SNAKE_CASE , output_dir=_SCREAMING_SNAKE_CASE , token=_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: get_last_daily_ci_artifacts(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowercase__ = {} for artifact_name in artifact_names: lowercase__ = os.path.join(_SCREAMING_SNAKE_CASE , F"""{artifact_name}.zip""" ) if os.path.isfile(_SCREAMING_SNAKE_CASE ): lowercase__ = {} with zipfile.ZipFile(_SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): # read the file with z.open(_SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read().decode('UTF-8' ) return results
45
1
lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: """Sunday""", 1: """Monday""", 2: """Tuesday""", 3: """Wednesday""", 4: """Thursday""", 5: """Friday""", 6: """Saturday""", } def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: assert len(str(_SCREAMING_SNAKE_CASE ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: lowercase__ = year // 100 lowercase__ = (5 * (century % 4) + 2) % 7 lowercase__ = year % 100 lowercase__ = centurian % 12 lowercase__ = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 lowercase__ = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) lowercase__ = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device lowercase_ = False class SCREAMING_SNAKE_CASE (unittest.TestCase ): pass @nightly @require_torch_gpu class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(a ) lowercase__ = VersatileDiffusionPipeline.from_pretrained(a , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = generator.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt='first prompt' , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> List[str]: """simple docstring""" lowercase__ = VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) lowercase__ = 'cyberpunk 2077' lowercase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.dual_guided( prompt=a , image=a , text_to_image_strength=0.75 , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = 'A painting of a squirrel eating a burger ' lowercase__ = torch.manual_seed(0 ) lowercase__ = pipe.text_to_image( prompt=a , generator=a , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowercase__ = pipe.image_variation(a , generator=a , output_type='numpy' ).images lowercase__ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) lowercase__ = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
45
1
import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Dict: lowercase__ = FileLock(str(tmpdir / 'foo.lock' ) ) lowercase__ = FileLock(str(tmpdir / 'foo.lock' ) ) lowercase__ = 0.0_1 with locka.acquire(): with pytest.raises(_SCREAMING_SNAKE_CASE ): lowercase__ = time.time() locka.acquire(_SCREAMING_SNAKE_CASE ) assert time.time() - _start > timeout def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: lowercase__ = 'a' * 1000 + '.lock' lowercase__ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('.lock' ) assert not locka._lock_file.endswith(_SCREAMING_SNAKE_CASE ) assert len(os.path.basename(locka._lock_file ) ) <= 255 lowercase__ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(_SCREAMING_SNAKE_CASE ): locka.acquire(0 )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) if len(_SCREAMING_SNAKE_CASE ) == 1: return True lowercase__ = series[1] - series[0] for index in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> float: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('Input series is not valid, valid series - [2, 4, 6]' ) if len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError('Input list must be a non empty list' ) lowercase__ = 0 for val in series: answer += val return answer / len(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
from __future__ import annotations from PIL import Image # Define glider example lowercase_ = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example lowercase_ = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> list[list[int]]: lowercase__ = [] for i in range(len(_SCREAMING_SNAKE_CASE ) ): lowercase__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours lowercase__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(_SCREAMING_SNAKE_CASE ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(_SCREAMING_SNAKE_CASE ) - 1: neighbour_count += cells[i + 1][j] if i < len(_SCREAMING_SNAKE_CASE ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. lowercase__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(_SCREAMING_SNAKE_CASE ) return next_generation def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[Image.Image]: lowercase__ = [] for _ in range(_SCREAMING_SNAKE_CASE ): # Create output image lowercase__ = Image.new('RGB' , (len(cells[0] ), len(_SCREAMING_SNAKE_CASE )) ) lowercase__ = img.load() # Save cells to image for x in range(len(_SCREAMING_SNAKE_CASE ) ): for y in range(len(cells[0] ) ): lowercase__ = 255 - cells[y][x] * 255 lowercase__ = (colour, colour, colour) # Save image images.append(_SCREAMING_SNAKE_CASE ) lowercase__ = new_generation(_SCREAMING_SNAKE_CASE ) return images if __name__ == "__main__": lowercase_ = generate_images(GLIDER, 16) images[0].save("""out.gif""", save_all=True, append_images=images[1:])
45
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
1
from __future__ import annotations import math from collections.abc import Callable def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 100 , ) -> float: lowercase__ = x_start lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) lowercase__ = 0.0 for _ in range(_SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase__ = (x_end - x_start) / steps + xa lowercase__ = fnc(_SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase__ = xa lowercase__ = fxa return length if __name__ == "__main__": def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> str: return math.sin(10 * x ) print("""f(x) = sin(10 * x)""") print("""The length of the curve from x = -10 to x = 10 is:""") lowercase_ = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
45
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
1
from __future__ import annotations import math import random from collections.abc import Collection from typing import overload class SCREAMING_SNAKE_CASE : def __init__( self : Optional[int] , a : Collection[float] | None = None )-> None: """simple docstring""" if components is None: lowercase__ = [] lowercase__ = list(a ) def __len__( self : int )-> int: """simple docstring""" return len(self.__components ) def __str__( self : Dict )-> str: """simple docstring""" return "(" + ",".join(map(a , self.__components ) ) + ")" def __add__( self : str , a : Vector )-> Vector: """simple docstring""" lowercase__ = len(self ) if size == len(a ): lowercase__ = [self.__components[i] + other.component(a ) for i in range(a )] return Vector(a ) else: raise Exception('must have the same size' ) def __sub__( self : List[Any] , a : Vector )-> Vector: """simple docstring""" lowercase__ = len(self ) if size == len(a ): lowercase__ = [self.__components[i] - other.component(a ) for i in range(a )] return Vector(a ) else: # error case raise Exception('must have the same size' ) @overload def __mul__( self : List[str] , a : float )-> Vector: """simple docstring""" ... @overload def __mul__( self : Any , a : Vector )-> float: """simple docstring""" ... def __mul__( self : Optional[Any] , a : float | Vector )-> float | Vector: """simple docstring""" if isinstance(a , (float, int) ): lowercase__ = [c * other for c in self.__components] return Vector(a ) elif isinstance(a , a ) and len(self ) == len(a ): lowercase__ = len(self ) lowercase__ = [self.__components[i] * other.component(a ) for i in range(a )] return sum(a ) else: # error case raise Exception('invalid operand!' ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> Vector: """simple docstring""" return Vector(self.__components ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , a : int )-> float: """simple docstring""" if isinstance(a , a ) and -len(self.__components ) <= i < len(self.__components ): return self.__components[i] else: raise Exception('index out of range' ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : int , a : float )-> None: """simple docstring""" assert -len(self.__components ) <= pos < len(self.__components ) lowercase__ = value def SCREAMING_SNAKE_CASE_ ( self : Optional[int] )-> float: """simple docstring""" if len(self.__components ) == 0: raise Exception('Vector is empty' ) lowercase__ = [c**2 for c in self.__components] return math.sqrt(sum(a ) ) def SCREAMING_SNAKE_CASE_ ( self : str , a : Vector , a : bool = False )-> float: """simple docstring""" lowercase__ = self * other lowercase__ = self.euclidean_length() * other.euclidean_length() if deg: return math.degrees(math.acos(num / den ) ) else: return math.acos(num / den ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Vector: assert isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return Vector([0] * dimension ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Vector: assert isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and (isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )) lowercase__ = [0] * dimension lowercase__ = 1 return Vector(_SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Vector: assert ( isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and (isinstance(_SCREAMING_SNAKE_CASE , (int, float) )) ) return x * scalar + y def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Vector: random.seed(_SCREAMING_SNAKE_CASE ) lowercase__ = [random.randint(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE )] return Vector(_SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE : def __init__( self : str , a : list[list[float]] , a : int , a : int )-> None: """simple docstring""" lowercase__ = matrix lowercase__ = w lowercase__ = h def __str__( self : List[Any] )-> str: """simple docstring""" lowercase__ = '' for i in range(self.__height ): ans += "|" for j in range(self.__width ): if j < self.__width - 1: ans += str(self.__matrix[i][j] ) + "," else: ans += str(self.__matrix[i][j] ) + "|\n" return ans def __add__( self : int , a : Matrix )-> Matrix: """simple docstring""" if self.__width == other.width() and self.__height == other.height(): lowercase__ = [] for i in range(self.__height ): lowercase__ = [ self.__matrix[i][j] + other.component(a , a ) for j in range(self.__width ) ] matrix.append(a ) return Matrix(a , self.__width , self.__height ) else: raise Exception('matrix must have the same dimension!' ) def __sub__( self : Optional[Any] , a : Matrix )-> Matrix: """simple docstring""" if self.__width == other.width() and self.__height == other.height(): lowercase__ = [] for i in range(self.__height ): lowercase__ = [ self.__matrix[i][j] - other.component(a , a ) for j in range(self.__width ) ] matrix.append(a ) return Matrix(a , self.__width , self.__height ) else: raise Exception('matrices must have the same dimension!' ) @overload def __mul__( self : Dict , a : float )-> Matrix: """simple docstring""" ... @overload def __mul__( self : str , a : Vector )-> Vector: """simple docstring""" ... def __mul__( self : int , a : float | Vector )-> Vector | Matrix: """simple docstring""" if isinstance(a , a ): # matrix-vector if len(a ) == self.__width: lowercase__ = zero_vector(self.__height ) for i in range(self.__height ): lowercase__ = [ self.__matrix[i][j] * other.component(a ) for j in range(self.__width ) ] ans.change_component(a , sum(a ) ) return ans else: raise Exception( 'vector must have the same size as the ' 'number of columns of the matrix!' ) elif isinstance(a , (int, float) ): # matrix-scalar lowercase__ = [ [self.__matrix[i][j] * other for j in range(self.__width )] for i in range(self.__height ) ] return Matrix(a , self.__width , self.__height ) return None def SCREAMING_SNAKE_CASE_ ( self : Tuple )-> int: """simple docstring""" return self.__height def SCREAMING_SNAKE_CASE_ ( self : int )-> int: """simple docstring""" return self.__width def SCREAMING_SNAKE_CASE_ ( self : str , a : int , a : int )-> float: """simple docstring""" if 0 <= x < self.__height and 0 <= y < self.__width: return self.__matrix[x][y] else: raise Exception('change_component: indices out of bounds' ) def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : int , a : int , a : float )-> None: """simple docstring""" if 0 <= x < self.__height and 0 <= y < self.__width: lowercase__ = value else: raise Exception('change_component: indices out of bounds' ) def SCREAMING_SNAKE_CASE_ ( self : Any , a : int , a : int )-> float: """simple docstring""" if self.__height != self.__width: raise Exception('Matrix is not square' ) lowercase__ = self.__matrix[:x] + self.__matrix[x + 1 :] for i in range(len(a ) ): lowercase__ = minor[i][:y] + minor[i][y + 1 :] return Matrix(a , self.__width - 1 , self.__height - 1 ).determinant() def SCREAMING_SNAKE_CASE_ ( self : Dict , a : int , a : int )-> float: """simple docstring""" if self.__height != self.__width: raise Exception('Matrix is not square' ) if 0 <= x < self.__height and 0 <= y < self.__width: return (-1) ** (x + y) * self.minor(a , a ) else: raise Exception('Indices out of bounds' ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] )-> float: """simple docstring""" if self.__height != self.__width: raise Exception('Matrix is not square' ) if self.__height < 1: raise Exception('Matrix has no element' ) elif self.__height == 1: return self.__matrix[0][0] elif self.__height == 2: return ( self.__matrix[0][0] * self.__matrix[1][1] - self.__matrix[0][1] * self.__matrix[1][0] ) else: lowercase__ = [ self.__matrix[0][y] * self.cofactor(0 , a ) for y in range(self.__width ) ] return sum(a ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Matrix: lowercase__ = [[0] * n for _ in range(_SCREAMING_SNAKE_CASE )] return Matrix(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Matrix: random.seed(_SCREAMING_SNAKE_CASE ) lowercase__ = [ [random.randint(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE )] for _ in range(_SCREAMING_SNAKE_CASE ) ] return Matrix(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
45
def __UpperCamelCase (_SCREAMING_SNAKE_CASE = 50 ) -> int: lowercase__ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f'''{solution() = }''')
45
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = torch.device("""cpu""") def __UpperCamelCase () -> Tuple: lowercase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowercase__ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Any: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02] ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: lowercase__ = dct.pop(_SCREAMING_SNAKE_CASE ) lowercase__ = val def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[str]: lowercase__ = [] for k in state_dict.keys(): lowercase__ = k if ".pwconv" in k: lowercase__ = k_new.replace('.pwconv' , '.point_wise_conv' ) if ".dwconv" in k: lowercase__ = k_new.replace('.dwconv' , '.depth_wise_conv' ) if ".Proj." in k: lowercase__ = k_new.replace('.Proj.' , '.proj.' ) if "patch_embed" in k_new: lowercase__ = k_new.replace('patch_embed' , 'swiftformer.patch_embed.patch_embedding' ) if "network" in k_new: lowercase__ = k_new.split('.' ) if ls[2].isdigit(): lowercase__ = 'swiftformer.encoder.network.' + ls[1] + '.blocks.' + ls[2] + '.' + '.'.join(ls[3:] ) else: lowercase__ = k_new.replace('network' , 'swiftformer.encoder.network' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: lowercase__ = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size lowercase__ = 1000 lowercase__ = 'huggingface/label-files' lowercase__ = 'imagenet-1k-id2label.json' lowercase__ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) lowercase__ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": lowercase__ = [3, 3, 6, 4] lowercase__ = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": lowercase__ = [3, 3, 9, 6] lowercase__ = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": lowercase__ = [4, 3, 10, 5] lowercase__ = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": lowercase__ = [4, 4, 12, 6] lowercase__ = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('https' ): lowercase__ = torch.hub.load_state_dict_from_url(_SCREAMING_SNAKE_CASE , map_location='cpu' , check_hash=_SCREAMING_SNAKE_CASE ) else: lowercase__ = torch.load(_SCREAMING_SNAKE_CASE , map_location='cpu' ) lowercase__ = checkpoint lowercase__ = create_rename_keys(_SCREAMING_SNAKE_CASE ) for rename_key_src, rename_key_dest in rename_keys: rename_key(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # load HuggingFace model lowercase__ = SwiftFormerForImageClassification(_SCREAMING_SNAKE_CASE ).eval() hf_model.load_state_dict(_SCREAMING_SNAKE_CASE ) # prepare test inputs lowercase__ = prepare_img() lowercase__ = ViTImageProcessor.from_pretrained('preprocessor_config' ) lowercase__ = processor(images=_SCREAMING_SNAKE_CASE , return_tensors='pt' ) # compare outputs from both models lowercase__ = get_expected_output(_SCREAMING_SNAKE_CASE ) lowercase__ = hf_model(inputs['pixel_values'] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , _SCREAMING_SNAKE_CASE , atol=1E-3 ) Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowercase_ = 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.""") lowercase_ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
45
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class SCREAMING_SNAKE_CASE (UpperCAmelCase ): def __init__( self : Optional[Any] , a : UNetaDModel , a : UNetaDModel , a : DDPMScheduler , a : Any , )-> Dict: """simple docstring""" super().__init__() lowercase__ = value_function lowercase__ = unet lowercase__ = scheduler lowercase__ = env lowercase__ = env.get_dataset() lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].mean() except: # noqa: E722 pass lowercase__ = {} for key in self.data.keys(): try: lowercase__ = self.data[key].std() except: # noqa: E722 pass lowercase__ = env.observation_space.shape[0] lowercase__ = env.action_space.shape[0] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Any , a : int )-> Dict: """simple docstring""" return (x_in - self.means[key]) / self.stds[key] def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , a : str , a : List[str] )-> str: """simple docstring""" return x_in * self.stds[key] + self.means[key] def SCREAMING_SNAKE_CASE_ ( self : List[str] , a : Tuple )-> Tuple: """simple docstring""" if type(a ) is dict: return {k: self.to_torch(a ) for k, v in x_in.items()} elif torch.is_tensor(a ): return x_in.to(self.unet.device ) return torch.tensor(a , device=self.unet.device ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , a : Optional[int] , a : Dict , a : Optional[Any] )-> List[Any]: """simple docstring""" for key, val in cond.items(): lowercase__ = val.clone() return x_in def SCREAMING_SNAKE_CASE_ ( self : Tuple , a : Optional[Any] , a : Any , a : Optional[Any] , a : Optional[int] )-> List[Any]: """simple docstring""" lowercase__ = x.shape[0] lowercase__ = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model lowercase__ = torch.full((batch_size,) , a , device=self.unet.device , dtype=torch.long ) for _ in range(a ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models lowercase__ = self.value_function(x.permute(0 , 2 , 1 ) , a ).sample lowercase__ = torch.autograd.grad([y.sum()] , [x] )[0] lowercase__ = self.scheduler._get_variance(a ) lowercase__ = torch.exp(0.5 * posterior_variance ) lowercase__ = model_std * grad lowercase__ = 0 lowercase__ = x.detach() lowercase__ = x + scale * grad lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.unet(x.permute(0 , 2 , 1 ) , a ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg lowercase__ = self.scheduler.step(a , a , a , predict_epsilon=a )['prev_sample'] # apply conditions to the trajectory (set the initial state) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) return x, y def __call__( self : Any , a : Tuple , a : int=64 , a : Tuple=32 , a : List[Any]=2 , a : List[str]=0.1 )-> List[Any]: """simple docstring""" lowercase__ = self.normalize(a , 'observations' ) lowercase__ = obs[None].repeat(a , axis=0 ) lowercase__ = {0: self.to_torch(a )} lowercase__ = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) lowercase__ = randn_tensor(a , device=self.unet.device ) lowercase__ = self.reset_xa(a , a , self.action_dim ) lowercase__ = self.to_torch(a ) # run the diffusion process lowercase__ , lowercase__ = self.run_diffusion(a , a , a , a ) # sort output trajectories by value lowercase__ = y.argsort(0 , descending=a ).squeeze() lowercase__ = x[sorted_idx] lowercase__ = sorted_values[:, :, : self.action_dim] lowercase__ = actions.detach().cpu().numpy() lowercase__ = self.de_normalize(a , key='actions' ) # select the action with the highest value if y is not None: lowercase__ = 0 else: # if we didn't run value guiding, select a random action lowercase__ = np.random.randint(0 , a ) lowercase__ = denorm_actions[selected_index, 0] return denorm_actions
45
1
def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowercase__ = [] lowercase__ = [] lowercase__ = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator lowercase__ = len(_SCREAMING_SNAKE_CASE ) if (len(_SCREAMING_SNAKE_CASE ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(_SCREAMING_SNAKE_CASE ) , 'Postfix'.center(_SCREAMING_SNAKE_CASE ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_SCREAMING_SNAKE_CASE ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_SCREAMING_SNAKE_CASE ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_SCREAMING_SNAKE_CASE ) == 0: stack.append(_SCREAMING_SNAKE_CASE ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_SCREAMING_SNAKE_CASE ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_SCREAMING_SNAKE_CASE ) # push x to stack print( x.center(8 ) , (''.join(_SCREAMING_SNAKE_CASE )).ljust(_SCREAMING_SNAKE_CASE ) , (''.join(_SCREAMING_SNAKE_CASE )).ljust(_SCREAMING_SNAKE_CASE ) , sep=' | ' , ) # Output in tabular format while len(_SCREAMING_SNAKE_CASE ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(_SCREAMING_SNAKE_CASE )).ljust(_SCREAMING_SNAKE_CASE ) , (''.join(_SCREAMING_SNAKE_CASE )).ljust(_SCREAMING_SNAKE_CASE ) , sep=' | ' , ) # Output in tabular format return "".join(_SCREAMING_SNAKE_CASE ) # return Postfix as str def __UpperCamelCase (_SCREAMING_SNAKE_CASE ) -> List[Any]: lowercase__ = list(infix[::-1] ) # reverse the infix equation for i in range(len(_SCREAMING_SNAKE_CASE ) ): if infix[i] == "(": lowercase__ = ')' # change "(" to ")" elif infix[i] == ")": lowercase__ = '(' # change ")" to "(" return (infix_2_postfix(''.join(_SCREAMING_SNAKE_CASE ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": lowercase_ = input("""\nEnter an Infix Equation = """) # Input an Infix equation lowercase_ = """""".join(Infix.split()) # Remove spaces from the input print("""\n\t""", Infix, """(Infix) -> """, infix_2_prefix(Infix), """(Prefix)""")
45
from PIL import Image def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Image: def brightness(_SCREAMING_SNAKE_CASE ) -> float: return 128 + level + (c - 128) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)' ) return img.point(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change brightness to 100 lowercase_ = change_brightness(img, 100) brigt_img.save("""image_data/lena_brightness.png""", format="""png""")
45
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase_ = { """configuration_groupvit""": [ """GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GroupViTConfig""", """GroupViTOnnxConfig""", """GroupViTTextConfig""", """GroupViTVisionConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """GroupViTModel""", """GroupViTPreTrainedModel""", """GroupViTTextModel""", """GroupViTVisionModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFGroupViTModel""", """TFGroupViTPreTrainedModel""", """TFGroupViTTextModel""", """TFGroupViTVisionModel""", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
45
import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self : Any , a : str , a : List[Any]=7 , a : int=3 , a : int=18 , a : Optional[Any]=30 , a : Optional[int]=400 , a : int=True , a : Tuple=None , a : Optional[Any]=True , a : str=False , a : str=True , a : int=True , a : Tuple=[0.5, 0.5, 0.5] , a : Any=[0.5, 0.5, 0.5] , )-> Optional[int]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size if size is not None else {'height': 18, 'width': 20} lowercase__ = do_thumbnail lowercase__ = do_align_axis lowercase__ = do_pad lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (UpperCAmelCase , unittest.TestCase ): _UpperCamelCase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int )-> List[Any]: """simple docstring""" lowercase__ = DonutImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] )-> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Any )-> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a , 'do_resize' ) ) self.assertTrue(hasattr(a , 'size' ) ) self.assertTrue(hasattr(a , 'do_thumbnail' ) ) self.assertTrue(hasattr(a , 'do_align_long_axis' ) ) self.assertTrue(hasattr(a , 'do_pad' ) ) self.assertTrue(hasattr(a , 'do_normalize' ) ) self.assertTrue(hasattr(a , 'image_mean' ) ) self.assertTrue(hasattr(a , 'image_std' ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Dict: """simple docstring""" pass @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : str )-> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a ) for image in image_inputs: self.assertIsInstance(a , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] )-> Tuple: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , numpify=a ) for image in image_inputs: self.assertIsInstance(a , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def SCREAMING_SNAKE_CASE_ ( self : List[str] )-> Dict: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=a , torchify=a ) for image in image_inputs: self.assertIsInstance(a , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase__ = image_processing(a , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
45
1