code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] self.assertTrue(is_safetensors_compatible(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', # Removed: 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertTrue(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = [ 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertTrue(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Tuple ): # pass variant but use the non-variant filenames SCREAMING_SNAKE_CASE_ = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertTrue(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertFalse(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = [ 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertTrue(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : int ): # pass variant but use the non-variant filenames SCREAMING_SNAKE_CASE_ = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertTrue(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', # 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ = 'fp16' self.assertFalse(is_safetensors_compatible(_lowerCAmelCase , variant=_lowerCAmelCase ) )
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
from typing import Callable, Optional from .. import Features from ..packaged_modules.generator.generator import Generator from .abc import AbstractDatasetInputStream class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : Callable , _lowerCAmelCase : Optional[Features] = None , _lowerCAmelCase : str = None , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[dict] = None , _lowerCAmelCase : Optional[int] = None , **_lowerCAmelCase : Union[str, Any] , ): super().__init__( features=_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase , streaming=_lowerCAmelCase , num_proc=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = Generator( cache_dir=_lowerCAmelCase , features=_lowerCAmelCase , generator=_lowerCAmelCase , gen_kwargs=_lowerCAmelCase , **_lowerCAmelCase , ) def lowerCAmelCase_ ( self : Dict ): # Build iterable dataset if self.streaming: SCREAMING_SNAKE_CASE_ = self.builder.as_streaming_dataset(split='train' ) # Build regular (map-style) dataset else: SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None self.builder.download_and_prepare( download_config=_lowerCAmelCase , download_mode=_lowerCAmelCase , verification_mode=_lowerCAmelCase , base_path=_lowerCAmelCase , num_proc=self.num_proc , ) SCREAMING_SNAKE_CASE_ = self.builder.as_dataset( split='train' , verification_mode=_lowerCAmelCase , in_memory=self.keep_in_memory ) return dataset
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
import argparse import os from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_task_guides.py lowerCamelCase__ : Dict = 'src/transformers' lowerCamelCase__ : List[Any] = 'docs/source/en/tasks' def UpperCAmelCase_ ( __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Dict ) -> Optional[Any]: with open(__UpperCAmelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: SCREAMING_SNAKE_CASE_ = f.readlines() # Find the start prompt. SCREAMING_SNAKE_CASE_ = 0 while not lines[start_index].startswith(__UpperCAmelCase ): start_index += 1 start_index += 1 SCREAMING_SNAKE_CASE_ = start_index while not lines[end_index].startswith(__UpperCAmelCase ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # This is to make sure the transformers module imported is the one in the repo. lowerCamelCase__ : str = direct_transformers_import(TRANSFORMERS_PATH) lowerCamelCase__ : List[Any] = { 'asr.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, 'audio_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, 'language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, 'image_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, 'masked_language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, 'multiple_choice.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, 'object_detection.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, 'question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, 'semantic_segmentation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, 'sequence_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, 'summarization.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, 'token_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, 'translation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, 'video_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, 'document_question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, 'monocular_depth_estimation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). lowerCamelCase__ : Any = { 'summarization.md': ('nllb',), 'translation.md': ('nllb',), } def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = TASK_GUIDE_TO_MODELS[task_guide] SCREAMING_SNAKE_CASE_ = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(__UpperCAmelCase , set() ) SCREAMING_SNAKE_CASE_ = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([f"[{name}](../model_doc/{code})" for code, name in model_names.items()] ) + "\n" def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any]=False ) -> int: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = _find_text_in_file( filename=os.path.join(__UpperCAmelCase , __UpperCAmelCase ) , start_prompt='<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->' , end_prompt='<!--End of the generated tip-->' , ) SCREAMING_SNAKE_CASE_ = get_model_list_for_task(__UpperCAmelCase ) if current_list != new_list: if overwrite: with open(os.path.join(__UpperCAmelCase , __UpperCAmelCase ) , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:] ) else: raise ValueError( f"The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`" ' to fix this.' ) if __name__ == "__main__": lowerCamelCase__ : str = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') lowerCamelCase__ : Union[str, Any] = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
31
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['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 lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
import baseaa def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> bytes: return baseaa.aaaencode(string.encode('utf-8' ) ) def UpperCAmelCase_ ( __UpperCAmelCase : bytes ) -> str: return baseaa.aaadecode(__UpperCAmelCase ).decode('utf-8' ) if __name__ == "__main__": import doctest doctest.testmod()
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version lowerCamelCase__ : str = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') lowerCamelCase__ : Optional[int] = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) lowerCamelCase__ : str = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Optional[Any]: with open(__UpperCAmelCase , 'rb' ) as f: SCREAMING_SNAKE_CASE_ = Image.open(__UpperCAmelCase ) return im.convert('RGB' ) @dataclass class lowerCamelCase_ : '''simple docstring''' lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": "Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub)." } , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "A folder containing the training data."} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "A folder containing the validation data."} ) lowercase_ = field( default=0.1_5 , metadata={"help": "Percent to split off of train for validation."} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) def lowerCAmelCase_ ( self : List[Any] ): if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( 'You must specify either a dataset name from the hub or a train and/or validation directory.' ) @dataclass class lowerCamelCase_ : '''simple docstring''' lowercase_ = field( default="google/vit-base-patch16-224-in21k" , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(_SCREAMING_SNAKE_CASE )} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Where do you want to store the pretrained models downloaded from s3"} ) lowercase_ = field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Name or path of preprocessor config."} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."} , ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = torch.stack([example['pixel_values'] for example in examples] ) SCREAMING_SNAKE_CASE_ = torch.tensor([example['labels'] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def UpperCAmelCase_ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. SCREAMING_SNAKE_CASE_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('run_image_classification' , __UpperCAmelCase , __UpperCAmelCase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() SCREAMING_SNAKE_CASE_ = training_args.get_process_log_level() logger.setLevel(__UpperCAmelCase ) transformers.utils.logging.set_verbosity(__UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + f"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(f"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. SCREAMING_SNAKE_CASE_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. " 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: SCREAMING_SNAKE_CASE_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir , task='image-classification' , use_auth_token=True if model_args.use_auth_token else None , ) else: SCREAMING_SNAKE_CASE_ = {} if data_args.train_dir is not None: SCREAMING_SNAKE_CASE_ = os.path.join(data_args.train_dir , '**' ) if data_args.validation_dir is not None: SCREAMING_SNAKE_CASE_ = os.path.join(data_args.validation_dir , '**' ) SCREAMING_SNAKE_CASE_ = load_dataset( 'imagefolder' , data_files=__UpperCAmelCase , cache_dir=model_args.cache_dir , task='image-classification' , ) # If we don't have a validation split, split off a percentage of train as validation. SCREAMING_SNAKE_CASE_ = None if 'validation' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __UpperCAmelCase ) and data_args.train_val_split > 0.0: SCREAMING_SNAKE_CASE_ = dataset['train'].train_test_split(data_args.train_val_split ) SCREAMING_SNAKE_CASE_ = split['train'] SCREAMING_SNAKE_CASE_ = split['test'] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. SCREAMING_SNAKE_CASE_ = dataset['train'].features['labels'].names SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = {}, {} for i, label in enumerate(__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = str(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = label # Load the accuracy metric from the datasets package SCREAMING_SNAKE_CASE_ = evaluate.load('accuracy' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCAmelCase : Any ): return metric.compute(predictions=np.argmax(p.predictions , axis=1 ) , references=p.label_ids ) SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(__UpperCAmelCase ) , labelaid=__UpperCAmelCase , idalabel=__UpperCAmelCase , finetuning_task='image-classification' , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) SCREAMING_SNAKE_CASE_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: SCREAMING_SNAKE_CASE_ = image_processor.size['shortest_edge'] else: SCREAMING_SNAKE_CASE_ = (image_processor.size['height'], image_processor.size['width']) SCREAMING_SNAKE_CASE_ = Normalize(mean=image_processor.image_mean , std=image_processor.image_std ) SCREAMING_SNAKE_CASE_ = Compose( [ RandomResizedCrop(__UpperCAmelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) SCREAMING_SNAKE_CASE_ = Compose( [ Resize(__UpperCAmelCase ), CenterCrop(__UpperCAmelCase ), ToTensor(), normalize, ] ) def train_transforms(__UpperCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = [ _train_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image'] ] return example_batch def val_transforms(__UpperCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = [_val_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE_ = ( dataset['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(__UpperCAmelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE_ = ( dataset['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(__UpperCAmelCase ) # Initalize our trainer SCREAMING_SNAKE_CASE_ = Trainer( model=__UpperCAmelCase , args=__UpperCAmelCase , train_dataset=dataset['train'] if training_args.do_train else None , eval_dataset=dataset['validation'] if training_args.do_eval else None , compute_metrics=__UpperCAmelCase , tokenizer=__UpperCAmelCase , data_collator=__UpperCAmelCase , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE_ = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE_ = last_checkpoint SCREAMING_SNAKE_CASE_ = trainer.train(resume_from_checkpoint=__UpperCAmelCase ) trainer.save_model() trainer.log_metrics('train' , train_result.metrics ) trainer.save_metrics('train' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: SCREAMING_SNAKE_CASE_ = trainer.evaluate() trainer.log_metrics('eval' , __UpperCAmelCase ) trainer.save_metrics('eval' , __UpperCAmelCase ) # Write model card and (optionally) push to hub SCREAMING_SNAKE_CASE_ = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'image-classification', 'dataset': data_args.dataset_name, 'tags': ['image-classification', 'vision'], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCAmelCase ) else: trainer.create_model_card(**__UpperCAmelCase ) if __name__ == "__main__": main()
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 lowercase_ = None class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 2 @register_to_config def __init__( self : Optional[Any] , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 100 , _lowerCAmelCase : float = 1.007 , _lowerCAmelCase : float = 80 , _lowerCAmelCase : float = 0.05 , _lowerCAmelCase : float = 50 , ): # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE_ = sigma_max # setable values SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None # sigma(t_i) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : Optional[int] = None ): return sample def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int , _lowerCAmelCase : Union[str, torch.device] = None ): SCREAMING_SNAKE_CASE_ = num_inference_steps SCREAMING_SNAKE_CASE_ = np.arange(0 , self.num_inference_steps )[::-1].copy() SCREAMING_SNAKE_CASE_ = torch.from_numpy(_lowerCAmelCase ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase , dtype=torch.floataa , device=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : float , _lowerCAmelCase : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: SCREAMING_SNAKE_CASE_ = min(self.config.s_churn / self.num_inference_steps , 2**0.5 - 1 ) else: SCREAMING_SNAKE_CASE_ = 0 # sample eps ~ N(0, S_noise^2 * I) SCREAMING_SNAKE_CASE_ = self.config.s_noise * randn_tensor(sample.shape , generator=_lowerCAmelCase ).to(sample.device ) SCREAMING_SNAKE_CASE_ = sigma + gamma * sigma SCREAMING_SNAKE_CASE_ = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : float , _lowerCAmelCase : float , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : bool = True , ): SCREAMING_SNAKE_CASE_ = sample_hat + sigma_hat * model_output SCREAMING_SNAKE_CASE_ = (sample_hat - pred_original_sample) / sigma_hat SCREAMING_SNAKE_CASE_ = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=_lowerCAmelCase , derivative=_lowerCAmelCase , pred_original_sample=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : float , _lowerCAmelCase : float , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : torch.FloatTensor , _lowerCAmelCase : bool = True , ): SCREAMING_SNAKE_CASE_ = sample_prev + sigma_prev * model_output SCREAMING_SNAKE_CASE_ = (sample_prev - pred_original_sample) / sigma_prev SCREAMING_SNAKE_CASE_ = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=_lowerCAmelCase , derivative=_lowerCAmelCase , pred_original_sample=_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError()
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
1
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCamelCase__ : Dict = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")\n >>> pipe_prior.to("cuda")\n >>> prompt = "red cat, 4k photo"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> zero_image_emb = out.negative_image_embeds\n >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")\n >>> pipe.to("cuda")\n >>> image = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=50,\n ... ).images\n >>> image[0].save("cat.png")\n ```\n' def UpperCAmelCase_ ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Any=8 ) -> Dict: SCREAMING_SNAKE_CASE_ = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 SCREAMING_SNAKE_CASE_ = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , _lowerCAmelCase : UNetaDConditionModel , _lowerCAmelCase : DDPMScheduler , _lowerCAmelCase : VQModel , ): super().__init__() self.register_modules( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , movq=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any] ): if latents is None: SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase , dtype=_lowerCAmelCase ) else: if latents.shape != shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {shape}" ) SCREAMING_SNAKE_CASE_ = latents.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = latents * scheduler.init_noise_sigma return latents def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) SCREAMING_SNAKE_CASE_ = torch.device(F"cuda:{gpu_id}" ) SCREAMING_SNAKE_CASE_ = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Any=0 ): if is_accelerate_available() and is_accelerate_version('>=' , '0.17.0.dev0' ): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.' ) SCREAMING_SNAKE_CASE_ = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('cpu' , silence_dtype_warnings=_lowerCAmelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) SCREAMING_SNAKE_CASE_ = None for cpu_offloaded_model in [self.unet, self.movq]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cpu_offload_with_hook(_lowerCAmelCase , _lowerCAmelCase , prev_module_hook=_lowerCAmelCase ) # We'll offload the last model manually. SCREAMING_SNAKE_CASE_ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def lowerCAmelCase_ ( self : int ): if not hasattr(self.unet , '_hf_hook' ): return self.device for module in self.unet.modules(): if ( hasattr(_lowerCAmelCase , '_hf_hook' ) and hasattr(module._hf_hook , 'execution_device' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_lowerCAmelCase ) def __call__( self : Any , _lowerCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , _lowerCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 100 , _lowerCAmelCase : float = 4.0 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _lowerCAmelCase : Optional[torch.FloatTensor] = None , _lowerCAmelCase : Optional[str] = "pil" , _lowerCAmelCase : bool = True , ): SCREAMING_SNAKE_CASE_ = self._execution_device SCREAMING_SNAKE_CASE_ = guidance_scale > 1.0 if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = torch.cat(_lowerCAmelCase , dim=0 ) SCREAMING_SNAKE_CASE_ = image_embeds.shape[0] * num_images_per_prompt if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = torch.cat(_lowerCAmelCase , dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ = image_embeds.repeat_interleave(_lowerCAmelCase , dim=0 ) SCREAMING_SNAKE_CASE_ = negative_image_embeds.repeat_interleave(_lowerCAmelCase , dim=0 ) SCREAMING_SNAKE_CASE_ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_lowerCAmelCase ) self.scheduler.set_timesteps(_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.scheduler.timesteps SCREAMING_SNAKE_CASE_ = self.unet.config.in_channels SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = downscale_height_and_width(_lowerCAmelCase , _lowerCAmelCase , self.movq_scale_factor ) # create initial latent SCREAMING_SNAKE_CASE_ = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(_lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE_ = {'image_embeds': image_embeds} SCREAMING_SNAKE_CASE_ = self.unet( sample=_lowerCAmelCase , timestep=_lowerCAmelCase , encoder_hidden_states=_lowerCAmelCase , added_cond_kwargs=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = noise_pred.split(latents.shape[1] , dim=1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = variance_pred.chunk(2 ) SCREAMING_SNAKE_CASE_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) SCREAMING_SNAKE_CASE_ = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , 'variance_type' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE_ = self.scheduler.step( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , generator=_lowerCAmelCase , )[0] # post-processing SCREAMING_SNAKE_CASE_ = self.movq.decode(_lowerCAmelCase , force_not_quantize=_lowerCAmelCase )['sample'] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: SCREAMING_SNAKE_CASE_ = image * 0.5 + 0.5 SCREAMING_SNAKE_CASE_ = image.clamp(0 , 1 ) SCREAMING_SNAKE_CASE_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE_ = self.numpy_to_pil(_lowerCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=_lowerCAmelCase )
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: lowerCamelCase__ : int = None lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : str = {'vocab_file': 'sentencepiece.bpe.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : Union[str, Any] = { 'vocab_file': { 'facebook/nllb-200-distilled-600M': ( 'https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model' ), }, 'tokenizer_file': { 'facebook/nllb-200-distilled-600M': ( 'https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json' ), }, } lowerCamelCase__ : Optional[Any] = { 'facebook/nllb-large-en-ro': 1_024, 'facebook/nllb-200-distilled-600M': 1_024, } # fmt: off lowerCamelCase__ : List[Any] = ['ace_Arab', 'ace_Latn', 'acm_Arab', 'acq_Arab', 'aeb_Arab', 'afr_Latn', 'ajp_Arab', 'aka_Latn', 'amh_Ethi', 'apc_Arab', 'arb_Arab', 'ars_Arab', 'ary_Arab', 'arz_Arab', 'asm_Beng', 'ast_Latn', 'awa_Deva', 'ayr_Latn', 'azb_Arab', 'azj_Latn', 'bak_Cyrl', 'bam_Latn', 'ban_Latn', 'bel_Cyrl', 'bem_Latn', 'ben_Beng', 'bho_Deva', 'bjn_Arab', 'bjn_Latn', 'bod_Tibt', 'bos_Latn', 'bug_Latn', 'bul_Cyrl', 'cat_Latn', 'ceb_Latn', 'ces_Latn', 'cjk_Latn', 'ckb_Arab', 'crh_Latn', 'cym_Latn', 'dan_Latn', 'deu_Latn', 'dik_Latn', 'dyu_Latn', 'dzo_Tibt', 'ell_Grek', 'eng_Latn', 'epo_Latn', 'est_Latn', 'eus_Latn', 'ewe_Latn', 'fao_Latn', 'pes_Arab', 'fij_Latn', 'fin_Latn', 'fon_Latn', 'fra_Latn', 'fur_Latn', 'fuv_Latn', 'gla_Latn', 'gle_Latn', 'glg_Latn', 'grn_Latn', 'guj_Gujr', 'hat_Latn', 'hau_Latn', 'heb_Hebr', 'hin_Deva', 'hne_Deva', 'hrv_Latn', 'hun_Latn', 'hye_Armn', 'ibo_Latn', 'ilo_Latn', 'ind_Latn', 'isl_Latn', 'ita_Latn', 'jav_Latn', 'jpn_Jpan', 'kab_Latn', 'kac_Latn', 'kam_Latn', 'kan_Knda', 'kas_Arab', 'kas_Deva', 'kat_Geor', 'knc_Arab', 'knc_Latn', 'kaz_Cyrl', 'kbp_Latn', 'kea_Latn', 'khm_Khmr', 'kik_Latn', 'kin_Latn', 'kir_Cyrl', 'kmb_Latn', 'kon_Latn', 'kor_Hang', 'kmr_Latn', 'lao_Laoo', 'lvs_Latn', 'lij_Latn', 'lim_Latn', 'lin_Latn', 'lit_Latn', 'lmo_Latn', 'ltg_Latn', 'ltz_Latn', 'lua_Latn', 'lug_Latn', 'luo_Latn', 'lus_Latn', 'mag_Deva', 'mai_Deva', 'mal_Mlym', 'mar_Deva', 'min_Latn', 'mkd_Cyrl', 'plt_Latn', 'mlt_Latn', 'mni_Beng', 'khk_Cyrl', 'mos_Latn', 'mri_Latn', 'zsm_Latn', 'mya_Mymr', 'nld_Latn', 'nno_Latn', 'nob_Latn', 'npi_Deva', 'nso_Latn', 'nus_Latn', 'nya_Latn', 'oci_Latn', 'gaz_Latn', 'ory_Orya', 'pag_Latn', 'pan_Guru', 'pap_Latn', 'pol_Latn', 'por_Latn', 'prs_Arab', 'pbt_Arab', 'quy_Latn', 'ron_Latn', 'run_Latn', 'rus_Cyrl', 'sag_Latn', 'san_Deva', 'sat_Beng', 'scn_Latn', 'shn_Mymr', 'sin_Sinh', 'slk_Latn', 'slv_Latn', 'smo_Latn', 'sna_Latn', 'snd_Arab', 'som_Latn', 'sot_Latn', 'spa_Latn', 'als_Latn', 'srd_Latn', 'srp_Cyrl', 'ssw_Latn', 'sun_Latn', 'swe_Latn', 'swh_Latn', 'szl_Latn', 'tam_Taml', 'tat_Cyrl', 'tel_Telu', 'tgk_Cyrl', 'tgl_Latn', 'tha_Thai', 'tir_Ethi', 'taq_Latn', 'taq_Tfng', 'tpi_Latn', 'tsn_Latn', 'tso_Latn', 'tuk_Latn', 'tum_Latn', 'tur_Latn', 'twi_Latn', 'tzm_Tfng', 'uig_Arab', 'ukr_Cyrl', 'umb_Latn', 'urd_Arab', 'uzn_Latn', 'vec_Latn', 'vie_Latn', 'war_Latn', 'wol_Latn', 'xho_Latn', 'ydd_Hebr', 'yor_Latn', 'yue_Hant', 'zho_Hans', 'zho_Hant', 'zul_Latn'] class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = ["input_ids", "attention_mask"] lowercase_ = NllbTokenizer lowercase_ = [] lowercase_ = [] def __init__( self : Tuple , _lowerCAmelCase : Union[str, Any]=None , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : Union[str, Any]="<s>" , _lowerCAmelCase : Tuple="</s>" , _lowerCAmelCase : Any="</s>" , _lowerCAmelCase : Union[str, Any]="<s>" , _lowerCAmelCase : Dict="<unk>" , _lowerCAmelCase : int="<pad>" , _lowerCAmelCase : Optional[int]="<mask>" , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : Any=None , _lowerCAmelCase : str=None , _lowerCAmelCase : Union[str, Any]=False , **_lowerCAmelCase : List[str] , ): # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_ = AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token SCREAMING_SNAKE_CASE_ = legacy_behaviour super().__init__( vocab_file=_lowerCAmelCase , tokenizer_file=_lowerCAmelCase , bos_token=_lowerCAmelCase , eos_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , unk_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , src_lang=_lowerCAmelCase , tgt_lang=_lowerCAmelCase , additional_special_tokens=_lowerCAmelCase , legacy_behaviour=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True SCREAMING_SNAKE_CASE_ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) SCREAMING_SNAKE_CASE_ = { lang_code: self.convert_tokens_to_ids(_lowerCAmelCase ) for lang_code in FAIRSEQ_LANGUAGE_CODES } SCREAMING_SNAKE_CASE_ = src_lang if src_lang is not None else 'eng_Latn' SCREAMING_SNAKE_CASE_ = self.convert_tokens_to_ids(self._src_lang ) SCREAMING_SNAKE_CASE_ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def lowerCAmelCase_ ( self : Tuple ): return self._src_lang @src_lang.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] , _lowerCAmelCase : Optional[str] , **_lowerCAmelCase : Optional[Any] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) SCREAMING_SNAKE_CASE_ = src_lang SCREAMING_SNAKE_CASE_ = self(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.convert_tokens_to_ids(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tgt_lang_id return inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : str = "eng_Latn" , _lowerCAmelCase : Optional[List[str]] = None , _lowerCAmelCase : str = "fra_Latn" , **_lowerCAmelCase : List[str] , ): SCREAMING_SNAKE_CASE_ = src_lang SCREAMING_SNAKE_CASE_ = tgt_lang return super().prepare_seqaseq_batch(_lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def lowerCAmelCase_ ( self : Any ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = self.convert_tokens_to_ids(_lowerCAmelCase ) if self.legacy_behaviour: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [self.eos_token_id, self.cur_lang_code] else: SCREAMING_SNAKE_CASE_ = [self.cur_lang_code] SCREAMING_SNAKE_CASE_ = [self.eos_token_id] SCREAMING_SNAKE_CASE_ = self.convert_ids_to_tokens(self.prefix_tokens ) SCREAMING_SNAKE_CASE_ = self.convert_ids_to_tokens(self.suffix_tokens ) SCREAMING_SNAKE_CASE_ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = self.convert_tokens_to_ids(_lowerCAmelCase ) if self.legacy_behaviour: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [self.eos_token_id, self.cur_lang_code] else: SCREAMING_SNAKE_CASE_ = [self.cur_lang_code] SCREAMING_SNAKE_CASE_ = [self.eos_token_id] SCREAMING_SNAKE_CASE_ = self.convert_ids_to_tokens(self.prefix_tokens ) SCREAMING_SNAKE_CASE_ = self.convert_ids_to_tokens(self.suffix_tokens ) SCREAMING_SNAKE_CASE_ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory." ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
import qiskit def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> qiskit.result.counts.Counts: SCREAMING_SNAKE_CASE_ = qiskit.Aer.get_backend('aer_simulator' ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ = qiskit.QuantumCircuit(__UpperCAmelCase , __UpperCAmelCase ) # Map the quantum measurement to the classical bits circuit.measure([0] , [0] ) # Execute the circuit on the simulator SCREAMING_SNAKE_CASE_ = qiskit.execute(__UpperCAmelCase , __UpperCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(__UpperCAmelCase ) if __name__ == "__main__": print(f'''Total count for various states are: {single_qubit_measure(1, 1)}''')
31
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
import flax.linen as nn import jax import jax.numpy as jnp class lowerCamelCase_ ( nn.Module ): '''simple docstring''' lowercase_ = 42 lowercase_ = jnp.floataa def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : str , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = hidden_states.shape SCREAMING_SNAKE_CASE_ = jax.image.resize( _lowerCAmelCase , shape=(batch, height * 2, width * 2, channels) , method='nearest' , ) SCREAMING_SNAKE_CASE_ = self.conv(_lowerCAmelCase ) return hidden_states class lowerCamelCase_ ( nn.Module ): '''simple docstring''' lowercase_ = 42 lowercase_ = jnp.floataa def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : Tuple , _lowerCAmelCase : int ): # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) SCREAMING_SNAKE_CASE_ = self.conv(_lowerCAmelCase ) return hidden_states class lowerCamelCase_ ( nn.Module ): '''simple docstring''' lowercase_ = 42 lowercase_ = None lowercase_ = 0.0 lowercase_ = None lowercase_ = jnp.floataa def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.in_channels if self.out_channels is None else self.out_channels SCREAMING_SNAKE_CASE_ = nn.GroupNorm(num_groups=32 , epsilon=1E-5 ) SCREAMING_SNAKE_CASE_ = nn.Conv( _lowerCAmelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) SCREAMING_SNAKE_CASE_ = nn.Dense(_lowerCAmelCase , dtype=self.dtype ) SCREAMING_SNAKE_CASE_ = nn.GroupNorm(num_groups=32 , epsilon=1E-5 ) SCREAMING_SNAKE_CASE_ = nn.Dropout(self.dropout_prob ) SCREAMING_SNAKE_CASE_ = nn.Conv( _lowerCAmelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) SCREAMING_SNAKE_CASE_ = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut SCREAMING_SNAKE_CASE_ = None if use_nin_shortcut: SCREAMING_SNAKE_CASE_ = nn.Conv( _lowerCAmelCase , kernel_size=(1, 1) , strides=(1, 1) , padding='VALID' , dtype=self.dtype , ) def __call__( self : Union[str, Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any]=True ): SCREAMING_SNAKE_CASE_ = hidden_states SCREAMING_SNAKE_CASE_ = self.norma(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = nn.swish(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.conva(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.time_emb_proj(nn.swish(_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = jnp.expand_dims(jnp.expand_dims(_lowerCAmelCase , 1 ) , 1 ) SCREAMING_SNAKE_CASE_ = hidden_states + temb SCREAMING_SNAKE_CASE_ = self.norma(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = nn.swish(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dropout(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.conva(_lowerCAmelCase ) if self.conv_shortcut is not None: SCREAMING_SNAKE_CASE_ = self.conv_shortcut(_lowerCAmelCase ) return hidden_states + residual
31
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_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[Any] = { 'configuration_autoformer': [ 'AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AutoformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : List[str] = [ 'AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'AutoformerForPrediction', 'AutoformerModel', 'AutoformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
1
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = BertJapaneseTokenizer lowercase_ = False lowercase_ = True def lowerCAmelCase_ ( self : int ): super().setUp() SCREAMING_SNAKE_CASE_ = [ '[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは', '世界', '##世界', '、', '##、', '。', '##。', ] SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = 'こんにちは、世界。 \nこんばんは、世界。' SCREAMING_SNAKE_CASE_ = 'こんにちは 、 世界 。 こんばんは 、 世界 。' return input_text, output_text def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Tuple ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.get_input_output_texts(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.decode(_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase ) return text, ids def lowerCAmelCase_ ( self : str ): pass # TODO add if relevant def lowerCAmelCase_ ( self : Dict ): pass # TODO add if relevant def lowerCAmelCase_ ( self : List[Any] ): pass # TODO add if relevant def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('こんにちは、世界。\nこんばんは、世界。' ) self.assertListEqual(_lowerCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class(self.vocab_file , word_tokenizer_type='mecab' ) self.assertIsNotNone(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'こんにちは、世界。\nこんばんは、世界。' SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_lowerCAmelCase , 'wb' ) as handle: pickle.dump(_lowerCAmelCase , _lowerCAmelCase ) with open(_lowerCAmelCase , 'rb' ) as handle: SCREAMING_SNAKE_CASE_ = pickle.load(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_new.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = MecabTokenizer(mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def lowerCAmelCase_ ( self : int ): try: SCREAMING_SNAKE_CASE_ = MecabTokenizer(mecab_dic='unidic_lite' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def lowerCAmelCase_ ( self : Any ): try: SCREAMING_SNAKE_CASE_ = MecabTokenizer(mecab_dic='unidic' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = MecabTokenizer(do_lower_case=_lowerCAmelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iphone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def lowerCAmelCase_ ( self : List[Any] ): try: SCREAMING_SNAKE_CASE_ = MecabTokenizer( do_lower_case=_lowerCAmelCase , normalize_text=_lowerCAmelCase , mecab_option='-d /usr/local/lib/mecab/dic/jumandic' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '\u3000', '。'] , ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = MecabTokenizer(normalize_text=_lowerCAmelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', ' ', '。'] , ) @require_sudachi def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class(self.vocab_file , word_tokenizer_type='sudachi' ) self.assertIsNotNone(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'こんにちは、世界。\nこんばんは、世界。' SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_lowerCAmelCase , 'wb' ) as handle: pickle.dump(_lowerCAmelCase , _lowerCAmelCase ) with open(_lowerCAmelCase , 'rb' ) as handle: SCREAMING_SNAKE_CASE_ = pickle.load(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_new.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) @require_sudachi def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='A' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国', '人', '参政', '権'] ) @require_sudachi def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='B' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人', '参政権'] ) @require_sudachi def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='C' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人参政権'] ) @require_sudachi def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(do_lower_case=_lowerCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iphone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(normalize_text=_lowerCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', '\u3000', '。', ' ', ' '] , ) @require_sudachi def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = SudachiTokenizer(trim_whitespace=_lowerCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) @require_jumanpp def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class(self.vocab_file , word_tokenizer_type='jumanpp' ) self.assertIsNotNone(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'こんにちは、世界。\nこんばんは、世界。' SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(_lowerCAmelCase , 'wb' ) as handle: pickle.dump(_lowerCAmelCase , _lowerCAmelCase ) with open(_lowerCAmelCase , 'rb' ) as handle: SCREAMING_SNAKE_CASE_ = pickle.load(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_new.tokenize(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) @require_jumanpp def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = JumanppTokenizer(do_lower_case=_lowerCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iphone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = JumanppTokenizer(normalize_text=_lowerCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['ア', 'ッ', 'フ', '゚', 'ル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = JumanppTokenizer(trim_whitespace=_lowerCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '。'] , ) @require_jumanpp def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('ありがとうございますm(_ _)m見つけるのが大変です。' ) , ['ありがとう', 'ございます', 'm(_ _)m', '見つける', 'の', 'が', '大変です', '。'] , ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = ['[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは'] SCREAMING_SNAKE_CASE_ = {} for i, token in enumerate(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = i SCREAMING_SNAKE_CASE_ = WordpieceTokenizer(vocab=_lowerCAmelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こんにちは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは' ) , ['こん', '##ばんは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは こんばんにちは こんにちは' ) , ['こん', '##ばんは', '[UNK]', 'こんにちは'] ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = BertJapaneseTokenizer.from_pretrained('nlp-waseda/roberta-base-japanese-with-auto-jumanpp' ) SCREAMING_SNAKE_CASE_ = tokenizer.subword_tokenizer SCREAMING_SNAKE_CASE_ = subword_tokenizer.tokenize('国境 の 長い トンネル を 抜ける と 雪国 であった 。' ) self.assertListEqual(_lowerCAmelCase , ['▁国境', '▁の', '▁長い', '▁トンネル', '▁を', '▁抜ける', '▁と', '▁雪', '国', '▁であった', '▁。'] ) SCREAMING_SNAKE_CASE_ = subword_tokenizer.tokenize('こんばんは こんばん にち は こんにちは' ) self.assertListEqual(_lowerCAmelCase , ['▁こん', 'ばん', 'は', '▁こん', 'ばん', '▁に', 'ち', '▁は', '▁こんにちは'] ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese' ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('ありがとう。' , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('どういたしまして。' , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase , _lowerCAmelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = BertJapaneseTokenizer lowercase_ = False def lowerCAmelCase_ ( self : int ): super().setUp() SCREAMING_SNAKE_CASE_ = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def lowerCAmelCase_ ( self : List[Any] , **_lowerCAmelCase : Union[str, Any] ): return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='character' , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = 'こんにちは、世界。 \nこんばんは、世界。' SCREAMING_SNAKE_CASE_ = 'こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。' return input_text, output_text def lowerCAmelCase_ ( self : Optional[Any] ): pass # TODO add if relevant def lowerCAmelCase_ ( self : int ): pass # TODO add if relevant def lowerCAmelCase_ ( self : List[Any] ): pass # TODO add if relevant def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='character' ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('こんにちは、世界。 \nこんばんは、世界。' ) self.assertListEqual( _lowerCAmelCase , ['こ', 'ん', 'に', 'ち', 'は', '、', '世', '界', '。', 'こ', 'ん', 'ば', 'ん', 'は', '、', '世', '界', '。'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] SCREAMING_SNAKE_CASE_ = {} for i, token in enumerate(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = i SCREAMING_SNAKE_CASE_ = CharacterTokenizer(vocab=_lowerCAmelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こ', 'ん', 'に', 'ち', 'は'] ) self.assertListEqual(tokenizer.tokenize('こんにちほ' ) , ['こ', 'ん', 'に', 'ち', '[UNK]'] ) def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese-char' ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('ありがとう。' , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.encode('どういたしまして。' , add_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase , _lowerCAmelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = 'cl-tohoku/bert-base-japanese' SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained(_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = 'cl-tohoku/bert-base-japanese' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertTokenizer.from_pretrained(_lowerCAmelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) ) SCREAMING_SNAKE_CASE_ = 'bert-base-cased' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertJapaneseTokenizer.from_pretrained(_lowerCAmelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) )
31
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
1
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class lowerCamelCase_ : '''simple docstring''' lowercase_ = BlenderbotConfig lowercase_ = {} lowercase_ = "gelu" def __init__( self : List[str] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=13 , _lowerCAmelCase : Tuple=7 , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : Any=False , _lowerCAmelCase : Dict=99 , _lowerCAmelCase : Any=32 , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Tuple=4 , _lowerCAmelCase : List[str]=37 , _lowerCAmelCase : Optional[Any]=0.1 , _lowerCAmelCase : Dict=0.1 , _lowerCAmelCase : int=20 , _lowerCAmelCase : List[str]=2 , _lowerCAmelCase : List[str]=1 , _lowerCAmelCase : List[Any]=0 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = pad_token_id SCREAMING_SNAKE_CASE_ = bos_token_id def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE_ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) SCREAMING_SNAKE_CASE_ = prepare_blenderbot_inputs_dict(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return config, inputs_dict def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = TFBlenderbotModel(config=_lowerCAmelCase ).get_decoder() SCREAMING_SNAKE_CASE_ = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE_ = input_ids[:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['attention_mask'][:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['head_mask'] SCREAMING_SNAKE_CASE_ = 1 # first forward pass SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase , head_mask=_lowerCAmelCase , use_cache=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE_ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE_ = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE_ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase , past_key_values=_lowerCAmelCase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE_ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE_ = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE_ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_lowerCAmelCase , _lowerCAmelCase , rtol=1E-3 ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[Any]=None , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , __UpperCAmelCase : Dict=None , ) -> str: if attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.cast(tf.math.not_equal(__UpperCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () lowercase_ = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () lowercase_ = ( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = TFBlenderbotModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_lowerCAmelCase ) @require_tokenizers @require_tf class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' lowercase_ = ["My friends are cool but they eat too many carbs."] lowercase_ = "facebook/blenderbot-400M-distill" @cached_property def lowerCAmelCase_ ( self : List[str] ): return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.tokenizer(self.src_text , return_tensors='tf' ) SCREAMING_SNAKE_CASE_ = self.model.generate( model_inputs.input_ids , ) SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=_lowerCAmelCase )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
31
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa lowerCamelCase__ : Union[str, Any] = logging.getLogger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "summarization" lowercase_ = ["loss"] lowercase_ = ROUGE_KEYS lowercase_ = "rouge2" def __init__( self : Optional[Any] , _lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Optional[Any] ): if hparams.sortish_sampler and hparams.gpus > 1: SCREAMING_SNAKE_CASE_ = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError('Dynamic Batch size does not work for multi-gpu training' ) if hparams.sortish_sampler: raise ValueError('--sortish_sampler and --max_tokens_per_batch may not be used simultaneously' ) super().__init__(_lowerCAmelCase , num_labels=_lowerCAmelCase , mode=self.mode , **_lowerCAmelCase ) use_task_specific_params(self.model , 'summarization' ) save_git_info(self.hparams.output_dir ) SCREAMING_SNAKE_CASE_ = Path(self.output_dir ) / 'metrics.json' SCREAMING_SNAKE_CASE_ = Path(self.output_dir ) / 'hparams.pkl' pickle_save(self.hparams , self.hparams_save_path ) SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = defaultdict(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.config.model_type SCREAMING_SNAKE_CASE_ = self.config.tgt_vocab_size if self.model_type == 'fsmt' else self.config.vocab_size SCREAMING_SNAKE_CASE_ = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } SCREAMING_SNAKE_CASE_ = { 'train': self.hparams.n_train, 'val': self.hparams.n_val, 'test': self.hparams.n_test, } SCREAMING_SNAKE_CASE_ = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} SCREAMING_SNAKE_CASE_ = { 'train': self.hparams.max_target_length, 'val': self.hparams.val_max_target_length, 'test': self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], F"target_lens: {self.target_lens}" assert self.target_lens["train"] <= self.target_lens["test"], F"target_lens: {self.target_lens}" if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) SCREAMING_SNAKE_CASE_ = get_git_info()['repo_sha'] SCREAMING_SNAKE_CASE_ = hparams.num_workers SCREAMING_SNAKE_CASE_ = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = self.tokenizer.lang_code_to_id[hparams.tgt_lang] SCREAMING_SNAKE_CASE_ = self.decoder_start_token_id SCREAMING_SNAKE_CASE_ = ( SeqaSeqDataset if hasattr(self.tokenizer , 'prepare_seq2seq_batch' ) else LegacySeqaSeqDataset ) SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: SCREAMING_SNAKE_CASE_ = self.hparams.eval_max_gen_length else: SCREAMING_SNAKE_CASE_ = self.model.config.max_length SCREAMING_SNAKE_CASE_ = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def lowerCAmelCase_ ( self : int , _lowerCAmelCase : Dict[str, torch.Tensor] ): SCREAMING_SNAKE_CASE_ = { k: self.tokenizer.batch_decode(v.tolist() ) if 'mask' not in k else v.shape for k, v in batch.items() } save_json(_lowerCAmelCase , Path(self.output_dir ) / 'text_batch.json' ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / 'tok_batch.json' ) SCREAMING_SNAKE_CASE_ = True return readable_batch def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Optional[int] , **_lowerCAmelCase : Optional[int] ): return self.model(_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : List[int] ): SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode( _lowerCAmelCase , skip_special_tokens=_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase ) return lmap(str.strip , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : dict ): SCREAMING_SNAKE_CASE_ = self.tokenizer.pad_token_id SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = batch['input_ids'], batch['attention_mask'] SCREAMING_SNAKE_CASE_ = batch['labels'] if isinstance(self.model , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = self.model._shift_right(_lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ = shift_tokens_right(_lowerCAmelCase , _lowerCAmelCase ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero SCREAMING_SNAKE_CASE_ = decoder_input_ids self.save_readable_batch(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self(_lowerCAmelCase , attention_mask=_lowerCAmelCase , decoder_input_ids=_lowerCAmelCase , use_cache=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = outputs['logits'] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id SCREAMING_SNAKE_CASE_ = nn.CrossEntropyLoss(ignore_index=_lowerCAmelCase ) assert lm_logits.shape[-1] == self.vocab_size SCREAMING_SNAKE_CASE_ = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(_lowerCAmelCase , dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = label_smoothed_nll_loss( _lowerCAmelCase , _lowerCAmelCase , self.hparams.label_smoothing , ignore_index=_lowerCAmelCase ) return (loss,) @property def lowerCAmelCase_ ( self : Any ): return self.tokenizer.pad_token_id def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Dict , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = self._step(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = dict(zip(self.loss_names , _lowerCAmelCase ) ) # tokens per batch SCREAMING_SNAKE_CASE_ = batch['input_ids'].ne(self.pad ).sum() + batch['labels'].ne(self.pad ).sum() SCREAMING_SNAKE_CASE_ = batch['input_ids'].shape[0] SCREAMING_SNAKE_CASE_ = batch['input_ids'].eq(self.pad ).sum() SCREAMING_SNAKE_CASE_ = batch['input_ids'].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : List[str] , _lowerCAmelCase : int ): return self._generative_step(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Union[str, Any]="val" ): self.step_count += 1 SCREAMING_SNAKE_CASE_ = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} SCREAMING_SNAKE_CASE_ = losses['loss'] SCREAMING_SNAKE_CASE_ = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ['gen_time', 'gen_len'] } SCREAMING_SNAKE_CASE_ = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).type_as(_lowerCAmelCase ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {F"{prefix}_avg_{k}": x for k, x in losses.items()} SCREAMING_SNAKE_CASE_ = self.step_count self.metrics[prefix].append(_lowerCAmelCase ) # callback writes this to self.metrics_save_path SCREAMING_SNAKE_CASE_ = flatten_list([x['preds'] for x in outputs] ) return { "log": all_metrics, "preds": preds, F"{prefix}_loss": loss, F"{prefix}_{self.val_metric}": metric_tensor, } def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Optional[int] ): return calculate_rouge(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : dict ): SCREAMING_SNAKE_CASE_ = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') SCREAMING_SNAKE_CASE_ = self.model.generate( batch['input_ids'] , attention_mask=batch['attention_mask'] , use_cache=_lowerCAmelCase , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) SCREAMING_SNAKE_CASE_ = (time.time() - ta) / batch['input_ids'].shape[0] SCREAMING_SNAKE_CASE_ = self.ids_to_clean_text(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.ids_to_clean_text(batch['labels'] ) SCREAMING_SNAKE_CASE_ = self._step(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = dict(zip(self.loss_names , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = self.calc_generative_metrics(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = np.mean(lmap(_lowerCAmelCase , _lowerCAmelCase ) ) base_metrics.update(gen_time=_lowerCAmelCase , gen_len=_lowerCAmelCase , preds=_lowerCAmelCase , target=_lowerCAmelCase , **_lowerCAmelCase ) return base_metrics def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Optional[Any] ): return self._generative_step(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : str ): return self.validation_epoch_end(_lowerCAmelCase , prefix='test' ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.n_obs[type_path] SCREAMING_SNAKE_CASE_ = self.target_lens[type_path] SCREAMING_SNAKE_CASE_ = self.dataset_class( self.tokenizer , type_path=_lowerCAmelCase , n_obs=_lowerCAmelCase , max_target_length=_lowerCAmelCase , **self.dataset_kwargs , ) return dataset def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : int , _lowerCAmelCase : bool = False ): SCREAMING_SNAKE_CASE_ = self.get_dataset(_lowerCAmelCase ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": SCREAMING_SNAKE_CASE_ = dataset.make_sortish_sampler(_lowerCAmelCase , distributed=self.hparams.gpus > 1 ) return DataLoader( _lowerCAmelCase , batch_size=_lowerCAmelCase , collate_fn=dataset.collate_fn , shuffle=_lowerCAmelCase , num_workers=self.num_workers , sampler=_lowerCAmelCase , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": SCREAMING_SNAKE_CASE_ = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( _lowerCAmelCase , batch_sampler=_lowerCAmelCase , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( _lowerCAmelCase , batch_size=_lowerCAmelCase , collate_fn=dataset.collate_fn , shuffle=_lowerCAmelCase , num_workers=self.num_workers , sampler=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.get_dataloader('train' , batch_size=self.hparams.train_batch_size , shuffle=_lowerCAmelCase ) return dataloader def lowerCAmelCase_ ( self : Any ): return self.get_dataloader('val' , batch_size=self.hparams.eval_batch_size ) def lowerCAmelCase_ ( self : Optional[int] ): return self.get_dataloader('test' , batch_size=self.hparams.eval_batch_size ) @staticmethod def lowerCAmelCase_ ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple ): BaseTransformer.add_model_specific_args(_lowerCAmelCase , _lowerCAmelCase ) add_generic_args(_lowerCAmelCase , _lowerCAmelCase ) parser.add_argument( '--max_source_length' , default=1_024 , type=_lowerCAmelCase , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument( '--max_target_length' , default=56 , type=_lowerCAmelCase , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument( '--val_max_target_length' , default=142 , type=_lowerCAmelCase , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument( '--test_max_target_length' , default=142 , type=_lowerCAmelCase , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument('--freeze_encoder' , action='store_true' ) parser.add_argument('--freeze_embeds' , action='store_true' ) parser.add_argument('--sortish_sampler' , action='store_true' , default=_lowerCAmelCase ) parser.add_argument('--overwrite_output_dir' , action='store_true' , default=_lowerCAmelCase ) parser.add_argument('--max_tokens_per_batch' , type=_lowerCAmelCase , default=_lowerCAmelCase ) parser.add_argument('--logger_name' , type=_lowerCAmelCase , choices=['default', 'wandb', 'wandb_shared'] , default='default' ) parser.add_argument('--n_train' , type=_lowerCAmelCase , default=-1 , required=_lowerCAmelCase , help='# examples. -1 means use all.' ) parser.add_argument('--n_val' , type=_lowerCAmelCase , default=500 , required=_lowerCAmelCase , help='# examples. -1 means use all.' ) parser.add_argument('--n_test' , type=_lowerCAmelCase , default=-1 , required=_lowerCAmelCase , help='# examples. -1 means use all.' ) parser.add_argument( '--task' , type=_lowerCAmelCase , default='summarization' , required=_lowerCAmelCase , help='# examples. -1 means use all.' ) parser.add_argument('--label_smoothing' , type=_lowerCAmelCase , default=0.0 , required=_lowerCAmelCase ) parser.add_argument('--src_lang' , type=_lowerCAmelCase , default='' , required=_lowerCAmelCase ) parser.add_argument('--tgt_lang' , type=_lowerCAmelCase , default='' , required=_lowerCAmelCase ) parser.add_argument('--eval_beams' , type=_lowerCAmelCase , default=_lowerCAmelCase , required=_lowerCAmelCase ) parser.add_argument( '--val_metric' , type=_lowerCAmelCase , default=_lowerCAmelCase , required=_lowerCAmelCase , choices=['bleu', 'rouge2', 'loss', None] ) parser.add_argument('--eval_max_gen_length' , type=_lowerCAmelCase , default=_lowerCAmelCase , help='never generate more than n tokens' ) parser.add_argument('--save_top_k' , type=_lowerCAmelCase , default=1 , required=_lowerCAmelCase , help='How many checkpoints to save' ) parser.add_argument( '--early_stopping_patience' , type=_lowerCAmelCase , default=-1 , required=_lowerCAmelCase , help=( '-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So' ' val_check_interval will effect it.' ) , ) return parser class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "translation" lowercase_ = ["loss"] lowercase_ = ["bleu"] lowercase_ = "bleu" def __init__( self : str , _lowerCAmelCase : List[Any] , **_lowerCAmelCase : Optional[int] ): super().__init__(_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = hparams.src_lang SCREAMING_SNAKE_CASE_ = hparams.tgt_lang def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Dict , _lowerCAmelCase : Any ): return calculate_bleu(_lowerCAmelCase , _lowerCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any]=None ) -> SummarizationModule: Path(args.output_dir ).mkdir(exist_ok=__UpperCAmelCase ) check_output_dir(__UpperCAmelCase , expected_items=3 ) if model is None: if "summarization" in args.task: SCREAMING_SNAKE_CASE_ = SummarizationModule(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = TranslationModule(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith('/tmp' ) or str(args.output_dir ).startswith('/var' ) ): SCREAMING_SNAKE_CASE_ = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger SCREAMING_SNAKE_CASE_ = os.environ.get('WANDB_PROJECT' , __UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = WandbLogger(name=model.output_dir.name , project=__UpperCAmelCase ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger SCREAMING_SNAKE_CASE_ = WandbLogger(name=model.output_dir.name , project=f"hf_{dataset}" ) if args.early_stopping_patience >= 0: SCREAMING_SNAKE_CASE_ = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = args.val_metric == 'loss' SCREAMING_SNAKE_CASE_ = generic_train( __UpperCAmelCase , __UpperCAmelCase , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , __UpperCAmelCase ) , early_stopping_callback=__UpperCAmelCase , logger=__UpperCAmelCase , ) pickle_save(model.hparams , model.output_dir / 'hparams.pkl' ) if not args.do_predict: return model SCREAMING_SNAKE_CASE_ = '' SCREAMING_SNAKE_CASE_ = sorted(glob.glob(os.path.join(args.output_dir , '*.ckpt' ) , recursive=__UpperCAmelCase ) ) if checkpoints: SCREAMING_SNAKE_CASE_ = checkpoints[-1] SCREAMING_SNAKE_CASE_ = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": lowerCamelCase__ : Tuple = argparse.ArgumentParser() lowerCamelCase__ : Optional[Any] = pl.Trainer.add_argparse_args(parser) lowerCamelCase__ : List[str] = SummarizationModule.add_model_specific_args(parser, os.getcwd()) lowerCamelCase__ : Optional[int] = parser.parse_args() main(args)
31
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCamelCase__ : Dict = version.parse(importlib_metadata.version('nltk')) if NLTK_VERSION >= version.Version('3.6.4'): from nltk import word_tokenize lowerCamelCase__ : List[Any] = '\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n' lowerCamelCase__ : Optional[int] = '\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n' lowerCamelCase__ : int = '\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n \'meteor\': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric(\'meteor\')\n >>> predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party"]\n >>> references = ["It is a guide to action that ensures that the military will forever heed Party commands"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results["meteor"], 4))\n 0.6944\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase_ ( datasets.Metric ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , codebase_urls=['https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py'] , reference_urls=[ 'https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score', 'https://en.wikipedia.org/wiki/METEOR', ] , ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : Optional[Any] ): import nltk nltk.download('wordnet' ) if NLTK_VERSION >= version.Version('3.6.5' ): nltk.download('punkt' ) if NLTK_VERSION >= version.Version('3.6.6' ): nltk.download('omw-1.4' ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict=0.9 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : List[str]=0.5 ): if NLTK_VERSION >= version.Version('3.6.5' ): SCREAMING_SNAKE_CASE_ = [ meteor_score.single_meteor_score( word_tokenize(_lowerCAmelCase ) , word_tokenize(_lowerCAmelCase ) , alpha=_lowerCAmelCase , beta=_lowerCAmelCase , gamma=_lowerCAmelCase ) for ref, pred in zip(_lowerCAmelCase , _lowerCAmelCase ) ] else: SCREAMING_SNAKE_CASE_ = [ meteor_score.single_meteor_score(_lowerCAmelCase , _lowerCAmelCase , alpha=_lowerCAmelCase , beta=_lowerCAmelCase , gamma=_lowerCAmelCase ) for ref, pred in zip(_lowerCAmelCase , _lowerCAmelCase ) ] return {"meteor": np.mean(_lowerCAmelCase )}
31
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['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 lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
1
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = '▁' lowerCamelCase__ : List[Any] = {'vocab_file': 'vocab.txt', 'sentencepiece_model_ckpt': 'sentencepiece.bpe.model'} lowerCamelCase__ : Union[str, Any] = { 'sentencepiece_model_file': 'sentencepiece.bpe.model', 'vocab_file': 'vocab.txt', } lowerCamelCase__ : List[str] = { 'vocab_file': { 'ernie-m-base': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt', 'ernie-m-large': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt', }, 'sentencepiece_model_file': { 'ernie-m-base': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model', 'ernie-m-large': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model', }, } lowerCamelCase__ : Dict = { 'ernie-m-base': 514, 'ernie-m-large': 514, } lowerCamelCase__ : List[str] = { 'ernie-m-base': {'do_lower_case': False}, 'ernie-m-large': {'do_lower_case': False}, } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = ["input_ids"] lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_INIT_CONFIGURATION lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = RESOURCE_FILES_NAMES def __init__( self : List[str] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int=None , _lowerCAmelCase : Tuple=False , _lowerCAmelCase : List[Any]="utf8" , _lowerCAmelCase : Tuple="[UNK]" , _lowerCAmelCase : Tuple="[SEP]" , _lowerCAmelCase : Dict="[PAD]" , _lowerCAmelCase : str="[CLS]" , _lowerCAmelCase : Dict="[MASK]" , _lowerCAmelCase : Optional[Dict[str, Any]] = None , **_lowerCAmelCase : Tuple , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , vocab_file=_lowerCAmelCase , encoding=_lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = sentencepiece_model_ckpt SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_lowerCAmelCase ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: SCREAMING_SNAKE_CASE_ = self.load_vocab(filepath=_lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ = {self.sp_model.id_to_piece(_lowerCAmelCase ): id for id in range(self.sp_model.get_piece_size() )} SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.vocab.items()} def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): if text is None: return None SCREAMING_SNAKE_CASE_ = self.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = '', [] for i, ch in enumerate(_lowerCAmelCase ): if ch in self.SP_CHAR_MAPPING: SCREAMING_SNAKE_CASE_ = self.SP_CHAR_MAPPING.get(_lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ = unicodedata.normalize('NFKC' , _lowerCAmelCase ) if self.is_whitespace(_lowerCAmelCase ): continue normalized_text += ch char_mapping.extend([i] * len(_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = normalized_text, [], 0 if self.do_lower_case: SCREAMING_SNAKE_CASE_ = text.lower() for token in split_tokens: if token[:1] == "▁": SCREAMING_SNAKE_CASE_ = token[1:] SCREAMING_SNAKE_CASE_ = text[offset:].index(_lowerCAmelCase ) + offset SCREAMING_SNAKE_CASE_ = start + len(_lowerCAmelCase ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) SCREAMING_SNAKE_CASE_ = end return token_mapping @property def lowerCAmelCase_ ( self : Optional[Any] ): return len(self.vocab ) def lowerCAmelCase_ ( self : Optional[int] ): return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.__dict__.copy() SCREAMING_SNAKE_CASE_ = None return state def __setstate__( self : int , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : List[Any] ): return "".join((self.SP_CHAR_MAPPING.get(_lowerCAmelCase , _lowerCAmelCase ) for c in text) ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : int=64 , _lowerCAmelCase : Tuple=0.1 ): if self.sp_model_kwargs.get('enable_sampling' ) is True: SCREAMING_SNAKE_CASE_ = True if self.sp_model_kwargs.get('alpha' ) is not None: SCREAMING_SNAKE_CASE_ = self.sp_model_kwargs.get('alpha' ) if self.sp_model_kwargs.get('nbest_size' ) is not None: SCREAMING_SNAKE_CASE_ = self.sp_model_kwargs.get('nbest_size' ) if not enable_sampling: SCREAMING_SNAKE_CASE_ = self.sp_model.EncodeAsPieces(_lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ = self.sp_model.SampleEncodeAsPieces(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [] for pi, piece in enumerate(_lowerCAmelCase ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(_lowerCAmelCase ) and pi != 0: new_pieces.append(_lowerCAmelCase ) continue else: continue SCREAMING_SNAKE_CASE_ = 0 for i, chunk in enumerate(_lowerCAmelCase ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(_lowerCAmelCase ) or self.is_punct(_lowerCAmelCase ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) SCREAMING_SNAKE_CASE_ = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) SCREAMING_SNAKE_CASE_ = i if len(_lowerCAmelCase ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = ''.join(_lowerCAmelCase ).replace(_lowerCAmelCase , ' ' ).strip() return out_string def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = self.convert_ids_to_tokens(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = ''.join(_lowerCAmelCase ).replace(_lowerCAmelCase , ' ' ).strip() return out_string def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Any ): return self.vocab.get(_lowerCAmelCase , self.vocab.get(self.unk_token ) ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[str] ): return self.reverse_vocab.get(_lowerCAmelCase , self.unk_token ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any]=None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] SCREAMING_SNAKE_CASE_ = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : str=None ): if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : List[str] , _lowerCAmelCase : str=None , _lowerCAmelCase : Any=False ): if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_lowerCAmelCase )) + [1, 1] + ([0] * len(_lowerCAmelCase )) + [1] return [1] + ([0] * len(_lowerCAmelCase )) + [1] def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method if token_ids_a is None: # [CLS] X [SEP] return (len(_lowerCAmelCase ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(_lowerCAmelCase ) + 1) + [1] * (len(_lowerCAmelCase ) + 3) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Any ): if "\u4e00" <= char <= "\u9fff": return True return False def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Dict ): if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Union[str, Any] ): if char in ",;:.?!~,;:。?!《》【】": return True return False def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int] ): if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(_lowerCAmelCase ) == 1: SCREAMING_SNAKE_CASE_ = unicodedata.category(_lowerCAmelCase ) if cat == "Zs": return True return False def lowerCAmelCase_ ( self : int , _lowerCAmelCase : Tuple ): SCREAMING_SNAKE_CASE_ = {} with io.open(_lowerCAmelCase , 'r' , encoding='utf-8' ) as f: for index, line in enumerate(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = line.rstrip('\n' ) SCREAMING_SNAKE_CASE_ = int(_lowerCAmelCase ) return token_to_idx def lowerCAmelCase_ ( self : str , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): SCREAMING_SNAKE_CASE_ = 0 if os.path.isdir(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) else: SCREAMING_SNAKE_CASE_ = (filename_prefix + '-' if filename_prefix else '') + save_directory with open(_lowerCAmelCase , 'w' , encoding='utf-8' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda _lowerCAmelCase : kv[1] ): if index != token_index: logger.warning( F"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive." ' Please check that the vocabulary is not corrupted!' ) SCREAMING_SNAKE_CASE_ = token_index writer.write(token + '\n' ) index += 1 SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCAmelCase , 'sentencepiece.bpe.model' ) with open(_lowerCAmelCase , 'wb' ) as fi: SCREAMING_SNAKE_CASE_ = self.sp_model.serialized_model_proto() fi.write(_lowerCAmelCase ) return (vocab_file,)
31
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
1
from math import isqrt def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> bool: return all(number % divisor != 0 for divisor in range(2 , isqrt(__UpperCAmelCase ) + 1 ) ) def UpperCAmelCase_ ( __UpperCAmelCase : int = 10**6 ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 7 while prime_candidate < max_prime: primes_count += is_prime(__UpperCAmelCase ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(f'''{solution() = }''')
31
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int = 2_00 ) -> int: SCREAMING_SNAKE_CASE_ = [1, 2, 5, 10, 20, 50, 1_00, 2_00] SCREAMING_SNAKE_CASE_ = [0] * (pence + 1) SCREAMING_SNAKE_CASE_ = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(__UpperCAmelCase , pence + 1 , 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(200) == 73_682
31
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
1
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
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 lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['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 lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
1
lowerCamelCase__ : str = [ 'Audio', 'Array2D', 'Array3D', 'Array4D', 'Array5D', 'ClassLabel', 'Features', 'Sequence', 'Value', 'Image', 'Translation', 'TranslationVariableLanguages', ] from .audio import Audio from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value from .image import Image from .translation import Translation, TranslationVariableLanguages
31
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
1
import fire from utils import calculate_rouge, save_json def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Tuple=None , **__UpperCAmelCase : List[str] ) -> Any: SCREAMING_SNAKE_CASE_ = [x.strip() for x in open(__UpperCAmelCase ).readlines()] SCREAMING_SNAKE_CASE_ = [x.strip() for x in open(__UpperCAmelCase ).readlines()][: len(__UpperCAmelCase )] SCREAMING_SNAKE_CASE_ = calculate_rouge(__UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ) if save_path is not None: save_json(__UpperCAmelCase , __UpperCAmelCase , indent=__UpperCAmelCase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCamelCase__ : Tuple = logging.get_logger(__name__) # General docstring lowerCamelCase__ : List[Any] = 'MobileNetV1Config' # Base docstring lowerCamelCase__ : Dict = 'google/mobilenet_v1_1.0_224' lowerCamelCase__ : Tuple = [1, 1_024, 7, 7] # Image classification docstring lowerCamelCase__ : Any = 'google/mobilenet_v1_1.0_224' lowerCamelCase__ : Tuple = 'tabby, tabby cat' lowerCamelCase__ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Union[str, Any]=None ) -> Dict: SCREAMING_SNAKE_CASE_ = {} if isinstance(__UpperCAmelCase , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = model.mobilenet_va else: SCREAMING_SNAKE_CASE_ = model SCREAMING_SNAKE_CASE_ = 'MobilenetV1/Conv2d_0/' SCREAMING_SNAKE_CASE_ = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_ = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_ = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_ = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_ = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_ = i + 1 SCREAMING_SNAKE_CASE_ = i * 2 SCREAMING_SNAKE_CASE_ = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_ = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_ = pointer.convolution.weight SCREAMING_SNAKE_CASE_ = pointer.normalization.bias SCREAMING_SNAKE_CASE_ = pointer.normalization.weight SCREAMING_SNAKE_CASE_ = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_ = pointer.normalization.running_var SCREAMING_SNAKE_CASE_ = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_ = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_ = pointer.convolution.weight SCREAMING_SNAKE_CASE_ = pointer.normalization.bias SCREAMING_SNAKE_CASE_ = pointer.normalization.weight SCREAMING_SNAKE_CASE_ = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_ = pointer.normalization.running_var if isinstance(__UpperCAmelCase , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = 'MobilenetV1/Logits/Conv2d_1c_1x1/' SCREAMING_SNAKE_CASE_ = model.classifier.weight SCREAMING_SNAKE_CASE_ = model.classifier.bias return tf_to_pt_map def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any] ) -> Optional[Any]: try: import numpy as np import tensorflow as tf except ImportError: logger.error( 'Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see ' 'https://www.tensorflow.org/install/ for installation instructions.' ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_ = tf.train.list_variables(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_ = tf.train.load_variable(__UpperCAmelCase , __UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_ = _build_tf_to_pytorch_map(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_ = tf_weights[name] if "depthwise_weights" in name: logger.info('Transposing depthwise' ) SCREAMING_SNAKE_CASE_ = np.transpose(__UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info('Transposing' ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_ = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_ = np.transpose(__UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_ = torch.from_numpy(__UpperCAmelCase ) tf_weights.pop(__UpperCAmelCase , __UpperCAmelCase ) tf_weights.pop(name + '/RMSProp' , __UpperCAmelCase ) tf_weights.pop(name + '/RMSProp_1' , __UpperCAmelCase ) tf_weights.pop(name + '/ExponentialMovingAverage' , __UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def UpperCAmelCase_ ( __UpperCAmelCase : torch.Tensor , __UpperCAmelCase : nn.Convad ) -> torch.Tensor: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_ = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_ = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_ = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_ = pad_along_width // 2 SCREAMING_SNAKE_CASE_ = pad_along_width - pad_left SCREAMING_SNAKE_CASE_ = pad_along_height // 2 SCREAMING_SNAKE_CASE_ = pad_along_height - pad_top SCREAMING_SNAKE_CASE_ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(__UpperCAmelCase , __UpperCAmelCase , 'constant' , 0.0 ) class lowerCamelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : MobileNetVaConfig , _lowerCAmelCase : int , _lowerCAmelCase : int , _lowerCAmelCase : int , _lowerCAmelCase : Optional[int] = 1 , _lowerCAmelCase : Optional[int] = 1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[bool] = True , _lowerCAmelCase : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_ = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups." ) if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups." ) SCREAMING_SNAKE_CASE_ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) SCREAMING_SNAKE_CASE_ = nn.Convad( in_channels=_lowerCAmelCase , out_channels=_lowerCAmelCase , kernel_size=_lowerCAmelCase , stride=_lowerCAmelCase , padding=_lowerCAmelCase , groups=_lowerCAmelCase , bias=_lowerCAmelCase , padding_mode='zeros' , ) if use_normalization: SCREAMING_SNAKE_CASE_ = nn.BatchNormad( num_features=_lowerCAmelCase , eps=config.layer_norm_eps , momentum=0.9997 , affine=_lowerCAmelCase , track_running_stats=_lowerCAmelCase , ) else: SCREAMING_SNAKE_CASE_ = None if use_activation: if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = ACTaFN[use_activation] elif isinstance(config.hidden_act , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_ = config.hidden_act else: SCREAMING_SNAKE_CASE_ = None def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : torch.Tensor ): if self.config.tf_padding: SCREAMING_SNAKE_CASE_ = apply_tf_padding(_lowerCAmelCase , self.convolution ) SCREAMING_SNAKE_CASE_ = self.convolution(_lowerCAmelCase ) if self.normalization is not None: SCREAMING_SNAKE_CASE_ = self.normalization(_lowerCAmelCase ) if self.activation is not None: SCREAMING_SNAKE_CASE_ = self.activation(_lowerCAmelCase ) return features class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MobileNetVaConfig lowercase_ = load_tf_weights_in_mobilenet_va lowercase_ = "mobilenet_v1" lowercase_ = "pixel_values" lowercase_ = False def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Union[nn.Linear, nn.Convad] ): if isinstance(_lowerCAmelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(_lowerCAmelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) lowerCamelCase__ : Optional[int] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCamelCase__ : str = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top." , _SCREAMING_SNAKE_CASE , ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : MobileNetVaConfig , _lowerCAmelCase : bool = True ): super().__init__(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = config SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = max(int(depth * config.depth_multiplier ) , config.min_depth ) SCREAMING_SNAKE_CASE_ = MobileNetVaConvLayer( _lowerCAmelCase , in_channels=config.num_channels , out_channels=_lowerCAmelCase , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_ = nn.ModuleList() for i in range(13 ): SCREAMING_SNAKE_CASE_ = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( _lowerCAmelCase , in_channels=_lowerCAmelCase , out_channels=_lowerCAmelCase , kernel_size=3 , stride=strides[i] , groups=_lowerCAmelCase , ) ) self.layer.append( MobileNetVaConvLayer( _lowerCAmelCase , in_channels=_lowerCAmelCase , out_channels=_lowerCAmelCase , kernel_size=1 , ) ) SCREAMING_SNAKE_CASE_ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Any ): raise NotImplementedError @add_start_docstrings_to_model_forward(_lowerCAmelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_lowerCAmelCase , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[torch.Tensor] = None , _lowerCAmelCase : Optional[bool] = None , _lowerCAmelCase : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError('You have to specify pixel_values' ) SCREAMING_SNAKE_CASE_ = self.conv_stem(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): SCREAMING_SNAKE_CASE_ = layer_module(_lowerCAmelCase ) if output_hidden_states: SCREAMING_SNAKE_CASE_ = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_ = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_ = torch.flatten(self.pooler(_lowerCAmelCase ) , start_dim=1 ) else: SCREAMING_SNAKE_CASE_ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_lowerCAmelCase , pooler_output=_lowerCAmelCase , hidden_states=_lowerCAmelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , _SCREAMING_SNAKE_CASE , ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : int , _lowerCAmelCase : MobileNetVaConfig ): super().__init__(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = config.num_labels SCREAMING_SNAKE_CASE_ = MobileNetVaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_ = nn.Dropout(config.classifier_dropout_prob , inplace=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = nn.Linear(_lowerCAmelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_lowerCAmelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_lowerCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[torch.Tensor] = None , _lowerCAmelCase : Optional[bool] = None , _lowerCAmelCase : Optional[torch.Tensor] = None , _lowerCAmelCase : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_ = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_ = self.mobilenet_va(_lowerCAmelCase , output_hidden_states=_lowerCAmelCase , return_dict=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_ = self.classifier(self.dropout(_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_ = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_ = 'single_label_classification' else: SCREAMING_SNAKE_CASE_ = 'multi_label_classification' if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_ = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_ = loss_fct(logits.squeeze() , labels.squeeze() ) else: SCREAMING_SNAKE_CASE_ = loss_fct(_lowerCAmelCase , _lowerCAmelCase ) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_ = CrossEntropyLoss() SCREAMING_SNAKE_CASE_ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_ = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_ = loss_fct(_lowerCAmelCase , _lowerCAmelCase ) if not return_dict: SCREAMING_SNAKE_CASE_ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=_lowerCAmelCase , logits=_lowerCAmelCase , hidden_states=outputs.hidden_states , )
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
import unittest from transformers import load_tool from transformers.utils import is_torch_available if is_torch_available(): import torch from transformers.testing_utils import require_torch from .test_tools_common import ToolTesterMixin @require_torch class lowerCamelCase_ ( unittest.TestCase , _SCREAMING_SNAKE_CASE ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = load_tool('text-to-speech' ) self.tool.setup() def lowerCAmelCase_ ( self : int ): # SpeechT5 isn't deterministic torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = self.tool('hey' ) SCREAMING_SNAKE_CASE_ = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.000_5966_6688_3211_5829, -0.000_3657_6401_9079_5064, -0.0001_3439_5027_9988_3485] ) , ) ) def lowerCAmelCase_ ( self : List[Any] ): # SpeechT5 isn't deterministic torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = self.tool('hey' ) SCREAMING_SNAKE_CASE_ = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.000_5966_6688_3211_5829, -0.000_3657_6401_9079_5064, -0.0001_3439_5027_9988_3485] ) , ) )
31
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
from collections.abc import Sequence from queue import Queue class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int=None , _lowerCAmelCase : Optional[Any]=None ): SCREAMING_SNAKE_CASE_ = start SCREAMING_SNAKE_CASE_ = end SCREAMING_SNAKE_CASE_ = val SCREAMING_SNAKE_CASE_ = (start + end) // 2 SCREAMING_SNAKE_CASE_ = left SCREAMING_SNAKE_CASE_ = right def __repr__( self : List[str] ): return F"SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})" class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : Sequence , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = collection SCREAMING_SNAKE_CASE_ = function if self.collection: SCREAMING_SNAKE_CASE_ = self._build_tree(0 , len(_lowerCAmelCase ) - 1 ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Optional[Any] ): self._update_tree(self.root , _lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : int , _lowerCAmelCase : List[Any] ): return self._query_range(self.root , _lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple ): if start == end: return SegmentTreeNode(_lowerCAmelCase , _lowerCAmelCase , self.collection[start] ) SCREAMING_SNAKE_CASE_ = (start + end) // 2 SCREAMING_SNAKE_CASE_ = self._build_tree(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self._build_tree(mid + 1 , _lowerCAmelCase ) return SegmentTreeNode(_lowerCAmelCase , _lowerCAmelCase , self.fn(left.val , right.val ) , _lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : List[str] ): if node.start == i and node.end == i: SCREAMING_SNAKE_CASE_ = val return if i <= node.mid: self._update_tree(node.left , _lowerCAmelCase , _lowerCAmelCase ) else: self._update_tree(node.right , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.fn(node.left.val , node.right.val ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : int ): if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left , _lowerCAmelCase , _lowerCAmelCase ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left , _lowerCAmelCase , node.mid ) , self._query_range(node.right , node.mid + 1 , _lowerCAmelCase ) , ) else: # range in right child tree return self._query_range(node.right , _lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): if self.root is not None: SCREAMING_SNAKE_CASE_ = Queue() queue.put(self.root ) while not queue.empty(): SCREAMING_SNAKE_CASE_ = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print('*' * 50) lowerCamelCase__ : Any = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
import argparse import torch from transformers import GPTaLMHeadModel, RobertaForMaskedLM if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = argparse.ArgumentParser( description=( 'Extraction some layers of the full RobertaForMaskedLM or GPT2LMHeadModel for Transfer Learned' ' Distillation' ) ) parser.add_argument('--model_type', default='roberta', choices=['roberta', 'gpt2']) parser.add_argument('--model_name', default='roberta-large', type=str) parser.add_argument('--dump_checkpoint', default='serialization_dir/tf_roberta_048131723.pth', type=str) parser.add_argument('--vocab_transform', action='store_true') lowerCamelCase__ : str = parser.parse_args() if args.model_type == "roberta": lowerCamelCase__ : Optional[Any] = RobertaForMaskedLM.from_pretrained(args.model_name) lowerCamelCase__ : Dict = 'roberta' elif args.model_type == "gpt2": lowerCamelCase__ : Optional[int] = GPTaLMHeadModel.from_pretrained(args.model_name) lowerCamelCase__ : Optional[int] = 'transformer' lowerCamelCase__ : Tuple = model.state_dict() lowerCamelCase__ : str = {} # Embeddings # if args.model_type == "gpt2": for param_name in ["wte.weight", "wpe.weight"]: lowerCamelCase__ : int = state_dict[f'''{prefix}.{param_name}'''] else: for w in ["word_embeddings", "position_embeddings", "token_type_embeddings"]: lowerCamelCase__ : Tuple = f'''{prefix}.embeddings.{w}.weight''' lowerCamelCase__ : Dict = state_dict[param_name] for w in ["weight", "bias"]: lowerCamelCase__ : Any = f'''{prefix}.embeddings.LayerNorm.{w}''' lowerCamelCase__ : Tuple = state_dict[param_name] # Transformer Blocks # lowerCamelCase__ : Tuple = 0 for teacher_idx in [0, 2, 4, 7, 9, 11]: if args.model_type == "gpt2": for layer in ["ln_1", "attn.c_attn", "attn.c_proj", "ln_2", "mlp.c_fc", "mlp.c_proj"]: for w in ["weight", "bias"]: lowerCamelCase__ : Tuple = state_dict[ f'''{prefix}.h.{teacher_idx}.{layer}.{w}''' ] lowerCamelCase__ : List[str] = state_dict[f'''{prefix}.h.{teacher_idx}.attn.bias'''] else: for layer in [ "attention.self.query", "attention.self.key", "attention.self.value", "attention.output.dense", "attention.output.LayerNorm", "intermediate.dense", "output.dense", "output.LayerNorm", ]: for w in ["weight", "bias"]: lowerCamelCase__ : Optional[int] = state_dict[ f'''{prefix}.encoder.layer.{teacher_idx}.{layer}.{w}''' ] std_idx += 1 # Language Modeling Head ###s if args.model_type == "roberta": for layer in ["lm_head.decoder.weight", "lm_head.bias"]: lowerCamelCase__ : Dict = state_dict[f'''{layer}'''] if args.vocab_transform: for w in ["weight", "bias"]: lowerCamelCase__ : Tuple = state_dict[f'''lm_head.dense.{w}'''] lowerCamelCase__ : Union[str, Any] = state_dict[f'''lm_head.layer_norm.{w}'''] elif args.model_type == "gpt2": for w in ["weight", "bias"]: lowerCamelCase__ : int = state_dict[f'''{prefix}.ln_f.{w}'''] lowerCamelCase__ : Any = state_dict['lm_head.weight'] print(f'''N layers selected for distillation: {std_idx}''') print(f'''Number of params transferred for distillation: {len(compressed_sd.keys())}''') print(f'''Save transferred checkpoint to {args.dump_checkpoint}.''') torch.save(compressed_sd, args.dump_checkpoint)
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> list: SCREAMING_SNAKE_CASE_ = word.split() def justify(__UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int ) -> str: SCREAMING_SNAKE_CASE_ = max_width - width SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) if len(__UpperCAmelCase ) == 1: # if there is only word in line # just insert overall_spaces_count for the remainder of line return line[0] + " " * overall_spaces_count else: SCREAMING_SNAKE_CASE_ = words_count - 1 # num_spaces_between_words_list[i] : tells you to insert # num_spaces_between_words_list[i] spaces # after word on line[i] SCREAMING_SNAKE_CASE_ = spaces_to_insert_between_words * [ overall_spaces_count // spaces_to_insert_between_words ] SCREAMING_SNAKE_CASE_ = ( overall_spaces_count % spaces_to_insert_between_words ) # distribute spaces via round robin to the left words for i in range(__UpperCAmelCase ): num_spaces_between_words_list[i] += 1 SCREAMING_SNAKE_CASE_ = [] for i in range(__UpperCAmelCase ): # add the word aligned_words_list.append(line[i] ) # add the spaces to insert aligned_words_list.append(num_spaces_between_words_list[i] * ' ' ) # just add the last word to the sentence aligned_words_list.append(line[-1] ) # join the aligned words list to form a justified line return "".join(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 0 for word in words: if width + len(__UpperCAmelCase ) + len(__UpperCAmelCase ) <= max_width: # keep adding words until we can fill out max_width # width = sum of length of all words (without overall_spaces_count) # len(word) = length of current word # len(line) = number of overall_spaces_count to insert between words line.append(__UpperCAmelCase ) width += len(__UpperCAmelCase ) else: # justify the line and add it to result answer.append(justify(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) ) # reset new line and new width SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = [word], len(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = max_width - width - len(__UpperCAmelCase ) answer.append(' '.join(__UpperCAmelCase ) + (remaining_spaces + 1) * ' ' ) return answer if __name__ == "__main__": from doctest import testmod testmod()
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
1
from __future__ import annotations def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) // 2 # choose the middle 3 elements SCREAMING_SNAKE_CASE_ = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[str]=13 , _lowerCAmelCase : Any=7 , _lowerCAmelCase : Union[str, Any]=True , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Any=99 , _lowerCAmelCase : int=32 , _lowerCAmelCase : List[str]=5 , _lowerCAmelCase : List[str]=4 , _lowerCAmelCase : Optional[Any]=37 , _lowerCAmelCase : int="gelu" , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : str=512 , _lowerCAmelCase : Tuple=16 , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=0.02 , _lowerCAmelCase : Optional[Any]=4 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_attention_mask SCREAMING_SNAKE_CASE_ = use_token_type_ids SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = num_choices def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = None if self.use_attention_mask: SCREAMING_SNAKE_CASE_ = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE_ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = FlaxAlbertModelTester(self ) @slow def lowerCAmelCase_ ( self : Tuple ): for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class_name.from_pretrained('albert-base-v2' ) SCREAMING_SNAKE_CASE_ = model(np.ones((1, 1) ) ) self.assertIsNotNone(_lowerCAmelCase ) @require_flax class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = FlaxAlbertModel.from_pretrained('albert-base-v2' ) SCREAMING_SNAKE_CASE_ = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) SCREAMING_SNAKE_CASE_ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (1, 11, 768) self.assertEqual(output.shape , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , _lowerCAmelCase , atol=1E-4 ) )
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : int = { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/config.json', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "xlnet" lowercase_ = ["mems"] lowercase_ = { "n_token": "vocab_size", # Backward compatibility "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : str , _lowerCAmelCase : Optional[int]=32_000 , _lowerCAmelCase : Optional[Any]=1_024 , _lowerCAmelCase : str=24 , _lowerCAmelCase : Any=16 , _lowerCAmelCase : Union[str, Any]=4_096 , _lowerCAmelCase : List[str]="gelu" , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : Optional[int]="bi" , _lowerCAmelCase : Any=0.02 , _lowerCAmelCase : List[Any]=1E-12 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]=512 , _lowerCAmelCase : int=None , _lowerCAmelCase : Dict=True , _lowerCAmelCase : str=False , _lowerCAmelCase : str=False , _lowerCAmelCase : str=-1 , _lowerCAmelCase : Dict=False , _lowerCAmelCase : int="last" , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : Optional[int]="tanh" , _lowerCAmelCase : Union[str, Any]=0.1 , _lowerCAmelCase : Any=5 , _lowerCAmelCase : List[Any]=5 , _lowerCAmelCase : Optional[int]=5 , _lowerCAmelCase : Optional[int]=1 , _lowerCAmelCase : Dict=2 , **_lowerCAmelCase : str , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_layer SCREAMING_SNAKE_CASE_ = n_head if d_model % n_head != 0: raise ValueError(F"'d_model % n_head' ({d_model % n_head}) should be equal to 0" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})" ) SCREAMING_SNAKE_CASE_ = d_model // n_head SCREAMING_SNAKE_CASE_ = ff_activation SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = untie_r SCREAMING_SNAKE_CASE_ = attn_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = mem_len SCREAMING_SNAKE_CASE_ = reuse_len SCREAMING_SNAKE_CASE_ = bi_data SCREAMING_SNAKE_CASE_ = clamp_len SCREAMING_SNAKE_CASE_ = same_length SCREAMING_SNAKE_CASE_ = summary_type SCREAMING_SNAKE_CASE_ = summary_use_proj SCREAMING_SNAKE_CASE_ = summary_activation SCREAMING_SNAKE_CASE_ = summary_last_dropout SCREAMING_SNAKE_CASE_ = start_n_top SCREAMING_SNAKE_CASE_ = end_n_top SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = pad_token_id SCREAMING_SNAKE_CASE_ = eos_token_id if "use_cache" in kwargs: warnings.warn( 'The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`' ' instead.' , _lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = kwargs['use_cache'] SCREAMING_SNAKE_CASE_ = use_mems_eval SCREAMING_SNAKE_CASE_ = use_mems_train super().__init__(pad_token_id=_lowerCAmelCase , bos_token_id=_lowerCAmelCase , eos_token_id=_lowerCAmelCase , **_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Union[str, Any] ): logger.info(F"The model {self.model_type} is one of the few models that has no sequence length limit." ) return -1 @max_position_embeddings.setter def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[int] ): # Message copied from Transformer-XL documentation raise NotImplementedError( F"The model {self.model_type} is one of the few models that has no sequence length limit." )
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = ["image_processor", "tokenizer"] lowercase_ = "BlipImageProcessor" lowercase_ = ("BertTokenizer", "BertTokenizerFast") def __init__( self : Optional[Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = False super().__init__(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : str , _lowerCAmelCase : ImageInput = None , _lowerCAmelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _lowerCAmelCase : bool = True , _lowerCAmelCase : Union[bool, str, PaddingStrategy] = False , _lowerCAmelCase : Union[bool, str, TruncationStrategy] = None , _lowerCAmelCase : Optional[int] = None , _lowerCAmelCase : int = 0 , _lowerCAmelCase : Optional[int] = None , _lowerCAmelCase : Optional[bool] = None , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = True , _lowerCAmelCase : Optional[Union[str, TensorType]] = None , **_lowerCAmelCase : Optional[int] , ): if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None: SCREAMING_SNAKE_CASE_ = self.tokenizer SCREAMING_SNAKE_CASE_ = self.tokenizer( text=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=_lowerCAmelCase , stride=_lowerCAmelCase , pad_to_multiple_of=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , return_overflowing_tokens=_lowerCAmelCase , return_special_tokens_mask=_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_length=_lowerCAmelCase , verbose=_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase , ) return text_encoding # add pixel_values SCREAMING_SNAKE_CASE_ = self.image_processor(_lowerCAmelCase , return_tensors=_lowerCAmelCase ) if text is not None: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=_lowerCAmelCase , stride=_lowerCAmelCase , pad_to_multiple_of=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , return_overflowing_tokens=_lowerCAmelCase , return_special_tokens_mask=_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_length=_lowerCAmelCase , verbose=_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase , ) else: SCREAMING_SNAKE_CASE_ = None if text_encoding is not None: encoding_image_processor.update(_lowerCAmelCase ) return encoding_image_processor def lowerCAmelCase_ ( self : Optional[Any] , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : List[str] ): return self.tokenizer.batch_decode(*_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , *_lowerCAmelCase : Dict , **_lowerCAmelCase : Union[str, Any] ): return self.tokenizer.decode(*_lowerCAmelCase , **_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
31
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) def UpperCAmelCase_ ( __UpperCAmelCase : int=None , __UpperCAmelCase : Tuple=None ) -> str: return field(default_factory=lambda: default , metadata=__UpperCAmelCase ) @dataclass class lowerCamelCase_ : '''simple docstring''' lowercase_ = list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowercase_ = list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowercase_ = list_field( default=[8, 32, 128, 512] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Use FP16 to accelerate inference."} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Benchmark training of model"} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Verbose memory tracing"} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Trace memory line by line"} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Save result to a CSV file"} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Save all print statements in a log file"} ) lowercase_ = field(default=_SCREAMING_SNAKE_CASE , metadata={"help": "Whether to print environment information"} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowercase_ = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowercase_ = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowercase_ = field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowercase_ = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowercase_ = field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowercase_ = field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowercase_ = field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowercase_ = field( default=_SCREAMING_SNAKE_CASE , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def lowerCAmelCase_ ( self : List[str] ): warnings.warn( F"The class {self.__class__} is deprecated. Hugging Face Benchmarking utils" ' are deprecated in general and it is advised to use external Benchmarking libraries ' ' to benchmark Transformer models.' , _lowerCAmelCase , ) def lowerCAmelCase_ ( self : str ): return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def lowerCAmelCase_ ( self : Dict ): if len(self.models ) <= 0: raise ValueError( 'Please make sure you provide at least one model name / model identifier, *e.g.* `--models' ' bert-base-cased` or `args.models = [\'bert-base-cased\'].' ) return self.models @property def lowerCAmelCase_ ( self : Any ): if not self.multi_process: return False elif self.is_tpu: logger.info('Multiprocessing is currently not possible on TPU.' ) return False else: return True
31
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_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
1
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Tuple , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Dict=7 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Optional[int]=True , _lowerCAmelCase : str=99 , _lowerCAmelCase : List[str]=32 , _lowerCAmelCase : int=32 , _lowerCAmelCase : List[Any]=2 , _lowerCAmelCase : List[Any]=4 , _lowerCAmelCase : Optional[Any]=37 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : List[Any]=512 , _lowerCAmelCase : List[Any]=0.02 , _lowerCAmelCase : List[Any]=0 , _lowerCAmelCase : Optional[Any]=None , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_input_mask SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = bos_token_id def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: SCREAMING_SNAKE_CASE_ = input_mask.numpy() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = input_mask.shape SCREAMING_SNAKE_CASE_ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = self.get_config() return config, input_ids, tf.convert_to_tensor(_lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Any , _lowerCAmelCase : int , _lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = TFBlipTextModel(config=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase , training=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , training=_lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (TFBlipTextModel,) if is_tf_available() else () lowercase_ = False lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = BlipTextModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : Union[str, Any] ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): pass def lowerCAmelCase_ ( self : List[str] ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def lowerCAmelCase_ ( self : str ): pass @slow def lowerCAmelCase_ ( self : Optional[int] ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = TFBlipTextModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_lowerCAmelCase )
31
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
1
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = '▁' lowerCamelCase__ : Any = { 'vocab_file': 'vocab.json', 'spm_file': 'sentencepiece.bpe.model', } lowerCamelCase__ : Any = { 'vocab_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json' ), }, 'spm_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model' ) }, } lowerCamelCase__ : Union[str, Any] = { 'facebook/s2t-small-librispeech-asr': 1_024, } lowerCamelCase__ : Dict = ['pt', 'fr', 'ru', 'nl', 'ro', 'it', 'es', 'de'] lowerCamelCase__ : List[Any] = {'mustc': MUSTC_LANGS} class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = MAX_MODEL_INPUT_SIZES lowercase_ = ["input_ids", "attention_mask"] lowercase_ = [] def __init__( self : Any , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[str]="<s>" , _lowerCAmelCase : Tuple="</s>" , _lowerCAmelCase : str="<pad>" , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : int=False , _lowerCAmelCase : int=False , _lowerCAmelCase : Optional[int]=None , _lowerCAmelCase : Union[str, Any]=None , _lowerCAmelCase : Optional[Dict[str, Any]] = None , **_lowerCAmelCase : str , ): SCREAMING_SNAKE_CASE_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_lowerCAmelCase , eos_token=_lowerCAmelCase , unk_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , do_upper_case=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , tgt_lang=_lowerCAmelCase , lang_codes=_lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_upper_case SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = load_json(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE_ = spm_file SCREAMING_SNAKE_CASE_ = load_spm(_lowerCAmelCase , self.sp_model_kwargs ) if lang_codes is not None: SCREAMING_SNAKE_CASE_ = lang_codes SCREAMING_SNAKE_CASE_ = LANGUAGES[lang_codes] SCREAMING_SNAKE_CASE_ = [F"<lang:{lang}>" for lang in self.langs] SCREAMING_SNAKE_CASE_ = {lang: self.sp_model.PieceToId(F"<lang:{lang}>" ) for lang in self.langs} SCREAMING_SNAKE_CASE_ = self.lang_tokens SCREAMING_SNAKE_CASE_ = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: SCREAMING_SNAKE_CASE_ = {} @property def lowerCAmelCase_ ( self : Optional[Any] ): return len(self.encoder ) @property def lowerCAmelCase_ ( self : Dict ): return self._tgt_lang @tgt_lang.setter def lowerCAmelCase_ ( self : str , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = new_tgt_lang self.set_tgt_lang_special_tokens(_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = self.lang_code_to_id[tgt_lang] SCREAMING_SNAKE_CASE_ = [lang_code_id] def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : str ): return self.sp_model.encode(_lowerCAmelCase , out_type=_lowerCAmelCase ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : Optional[Any] ): return self.encoder.get(_lowerCAmelCase , self.encoder[self.unk_token] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int ): return self.decoder.get(_lowerCAmelCase , self.unk_token ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = '' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: SCREAMING_SNAKE_CASE_ = self.sp_model.decode(_lowerCAmelCase ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " SCREAMING_SNAKE_CASE_ = [] else: current_sub_tokens.append(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.sp_model.decode(_lowerCAmelCase ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str=None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None , _lowerCAmelCase : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_lowerCAmelCase , token_ids_a=_lowerCAmelCase , already_has_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [1] * len(self.prefix_tokens ) SCREAMING_SNAKE_CASE_ = [1] if token_ids_a is None: return prefix_ones + ([0] * len(_lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(_lowerCAmelCase )) + ([0] * len(_lowerCAmelCase )) + suffix_ones def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.__dict__.copy() SCREAMING_SNAKE_CASE_ = None return state def __setstate__( self : int , _lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = load_spm(self.spm_file , self.sp_model_kwargs ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): SCREAMING_SNAKE_CASE_ = Path(_lowerCAmelCase ) assert save_dir.is_dir(), F"{save_directory} should be a directory" SCREAMING_SNAKE_CASE_ = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['vocab_file'] ) SCREAMING_SNAKE_CASE_ = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['spm_file'] ) save_json(self.encoder , _lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(_lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , _lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(_lowerCAmelCase , 'wb' ) as fi: SCREAMING_SNAKE_CASE_ = self.sp_model.serialized_model_proto() fi.write(_lowerCAmelCase ) return (str(_lowerCAmelCase ), str(_lowerCAmelCase )) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: SCREAMING_SNAKE_CASE_ = sentencepiece.SentencePieceProcessor(**__UpperCAmelCase ) spm.Load(str(__UpperCAmelCase ) ) return spm def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Union[Dict, List]: with open(__UpperCAmelCase , 'r' ) as f: return json.load(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : str ) -> None: with open(__UpperCAmelCase , 'w' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase , indent=2 )
31
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
1
import string from math import logaa def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : str ) -> int: SCREAMING_SNAKE_CASE_ = document.translate( str.maketrans('' , '' , string.punctuation ) ).replace('\n' , '' ) SCREAMING_SNAKE_CASE_ = document_without_punctuation.split(' ' ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : str ) -> tuple[int, int]: SCREAMING_SNAKE_CASE_ = corpus.lower().translate( str.maketrans('' , '' , string.punctuation ) ) # strip all punctuation and replace it with '' SCREAMING_SNAKE_CASE_ = corpus_without_punctuation.split('\n' ) SCREAMING_SNAKE_CASE_ = term.lower() return (len([doc for doc in docs if term in doc] ), len(__UpperCAmelCase )) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : int=False ) -> float: if smoothing: if n == 0: raise ValueError('log10(0) is undefined.' ) return round(1 + logaa(n / (1 + df) ) , 3 ) if df == 0: raise ZeroDivisionError('df must be > 0' ) elif n == 0: raise ValueError('log10(0) is undefined.' ) return round(logaa(n / df ) , 3 ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> float: return round(tf * idf , 3 )
31
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : int = 50 ) -> int: SCREAMING_SNAKE_CASE_ = [1] * (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 ): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
31
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
1
lowerCamelCase__ : Tuple = { 'Pillow': 'Pillow', 'accelerate': 'accelerate>=0.11.0', 'compel': 'compel==0.1.8', 'black': 'black~=23.1', 'datasets': 'datasets', 'filelock': 'filelock', 'flax': 'flax>=0.4.1', 'hf-doc-builder': 'hf-doc-builder>=0.3.0', 'huggingface-hub': 'huggingface-hub>=0.13.2', 'requests-mock': 'requests-mock==1.10.0', 'importlib_metadata': 'importlib_metadata', 'invisible-watermark': 'invisible-watermark', 'isort': 'isort>=5.5.4', 'jax': 'jax>=0.2.8,!=0.3.2', 'jaxlib': 'jaxlib>=0.1.65', 'Jinja2': 'Jinja2', 'k-diffusion': 'k-diffusion>=0.0.12', 'torchsde': 'torchsde', 'note_seq': 'note_seq', 'librosa': 'librosa', 'numpy': 'numpy', 'omegaconf': 'omegaconf', 'parameterized': 'parameterized', 'protobuf': 'protobuf>=3.20.3,<4', 'pytest': 'pytest', 'pytest-timeout': 'pytest-timeout', 'pytest-xdist': 'pytest-xdist', 'ruff': 'ruff>=0.0.241', 'safetensors': 'safetensors', 'sentencepiece': 'sentencepiece>=0.1.91,!=0.1.92', 'scipy': 'scipy', 'onnx': 'onnx', 'regex': 'regex!=2019.12.17', 'requests': 'requests', 'tensorboard': 'tensorboard', 'torch': 'torch>=1.4', 'torchvision': 'torchvision', 'transformers': 'transformers>=4.25.1', 'urllib3': 'urllib3<=2.0.0', }
31
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['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 lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
1
from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'google/efficientnet-b7': 'https://huggingface.co/google/efficientnet-b7/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "efficientnet" def __init__( self : List[Any] , _lowerCAmelCase : int = 3 , _lowerCAmelCase : int = 600 , _lowerCAmelCase : float = 2.0 , _lowerCAmelCase : float = 3.1 , _lowerCAmelCase : int = 8 , _lowerCAmelCase : List[int] = [3, 3, 5, 3, 5, 5, 3] , _lowerCAmelCase : List[int] = [32, 16, 24, 40, 80, 112, 192] , _lowerCAmelCase : List[int] = [16, 24, 40, 80, 112, 192, 320] , _lowerCAmelCase : List[int] = [] , _lowerCAmelCase : List[int] = [1, 2, 2, 2, 1, 2, 1] , _lowerCAmelCase : List[int] = [1, 2, 2, 3, 3, 4, 1] , _lowerCAmelCase : List[int] = [1, 6, 6, 6, 6, 6, 6] , _lowerCAmelCase : float = 0.25 , _lowerCAmelCase : str = "swish" , _lowerCAmelCase : int = 2_560 , _lowerCAmelCase : str = "mean" , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 0.001 , _lowerCAmelCase : float = 0.99 , _lowerCAmelCase : float = 0.5 , _lowerCAmelCase : float = 0.2 , **_lowerCAmelCase : str , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = dropout_rate SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(_lowerCAmelCase ) * 4 class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = version.parse("1.11" ) @property def lowerCAmelCase_ ( self : Union[str, Any] ): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def lowerCAmelCase_ ( self : List[Any] ): return 1E-5
31
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'} class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "openai-gpt" lowercase_ = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Optional[int] , _lowerCAmelCase : Union[str, Any]=40_478 , _lowerCAmelCase : Union[str, Any]=512 , _lowerCAmelCase : int=768 , _lowerCAmelCase : str=12 , _lowerCAmelCase : Optional[int]=12 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : List[str]=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Optional[int]=0.1 , _lowerCAmelCase : Tuple=1E-5 , _lowerCAmelCase : Union[str, Any]=0.02 , _lowerCAmelCase : Any="cls_index" , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : List[str]=0.1 , **_lowerCAmelCase : Optional[int] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = n_positions SCREAMING_SNAKE_CASE_ = n_embd SCREAMING_SNAKE_CASE_ = n_layer SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = afn SCREAMING_SNAKE_CASE_ = resid_pdrop SCREAMING_SNAKE_CASE_ = embd_pdrop SCREAMING_SNAKE_CASE_ = attn_pdrop SCREAMING_SNAKE_CASE_ = layer_norm_epsilon SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = summary_type SCREAMING_SNAKE_CASE_ = summary_use_proj SCREAMING_SNAKE_CASE_ = summary_activation SCREAMING_SNAKE_CASE_ = summary_first_dropout SCREAMING_SNAKE_CASE_ = summary_proj_to_labels super().__init__(**_lowerCAmelCase )
31
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> float: if not nums: # Makes sure that the list is not empty raise ValueError('List is empty' ) SCREAMING_SNAKE_CASE_ = sum(__UpperCAmelCase ) / len(__UpperCAmelCase ) # Calculate the average return sum(abs(x - average ) for x in nums ) / len(__UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod()
31
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
1
from __future__ import annotations def UpperCAmelCase_ ( __UpperCAmelCase : list[int] , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : int ) -> None: if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = array[indexa], array[indexa] def UpperCAmelCase_ ( __UpperCAmelCase : list[int] , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : int ) -> None: if length > 1: SCREAMING_SNAKE_CASE_ = int(length / 2 ) for i in range(__UpperCAmelCase , low + middle ): comp_and_swap(__UpperCAmelCase , __UpperCAmelCase , i + middle , __UpperCAmelCase ) bitonic_merge(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) bitonic_merge(__UpperCAmelCase , low + middle , __UpperCAmelCase , __UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : int ) -> None: if length > 1: SCREAMING_SNAKE_CASE_ = int(length / 2 ) bitonic_sort(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , 1 ) bitonic_sort(__UpperCAmelCase , low + middle , __UpperCAmelCase , 0 ) bitonic_merge(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = input('Enter numbers separated by a comma:\n').strip() lowerCamelCase__ : Tuple = [int(item.strip()) for item in user_input.split(',')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('\nSorted array in ascending order is: ', end='') print(*unsorted, sep=', ') bitonic_merge(unsorted, 0, len(unsorted), 0) print('Sorted array in descending order is: ', end='') print(*unsorted, sep=', ')
31
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 lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['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 lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
1
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('socket.socket' ) @patch('builtins.open' ) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> Union[str, Any]: # ===== initialization ===== SCREAMING_SNAKE_CASE_ = Mock() SCREAMING_SNAKE_CASE_ = conn, Mock() SCREAMING_SNAKE_CASE_ = iter([1, None] ) SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase : next(__UpperCAmelCase ) # ===== invoke ===== send_file(filename='mytext.txt' , testing=__UpperCAmelCase ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
31
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
1
from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "" lowercase_ = "hf-legacy" # "hf://"" is reserved for hffs def __init__( self : List[str] , _lowerCAmelCase : Optional[DatasetInfo] = None , _lowerCAmelCase : Optional[str] = None , **_lowerCAmelCase : Optional[int] , ): super().__init__(self , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = repo_info SCREAMING_SNAKE_CASE_ = token SCREAMING_SNAKE_CASE_ = None def lowerCAmelCase_ ( self : Tuple ): if self.dir_cache is None: SCREAMING_SNAKE_CASE_ = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes SCREAMING_SNAKE_CASE_ = { 'name': hf_file.rfilename, 'size': None, 'type': 'file', } self.dir_cache.update( { str(_lowerCAmelCase ): {'name': str(_lowerCAmelCase ), 'size': None, 'type': 'directory'} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : str , _lowerCAmelCase : str = "rb" , **_lowerCAmelCase : Optional[Any] , ): if not isinstance(self.repo_info , _lowerCAmelCase ): raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}" ) SCREAMING_SNAKE_CASE_ = hf_hub_url(self.repo_info.id , _lowerCAmelCase , revision=self.repo_info.sha ) return fsspec.open( _lowerCAmelCase , mode=_lowerCAmelCase , headers=get_authentication_headers_for_url(_lowerCAmelCase , use_auth_token=self.token ) , client_kwargs={'trust_env': True} , ).open() def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): self._get_dirs() SCREAMING_SNAKE_CASE_ = self._strip_protocol(_lowerCAmelCase ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : List[str] , _lowerCAmelCase : Union[str, Any]=False , **_lowerCAmelCase : List[str] ): self._get_dirs() SCREAMING_SNAKE_CASE_ = PurePosixPath(path.strip('/' ) ) SCREAMING_SNAKE_CASE_ = {} for p, f in self.dir_cache.items(): SCREAMING_SNAKE_CASE_ = PurePosixPath(p.strip('/' ) ) SCREAMING_SNAKE_CASE_ = p.parent if root == path: SCREAMING_SNAKE_CASE_ = f SCREAMING_SNAKE_CASE_ = list(paths.values() ) if detail: return out else: return sorted(f['name'] for f in out )
31
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
1
from __future__ import annotations from typing import Any class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int = 6 ): SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None self.create_linked_list(_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = Node() SCREAMING_SNAKE_CASE_ = current_node SCREAMING_SNAKE_CASE_ = current_node SCREAMING_SNAKE_CASE_ = current_node for _ in range(1 , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = Node() SCREAMING_SNAKE_CASE_ = current_node SCREAMING_SNAKE_CASE_ = previous_node SCREAMING_SNAKE_CASE_ = current_node SCREAMING_SNAKE_CASE_ = self.front SCREAMING_SNAKE_CASE_ = previous_node def lowerCAmelCase_ ( self : str ): return ( self.front == self.rear and self.front is not None and self.front.data is None ) def lowerCAmelCase_ ( self : str ): self.check_can_perform_operation() return self.front.data if self.front else None def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Any ): if self.rear is None: return self.check_is_full() if not self.is_empty(): SCREAMING_SNAKE_CASE_ = self.rear.next if self.rear: SCREAMING_SNAKE_CASE_ = data def lowerCAmelCase_ ( self : str ): self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: SCREAMING_SNAKE_CASE_ = self.front.data SCREAMING_SNAKE_CASE_ = None return data SCREAMING_SNAKE_CASE_ = self.front SCREAMING_SNAKE_CASE_ = old_front.next SCREAMING_SNAKE_CASE_ = old_front.data SCREAMING_SNAKE_CASE_ = None return data def lowerCAmelCase_ ( self : Any ): if self.is_empty(): raise Exception('Empty Queue' ) def lowerCAmelCase_ ( self : Any ): if self.rear and self.rear.next == self.front: raise Exception('Full Queue' ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] ): SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None if __name__ == "__main__": import doctest doctest.testmod()
31
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
1
import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class lowerCamelCase_ : '''simple docstring''' def __init__( self : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any]=sys.maxsize ): SCREAMING_SNAKE_CASE_ = 'bilinear' SCREAMING_SNAKE_CASE_ = max_size SCREAMING_SNAKE_CASE_ = short_edge_length def __call__( self : int , _lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = [] for img in imgs: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = img.shape[:2] # later: provide list and randomly choose index for resize SCREAMING_SNAKE_CASE_ = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1 ) if size == 0: return img SCREAMING_SNAKE_CASE_ = size * 1.0 / min(_lowerCAmelCase , _lowerCAmelCase ) if h < w: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = size, scale * w else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = scale * h, size if max(_lowerCAmelCase , _lowerCAmelCase ) > self.max_size: SCREAMING_SNAKE_CASE_ = self.max_size * 1.0 / max(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = newh * scale SCREAMING_SNAKE_CASE_ = neww * scale SCREAMING_SNAKE_CASE_ = int(neww + 0.5 ) SCREAMING_SNAKE_CASE_ = int(newh + 0.5 ) if img.dtype == np.uinta: SCREAMING_SNAKE_CASE_ = Image.fromarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR ) SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ = img.permute(2 , 0 , 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw SCREAMING_SNAKE_CASE_ = nn.functional.interpolate( _lowerCAmelCase , (newh, neww) , mode=self.interp_method , align_corners=_lowerCAmelCase ).squeeze(0 ) img_augs.append(_lowerCAmelCase ) return img_augs class lowerCamelCase_ : '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST ) SCREAMING_SNAKE_CASE_ = cfg.INPUT.FORMAT SCREAMING_SNAKE_CASE_ = cfg.SIZE_DIVISIBILITY SCREAMING_SNAKE_CASE_ = cfg.PAD_VALUE SCREAMING_SNAKE_CASE_ = cfg.INPUT.MAX_SIZE_TEST SCREAMING_SNAKE_CASE_ = cfg.MODEL.DEVICE SCREAMING_SNAKE_CASE_ = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) SCREAMING_SNAKE_CASE_ = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) SCREAMING_SNAKE_CASE_ = lambda _lowerCAmelCase : (x - self.pixel_mean) / self.pixel_std def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = tuple(max(_lowerCAmelCase ) for s in zip(*[img.shape for img in images] ) ) SCREAMING_SNAKE_CASE_ = [im.shape[-2:] for im in images] SCREAMING_SNAKE_CASE_ = [ nn.functional.pad( _lowerCAmelCase , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(_lowerCAmelCase , _lowerCAmelCase ) ] return torch.stack(_lowerCAmelCase ), torch.tensor(_lowerCAmelCase ) def __call__( self : List[str] , _lowerCAmelCase : str , _lowerCAmelCase : List[str]=False ): with torch.no_grad(): if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = [images] if single_image: assert len(_lowerCAmelCase ) == 1 for i in range(len(_lowerCAmelCase ) ): if isinstance(images[i] , torch.Tensor ): images.insert(_lowerCAmelCase , images.pop(_lowerCAmelCase ).to(self.device ).float() ) elif not isinstance(images[i] , torch.Tensor ): images.insert( _lowerCAmelCase , torch.as_tensor(img_tensorize(images.pop(_lowerCAmelCase ) , input_format=self.input_format ) ) .to(self.device ) .float() , ) # resize smallest edge SCREAMING_SNAKE_CASE_ = torch.tensor([im.shape[:2] for im in images] ) SCREAMING_SNAKE_CASE_ = self.aug(_lowerCAmelCase ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic SCREAMING_SNAKE_CASE_ = [self.normalizer(_lowerCAmelCase ) for x in images] # now pad them to do the following operations SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.pad(_lowerCAmelCase ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad SCREAMING_SNAKE_CASE_ = torch.true_divide(_lowerCAmelCase , _lowerCAmelCase ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Any ) -> Union[str, Any]: boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : Tuple[int, int] ) -> Any: assert torch.isfinite(__UpperCAmelCase ).all(), "Box tensor contains infinite or NaN!" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = box_size tensor[:, 0].clamp_(min=0 , max=__UpperCAmelCase ) tensor[:, 1].clamp_(min=0 , max=__UpperCAmelCase ) tensor[:, 2].clamp_(min=0 , max=__UpperCAmelCase ) tensor[:, 3].clamp_(min=0 , max=__UpperCAmelCase )
31
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
1
import argparse import os import re lowerCamelCase__ : List[Any] = 'src/transformers/models/auto' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict lowerCamelCase__ : str = re.compile(r'[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict') # re pattern that matches identifiers in mappings lowerCamelCase__ : Tuple = re.compile(r'\s*\(\s*"(\S[^"]+)"') def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : bool = False ) -> int: with open(__UpperCAmelCase , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ = f.read() SCREAMING_SNAKE_CASE_ = content.split('\n' ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 0 while line_idx < len(__UpperCAmelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: SCREAMING_SNAKE_CASE_ = len(re.search(r'^(\s*)\S' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(' ' * indent + '(' ): new_lines.append(lines[line_idx] ) line_idx += 1 SCREAMING_SNAKE_CASE_ = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": SCREAMING_SNAKE_CASE_ = line_idx while not lines[line_idx].startswith(' ' * indent + ')' ): line_idx += 1 blocks.append('\n'.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers SCREAMING_SNAKE_CASE_ = sorted(__UpperCAmelCase , key=lambda __UpperCAmelCase : _re_identifier.search(__UpperCAmelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(__UpperCAmelCase , 'w' , encoding='utf-8' ) as f: f.write('\n'.join(__UpperCAmelCase ) ) elif "\n".join(__UpperCAmelCase ) != content: return True def UpperCAmelCase_ ( __UpperCAmelCase : bool = False ) -> str: SCREAMING_SNAKE_CASE_ = [os.path.join(__UpperCAmelCase , __UpperCAmelCase ) for f in os.listdir(__UpperCAmelCase ) if f.endswith('.py' )] SCREAMING_SNAKE_CASE_ = [sort_auto_mapping(__UpperCAmelCase , overwrite=__UpperCAmelCase ) for fname in fnames] if not overwrite and any(__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = [f for f, d in zip(__UpperCAmelCase , __UpperCAmelCase ) if d] raise ValueError( f"The following files have auto mappings that need sorting: {', '.join(__UpperCAmelCase )}. Run `make style` to fix" ' this.' ) if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('--check_only', action='store_true', help='Whether to only check or fix style.') lowerCamelCase__ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
31
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
1
from scipy.stats import pearsonr import datasets lowerCamelCase__ : Any = '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n' lowerCamelCase__ : Any = '\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n [\'p-value\', \'pearsonr\']\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n >>> print(round(results[\'p-value\'], 2))\n 0.15\n' lowerCamelCase__ : Union[str, Any] = '\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase_ ( datasets.Metric ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'] , ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str]=False ): if return_pvalue: SCREAMING_SNAKE_CASE_ = pearsonr(_lowerCAmelCase , _lowerCAmelCase ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(_lowerCAmelCase , _lowerCAmelCase )[0] )}
31
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
1
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_lowerCAmelCase , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_lowerCAmelCase , 'num_attention_heads' ) ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : str , _lowerCAmelCase : str , _lowerCAmelCase : int=13 , _lowerCAmelCase : Dict=64 , _lowerCAmelCase : Optional[Any]=3 , _lowerCAmelCase : Dict=3 , _lowerCAmelCase : List[Any]=2 , _lowerCAmelCase : Tuple=1 , _lowerCAmelCase : str=16 , _lowerCAmelCase : Dict=[128, 256, 384] , _lowerCAmelCase : List[str]=[4, 6, 8] , _lowerCAmelCase : List[Any]=[2, 3, 4] , _lowerCAmelCase : Optional[int]=[16, 16, 16] , _lowerCAmelCase : List[Any]=0 , _lowerCAmelCase : Optional[int]=[2, 2, 2] , _lowerCAmelCase : Optional[Any]=[2, 2, 2] , _lowerCAmelCase : Optional[int]=0.02 , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : int=2 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = kernel_size SCREAMING_SNAKE_CASE_ = stride SCREAMING_SNAKE_CASE_ = padding SCREAMING_SNAKE_CASE_ = hidden_sizes SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = key_dim SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = attention_ratio SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = [ ['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], ] SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = num_labels SCREAMING_SNAKE_CASE_ = initializer_range def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self : int ): 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 lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = LevitModel(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (self.image_size, self.image_size) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE_ = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) SCREAMING_SNAKE_CASE_ = 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 lowerCAmelCase_ ( self : str , _lowerCAmelCase : int , _lowerCAmelCase : Any , _lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = LevitForImageClassification(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ( (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher) if is_torch_available() else () ) lowercase_ = ( { "feature-extraction": LevitModel, "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher), } if is_torch_available() else {} ) lowercase_ = False lowercase_ = False lowercase_ = False lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = LevitModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase , has_text_modality=_lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : Union[str, Any] ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCAmelCase_ ( self : Dict ): return @unittest.skip(reason='Levit does not use inputs_embeds' ) def lowerCAmelCase_ ( self : str ): pass @unittest.skip(reason='Levit does not support input and output embeddings' ) def lowerCAmelCase_ ( self : Union[str, Any] ): pass @unittest.skip(reason='Levit does not output attentions' ) def lowerCAmelCase_ ( self : List[Any] ): pass def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): def check_hidden_states_output(_lowerCAmelCase : Optional[int] , _lowerCAmelCase : int , _lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = outputs.hidden_states SCREAMING_SNAKE_CASE_ = len(self.model_tester.depths ) + 1 self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (self.model_tester.image_size, self.model_tester.image_size) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE_ = floor( ( (height + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) SCREAMING_SNAKE_CASE_ = 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], ] , ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_ = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowerCAmelCase_ ( self : Union[str, Any] ): pass def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : str , _lowerCAmelCase : Tuple , _lowerCAmelCase : Optional[Any]=False ): SCREAMING_SNAKE_CASE_ = super()._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase , return_labels=_lowerCAmelCase ) if return_labels: if model_class.__name__ == "LevitForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # LevitForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(_lowerCAmelCase ) or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase , return_labels=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ).loss loss.backward() def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: if model_class in get_values(_lowerCAmelCase ) or not model_class.supports_gradient_checkpointing: continue # LevitForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "LevitForImageClassificationWithTeacher": continue SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) model.gradient_checkpointing_enable() model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase , return_labels=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ).loss loss.backward() def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = [ {'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(_lowerCAmelCase ), ] or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=F"Testing {model_class} with {problem_type['title']}" ): SCREAMING_SNAKE_CASE_ = problem_type['title'] SCREAMING_SNAKE_CASE_ = problem_type['num_labels'] SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase , return_labels=_lowerCAmelCase ) if problem_type["num_labels"] > 1: SCREAMING_SNAKE_CASE_ = inputs['labels'].unsqueeze(1 ).repeat(1 , problem_type['num_labels'] ) SCREAMING_SNAKE_CASE_ = 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=_lowerCAmelCase ) as warning_list: SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ).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 lowerCAmelCase_ ( self : Optional[int] ): for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = LevitModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def UpperCAmelCase_ ( ) -> List[Any]: SCREAMING_SNAKE_CASE_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self : Dict ): return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=_lowerCAmelCase , return_tensors='pt' ).to(_lowerCAmelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.tensor([1.0448, -0.3745, -1.8317] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _lowerCAmelCase , atol=1E-4 ) )
31
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
1
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : str=2 , _lowerCAmelCase : str=True , _lowerCAmelCase : Optional[Any]=False , _lowerCAmelCase : Tuple=10 , _lowerCAmelCase : List[str]=3 , _lowerCAmelCase : Union[str, Any]=32 * 8 , _lowerCAmelCase : Dict=32 * 8 , _lowerCAmelCase : Optional[Any]=4 , _lowerCAmelCase : List[str]=64 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_auxiliary_loss SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = min_size SCREAMING_SNAKE_CASE_ = max_size SCREAMING_SNAKE_CASE_ = num_labels SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = hidden_dim def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=_lowerCAmelCase ) > 0.5 ).float() SCREAMING_SNAKE_CASE_ = (torch.rand((self.batch_size, self.num_labels) , device=_lowerCAmelCase ) > 0.5).long() SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = MaskaFormerConfig( hidden_size=self.hidden_dim , ) SCREAMING_SNAKE_CASE_ = self.num_queries SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = [1, 1, 1, 1] SCREAMING_SNAKE_CASE_ = self.num_channels SCREAMING_SNAKE_CASE_ = 64 SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = self.hidden_dim SCREAMING_SNAKE_CASE_ = self.hidden_dim SCREAMING_SNAKE_CASE_ = self.hidden_dim return config def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = {'pixel_values': pixel_values, 'pixel_mask': pixel_mask} return config, inputs_dict def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = output.encoder_hidden_states SCREAMING_SNAKE_CASE_ = output.pixel_decoder_hidden_states SCREAMING_SNAKE_CASE_ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(_lowerCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_lowerCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_lowerCAmelCase ) , config.decoder_layers ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : Tuple=False ): with torch.no_grad(): SCREAMING_SNAKE_CASE_ = MaskaFormerModel(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(pixel_values=_lowerCAmelCase , pixel_mask=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , output_hidden_states=_lowerCAmelCase ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = MaskaFormerForUniversalSegmentation(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() def comm_check_on_output(_lowerCAmelCase : Any ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=_lowerCAmelCase , pixel_mask=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase ) comm_check_on_output(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model( pixel_values=_lowerCAmelCase , pixel_mask=_lowerCAmelCase , mask_labels=_lowerCAmelCase , class_labels=_lowerCAmelCase ) comm_check_on_output(_lowerCAmelCase ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () lowercase_ = {"feature-extraction": MaskaFormerModel} if is_torch_available() else {} lowercase_ = False lowercase_ = False lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = MaskaFormerModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase , has_text_modality=_lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(_lowerCAmelCase , **_lowerCAmelCase , output_hidden_states=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_lowerCAmelCase ) @unittest.skip(reason='Mask2Former does not use inputs_embeds' ) def lowerCAmelCase_ ( self : int ): pass @unittest.skip(reason='Mask2Former does not have a get_input_embeddings method' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip(reason='Mask2Former is not a generative model' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip(reason='Mask2Former does not use token embeddings' ) def lowerCAmelCase_ ( self : int ): pass @require_torch_multi_gpu @unittest.skip( reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def lowerCAmelCase_ ( self : Optional[int] ): pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowerCAmelCase_ ( self : List[Any] ): pass def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : Optional[Any] ): for model_name in ["facebook/mask2former-swin-small-coco-instance"]: SCREAMING_SNAKE_CASE_ = MaskaFormerModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = (self.model_tester.min_size,) * 2 SCREAMING_SNAKE_CASE_ = { 'pixel_values': torch.randn((2, 3, *size) , device=_lowerCAmelCase ), 'mask_labels': torch.randn((2, 10, *size) , device=_lowerCAmelCase ), 'class_labels': torch.zeros(2 , 10 , device=_lowerCAmelCase ).long(), } SCREAMING_SNAKE_CASE_ = self.model_tester.get_config() SCREAMING_SNAKE_CASE_ = MaskaFormerForUniversalSegmentation(_lowerCAmelCase ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) self.assertTrue(outputs.loss is not None ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(_lowerCAmelCase , **_lowerCAmelCase , output_hidden_states=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase , output_attentions=_lowerCAmelCase ) self.assertTrue(outputs.attentions is not None ) def lowerCAmelCase_ ( self : Tuple ): if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = self.all_model_classes[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , mask_labels=_lowerCAmelCase , class_labels=_lowerCAmelCase ).loss loss.backward() def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.all_model_classes[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = model_class(_lowerCAmelCase ).to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , mask_labels=_lowerCAmelCase , class_labels=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE_ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE_ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE_ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=_lowerCAmelCase ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) lowerCamelCase__ : List[Any] = 1E-4 def UpperCAmelCase_ ( ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_vision @slow class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self : Any ): return "facebook/mask2former-swin-small-coco-instance" @cached_property def lowerCAmelCase_ ( self : int ): return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(_lowerCAmelCase , return_tensors='pt' ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_lowerCAmelCase , (1, 3, 384, 384) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]] ).to(_lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , _lowerCAmelCase , atol=_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]] ).to(_lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , _lowerCAmelCase , atol=_lowerCAmelCase ) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]] ).to(_lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , _lowerCAmelCase , atol=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(_lowerCAmelCase ).eval() SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(_lowerCAmelCase , return_tensors='pt' ).to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_lowerCAmelCase , (1, 3, 384, 384) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) # masks_queries_logits SCREAMING_SNAKE_CASE_ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) SCREAMING_SNAKE_CASE_ = [ [-8.7839, -9.0056, -8.8121], [-7.4104, -7.0313, -6.5401], [-6.6105, -6.3427, -6.4675], ] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _lowerCAmelCase , atol=_lowerCAmelCase ) ) # class_queries_logits SCREAMING_SNAKE_CASE_ = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [ [1.8324, -8.0835, -4.1922], [0.8450, -9.0050, -3.6053], [0.3045, -7.7293, -3.0275], ] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _lowerCAmelCase , atol=_lowerCAmelCase ) ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(_lowerCAmelCase ).eval() SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = image_processor( [np.zeros((3, 800, 1_333) ), np.zeros((3, 800, 1_333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors='pt' , ) SCREAMING_SNAKE_CASE_ = inputs['pixel_values'].to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [el.to(_lowerCAmelCase ) for el in inputs['mask_labels']] SCREAMING_SNAKE_CASE_ = [el.to(_lowerCAmelCase ) for el in inputs['class_labels']] with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) self.assertTrue(outputs.loss is not None )
31
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
1
def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int | float]] ) -> int: SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = len(matrix[0] ) SCREAMING_SNAKE_CASE_ = min(__UpperCAmelCase , __UpperCAmelCase ) for row in range(__UpperCAmelCase ): # Check if diagonal element is not zero if matrix[row][row] != 0: # Eliminate all the elements below the diagonal for col in range(row + 1 , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = matrix[col][row] / matrix[row][row] for i in range(__UpperCAmelCase , __UpperCAmelCase ): matrix[col][i] -= multiplier * matrix[row][i] else: # Find a non-zero diagonal element to swap rows SCREAMING_SNAKE_CASE_ = True for i in range(row + 1 , __UpperCAmelCase ): if matrix[i][row] != 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = matrix[i], matrix[row] SCREAMING_SNAKE_CASE_ = False break if reduce: rank -= 1 for i in range(__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = matrix[i][rank] # Reduce the row pointer by one to stay on the same row row -= 1 return rank if __name__ == "__main__": import doctest doctest.testmod()
31
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
1
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging lowerCamelCase__ : Any = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : int = 101 ): SCREAMING_SNAKE_CASE_ = length def __len__( self : str ): return self.length def __getitem__( self : Dict , _lowerCAmelCase : str ): return i class lowerCamelCase_ : '''simple docstring''' def __call__( self : List[str] , _lowerCAmelCase : Optional[int] ): return {"input_ids": torch.tensor(_lowerCAmelCase ), "labels": torch.tensor(_lowerCAmelCase )} class lowerCamelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] ): super().__init__() # Add some (unused) params otherwise DDP will complain. SCREAMING_SNAKE_CASE_ = nn.Linear(120 , 80 ) def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : int , _lowerCAmelCase : int=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch_neuroncore def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = F"--nproc_per_node=2\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() SCREAMING_SNAKE_CASE_ = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE_ = F"--output_dir {output_dir}".split() SCREAMING_SNAKE_CASE_ = ['torchrun'] + distributed_args + args execute_subprocess_async(_lowerCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch_multi_gpu def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = F"--nproc_per_node={torch.cuda.device_count()}\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() SCREAMING_SNAKE_CASE_ = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE_ = F"--output_dir {output_dir}".split() SCREAMING_SNAKE_CASE_ = ['torchrun'] + distributed_args + args execute_subprocess_async(_lowerCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py lowerCamelCase__ : int = HfArgumentParser((TrainingArguments,)) lowerCamelCase__ : Optional[int] = parser.parse_args_into_dataclasses()[0] logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, ''' f'''distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}''' ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: lowerCamelCase__ : str = DummyDataset(dataset_length) def UpperCAmelCase_ ( __UpperCAmelCase : EvalPrediction ) -> Dict: SCREAMING_SNAKE_CASE_ = list(range(len(__UpperCAmelCase ) ) ) SCREAMING_SNAKE_CASE_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( 'Predictions and/or labels do not match expected results:\n - predictions: ' f"{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}" ) return {"success": success} lowerCamelCase__ : Tuple = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) lowerCamelCase__ : List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowerCamelCase__ : Dict = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowerCamelCase__ : List[str] = 2 lowerCamelCase__ : Tuple = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowerCamelCase__ : int = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowerCamelCase__ : Union[str, Any] = None
31
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_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
1
class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = name SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = weight def __repr__( self : int ): return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def lowerCAmelCase_ ( self : Any ): return self.value def lowerCAmelCase_ ( self : Optional[int] ): return self.name def lowerCAmelCase_ ( self : Dict ): return self.weight def lowerCAmelCase_ ( self : List[str] ): return self.value / self.weight def UpperCAmelCase_ ( __UpperCAmelCase : Tuple , __UpperCAmelCase : Tuple , __UpperCAmelCase : Dict ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__UpperCAmelCase ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[Any] ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = sorted(__UpperCAmelCase , key=__UpperCAmelCase , reverse=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 0.0, 0.0 for i in range(len(__UpperCAmelCase ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def UpperCAmelCase_ ( ) -> List[Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
31
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
1
from __future__ import annotations import math from collections.abc import Callable def UpperCAmelCase_ ( __UpperCAmelCase : Callable[[int | float], int | float] , __UpperCAmelCase : int | float , __UpperCAmelCase : int | float , __UpperCAmelCase : int = 1_00 , ) -> float: SCREAMING_SNAKE_CASE_ = x_start SCREAMING_SNAKE_CASE_ = fnc(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 0.0 for _ in range(__UpperCAmelCase ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE_ = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE_ = fnc(__UpperCAmelCase ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE_ = xa SCREAMING_SNAKE_CASE_ = fxa return length if __name__ == "__main__": def UpperCAmelCase_ ( __UpperCAmelCase : Tuple ) -> List[Any]: return math.sin(10 * x ) print('f(x) = sin(10 * x)') print('The length of the curve from x = -10 to x = 10 is:') lowerCamelCase__ : Any = 10 while i <= 100_000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
31
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
1
import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : List[str] = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.plbart.modeling_plbart import shift_tokens_right lowerCamelCase__ : int = 50_003 lowerCamelCase__ : List[str] = 50_002 @require_sentencepiece @require_tokenizers class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = PLBartTokenizer lowercase_ = None lowercase_ = False def lowerCAmelCase_ ( self : List[str] ): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_ = PLBartTokenizer(_lowerCAmelCase , language_codes='base' , keep_accents=_lowerCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = PLBartTokenizer(_lowerCAmelCase , language_codes='base' , keep_accents=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('This is a test' ) self.assertListEqual(_lowerCAmelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _lowerCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) self.assertListEqual( _lowerCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual( _lowerCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.vocab_size SCREAMING_SNAKE_CASE_ = [tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) for x in range(end - 4 , _lowerCAmelCase )] self.assertListEqual(_lowerCAmelCase , ['__java__', '__python__', '__en_XX__', '<mask>'] ) SCREAMING_SNAKE_CASE_ = 'java.lang.Exception, python.lang.Exception, javascript, php, ruby, go' SCREAMING_SNAKE_CASE_ = tokenizer(_lowerCAmelCase ).input_ids self.assertEqual( tokenizer.decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase ) , _lowerCAmelCase , ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = PLBartTokenizer(_lowerCAmelCase , language_codes='multi' , keep_accents=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('This is a test' ) self.assertListEqual(_lowerCAmelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _lowerCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) self.assertListEqual( _lowerCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) self.assertListEqual( _lowerCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.vocab_size SCREAMING_SNAKE_CASE_ = [tokenizer.convert_ids_to_tokens(_lowerCAmelCase ) for x in range(end - 7 , _lowerCAmelCase )] self.assertListEqual( _lowerCAmelCase , ['__java__', '__python__', '__en_XX__', '__javascript__', '__php__', '__ruby__', '__go__'] ) SCREAMING_SNAKE_CASE_ = 'java.lang.Exception, python.lang.Exception, javascript, php, ruby, go' SCREAMING_SNAKE_CASE_ = tokenizer(_lowerCAmelCase ).input_ids self.assertEqual( tokenizer.decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase ) , _lowerCAmelCase , ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' lowercase_ = "uclanlp/plbart-python-en_XX" lowercase_ = [ "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])", "def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])", ] lowercase_ = [ "Returns the maximum value of a b c.", "Sums the values of a b c.", ] lowercase_ = [ 134, 5_452, 33_460, 33_441, 33_463, 33_465, 33_463, 33_449, 988, 20, 33_456, 19, 33_456, 771, 39, 4_258, 889, 3_318, 33_441, 33_463, 33_465, 33_463, 33_449, 2_471, 2, PYTHON_CODE, ] @classmethod def lowerCAmelCase_ ( cls : Optional[int] ): SCREAMING_SNAKE_CASE_ = PLBartTokenizer.from_pretrained( cls.checkpoint_name , language_codes='base' , src_lang='python' , tgt_lang='en_XX' ) SCREAMING_SNAKE_CASE_ = 1 return cls def lowerCAmelCase_ ( self : Union[str, Any] ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['__java__'] , 50_001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['__python__'] , 50_002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['__en_XX__'] , 50_003 ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): self.assertIn(_lowerCAmelCase , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE_ = [EN_CODE, 9_037, 33_442, 57, 752, 153, 14, 56, 18, 9, 2] SCREAMING_SNAKE_CASE_ = self.tokenizer.decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_lowerCAmelCase ) self.assertEqual(_lowerCAmelCase , _lowerCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = ['def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])' * 20] self.assertIsInstance(src_text[0] , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = self.tokenizer(_lowerCAmelCase , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , _lowerCAmelCase ) self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', '__java__'] ) , [50_004, 50_001] ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = PLBartTokenizer.from_pretrained(_lowerCAmelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , _lowerCAmelCase ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=_lowerCAmelCase , return_tensors='pt' ) SCREAMING_SNAKE_CASE_ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 self.assertEqual(batch.input_ids[1][-2:].tolist() , [2, PYTHON_CODE] ) self.assertEqual(batch.decoder_input_ids[1][0] , _lowerCAmelCase ) self.assertEqual(batch.decoder_input_ids[1][-1] , 2 ) self.assertEqual(batch.labels[1][-2:].tolist() , [2, EN_CODE] ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=len(self.expected_src_tokens ) , return_tensors='pt' , ) SCREAMING_SNAKE_CASE_ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) self.assertEqual((2, 26) , batch.input_ids.shape ) self.assertEqual((2, 26) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE_ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , _lowerCAmelCase ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, PYTHON_CODE] ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer(self.src_text , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=3 , return_tensors='pt' ) SCREAMING_SNAKE_CASE_ = self.tokenizer( text_target=self.tgt_text , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=10 , return_tensors='pt' ) SCREAMING_SNAKE_CASE_ = targets['input_ids'] SCREAMING_SNAKE_CASE_ = shift_tokens_right(_lowerCAmelCase , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.tokenizer._build_translation_inputs( 'A test' , return_tensors='pt' , src_lang='en_XX' , tgt_lang='java' ) self.assertEqual( nested_simplify(_lowerCAmelCase ) , { # A, test, EOS, en_XX 'input_ids': [[150, 242, 2, 50_003]], 'attention_mask': [[1, 1, 1, 1]], # java 'forced_bos_token_id': 50_001, } , )
31
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
1
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
1
def __lowercase ( snake_case ): """simple docstring""" if num <= 0: raise ValueError('''Input must be a positive integer''' ) __magic_name__ :List[Any] = [True] * (num + 1) __magic_name__ :Tuple = 2 while p * p <= num: if primes[p]: for i in range(p * p, num + 1, snake_case ): __magic_name__ :List[str] = False p += 1 return [prime for prime in range(2, num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ : Tuple = int(input("""Enter a positive integer: """).strip()) print(prime_sieve_eratosthenes(user_num))
0
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['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 lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
0
from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class __lowerCamelCase (_a ): _lowercase = """""" _lowercase = """hf-legacy""" # "hf://"" is reserved for hffs def __init__( self: Tuple,A_: Optional[DatasetInfo] = None,A_: Optional[str] = None,**A_: List[str],): '''simple docstring''' super().__init__(self,**A_ ) __UpperCamelCase = repo_info __UpperCamelCase = token __UpperCamelCase = None def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' if self.dir_cache is None: __UpperCamelCase = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes __UpperCamelCase = { 'name': hf_file.rfilename, 'size': None, 'type': 'file', } self.dir_cache.update( { str(A_ ): {'name': str(A_ ), 'size': None, 'type': 'directory'} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def snake_case_ ( self: Any,A_: str,A_: str = "rb",**A_: str,): '''simple docstring''' if not isinstance(self.repo_info,A_ ): raise NotImplementedError(F'''Open is only implemented for dataset repositories, but got {self.repo_info}''' ) __UpperCamelCase = hf_hub_url(self.repo_info.id,A_,revision=self.repo_info.sha ) return fsspec.open( A_,mode=A_,headers=get_authentication_headers_for_url(A_,use_auth_token=self.token ),client_kwargs={'trust_env': True},).open() def snake_case_ ( self: int,A_: Optional[int],**A_: Any ): '''simple docstring''' self._get_dirs() __UpperCamelCase = self._strip_protocol(A_ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(A_ ) def snake_case_ ( self: List[Any],A_: Union[str, Any],A_: Any=False,**A_: Any ): '''simple docstring''' self._get_dirs() __UpperCamelCase = PurePosixPath(path.strip('/' ) ) __UpperCamelCase = {} for p, f in self.dir_cache.items(): __UpperCamelCase = PurePosixPath(p.strip('/' ) ) __UpperCamelCase = p.parent if root == path: __UpperCamelCase = f __UpperCamelCase = list(paths.values() ) if detail: return out else: return sorted(f['name'] for f in out )
1
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
0
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCAmelCase_ = logging.get_logger(__name__) class lowerCamelCase__ ( _A): """simple docstring""" a__ : str = ["input_values", "attention_mask"] def __init__( self : Tuple , __lowerCAmelCase : int = 1 , __lowerCAmelCase : int = 1_60_00 , __lowerCAmelCase : float = 0.0 , __lowerCAmelCase : bool = False , __lowerCAmelCase : int = 80 , __lowerCAmelCase : int = 16 , __lowerCAmelCase : int = 64 , __lowerCAmelCase : str = "hann_window" , __lowerCAmelCase : float = 1.0 , __lowerCAmelCase : float = 80 , __lowerCAmelCase : float = 76_00 , __lowerCAmelCase : float = 1E-10 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : bool = True , **__lowerCAmelCase : List[Any] , ) -> Dict: super().__init__(feature_size=__lowerCAmelCase , sampling_rate=__lowerCAmelCase , padding_value=__lowerCAmelCase , **__lowerCAmelCase ) _A = do_normalize _A = return_attention_mask _A = num_mel_bins _A = hop_length _A = win_length _A = win_function _A = frame_signal_scale _A = fmin _A = fmax _A = mel_floor _A = reduction_factor _A = win_length * sampling_rate // 10_00 _A = hop_length * sampling_rate // 10_00 _A = optimal_fft_length(self.sample_size ) _A = (self.n_fft // 2) + 1 _A = window_function(window_length=self.sample_size , name=self.win_function , periodic=__lowerCAmelCase ) _A = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='''slaney''' , mel_scale='''slaney''' , ) if frame_signal_scale != 1.0: warnings.warn( '''The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers''' , __lowerCAmelCase , ) if reduction_factor != 2.0: warnings.warn( '''The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers''' , __lowerCAmelCase , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def snake_case_ ( __lowerCAmelCase : List[np.ndarray] , __lowerCAmelCase : List[np.ndarray] , __lowerCAmelCase : float = 0.0 ) -> List[np.ndarray]: if attention_mask is not None: _A = np.array(__lowerCAmelCase , np.intaa ) _A = [] for vector, length in zip(__lowerCAmelCase , attention_mask.sum(-1 ) ): _A = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: _A = padding_value normed_input_values.append(__lowerCAmelCase ) else: _A = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def snake_case_ ( self : Dict , __lowerCAmelCase : np.ndarray , ) -> np.ndarray: _A = spectrogram( __lowerCAmelCase , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='''log10''' , ) return log_mel_spec.T def __call__( self : List[str] , __lowerCAmelCase : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , __lowerCAmelCase : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , __lowerCAmelCase : Union[bool, str, PaddingStrategy] = False , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , __lowerCAmelCase : Optional[int] = None , **__lowerCAmelCase : List[str] , ) -> BatchFeature: if audio is None and audio_target is None: raise ValueError('''You must provide either `audio` or `audio_target` values.''' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' f''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the ``sampling_rate`` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) if audio is not None: _A = self._process_audio( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase , ) else: _A = None if audio_target is not None: _A = self._process_audio( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase , ) if inputs is None: return inputs_target else: _A = inputs_target['''input_values'''] _A = inputs_target.get('''attention_mask''' ) if decoder_attention_mask is not None: _A = decoder_attention_mask return inputs def snake_case_ ( self : Any , __lowerCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __lowerCAmelCase : bool = False , __lowerCAmelCase : Union[bool, str, PaddingStrategy] = False , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , **__lowerCAmelCase : int , ) -> BatchFeature: _A = isinstance(__lowerCAmelCase , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(f'''Only mono-channel audio is supported for input to {self}''' ) _A = is_batched_numpy or ( isinstance(__lowerCAmelCase , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _A = [np.asarray(__lowerCAmelCase , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(__lowerCAmelCase , np.ndarray ): _A = np.asarray(__lowerCAmelCase , dtype=np.floataa ) elif isinstance(__lowerCAmelCase , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): _A = speech.astype(np.floataa ) # always return batch if not is_batched: _A = [speech] # needed to make pad() work on spectrogram inputs _A = self.feature_size # convert into correct format for padding if is_target: _A = [self._extract_mel_features(__lowerCAmelCase ) for waveform in speech] _A = BatchFeature({'''input_values''': features} ) _A = self.num_mel_bins else: _A = BatchFeature({'''input_values''': speech} ) _A = self.pad( __lowerCAmelCase , padding=__lowerCAmelCase , max_length=__lowerCAmelCase , truncation=__lowerCAmelCase , pad_to_multiple_of=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , **__lowerCAmelCase , ) _A = feature_size_hack # convert input values to correct format _A = padded_inputs['''input_values'''] if not isinstance(input_values[0] , np.ndarray ): _A = [np.asarray(__lowerCAmelCase , dtype=np.floataa ) for array in input_values] elif ( not isinstance(__lowerCAmelCase , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): _A = [array.astype(np.floataa ) for array in input_values] elif isinstance(__lowerCAmelCase , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): _A = input_values.astype(np.floataa ) # convert attention_mask to correct format _A = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: _A = [np.asarray(__lowerCAmelCase , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: _A = ( attention_mask if self._get_padding_strategies(__lowerCAmelCase , max_length=__lowerCAmelCase ) is not PaddingStrategy.DO_NOT_PAD else None ) _A = self.zero_mean_unit_var_norm( padded_inputs['''input_values'''] , attention_mask=__lowerCAmelCase , padding_value=self.padding_value ) if return_tensors is not None: _A = padded_inputs.convert_to_tensors(__lowerCAmelCase ) return padded_inputs def snake_case_ ( self : Union[str, Any] ) -> Dict[str, Any]: _A = super().to_dict() # Don't serialize these as they are derived from the other properties. _A = ['''window''', '''mel_filters''', '''sample_size''', '''sample_stride''', '''n_fft''', '''n_freqs'''] for name in names: if name in output: del output[name] return output
2
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
0
'''simple docstring''' def A_( A : bytes): return "".join([hex(A)[2:].zfill(2).upper() for byte in list(A)]) def A_( A : str): # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(A) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.') # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(A) <= set('0123456789ABCDEF'): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.') # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16) for i in range(0 , len(A) , 2)) if __name__ == "__main__": import doctest doctest.testmod()
3
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
0
"""simple docstring""" import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __UpperCamelCase : int = pytest.mark.integration @pytest.mark.parametrize('path' , ['paws', 'csv'] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Dict , _UpperCAmelCase : Optional[int] ): inspect_dataset(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase = path + '.py' assert script_name in os.listdir(_UpperCAmelCase ) assert "__pycache__" not in os.listdir(_UpperCAmelCase ) @pytest.mark.filterwarnings('ignore:inspect_metric is deprecated:FutureWarning' ) @pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning' ) @pytest.mark.parametrize('path' , ['accuracy'] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Any , _UpperCAmelCase : int ): inspect_metric(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase = path + '.py' assert script_name in os.listdir(_UpperCAmelCase ) assert "__pycache__" not in os.listdir(_UpperCAmelCase ) @pytest.mark.parametrize( 'path, config_name, expected_splits' , [ ('squad', 'plain_text', ['train', 'validation']), ('dalle-mini/wit', 'dalle-mini--wit', ['train']), ('paws', 'labeled_final', ['train', 'test', 'validation']), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str , _UpperCAmelCase : Any , _UpperCAmelCase : Union[str, Any] ): lowerCAmelCase = get_dataset_config_info(_UpperCAmelCase , config_name=_UpperCAmelCase ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( 'path, config_name, expected_exception' , [ ('paws', None, ValueError), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Optional[int] , _UpperCAmelCase : List[str] , _UpperCAmelCase : List[Any] ): with pytest.raises(_UpperCAmelCase ): get_dataset_config_info(_UpperCAmelCase , config_name=_UpperCAmelCase ) @pytest.mark.parametrize( 'path, expected' , [ ('squad', 'plain_text'), ('acronym_identification', 'default'), ('lhoestq/squad', 'plain_text'), ('lhoestq/test', 'default'), ('lhoestq/demo1', 'lhoestq--demo1'), ('dalle-mini/wit', 'dalle-mini--wit'), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : List[str] , _UpperCAmelCase : Union[str, Any] ): lowerCAmelCase = get_dataset_config_names(_UpperCAmelCase ) assert expected in config_names @pytest.mark.parametrize( 'path, expected_configs, expected_splits_in_first_config' , [ ('squad', ['plain_text'], ['train', 'validation']), ('dalle-mini/wit', ['dalle-mini--wit'], ['train']), ('paws', ['labeled_final', 'labeled_swap', 'unlabeled_final'], ['train', 'test', 'validation']), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : List[Any] , _UpperCAmelCase : Any , _UpperCAmelCase : List[str] ): lowerCAmelCase = get_dataset_infos(_UpperCAmelCase ) assert list(infos.keys() ) == expected_configs lowerCAmelCase = expected_configs[0] assert expected_config in infos lowerCAmelCase = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( 'path, expected_config, expected_splits' , [ ('squad', 'plain_text', ['train', 'validation']), ('dalle-mini/wit', 'dalle-mini--wit', ['train']), ('paws', 'labeled_final', ['train', 'test', 'validation']), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Any , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : str ): lowerCAmelCase = get_dataset_infos(_UpperCAmelCase ) assert expected_config in infos lowerCAmelCase = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( 'path, config_name, expected_exception' , [ ('paws', None, ValueError), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Tuple , _UpperCAmelCase : Tuple , _UpperCAmelCase : Union[str, Any] ): with pytest.raises(_UpperCAmelCase ): get_dataset_split_names(_UpperCAmelCase , config_name=_UpperCAmelCase )
4
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 lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['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 lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
0
'''simple docstring''' _lowercase = """ # Transformers 설치 방법 ! pip install transformers datasets # 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요. # ! pip install git+https://github.com/huggingface/transformers.git """ _lowercase = [{"""type""": """code""", """content""": INSTALL_CONTENT}] _lowercase = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
5
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Optional[Any] ): SCREAMING_SNAKE_CASE__ = DPTConfig() if "large" in checkpoint_url: SCREAMING_SNAKE_CASE__ = 1_024 SCREAMING_SNAKE_CASE__ = 4_096 SCREAMING_SNAKE_CASE__ = 24 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = [5, 11, 17, 23] SCREAMING_SNAKE_CASE__ = [256, 512, 1_024, 1_024] SCREAMING_SNAKE_CASE__ = (1, 384, 384) if "ade" in checkpoint_url: SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = 150 SCREAMING_SNAKE_CASE__ = """huggingface/label-files""" SCREAMING_SNAKE_CASE__ = """ade20k-id2label.json""" SCREAMING_SNAKE_CASE__ = json.load(open(cached_download(hf_hub_url(UpperCamelCase__ , UpperCamelCase__ , repo_type="""dataset""" ) ) , """r""" ) ) SCREAMING_SNAKE_CASE__ = {int(UpperCamelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE__ = idalabel SCREAMING_SNAKE_CASE__ = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE__ = [1, 150, 480, 480] return config, expected_shape def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: List[str] ): SCREAMING_SNAKE_CASE__ = ["""pretrained.model.head.weight""", """pretrained.model.head.bias"""] for k in ignore_keys: state_dict.pop(UpperCamelCase__ , UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: str ): if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.model""" , """dpt.encoder""" ) if "pretrained.model" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.model""" , """dpt.embeddings""" ) if "patch_embed" in name: SCREAMING_SNAKE_CASE__ = name.replace("""patch_embed""" , """patch_embeddings""" ) if "pos_embed" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pos_embed""" , """position_embeddings""" ) if "attn.proj" in name: SCREAMING_SNAKE_CASE__ = name.replace("""attn.proj""" , """attention.output.dense""" ) if "proj" in name and "project" not in name: SCREAMING_SNAKE_CASE__ = name.replace("""proj""" , """projection""" ) if "blocks" in name: SCREAMING_SNAKE_CASE__ = name.replace("""blocks""" , """layer""" ) if "mlp.fc1" in name: SCREAMING_SNAKE_CASE__ = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: SCREAMING_SNAKE_CASE__ = name.replace("""mlp.fc2""" , """output.dense""" ) if "norm1" in name: SCREAMING_SNAKE_CASE__ = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: SCREAMING_SNAKE_CASE__ = name.replace("""norm2""" , """layernorm_after""" ) if "scratch.output_conv" in name: SCREAMING_SNAKE_CASE__ = name.replace("""scratch.output_conv""" , """head""" ) if "scratch" in name: SCREAMING_SNAKE_CASE__ = name.replace("""scratch""" , """neck""" ) if "layer1_rn" in name: SCREAMING_SNAKE_CASE__ = name.replace("""layer1_rn""" , """convs.0""" ) if "layer2_rn" in name: SCREAMING_SNAKE_CASE__ = name.replace("""layer2_rn""" , """convs.1""" ) if "layer3_rn" in name: SCREAMING_SNAKE_CASE__ = name.replace("""layer3_rn""" , """convs.2""" ) if "layer4_rn" in name: SCREAMING_SNAKE_CASE__ = name.replace("""layer4_rn""" , """convs.3""" ) if "refinenet" in name: SCREAMING_SNAKE_CASE__ = int(name[len("""neck.refinenet""" ) : len("""neck.refinenet""" ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 SCREAMING_SNAKE_CASE__ = name.replace(f'''refinenet{layer_idx}''' , f'''fusion_stage.layers.{abs(layer_idx-4 )}''' ) if "out_conv" in name: SCREAMING_SNAKE_CASE__ = name.replace("""out_conv""" , """projection""" ) if "resConfUnit1" in name: SCREAMING_SNAKE_CASE__ = name.replace("""resConfUnit1""" , """residual_layer1""" ) if "resConfUnit2" in name: SCREAMING_SNAKE_CASE__ = name.replace("""resConfUnit2""" , """residual_layer2""" ) if "conv1" in name: SCREAMING_SNAKE_CASE__ = name.replace("""conv1""" , """convolution1""" ) if "conv2" in name: SCREAMING_SNAKE_CASE__ = name.replace("""conv2""" , """convolution2""" ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess1.0.project.0""" , """neck.reassemble_stage.readout_projects.0.0""" ) if "pretrained.act_postprocess2.0.project.0" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess2.0.project.0""" , """neck.reassemble_stage.readout_projects.1.0""" ) if "pretrained.act_postprocess3.0.project.0" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess3.0.project.0""" , """neck.reassemble_stage.readout_projects.2.0""" ) if "pretrained.act_postprocess4.0.project.0" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess4.0.project.0""" , """neck.reassemble_stage.readout_projects.3.0""" ) # resize blocks if "pretrained.act_postprocess1.3" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess1.3""" , """neck.reassemble_stage.layers.0.projection""" ) if "pretrained.act_postprocess1.4" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess1.4""" , """neck.reassemble_stage.layers.0.resize""" ) if "pretrained.act_postprocess2.3" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess2.3""" , """neck.reassemble_stage.layers.1.projection""" ) if "pretrained.act_postprocess2.4" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess2.4""" , """neck.reassemble_stage.layers.1.resize""" ) if "pretrained.act_postprocess3.3" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess3.3""" , """neck.reassemble_stage.layers.2.projection""" ) if "pretrained.act_postprocess4.3" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess4.3""" , """neck.reassemble_stage.layers.3.projection""" ) if "pretrained.act_postprocess4.4" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained.act_postprocess4.4""" , """neck.reassemble_stage.layers.3.resize""" ) if "pretrained" in name: SCREAMING_SNAKE_CASE__ = name.replace("""pretrained""" , """dpt""" ) if "bn" in name: SCREAMING_SNAKE_CASE__ = name.replace("""bn""" , """batch_norm""" ) if "head" in name: SCREAMING_SNAKE_CASE__ = name.replace("""head""" , """head.head""" ) if "encoder.norm" in name: SCREAMING_SNAKE_CASE__ = name.replace("""encoder.norm""" , """layernorm""" ) if "auxlayer" in name: SCREAMING_SNAKE_CASE__ = name.replace("""auxlayer""" , """auxiliary_head.head""" ) return name def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Dict , UpperCamelCase__: List[str] ): for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE__ = state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.weight''' ) SCREAMING_SNAKE_CASE__ = state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE__ = in_proj_weight[: config.hidden_size, :] SCREAMING_SNAKE_CASE__ = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE__ = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE__ = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE__ ( ): SCREAMING_SNAKE_CASE__ = """http://images.cocodataset.org/val2017/000000039769.jpg""" SCREAMING_SNAKE_CASE__ = Image.open(requests.get(UpperCamelCase__ , stream=UpperCamelCase__ ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Dict , UpperCamelCase__: Any , UpperCamelCase__: str , UpperCamelCase__: Dict ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = get_dpt_config(UpperCamelCase__ ) # load original state_dict from URL SCREAMING_SNAKE_CASE__ = torch.hub.load_state_dict_from_url(UpperCamelCase__ , map_location="""cpu""" ) # remove certain keys remove_ignore_keys_(UpperCamelCase__ ) # rename keys for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE__ = state_dict.pop(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = val # read in qkv matrices read_in_q_k_v(UpperCamelCase__ , UpperCamelCase__ ) # load HuggingFace model SCREAMING_SNAKE_CASE__ = DPTForSemanticSegmentation(UpperCamelCase__ ) if """ade""" in checkpoint_url else DPTForDepthEstimation(UpperCamelCase__ ) model.load_state_dict(UpperCamelCase__ ) model.eval() # Check outputs on an image SCREAMING_SNAKE_CASE__ = 480 if """ade""" in checkpoint_url else 384 SCREAMING_SNAKE_CASE__ = DPTImageProcessor(size=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = prepare_img() SCREAMING_SNAKE_CASE__ = image_processor(UpperCamelCase__ , return_tensors="""pt""" ) # forward pass SCREAMING_SNAKE_CASE__ = model(**UpperCamelCase__ ).logits if """ade""" in checkpoint_url else model(**UpperCamelCase__ ).predicted_depth # Assert logits SCREAMING_SNAKE_CASE__ = torch.tensor([[6.3_1_9_9, 6.3_6_2_9, 6.4_1_4_8], [6.3_8_5_0, 6.3_6_1_5, 6.4_1_6_6], [6.3_5_1_9, 6.3_1_7_6, 6.3_5_7_5]] ) if "ade" in checkpoint_url: SCREAMING_SNAKE_CASE__ = torch.tensor([[4.0_4_8_0, 4.2_4_2_0, 4.4_3_6_0], [4.3_1_2_4, 4.5_6_9_3, 4.8_2_6_1], [4.5_7_6_8, 4.8_9_6_5, 5.2_1_6_3]] ) assert outputs.shape == torch.Size(UpperCamelCase__ ) assert ( torch.allclose(outputs[0, 0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) if "ade" in checkpoint_url else torch.allclose(outputs[0, :3, :3] , UpperCamelCase__ ) ) Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCamelCase__ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(UpperCamelCase__ ) if push_to_hub: print("""Pushing model to hub...""" ) model.push_to_hub( repo_path_or_name=Path(UpperCamelCase__ , UpperCamelCase__ ) , organization="""nielsr""" , commit_message="""Add model""" , use_temp_dir=UpperCamelCase__ , ) image_processor.push_to_hub( repo_path_or_name=Path(UpperCamelCase__ , UpperCamelCase__ ) , organization="""nielsr""" , commit_message="""Add image processor""" , use_temp_dir=UpperCamelCase__ , ) if __name__ == "__main__": _lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt', type=str, help='URL of the original DPT checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', action='store_true', ) parser.add_argument( '--model_name', default='dpt-large', type=str, help='Name of the model, in case you\'re pushing to the hub.', ) _lowerCamelCase = parser.parse_args() convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
6
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
0
"""simple docstring""" class lowercase_ : '''simple docstring''' def __init__( self : List[Any] , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : int , _UpperCAmelCase : int ): _A = None _A = None _A = graph self._normalize_graph(_UpperCAmelCase , _UpperCAmelCase ) _A = len(_UpperCAmelCase ) _A = None def lowerCAmelCase_ ( self : Optional[Any] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : Dict ): if sources is int: _A = [sources] if sinks is int: _A = [sinks] if len(_UpperCAmelCase ) == 0 or len(_UpperCAmelCase ) == 0: return _A = sources[0] _A = sinks[0] # make fake vertex if there are more # than one source or sink if len(_UpperCAmelCase ) > 1 or len(_UpperCAmelCase ) > 1: _A = 0 for i in sources: max_input_flow += sum(self.graph[i] ) _A = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: _A = max_input_flow _A = 0 _A = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: _A = max_input_flow _A = size - 1 def lowerCAmelCase_ ( self : Optional[Any] ): if self.maximum_flow_algorithm is None: raise Exception('You need to set maximum flow algorithm before.' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def lowerCAmelCase_ ( self : List[str] , _UpperCAmelCase : Union[str, Any] ): _A = algorithm(self ) class lowercase_ : '''simple docstring''' def __init__( self : List[Any] , _UpperCAmelCase : Union[str, Any] ): _A = flow_network _A = flow_network.verticesCount _A = flow_network.sourceIndex _A = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that _A = flow_network.graph _A = False def lowerCAmelCase_ ( self : Optional[Any] ): if not self.executed: self._algorithm() _A = True def lowerCAmelCase_ ( self : int ): pass class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' def __init__( self : int , _UpperCAmelCase : Any ): super().__init__(_UpperCAmelCase ) # use this to save your result _A = -1 def lowerCAmelCase_ ( self : Optional[Any] ): if not self.executed: raise Exception('You should execute algorithm before using its result!' ) return self.maximum_flow class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' def __init__( self : Dict , _UpperCAmelCase : List[Any] ): super().__init__(_UpperCAmelCase ) _A = [[0] * self.verticies_count for i in range(self.verticies_count )] _A = [0] * self.verticies_count _A = [0] * self.verticies_count def lowerCAmelCase_ ( self : Dict ): _A = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule _A = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list _A = 0 while i < len(_UpperCAmelCase ): _A = vertices_list[i] _A = self.heights[vertex_index] self.process_vertex(_UpperCAmelCase ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(_UpperCAmelCase ) ) _A = 0 else: i += 1 _A = sum(self.preflow[self.source_index] ) def lowerCAmelCase_ ( self : int , _UpperCAmelCase : Any ): while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(_UpperCAmelCase , _UpperCAmelCase ) self.relabel(_UpperCAmelCase ) def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : Tuple , _UpperCAmelCase : Tuple ): _A = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def lowerCAmelCase_ ( self : Union[str, Any] , _UpperCAmelCase : int ): _A = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): _A = self.heights[to_index] if min_height is not None: _A = min_height + 1 if __name__ == "__main__": a = [0] a = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] a = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network a = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate a = flow_network.find_maximum_flow() print(F'''maximum flow is {maximum_flow}''')
7
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase__ : Union[str, Any] = { '''configuration_blip_2''': [ '''BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Blip2Config''', '''Blip2QFormerConfig''', '''Blip2VisionConfig''', ], '''processing_blip_2''': ['''Blip2Processor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Dict = [ '''BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Blip2Model''', '''Blip2QFormerModel''', '''Blip2PreTrainedModel''', '''Blip2ForConditionalGeneration''', '''Blip2VisionModel''', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys lowercase__ : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
8
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
0
import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ).convert('RGB' ) A__ = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.4814_5466, 0.457_8275, 0.4082_1073) , (0.2686_2954, 0.2613_0258, 0.2757_7711) ), ] ) A__ = transform(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) return image def A ( __UpperCamelCase ) -> Optional[int]: if "visual_encoder" in key: A__ = re.sub('visual_encoder*' , 'vision_model.encoder' , __UpperCamelCase ) if "blocks" in key: A__ = re.sub(r'blocks' , 'layers' , __UpperCamelCase ) if "attn" in key: A__ = re.sub(r'attn' , 'self_attn' , __UpperCamelCase ) if "norm1" in key: A__ = re.sub(r'norm1' , 'layer_norm1' , __UpperCamelCase ) if "norm2" in key: A__ = re.sub(r'norm2' , 'layer_norm2' , __UpperCamelCase ) if "encoder.norm" in key: A__ = re.sub(r'encoder.norm' , 'post_layernorm' , __UpperCamelCase ) if "encoder.patch_embed.proj" in key: A__ = re.sub(r'encoder.patch_embed.proj' , 'embeddings.patch_embedding' , __UpperCamelCase ) if "encoder.pos_embed" in key: A__ = re.sub(r'encoder.pos_embed' , 'embeddings.position_embedding' , __UpperCamelCase ) if "encoder.cls_token" in key: A__ = re.sub(r'encoder.cls_token' , 'embeddings.class_embedding' , __UpperCamelCase ) if "self_attn" in key: A__ = re.sub(r'self_attn.proj' , 'self_attn.projection' , __UpperCamelCase ) return key @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase=None ) -> List[Any]: if config_path is not None: A__ = BlipConfig.from_pretrained(__UpperCamelCase ) else: A__ = BlipConfig(projection_dim=512 , text_config={} , vision_config={} ) A__ = BlipForConditionalGeneration(__UpperCamelCase ).eval() A__ = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth' A__ = blip_decoder(pretrained=__UpperCamelCase , image_size=384 , vit='base' ) A__ = pt_model.eval() A__ = pt_model.state_dict() for key in modified_state_dict.copy(): A__ = modified_state_dict.pop(__UpperCamelCase ) A__ = rename_key(__UpperCamelCase ) A__ = value hf_model.load_state_dict(__UpperCamelCase ) A__ = 384 A__ = load_demo_image(image_size=__UpperCamelCase , device='cpu' ) A__ = BertTokenizer.from_pretrained('bert-base-uncased' ) A__ = tokenizer(['a picture of'] ).input_ids A__ = hf_model.generate(__UpperCamelCase , __UpperCamelCase ) assert out[0].tolist() == [30_522, 1_037, 3_861, 1_997, 1_037, 2_450, 3_564, 2_006, 1_996, 3_509, 2_007, 2_014, 3_899, 102] A__ = hf_model.generate(__UpperCamelCase ) assert out[0].tolist() == [30_522, 1_037, 2_450, 3_564, 2_006, 1_996, 3_509, 2_007, 2_014, 3_899, 102] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(__UpperCamelCase ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' A__ = ( 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth' ) A__ = blip_vqa(pretrained=__UpperCamelCase , image_size=__UpperCamelCase , vit='base' ) vqa_model.eval() A__ = vqa_model.state_dict() for key in modified_state_dict.copy(): A__ = modified_state_dict.pop(__UpperCamelCase ) A__ = rename_key(__UpperCamelCase ) A__ = value A__ = BlipForQuestionAnswering(__UpperCamelCase ) hf_vqa_model.load_state_dict(__UpperCamelCase ) A__ = ['How many dogs are in this image?'] A__ = tokenizer(__UpperCamelCase , return_tensors='pt' ).input_ids A__ = hf_vqa_model.generate(__UpperCamelCase , __UpperCamelCase ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '_vqa' ) A__ = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth' A__ = blip_itm(pretrained=__UpperCamelCase , image_size=__UpperCamelCase , vit='base' ) itm_model.eval() A__ = itm_model.state_dict() for key in modified_state_dict.copy(): A__ = modified_state_dict.pop(__UpperCamelCase ) A__ = rename_key(__UpperCamelCase ) A__ = value A__ = BlipForImageTextRetrieval(__UpperCamelCase ) A__ = ['A picture of a woman with a dog sitting in a beach'] A__ = tokenizer( __UpperCamelCase , return_tensors='pt' , padding='max_length' , truncation=__UpperCamelCase , max_length=35 , ).input_ids hf_itm_model.load_state_dict(__UpperCamelCase ) hf_itm_model.eval() A__ = hf_itm_model(__UpperCamelCase , __UpperCamelCase , use_itm_head=__UpperCamelCase ) A__ = hf_itm_model(__UpperCamelCase , __UpperCamelCase , use_itm_head=__UpperCamelCase ) assert out[0].item() == 0.2110_6874_9427_7954 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.4_5698_8453_8650_5127 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + '_itm' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
9
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
0
import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { "vocab_file": "vocab.json", "merges_file": "merges.txt", } _lowerCAmelCase = { "vocab_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json"}, "merges_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt"}, } _lowerCAmelCase = { "ctrl": 256, } _lowerCAmelCase = { "Pregnancy": 168_629, "Christianity": 7_675, "Explain": 106_423, "Fitness": 63_440, "Saving": 63_163, "Ask": 27_171, "Ass": 95_985, "Joke": 163_509, "Questions": 45_622, "Thoughts": 49_605, "Retail": 52_342, "Feminism": 164_338, "Writing": 11_992, "Atheism": 192_263, "Netflix": 48_616, "Computing": 39_639, "Opinion": 43_213, "Alone": 44_967, "Funny": 58_917, "Gaming": 40_358, "Human": 4_088, "India": 1_331, "Joker": 77_138, "Diet": 36_206, "Legal": 11_859, "Norman": 4_939, "Tip": 72_689, "Weight": 52_343, "Movies": 46_273, "Running": 23_425, "Science": 2_090, "Horror": 37_793, "Confession": 60_572, "Finance": 12_250, "Politics": 16_360, "Scary": 191_985, "Support": 12_654, "Technologies": 32_516, "Teenage": 66_160, "Event": 32_769, "Learned": 67_460, "Notion": 182_770, "Wikipedia": 37_583, "Books": 6_665, "Extract": 76_050, "Confessions": 102_701, "Conspiracy": 75_932, "Links": 63_674, "Narcissus": 150_425, "Relationship": 54_766, "Relationships": 134_796, "Reviews": 41_671, "News": 4_256, "Translation": 26_820, "multilingual": 128_406, } def _snake_case ( __snake_case ): _UpperCamelCase = set() _UpperCamelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _UpperCamelCase = char _UpperCamelCase = set(__snake_case ) return pairs class lowerCAmelCase_ ( __lowercase ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = CONTROL_CODES def __init__( self : Optional[int] , _A : Dict , _A : str , _A : Union[str, Any]="<unk>" , **_A : List[str] ): super().__init__(unk_token=_A , **_A ) with open(_A , encoding='''utf-8''' ) as vocab_handle: _UpperCamelCase = json.load(_A ) _UpperCamelCase = {v: k for k, v in self.encoder.items()} with open(_A , encoding='''utf-8''' ) as merges_handle: _UpperCamelCase = merges_handle.read().split('''\n''' )[1:-1] _UpperCamelCase = [tuple(merge.split() ) for merge in merges] _UpperCamelCase = dict(zip(_A , range(len(_A ) ) ) ) _UpperCamelCase = {} @property def UpperCamelCase_ ( self : Dict ): return len(self.encoder ) def UpperCamelCase_ ( self : Optional[Any] ): return dict(self.encoder , **self.added_tokens_encoder ) def UpperCamelCase_ ( self : List[str] , _A : Optional[int] ): if token in self.cache: return self.cache[token] _UpperCamelCase = tuple(_A ) _UpperCamelCase = tuple(list(word[:-1] ) + [word[-1] + '''</w>'''] ) _UpperCamelCase = get_pairs(_A ) if not pairs: return token while True: _UpperCamelCase = min(_A , key=lambda _A : self.bpe_ranks.get(_A , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break _UpperCamelCase , _UpperCamelCase = bigram _UpperCamelCase = [] _UpperCamelCase = 0 while i < len(_A ): try: _UpperCamelCase = word.index(_A , _A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _UpperCamelCase = j if word[i] == first and i < len(_A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _UpperCamelCase = tuple(_A ) _UpperCamelCase = new_word if len(_A ) == 1: break else: _UpperCamelCase = get_pairs(_A ) _UpperCamelCase = '''@@ '''.join(_A ) _UpperCamelCase = word[:-4] _UpperCamelCase = word return word def UpperCamelCase_ ( self : Tuple , _A : int ): _UpperCamelCase = [] _UpperCamelCase = re.findall(R'''\S+\n?''' , _A ) for token in words: split_tokens.extend(list(self.bpe(_A ).split(''' ''' ) ) ) return split_tokens def UpperCamelCase_ ( self : Optional[Any] , _A : List[str] ): return self.encoder.get(_A , self.encoder.get(self.unk_token ) ) def UpperCamelCase_ ( self : int , _A : int ): return self.decoder.get(_A , self.unk_token ) def UpperCamelCase_ ( self : str , _A : Optional[int] ): _UpperCamelCase = ''' '''.join(_A ).replace('''@@ ''' , '''''' ).strip() return out_string def UpperCamelCase_ ( self : Union[str, Any] , _A : str , _A : Optional[str] = None ): if not os.path.isdir(_A ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCamelCase = os.path.join( _A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) _UpperCamelCase = os.path.join( _A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(_A , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_A , ensure_ascii=_A ) + '''\n''' ) _UpperCamelCase = 0 with open(_A , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _A : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) _UpperCamelCase = token_index writer.write(''' '''.join(_A ) + '''\n''' ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
10
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
0
'''simple docstring''' from argparse import ArgumentParser, Namespace from typing import Any, List, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand try: from fastapi import Body, FastAPI, HTTPException from fastapi.routing import APIRoute from pydantic import BaseModel from starlette.responses import JSONResponse from uvicorn import run lowercase_ = True except (ImportError, AttributeError): lowercase_ = object def lowerCAmelCase (*__A , **__A): """simple docstring""" pass lowercase_ = False lowercase_ = logging.get_logger("transformers-cli/serving") def lowerCAmelCase (__A): """simple docstring""" _a = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(__A , args.host , args.port , args.workers) class __A ( A ): '''simple docstring''' __lowerCamelCase : dict class __A ( A ): '''simple docstring''' __lowerCamelCase : List[str] __lowerCamelCase : Optional[List[int]] class __A ( A ): '''simple docstring''' __lowerCamelCase : str class __A ( A ): '''simple docstring''' __lowerCamelCase : Any class __A ( A ): '''simple docstring''' @staticmethod def a__ (A ) -> Any: """simple docstring""" _a = parser.add_parser( '''serve''' , help='''CLI tool to run inference requests through REST and GraphQL endpoints.''' ) serve_parser.add_argument( '''--task''' , type=A , choices=get_supported_tasks() , help='''The task to run the pipeline on''' , ) serve_parser.add_argument('''--host''' , type=A , default='''localhost''' , help='''Interface the server will listen on.''' ) serve_parser.add_argument('''--port''' , type=A , default=8_888 , help='''Port the serving will listen to.''' ) serve_parser.add_argument('''--workers''' , type=A , default=1 , help='''Number of http workers''' ) serve_parser.add_argument('''--model''' , type=A , help='''Model\'s name or path to stored model.''' ) serve_parser.add_argument('''--config''' , type=A , help='''Model\'s config name or path to stored model.''' ) serve_parser.add_argument('''--tokenizer''' , type=A , help='''Tokenizer name to use.''' ) serve_parser.add_argument( '''--device''' , type=A , default=-1 , help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)''' , ) serve_parser.set_defaults(func=A ) def __init__(self , A , A , A , A ) -> List[str]: """simple docstring""" _a = pipeline _a = host _a = port _a = workers if not _serve_dependencies_installed: raise RuntimeError( '''Using serve command requires FastAPI and uvicorn. ''' '''Please install transformers with [serving]: pip install "transformers[serving]".''' '''Or install FastAPI and uvicorn separately.''' ) else: logger.info(f'''Serving model over {host}:{port}''' ) _a = FastAPI( routes=[ APIRoute( '''/''' , self.model_info , response_model=A , response_class=A , methods=['''GET'''] , ), APIRoute( '''/tokenize''' , self.tokenize , response_model=A , response_class=A , methods=['''POST'''] , ), APIRoute( '''/detokenize''' , self.detokenize , response_model=A , response_class=A , methods=['''POST'''] , ), APIRoute( '''/forward''' , self.forward , response_model=A , response_class=A , methods=['''POST'''] , ), ] , timeout=600 , ) def a__ (self ) -> List[str]: """simple docstring""" run(self._app , host=self.host , port=self.port , workers=self.workers ) def a__ (self ) -> List[Any]: """simple docstring""" return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def a__ (self , A = Body(A , embed=A ) , A = Body(A , embed=A ) ) -> str: """simple docstring""" try: _a = self._pipeline.tokenizer.tokenize(A ) if return_ids: _a = self._pipeline.tokenizer.convert_tokens_to_ids(A ) return ServeTokenizeResult(tokens=A , tokens_ids=A ) else: return ServeTokenizeResult(tokens=A ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(A )} ) def a__ (self , A = Body(A , embed=A ) , A = Body(A , embed=A ) , A = Body(A , embed=A ) , ) -> List[str]: """simple docstring""" try: _a = self._pipeline.tokenizer.decode(A , A , A ) return ServeDeTokenizeResult(model='''''' , text=A ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(A )} ) async def a__ (self , A=Body(A , embed=A ) ) -> List[str]: """simple docstring""" if len(A ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model _a = self._pipeline(A ) return ServeForwardResult(output=A ) except Exception as e: raise HTTPException(500 , {'''error''': str(A )} )
11
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
0
def UpperCamelCase ( lowercase_ ) -> List[Any]: '''simple docstring''' lowercase__ : Any = len(lowercase_ ) for i in range(length - 1 ): lowercase__ : List[str] = i for k in range(i + 1 , lowercase_ ): if collection[k] < collection[least]: lowercase__ : Any = k if least != i: lowercase__ , lowercase__ : Dict = (collection[i], collection[least]) return collection if __name__ == "__main__": lowerCamelCase__ : int = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(""",""")] print(selection_sort(unsorted))
12
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
0
'''simple docstring''' from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class UpperCAmelCase_ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" lowerCamelCase : Any = [r'h\.\d+\.attn\.bias', r'h\.\d+\.attn\.masked_bias'] @register_to_config def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 5_02_57 , SCREAMING_SNAKE_CASE_ = 10_24 , SCREAMING_SNAKE_CASE_ = 7_68 , SCREAMING_SNAKE_CASE_ = 12 , SCREAMING_SNAKE_CASE_ = 12 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "gelu_new" , SCREAMING_SNAKE_CASE_ = 0.1 , SCREAMING_SNAKE_CASE_ = 0.1 , SCREAMING_SNAKE_CASE_ = 0.1 , SCREAMING_SNAKE_CASE_ = 1E-5 , SCREAMING_SNAKE_CASE_ = 0.0_2 , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = False , SCREAMING_SNAKE_CASE_ = False , ) -> Tuple: super().__init__() __lowerCamelCase : int = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f'`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and' f' `n_embd`: {n_embd} are not equal.' ) __lowerCamelCase : List[Any] = prefix_inner_dim __lowerCamelCase : Any = prefix_hidden_dim __lowerCamelCase : Dict = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) __lowerCamelCase : Dict = ( nn.Linear(self.prefix_hidden_dim , SCREAMING_SNAKE_CASE_ ) if self.prefix_hidden_dim is not None else nn.Identity() ) __lowerCamelCase : str = GPTaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , n_positions=SCREAMING_SNAKE_CASE_ , n_embd=SCREAMING_SNAKE_CASE_ , n_layer=SCREAMING_SNAKE_CASE_ , n_head=SCREAMING_SNAKE_CASE_ , n_inner=SCREAMING_SNAKE_CASE_ , activation_function=SCREAMING_SNAKE_CASE_ , resid_pdrop=SCREAMING_SNAKE_CASE_ , embd_pdrop=SCREAMING_SNAKE_CASE_ , attn_pdrop=SCREAMING_SNAKE_CASE_ , layer_norm_epsilon=SCREAMING_SNAKE_CASE_ , initializer_range=SCREAMING_SNAKE_CASE_ , scale_attn_weights=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ , scale_attn_by_inverse_layer_idx=SCREAMING_SNAKE_CASE_ , reorder_and_upcast_attn=SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Dict = GPTaLMHeadModel(SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , ) -> List[str]: __lowerCamelCase : Any = self.transformer.transformer.wte(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Tuple = self.encode_prefix(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : List[str] = self.decode_prefix(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Any = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: __lowerCamelCase : Union[str, Any] = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) __lowerCamelCase : Optional[Any] = torch.cat((dummy_token, input_ids) , dim=1 ) __lowerCamelCase : Union[str, Any] = self.transformer(inputs_embeds=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> torch.Tensor: return torch.zeros(SCREAMING_SNAKE_CASE_ , self.prefix_length , dtype=torch.intaa , device=SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ ) -> List[Any]: return self.encode_prefix(SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: __lowerCamelCase : int = torch.split(SCREAMING_SNAKE_CASE_ , 1 , dim=0 ) __lowerCamelCase : List[str] = [] __lowerCamelCase : Tuple = [] for feature in features: __lowerCamelCase : List[str] = self.decode_prefix(feature.to(SCREAMING_SNAKE_CASE_ ) ) # back to the clip feature # Only support beam search for now __lowerCamelCase , __lowerCamelCase : int = self.generate_beam( input_embeds=SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) __lowerCamelCase : Optional[Any] = torch.stack(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Optional[int] = torch.stack(SCREAMING_SNAKE_CASE_ ) return generated_tokens, generated_seq_lengths @torch.no_grad() def lowercase_ ( self , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_ = 5 , SCREAMING_SNAKE_CASE_ = 67 , SCREAMING_SNAKE_CASE_ = 1.0 , SCREAMING_SNAKE_CASE_ = None , ) -> Union[str, Any]: __lowerCamelCase : Dict = eos_token_id __lowerCamelCase : Dict = None __lowerCamelCase : List[str] = None __lowerCamelCase : Tuple = torch.ones(SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ , dtype=torch.int ) __lowerCamelCase : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ , dtype=torch.bool ) if input_embeds is not None: __lowerCamelCase : Any = input_embeds else: __lowerCamelCase : Dict = self.transformer.transformer.wte(SCREAMING_SNAKE_CASE_ ) for i in range(SCREAMING_SNAKE_CASE_ ): __lowerCamelCase : Tuple = self.transformer(inputs_embeds=SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : int = outputs.logits __lowerCamelCase : Tuple = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) __lowerCamelCase : Optional[int] = logits.softmax(-1 ).log() if scores is None: __lowerCamelCase , __lowerCamelCase : Dict = logits.topk(SCREAMING_SNAKE_CASE_ , -1 ) __lowerCamelCase : Union[str, Any] = generated.expand(SCREAMING_SNAKE_CASE_ , *generated.shape[1:] ) __lowerCamelCase , __lowerCamelCase : Union[str, Any] = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: __lowerCamelCase : Optional[int] = next_tokens else: __lowerCamelCase : int = tokens.expand(SCREAMING_SNAKE_CASE_ , *tokens.shape[1:] ) __lowerCamelCase : Any = torch.cat((tokens, next_tokens) , dim=1 ) else: __lowerCamelCase : Optional[int] = -float(np.inf ) __lowerCamelCase : Union[str, Any] = 0 __lowerCamelCase : str = scores[:, None] + logits seq_lengths[~is_stopped] += 1 __lowerCamelCase : int = scores_sum / seq_lengths[:, None] __lowerCamelCase , __lowerCamelCase : Optional[int] = scores_sum_average.view(-1 ).topk(SCREAMING_SNAKE_CASE_ , -1 ) __lowerCamelCase : int = next_tokens // scores_sum.shape[1] __lowerCamelCase : List[str] = seq_lengths[next_tokens_source] __lowerCamelCase : Optional[int] = next_tokens % scores_sum.shape[1] __lowerCamelCase : Tuple = next_tokens.unsqueeze(1 ) __lowerCamelCase : List[str] = tokens[next_tokens_source] __lowerCamelCase : Tuple = torch.cat((tokens, next_tokens) , dim=1 ) __lowerCamelCase : int = generated[next_tokens_source] __lowerCamelCase : Optional[Any] = scores_sum_average * seq_lengths __lowerCamelCase : str = is_stopped[next_tokens_source] __lowerCamelCase : List[Any] = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) __lowerCamelCase : Any = torch.cat((generated, next_token_embed) , dim=1 ) __lowerCamelCase : Dict = is_stopped + next_tokens.eq(SCREAMING_SNAKE_CASE_ ).squeeze() if is_stopped.all(): break __lowerCamelCase : Tuple = scores / seq_lengths __lowerCamelCase : Tuple = scores.argsort(descending=SCREAMING_SNAKE_CASE_ ) # tokens tensors are already padded to max_seq_length __lowerCamelCase : Union[str, Any] = [tokens[i] for i in order] __lowerCamelCase : List[Any] = torch.stack(SCREAMING_SNAKE_CASE_ , dim=0 ) __lowerCamelCase : int = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
13
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
0
def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" _a : Optional[int] = [int(__a ) for i in ip_va_address.split('''.''' ) if i.isdigit()] return len(__a ) == 4 and all(0 <= int(__a ) <= 254 for octet in octets ) if __name__ == "__main__": a__ = input().strip() a__ = '''valid''' if is_ip_va_address_valid(ip) else '''invalid''' print(f'''{ip} is a {valid_or_invalid} IP v4 address.''')
14
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
0
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A ( UpperCAmelCase__ ): '''simple docstring''' A__ = 42 A__ = 42 def __init__(self : Optional[Any] , _UpperCAmelCase : UNetaDModel , _UpperCAmelCase : ScoreSdeVeScheduler ) -> Union[str, Any]: """simple docstring""" super().__init__() self.register_modules(unet=_UpperCAmelCase , scheduler=_UpperCAmelCase ) @torch.no_grad() def __call__(self : List[Any] , _UpperCAmelCase : int = 1 , _UpperCAmelCase : int = 2000 , _UpperCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _UpperCAmelCase : Optional[str] = "pil" , _UpperCAmelCase : bool = True , **_UpperCAmelCase : Any , ) -> Union[ImagePipelineOutput, Tuple]: """simple docstring""" lowercase__ = self.unet.config.sample_size lowercase__ = (batch_size, 3, img_size, img_size) lowercase__ = self.unet lowercase__ = randn_tensor(_UpperCAmelCase , generator=_UpperCAmelCase ) * self.scheduler.init_noise_sigma lowercase__ = sample.to(self.device ) self.scheduler.set_timesteps(_UpperCAmelCase ) self.scheduler.set_sigmas(_UpperCAmelCase ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): lowercase__ = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): lowercase__ = self.unet(_UpperCAmelCase , _UpperCAmelCase ).sample lowercase__ = self.scheduler.step_correct(_UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # prediction step lowercase__ = model(_UpperCAmelCase , _UpperCAmelCase ).sample lowercase__ = self.scheduler.step_pred(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ) lowercase__ , lowercase__ = output.prev_sample, output.prev_sample_mean lowercase__ = sample_mean.clamp(0 , 1 ) lowercase__ = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowercase__ = self.numpy_to_pil(_UpperCAmelCase ) if not return_dict: return (sample,) return ImagePipelineOutput(images=_UpperCAmelCase )
15
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
0
from datetime import datetime import matplotlib.pyplot as plt import torch def __a ( A__ : Tuple ): for param in module.parameters(): SCREAMING_SNAKE_CASE = False def __a ( ): SCREAMING_SNAKE_CASE = "cuda" if torch.cuda.is_available() else "cpu" if torch.backends.mps.is_available() and torch.backends.mps.is_built(): SCREAMING_SNAKE_CASE = "mps" if device == "mps": print( "WARNING: MPS currently doesn't seem to work, and messes up backpropagation without any visible torch" " errors. I recommend using CUDA on a colab notebook or CPU instead if you're facing inexplicable issues" " with generations." ) return device def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE = plt.imshow(A__ ) fig.axes.get_xaxis().set_visible(A__ ) fig.axes.get_yaxis().set_visible(A__ ) plt.show() def __a ( ): SCREAMING_SNAKE_CASE = datetime.now() SCREAMING_SNAKE_CASE = current_time.strftime("%H:%M:%S" ) return timestamp
16
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
0
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __SCREAMING_SNAKE_CASE ( a__ : Optional[int] ,a__ : Any ) -> Union[str, Any]: assert isinstance(a__ ,a__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" ,[False, True] ) def __SCREAMING_SNAKE_CASE ( a__ : Dict ,a__ : Optional[Any] ,a__ : Tuple ) -> List[Any]: __A : int = tmp_path / """cache""" __A : List[str] = {"""text""": """string"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __A : Any = TextDatasetReader(a__ ,cache_dir=a__ ,keep_in_memory=a__ ).read() _check_text_dataset(a__ ,a__ ) @pytest.mark.parametrize( """features""" ,[ None, {"""text""": """string"""}, {"""text""": """int32"""}, {"""text""": """float32"""}, ] ,) def __SCREAMING_SNAKE_CASE ( a__ : Union[str, Any] ,a__ : List[str] ,a__ : List[str] ) -> List[str]: __A : Optional[int] = tmp_path / """cache""" __A : List[Any] = {"""text""": """string"""} __A : List[str] = features.copy() if features else default_expected_features __A : Union[str, Any] = ( Features({feature: Value(a__ ) for feature, dtype in features.items()} ) if features is not None else None ) __A : str = TextDatasetReader(a__ ,features=a__ ,cache_dir=a__ ).read() _check_text_dataset(a__ ,a__ ) @pytest.mark.parametrize("""split""" ,[None, NamedSplit("""train""" ), """train""", """test"""] ) def __SCREAMING_SNAKE_CASE ( a__ : Any ,a__ : List[str] ,a__ : str ) -> Dict: __A : Optional[int] = tmp_path / """cache""" __A : List[Any] = {"""text""": """string"""} __A : List[Any] = TextDatasetReader(a__ ,cache_dir=a__ ,split=a__ ).read() _check_text_dataset(a__ ,a__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" ,[str, list] ) def __SCREAMING_SNAKE_CASE ( a__ : Dict ,a__ : List[Any] ,a__ : Optional[int] ) -> Optional[int]: if issubclass(a__ ,a__ ): __A : Optional[Any] = text_path elif issubclass(a__ ,a__ ): __A : str = [text_path] __A : List[str] = tmp_path / """cache""" __A : Tuple = {"""text""": """string"""} __A : List[Any] = TextDatasetReader(a__ ,cache_dir=a__ ).read() _check_text_dataset(a__ ,a__ ) def __SCREAMING_SNAKE_CASE ( a__ : Union[str, Any] ,a__ : List[Any] ,a__ : Optional[Any]=("train",) ) -> Tuple: assert isinstance(a__ ,a__ ) for split in splits: __A : Union[str, Any] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" ,[False, True] ) def __SCREAMING_SNAKE_CASE ( a__ : str ,a__ : str ,a__ : str ) -> Any: __A : Dict = tmp_path / """cache""" __A : Optional[int] = {"""text""": """string"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): __A : Dict = TextDatasetReader({"""train""": text_path} ,cache_dir=a__ ,keep_in_memory=a__ ).read() _check_text_datasetdict(a__ ,a__ ) @pytest.mark.parametrize( """features""" ,[ None, {"""text""": """string"""}, {"""text""": """int32"""}, {"""text""": """float32"""}, ] ,) def __SCREAMING_SNAKE_CASE ( a__ : str ,a__ : int ,a__ : Dict ) -> Tuple: __A : Any = tmp_path / """cache""" # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" __A : Optional[Any] = {"""text""": """string"""} __A : int = features.copy() if features else default_expected_features __A : Optional[Any] = ( Features({feature: Value(a__ ) for feature, dtype in features.items()} ) if features is not None else None ) __A : Tuple = TextDatasetReader({"""train""": text_path} ,features=a__ ,cache_dir=a__ ).read() _check_text_datasetdict(a__ ,a__ ) @pytest.mark.parametrize("""split""" ,[None, NamedSplit("""train""" ), """train""", """test"""] ) def __SCREAMING_SNAKE_CASE ( a__ : str ,a__ : List[Any] ,a__ : Tuple ) -> Any: if split: __A : int = {split: text_path} else: __A : Union[str, Any] = """train""" __A : int = {"""train""": text_path, """test""": text_path} __A : Any = tmp_path / """cache""" __A : str = {"""text""": """string"""} __A : List[str] = TextDatasetReader(a__ ,cache_dir=a__ ).read() _check_text_datasetdict(a__ ,a__ ,splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
17
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_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
0
'''simple docstring''' from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) @add_end_docstrings(__magic_name__ ) class lowerCAmelCase_ ( __magic_name__ ): def __init__( self , **_lowerCAmelCase ) -> List[Any]: super().__init__(**_lowerCAmelCase ) if self.framework != "pt": raise ValueError(f'''The {self.__class__} is only available in PyTorch.''' ) # No specific FOR_XXX available yet def __call__( self , _lowerCAmelCase , **_lowerCAmelCase ) -> Optional[Any]: return super().__call__(_lowerCAmelCase , **_lowerCAmelCase ) def _snake_case ( self , **_lowerCAmelCase ) -> Any: _lowerCAmelCase = {} if "candidate_labels" in kwargs: _lowerCAmelCase = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: _lowerCAmelCase = kwargs["hypothesis_template"] return preprocess_params, {}, {} def _snake_case ( self , _lowerCAmelCase , _lowerCAmelCase=None , _lowerCAmelCase="This is a sound of {}." ) -> List[str]: if isinstance(_lowerCAmelCase , _lowerCAmelCase ): if audio.startswith("http://" ) or audio.startswith("https://" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png _lowerCAmelCase = requests.get(_lowerCAmelCase ).content else: with open(_lowerCAmelCase , "rb" ) as f: _lowerCAmelCase = f.read() if isinstance(_lowerCAmelCase , _lowerCAmelCase ): _lowerCAmelCase = ffmpeg_read(_lowerCAmelCase , self.feature_extractor.sampling_rate ) if not isinstance(_lowerCAmelCase , np.ndarray ): raise ValueError("We expect a numpy ndarray as input" ) if len(audio.shape ) != 1: raise ValueError("We expect a single channel audio input for ZeroShotAudioClassificationPipeline" ) _lowerCAmelCase = self.feature_extractor( [audio] , sampling_rate=self.feature_extractor.sampling_rate , return_tensors="pt" ) _lowerCAmelCase = candidate_labels _lowerCAmelCase = [hypothesis_template.format(_lowerCAmelCase ) for x in candidate_labels] _lowerCAmelCase = self.tokenizer(_lowerCAmelCase , return_tensors=self.framework , padding=_lowerCAmelCase ) _lowerCAmelCase = [text_inputs] return inputs def _snake_case ( self , _lowerCAmelCase ) -> List[Any]: _lowerCAmelCase = model_inputs.pop("candidate_labels" ) _lowerCAmelCase = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , _lowerCAmelCase ): _lowerCAmelCase = text_inputs[0] else: # Batching case. _lowerCAmelCase = text_inputs[0][0] _lowerCAmelCase = self.model(**_lowerCAmelCase , **_lowerCAmelCase ) _lowerCAmelCase = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_audio, } return model_outputs def _snake_case ( self , _lowerCAmelCase ) -> str: _lowerCAmelCase = model_outputs.pop("candidate_labels" ) _lowerCAmelCase = model_outputs["logits"][0] if self.framework == "pt": _lowerCAmelCase = logits.softmax(dim=0 ) _lowerCAmelCase = probs.tolist() else: raise ValueError("`tf` framework not supported." ) _lowerCAmelCase = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(_lowerCAmelCase , _lowerCAmelCase ) , key=lambda _lowerCAmelCase : -x[0] ) ] return result
18
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase_ ( ) -> Generator[int, None, None]: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 2 while True: SCREAMING_SNAKE_CASE_ = factor_map.pop(__UpperCAmelCase , __UpperCAmelCase ) if factor: SCREAMING_SNAKE_CASE_ = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ = factor else: SCREAMING_SNAKE_CASE_ = prime yield prime prime += 1 def UpperCAmelCase_ ( __UpperCAmelCase : float = 1E10 ) -> int: SCREAMING_SNAKE_CASE_ = sieve() SCREAMING_SNAKE_CASE_ = 1 while True: SCREAMING_SNAKE_CASE_ = next(__UpperCAmelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCAmelCase ) n += 2 if __name__ == "__main__": print(solution())
31
0
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _a = logging.get_logger(__name__) _a = """▁""" _a = { """vocab_file""": """vocab.json""", """spm_file""": """sentencepiece.bpe.model""", } _a = { """vocab_file""": { """facebook/s2t-small-librispeech-asr""": ( """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json""" ), }, """spm_file""": { """facebook/s2t-small-librispeech-asr""": ( """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model""" ) }, } _a = { """facebook/s2t-small-librispeech-asr""": 1024, } _a = ["""pt""", """fr""", """ru""", """nl""", """ro""", """it""", """es""", """de"""] _a = {"""mustc""": MUSTC_LANGS} class _UpperCAmelCase( lowerCamelCase ): lowercase__ = VOCAB_FILES_NAMES lowercase__ = PRETRAINED_VOCAB_FILES_MAP lowercase__ = MAX_MODEL_INPUT_SIZES lowercase__ = ['input_ids', 'attention_mask'] lowercase__ = [] def __init__( self , __a , __a , __a="<s>" , __a="</s>" , __a="<pad>" , __a="<unk>" , __a=False , __a=False , __a=None , __a=None , __a = None , **__a , ) -> None: '''simple docstring''' _UpperCamelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__a , eos_token=__a , unk_token=__a , pad_token=__a , do_upper_case=__a , do_lower_case=__a , tgt_lang=__a , lang_codes=__a , sp_model_kwargs=self.sp_model_kwargs , **__a , ) _UpperCamelCase = do_upper_case _UpperCamelCase = do_lower_case _UpperCamelCase = load_json(__a) _UpperCamelCase = {v: k for k, v in self.encoder.items()} _UpperCamelCase = spm_file _UpperCamelCase = load_spm(__a , self.sp_model_kwargs) if lang_codes is not None: _UpperCamelCase = lang_codes _UpperCamelCase = LANGUAGES[lang_codes] _UpperCamelCase = [F'''<lang:{lang}>''' for lang in self.langs] _UpperCamelCase = {lang: self.sp_model.PieceToId(F'''<lang:{lang}>''') for lang in self.langs} _UpperCamelCase = self.lang_tokens _UpperCamelCase = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang) else: _UpperCamelCase = {} @property def UpperCAmelCase ( self) -> int: '''simple docstring''' return len(self.encoder) @property def UpperCAmelCase ( self) -> str: '''simple docstring''' return self._tgt_lang @tgt_lang.setter def UpperCAmelCase ( self , __a) -> None: '''simple docstring''' _UpperCamelCase = new_tgt_lang self.set_tgt_lang_special_tokens(__a) def UpperCAmelCase ( self , __a) -> None: '''simple docstring''' _UpperCamelCase = self.lang_code_to_id[tgt_lang] _UpperCamelCase = [lang_code_id] def UpperCAmelCase ( self , __a) -> List[str]: '''simple docstring''' return self.sp_model.encode(__a , out_type=__a) def UpperCAmelCase ( self , __a) -> Any: '''simple docstring''' return self.encoder.get(__a , self.encoder[self.unk_token]) def UpperCAmelCase ( self , __a) -> str: '''simple docstring''' return self.decoder.get(__a , self.unk_token) def UpperCAmelCase ( self , __a) -> str: '''simple docstring''' _UpperCamelCase = [] _UpperCamelCase = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: _UpperCamelCase = self.sp_model.decode(__a) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " _UpperCamelCase = [] else: current_sub_tokens.append(__a) _UpperCamelCase = self.sp_model.decode(__a) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def UpperCAmelCase ( self , __a , __a=None) -> List[int]: '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def UpperCAmelCase ( self , __a , __a = None , __a = False) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__a , token_ids_a=__a , already_has_special_tokens=__a) _UpperCamelCase = [1] * len(self.prefix_tokens) _UpperCamelCase = [1] if token_ids_a is None: return prefix_ones + ([0] * len(__a)) + suffix_ones return prefix_ones + ([0] * len(__a)) + ([0] * len(__a)) + suffix_ones def UpperCAmelCase ( self) -> Dict: '''simple docstring''' _UpperCamelCase = self.encoder.copy() vocab.update(self.added_tokens_encoder) return vocab def __getstate__( self) -> Dict: '''simple docstring''' _UpperCamelCase = self.__dict__.copy() _UpperCamelCase = None return state def __setstate__( self , __a) -> None: '''simple docstring''' _UpperCamelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs'''): _UpperCamelCase = {} _UpperCamelCase = load_spm(self.spm_file , self.sp_model_kwargs) def UpperCAmelCase ( self , __a , __a = None) -> Tuple[str]: '''simple docstring''' _UpperCamelCase = Path(__a) assert save_dir.is_dir(), F'''{save_directory} should be a directory''' _UpperCamelCase = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) _UpperCamelCase = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder , __a) if os.path.abspath(self.spm_file) != os.path.abspath(__a) and os.path.isfile(self.spm_file): copyfile(self.spm_file , __a) elif not os.path.isfile(self.spm_file): with open(__a , '''wb''') as fi: _UpperCamelCase = self.sp_model.serialized_model_proto() fi.write(__a) return (str(__a), str(__a)) def lowerCamelCase__ ( __snake_case, __snake_case ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" _UpperCamelCase = sentencepiece.SentencePieceProcessor(**__snake_case ) spm.Load(str(__snake_case ) ) return spm def lowerCamelCase__ ( __snake_case ) -> Union[Dict, List]: """simple docstring""" with open(__snake_case, '''r''' ) as f: return json.load(__snake_case ) def lowerCamelCase__ ( __snake_case, __snake_case ) -> None: """simple docstring""" with open(__snake_case, '''w''' ) as f: json.dump(__snake_case, __snake_case, indent=2 )
19
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Tuple=64 , _lowerCAmelCase : List[str]=None ): SCREAMING_SNAKE_CASE_ = np.random.default_rng(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = length SCREAMING_SNAKE_CASE_ = rng.normal(size=(length,) ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self : Optional[int] ): return self.length def __getitem__( self : str , _lowerCAmelCase : Union[str, Any] ): return {"x": self.x[i], "y": self.y[i]} class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Tuple , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : List[str]=0 , _lowerCAmelCase : str=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a[0] + self.b[0] class lowerCamelCase_ ( torch.nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Any=0 , _lowerCAmelCase : Optional[Any]=False ): super().__init__() SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(torch.tensor(_lowerCAmelCase ).float() ) SCREAMING_SNAKE_CASE_ = True def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Optional[int]=None ): if self.first_batch: print(F"Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}" ) SCREAMING_SNAKE_CASE_ = False return x * self.a + self.b def UpperCAmelCase_ ( __UpperCAmelCase : Dict , __UpperCAmelCase : int = 16 ) -> Union[str, Any]: from datasets import load_dataset from transformers import AutoTokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE_ = {'train': 'tests/test_samples/MRPC/train.csv', 'validation': 'tests/test_samples/MRPC/dev.csv'} SCREAMING_SNAKE_CASE_ = load_dataset('csv' , data_files=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = datasets['train'].unique('label' ) SCREAMING_SNAKE_CASE_ = {v: i for i, v in enumerate(__UpperCAmelCase )} def tokenize_function(__UpperCAmelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer( examples['sentence1'] , examples['sentence2'] , truncation=__UpperCAmelCase , max_length=__UpperCAmelCase , padding='max_length' ) if "label" in examples: SCREAMING_SNAKE_CASE_ = [label_to_id[l] for l in examples['label']] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset SCREAMING_SNAKE_CASE_ = datasets.map( __UpperCAmelCase , batched=__UpperCAmelCase , remove_columns=['sentence1', 'sentence2', 'label'] , ) def collate_fn(__UpperCAmelCase : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__UpperCAmelCase , padding='max_length' , max_length=1_28 , return_tensors='pt' ) return tokenizer.pad(__UpperCAmelCase , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['train'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=2 ) SCREAMING_SNAKE_CASE_ = DataLoader(tokenized_datasets['validation'] , shuffle=__UpperCAmelCase , collate_fn=__UpperCAmelCase , batch_size=1 ) return train_dataloader, eval_dataloader
31
0
from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _lowerCAmelCase: Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _lowerCAmelCase: Optional[Any] = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def _lowercase( __a : Any , __a : int , __a : str=8 ): a__ =height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 a__ =width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def _lowercase( __a : Tuple , __a : Optional[Any]=512 , __a : Any=512 ): a__ =pil_image.resize((w, h) , resample=Image.BICUBIC , reducing_gap=1 ) a__ =np.array(pil_image.convert('RGB' ) ) a__ =arr.astype(np.floataa ) / 1_27.5 - 1 a__ =np.transpose(__a , [2, 0, 1] ) a__ =torch.from_numpy(__a ).unsqueeze(0 ) return image class lowercase_ (lowercase__ ): def __init__( self , lowercase_ , lowercase_ , lowercase_ , ) -> Any: super().__init__() self.register_modules( unet=lowercase_ , scheduler=lowercase_ , movq=lowercase_ , ) a__ =2 ** (len(self.movq.config.block_out_channels) - 1) def __UpperCamelCase ( self , lowercase_ , lowercase_ , lowercase_) -> str: # get the original timestep using init_timestep a__ =min(int(num_inference_steps * strength) , lowercase_) a__ =max(num_inference_steps - init_timestep , 0) a__ =self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __UpperCamelCase ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_=None) -> Union[str, Any]: if not isinstance(lowercase_ , (torch.Tensor, PIL.Image.Image, list)): raise ValueError( F"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(lowercase_)}""") a__ =image.to(device=lowercase_ , dtype=lowercase_) a__ =batch_size * num_images_per_prompt if image.shape[1] == 4: a__ =image else: if isinstance(lowercase_ , lowercase_) and len(lowercase_) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(lowercase_)}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""") elif isinstance(lowercase_ , lowercase_): a__ =[ self.movq.encode(image[i : i + 1]).latent_dist.sample(generator[i]) for i in range(lowercase_) ] a__ =torch.cat(lowercase_ , dim=0) else: a__ =self.movq.encode(lowercase_).latent_dist.sample(lowercase_) a__ =self.movq.config.scaling_factor * init_latents a__ =torch.cat([init_latents] , dim=0) a__ =init_latents.shape a__ =randn_tensor(lowercase_ , generator=lowercase_ , device=lowercase_ , dtype=lowercase_) # get latents a__ =self.scheduler.add_noise(lowercase_ , lowercase_ , lowercase_) a__ =init_latents return latents def __UpperCamelCase ( self , lowercase_=0) -> str: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`') a__ =torch.device(F"""cuda:{gpu_id}""") a__ =[ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(lowercase_ , lowercase_) def __UpperCamelCase ( self , lowercase_=0) -> List[Any]: if is_accelerate_available() and is_accelerate_version('>=' , '0.17.0.dev0'): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.') a__ =torch.device(F"""cuda:{gpu_id}""") if self.device.type != "cpu": self.to('cpu' , silence_dtype_warnings=lowercase_) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) a__ =None for cpu_offloaded_model in [self.unet, self.movq]: a__ , a__ =cpu_offload_with_hook(lowercase_ , lowercase_ , prev_module_hook=lowercase_) # We'll offload the last model manually. a__ =hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __UpperCamelCase ( self) -> List[Any]: if not hasattr(self.unet , '_hf_hook'): return self.device for module in self.unet.modules(): if ( hasattr(lowercase_ , '_hf_hook') and hasattr(module._hf_hook , 'execution_device') and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device) return self.device @torch.no_grad() @replace_example_docstring(lowercase_) def __call__( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ = 512 , lowercase_ = 512 , lowercase_ = 100 , lowercase_ = 4.0 , lowercase_ = 0.3 , lowercase_ = 1 , lowercase_ = None , lowercase_ = "pil" , lowercase_ = True , ) -> Optional[int]: a__ =self._execution_device a__ =guidance_scale > 1.0 if isinstance(lowercase_ , lowercase_): a__ =torch.cat(lowercase_ , dim=0) a__ =image_embeds.shape[0] if isinstance(lowercase_ , lowercase_): a__ =torch.cat(lowercase_ , dim=0) if do_classifier_free_guidance: a__ =image_embeds.repeat_interleave(lowercase_ , dim=0) a__ =negative_image_embeds.repeat_interleave(lowercase_ , dim=0) a__ =torch.cat([negative_image_embeds, image_embeds] , dim=0).to(dtype=self.unet.dtype , device=lowercase_) if not isinstance(lowercase_ , lowercase_): a__ =[image] if not all(isinstance(lowercase_ , (PIL.Image.Image, torch.Tensor)) for i in image): raise ValueError( F"""Input is in incorrect format: {[type(lowercase_) for i in image]}. Currently, we only support PIL image and pytorch tensor""") a__ =torch.cat([prepare_image(lowercase_ , lowercase_ , lowercase_) for i in image] , dim=0) a__ =image.to(dtype=image_embeds.dtype , device=lowercase_) a__ =self.movq.encode(lowercase_)['latents'] a__ =latents.repeat_interleave(lowercase_ , dim=0) self.scheduler.set_timesteps(lowercase_ , device=lowercase_) a__ , a__ =self.get_timesteps(lowercase_ , lowercase_ , lowercase_) a__ =timesteps[:1].repeat(batch_size * num_images_per_prompt) a__ , a__ =downscale_height_and_width(lowercase_ , lowercase_ , self.movq_scale_factor) a__ =self.prepare_latents( lowercase_ , lowercase_ , lowercase_ , lowercase_ , image_embeds.dtype , lowercase_ , lowercase_) for i, t in enumerate(self.progress_bar(lowercase_)): # expand the latents if we are doing classifier free guidance a__ =torch.cat([latents] * 2) if do_classifier_free_guidance else latents a__ ={'image_embeds': image_embeds} a__ =self.unet( sample=lowercase_ , timestep=lowercase_ , encoder_hidden_states=lowercase_ , added_cond_kwargs=lowercase_ , return_dict=lowercase_ , )[0] if do_classifier_free_guidance: a__ , a__ =noise_pred.split(latents.shape[1] , dim=1) a__ , a__ =noise_pred.chunk(2) a__ , a__ =variance_pred.chunk(2) a__ =noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) a__ =torch.cat([noise_pred, variance_pred_text] , dim=1) if not ( hasattr(self.scheduler.config , 'variance_type') and self.scheduler.config.variance_type in ["learned", "learned_range"] ): a__ , a__ =noise_pred.split(latents.shape[1] , dim=1) # compute the previous noisy sample x_t -> x_t-1 a__ =self.scheduler.step( lowercase_ , lowercase_ , lowercase_ , generator=lowercase_ , )[0] # post-processing a__ =self.movq.decode(lowercase_ , force_not_quantize=lowercase_)['sample'] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""") if output_type in ["np", "pil"]: a__ =image * 0.5 + 0.5 a__ =image.clamp(0 , 1) a__ =image.cpu().permute(0 , 2 , 3 , 1).float().numpy() if output_type == "pil": a__ =self.numpy_to_pil(lowercase_) if not return_dict: return (image,) return ImagePipelineOutput(images=lowercase_)
20
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , *_lowerCAmelCase : Optional[Any] , **_lowerCAmelCase : Any ): warnings.warn( 'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use LayoutLMv2ImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
0
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def lowerCAmelCase_ ( lowerCamelCase ): __magic_name__ : Tuple =[] embed.append( ( F"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", F"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( F"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", F"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( F"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", F"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( F"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", F"stage{idx}.patch_embed.norm.bias", ) ) return embed def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase ): __magic_name__ : List[Any] =[] attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", F"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", F"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", F"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", F"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", F"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", F"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", F"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", F"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", F"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", F"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( F"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", F"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", F"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", F"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", F"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", F"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", F"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", F"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", F"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (F"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", F"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def lowerCAmelCase_ ( lowerCamelCase ): __magic_name__ : Optional[Any] =[] token.append((F"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def lowerCAmelCase_ ( ): __magic_name__ : Any =[] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ): __magic_name__ : Dict ="""imagenet-1k-id2label.json""" __magic_name__ : Union[str, Any] =1000 __magic_name__ : int ="""huggingface/label-files""" __magic_name__ : Optional[Any] =num_labels __magic_name__ : str =json.load(open(cached_download(hf_hub_url(lowerCamelCase , lowerCamelCase , repo_type="""dataset""" ) ) , """r""" ) ) __magic_name__ : Optional[Any] ={int(lowerCamelCase ): v for k, v in idalabel.items()} __magic_name__ : List[str] =idalabel __magic_name__ : Optional[Any] ={v: k for k, v in idalabel.items()} __magic_name__ : Dict =CvtConfig(num_labels=lowerCamelCase , idalabel=lowerCamelCase , labelaid=lowerCamelCase ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": __magic_name__ : Tuple =[1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": __magic_name__ : Optional[Any] =[1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: __magic_name__ : int =[2, 2, 20] __magic_name__ : Tuple =[3, 12, 16] __magic_name__ : Optional[Any] =[192, 768, 1024] __magic_name__ : str =CvtForImageClassification(lowerCamelCase ) __magic_name__ : Dict =AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) __magic_name__ : str =image_size __magic_name__ : List[str] =torch.load(lowerCamelCase , map_location=torch.device("""cpu""" ) ) __magic_name__ : int =OrderedDict() __magic_name__ : Optional[Any] =[] for idx in range(len(config.depth ) ): if config.cls_token[idx]: __magic_name__ : str =list_of_state_dict + cls_token(lowerCamelCase ) __magic_name__ : List[str] =list_of_state_dict + embeddings(lowerCamelCase ) for cnt in range(config.depth[idx] ): __magic_name__ : Optional[Any] =list_of_state_dict + attention(lowerCamelCase , lowerCamelCase ) __magic_name__ : List[Any] =list_of_state_dict + final() for gg in list_of_state_dict: print(lowerCamelCase ) for i in range(len(lowerCamelCase ) ): __magic_name__ : Optional[int] =original_weights[list_of_state_dict[i][1]] model.load_state_dict(lowerCamelCase ) model.save_pretrained(lowerCamelCase ) image_processor.save_pretrained(lowerCamelCase ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": UpperCAmelCase_ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( "--cvt_model", default="cvt-w24", type=str, help="Name of the cvt model you'd like to convert.", ) parser.add_argument( "--image_size", default=384, type=int, help="Input Image Size", ) parser.add_argument( "--cvt_file_name", default=R"cvtmodels\CvT-w24-384x384-IN-22k.pth", type=str, help="Input Image Size", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) UpperCAmelCase_ : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
21
def UpperCAmelCase_ ( ) -> list[list[int]]: return [list(range(10_00 - i , -10_00 - i , -1 ) ) for i in range(10_00 )] lowerCamelCase__ : List[Any] = generate_large_matrix() lowerCamelCase__ : List[Any] = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> None: assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def UpperCAmelCase_ ( __UpperCAmelCase : list[int] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: SCREAMING_SNAKE_CASE_ = (left + right) // 2 SCREAMING_SNAKE_CASE_ = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: SCREAMING_SNAKE_CASE_ = mid + 1 else: SCREAMING_SNAKE_CASE_ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __UpperCAmelCase : list[list[int]] ) -> int: SCREAMING_SNAKE_CASE_ = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def UpperCAmelCase_ ( ) -> None: from timeit import timeit print('Running benchmarks' ) SCREAMING_SNAKE_CASE_ = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): SCREAMING_SNAKE_CASE_ = timeit(f"{func}(grid=grid)" , setup=__UpperCAmelCase , number=5_00 ) print(f"{func}() took {time:0.4f} seconds" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
31
0
'''simple docstring''' import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class A : def __init__( self : Dict , lowerCAmelCase_ : str , lowerCAmelCase_ : Tuple=99 , lowerCAmelCase_ : int=13 , lowerCAmelCase_ : Tuple=7 , lowerCAmelCase_ : Tuple=9 , lowerCAmelCase_ : int=True , lowerCAmelCase_ : Any=True , lowerCAmelCase_ : Optional[int]=False , lowerCAmelCase_ : List[str]=32 , lowerCAmelCase_ : Optional[int]=5 , lowerCAmelCase_ : Any=4 , lowerCAmelCase_ : Tuple=37 , lowerCAmelCase_ : Any=8 , lowerCAmelCase_ : int=0.1 , lowerCAmelCase_ : Any=0.0_0_2 , lowerCAmelCase_ : int=1 , lowerCAmelCase_ : Union[str, Any]=0 , lowerCAmelCase_ : List[Any]=0 , lowerCAmelCase_ : Tuple=None , lowerCAmelCase_ : List[Any]=None , ) -> List[Any]: """simple docstring""" _a = parent _a = batch_size _a = encoder_seq_length _a = decoder_seq_length # For common tests _a = self.decoder_seq_length _a = is_training _a = use_attention_mask _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = d_ff _a = relative_attention_num_buckets _a = dropout_rate _a = initializer_factor _a = eos_token_id _a = pad_token_id _a = decoder_start_token_id _a = None _a = decoder_layers def __lowerCAmelCase ( self : str ) -> Union[str, Any]: """simple docstring""" return TaConfig.from_pretrained('''google/umt5-base''' ) def __lowerCAmelCase ( self : Dict , lowerCAmelCase_ : str , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : List[Any]=None , lowerCAmelCase_ : Optional[Any]=None , lowerCAmelCase_ : str=None , lowerCAmelCase_ : str=None , lowerCAmelCase_ : Union[str, Any]=None , ) -> List[str]: """simple docstring""" if attention_mask is None: _a = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _a = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _a = torch.ones(config.num_hidden_layers , config.num_attention_heads , device=lowerCAmelCase_ ) if decoder_head_mask is None: _a = torch.ones(config.num_decoder_layers , config.num_attention_heads , device=lowerCAmelCase_ ) if cross_attn_head_mask is None: _a = torch.ones( config.num_decoder_layers , config.num_attention_heads , device=lowerCAmelCase_ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def __lowerCAmelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" _a = ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) _a = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _a = input_ids.clamp(self.pad_token_id + 1 ) _a = decoder_input_ids.clamp(self.pad_token_id + 1 ) _a = self.get_config() _a = config.num_attention_heads _a = self.prepare_inputs_dict(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return config, input_dict def __lowerCAmelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self : List[str] ) -> Union[str, Any]: """simple docstring""" return TaConfig( vocab_size=1_66 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __lowerCAmelCase ( self : int ) -> str: """simple docstring""" return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __lowerCAmelCase ( self : str , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : Any , lowerCAmelCase_ : List[Any] , ) -> Optional[Any]: """simple docstring""" _a = UMTaModel(config=lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) model.eval() _a = model( input_ids=lowerCAmelCase_ , decoder_input_ids=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ , decoder_attention_mask=lowerCAmelCase_ , ) _a = model(input_ids=lowerCAmelCase_ , decoder_input_ids=lowerCAmelCase_ ) _a = result.last_hidden_state _a = result.past_key_values _a = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(lowerCAmelCase_ ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def __lowerCAmelCase ( self : Dict , lowerCAmelCase_ : Any , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Any , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Tuple , ) -> Union[str, Any]: """simple docstring""" _a = UMTaModel(config=lowerCAmelCase_ ).get_decoder().to(lowerCAmelCase_ ).eval() # first forward pass _a = model(lowerCAmelCase_ , use_cache=lowerCAmelCase_ ) _a = model(lowerCAmelCase_ ) _a = model(lowerCAmelCase_ , use_cache=lowerCAmelCase_ ) self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) ) self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) + 1 ) _a , _a = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = model(lowerCAmelCase_ )['''last_hidden_state'''] _a = model(lowerCAmelCase_ , past_key_values=lowerCAmelCase_ )['''last_hidden_state'''] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -1, random_slice_idx].detach() _a = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCAmelCase_ , lowerCAmelCase_ , atol=1e-3 ) ) def __lowerCAmelCase ( self : List[str] , lowerCAmelCase_ : Dict , lowerCAmelCase_ : int , ) -> Optional[int]: """simple docstring""" _a = UMTaModel(config=lowerCAmelCase_ ).to(lowerCAmelCase_ ).half().eval() _a = model(**lowerCAmelCase_ )['''last_hidden_state'''] self.parent.assertFalse(torch.isnan(lowerCAmelCase_ ).any().item() ) @require_torch class A ( _a ,_a ,_a ,unittest.TestCase ): lowercase_ = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) lowercase_ = (UMTaForConditionalGeneration,) if is_torch_available() else () lowercase_ = ( { 'conversational': UMTaForConditionalGeneration, 'feature-extraction': UMTaModel, 'summarization': UMTaForConditionalGeneration, 'text2text-generation': UMTaForConditionalGeneration, 'translation': UMTaForConditionalGeneration, 'question-answering': UMTaForQuestionAnswering, } if is_torch_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False lowercase_ = True lowercase_ = True # The small UMT5 model needs higher percentages for CPU/MP tests lowercase_ = [0.8, 0.9] def __lowerCAmelCase ( self : str ) -> int: """simple docstring""" _a = UMTaModelTester(self ) @unittest.skip('''Test has a segmentation fault on torch 1.8.0''' ) def __lowerCAmelCase ( self : Dict ) -> Dict: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() _a = UMTaModel(config_and_inputs[0] ).to(lowerCAmelCase_ ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( lowerCAmelCase_ , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , F'{tmpdirname}/t5_test.onnx' , export_params=lowerCAmelCase_ , opset_version=9 , input_names=['''input_ids''', '''decoder_input_ids'''] , ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def __lowerCAmelCase ( self : List[Any] ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*lowerCAmelCase_ ) def __lowerCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" _a = ['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions'''] _a = self.model_tester.prepare_config_and_inputs() _a = config_and_inputs[0] _a = UMTaForConditionalGeneration(lowerCAmelCase_ ).eval() model.to(lowerCAmelCase_ ) _a = { '''head_mask''': torch.zeros(config.num_layers , config.num_heads , device=lowerCAmelCase_ ), '''decoder_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=lowerCAmelCase_ ), '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=lowerCAmelCase_ ), } for attn_name, (name, mask) in zip(lowerCAmelCase_ , head_masking.items() ): _a = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": _a = torch.ones( config.num_decoder_layers , config.num_heads , device=lowerCAmelCase_ ) _a = model.generate( config_and_inputs[1]['''input_ids'''] , num_beams=1 , max_length=3 , output_attentions=lowerCAmelCase_ , return_dict_in_generate=lowerCAmelCase_ , **lowerCAmelCase_ , ) # We check the state of decoder_attentions and cross_attentions just from the last step _a = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip('''Does not work on the tiny model as we keep hitting edge cases.''' ) def __lowerCAmelCase ( self : str ) -> Union[str, Any]: """simple docstring""" pass @require_torch @require_sentencepiece @require_tokenizers class A ( unittest.TestCase ): @slow @unittest.skip( '''Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged''' ) def __lowerCAmelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" _a = UMTaForConditionalGeneration.from_pretrained('''google/umt5-small''' , return_dict=lowerCAmelCase_ ).to(lowerCAmelCase_ ) _a = AutoTokenizer.from_pretrained('''google/umt5-small''' , use_fast=lowerCAmelCase_ , legacy=lowerCAmelCase_ ) _a = [ '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''', '''No se como puedo <extra_id_0>.''', '''This is the reason why we <extra_id_0> them.''', '''The <extra_id_0> walks in <extra_id_1>, seats''', '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''', ] _a = tokenizer(lowerCAmelCase_ , return_tensors='''pt''' , padding=lowerCAmelCase_ ).input_ids # fmt: off _a = torch.tensor( [ [ 3_85_30, 21_07_03, 25_62_99, 14_10, 25_62_98, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_26, 3_21, 6_71, 2_59_22, 25_62_99, 2_74, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 14_60, 3_39, 3_12, 1_90_14, 1_06_20, 7_58, 25_62_99, 23_55,2_74, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_17, 25_62_99, 1_48_69, 2_81, 3_01, 25_62_98, 2_75, 11_99_83,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_20, 25_62_99, 1_48_69, 2_81, 22_34, 2_89, 22_75, 3_33,6_13_91, 2_89, 25_62_98, 5_43, 25_62_97, 16_87_14, 3_29, 25_62_96,2_74, 1], ] ) # fmt: on torch.testing.assert_allclose(lowerCAmelCase_ , lowerCAmelCase_ ) _a = model.generate(input_ids.to(lowerCAmelCase_ ) ) _a = [ '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''', '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', ] _a = tokenizer.batch_decode(lowerCAmelCase_ ) self.assertEqual(lowerCAmelCase_ , lowerCAmelCase_ )
22
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[int] = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ['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 lowerCamelCase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
31
0
from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def _snake_case (__lowercase , __lowercase , __lowercase=1e-12): UpperCamelCase_ = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(__lowercase , axis=1) , a_min=__lowercase)).T UpperCamelCase_ = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(__lowercase , axis=1) , a_min=__lowercase)).T return jnp.matmul(__lowercase , norm_emb_a.T) class _a ( nn.Module ): """simple docstring""" A_ = 42 A_ = jnp.floataa def _UpperCAmelCase ( self ) -> Dict: UpperCamelCase_ = FlaxCLIPVisionModule(self.config.vision_config ) UpperCamelCase_ = nn.Dense(self.config.projection_dim , use_bias=_UpperCAmelCase , dtype=self.dtype ) UpperCamelCase_ = self.param('concept_embeds' , jax.nn.initializers.ones , (17, self.config.projection_dim) ) UpperCamelCase_ = self.param( 'special_care_embeds' , jax.nn.initializers.ones , (3, self.config.projection_dim) ) UpperCamelCase_ = self.param('concept_embeds_weights' , jax.nn.initializers.ones , (17,) ) UpperCamelCase_ = self.param('special_care_embeds_weights' , jax.nn.initializers.ones , (3,) ) def __call__( self , _UpperCAmelCase ) -> List[str]: UpperCamelCase_ = self.vision_model(_UpperCAmelCase )[1] UpperCamelCase_ = self.visual_projection(_UpperCAmelCase ) UpperCamelCase_ = jax_cosine_distance(_UpperCAmelCase , self.special_care_embeds ) UpperCamelCase_ = jax_cosine_distance(_UpperCAmelCase , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs UpperCamelCase_ = 0.0 UpperCamelCase_ = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment UpperCamelCase_ = jnp.round(_UpperCAmelCase , 3 ) UpperCamelCase_ = jnp.any(special_scores > 0 , axis=1 , keepdims=_UpperCAmelCase ) # Use a lower threshold if an image has any special care concept UpperCamelCase_ = is_special_care * 0.0_1 UpperCamelCase_ = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment UpperCamelCase_ = jnp.round(_UpperCAmelCase , 3 ) UpperCamelCase_ = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class _a ( UpperCAmelCase__ ): """simple docstring""" A_ = CLIPConfig A_ = """clip_input""" A_ = FlaxStableDiffusionSafetyCheckerModule def __init__( self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = 0 , _UpperCAmelCase = jnp.floataa , _UpperCAmelCase = True , **_UpperCAmelCase , ) -> Union[str, Any]: if input_shape is None: UpperCamelCase_ = (1, 224, 224, 3) UpperCamelCase_ = self.module_class(config=_UpperCAmelCase , dtype=_UpperCAmelCase , **_UpperCAmelCase ) super().__init__(_UpperCAmelCase , _UpperCAmelCase , input_shape=_UpperCAmelCase , seed=_UpperCAmelCase , dtype=_UpperCAmelCase , _do_init=_do_init ) def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = None ) -> FrozenDict: # init input tensor UpperCamelCase_ = jax.random.normal(_UpperCAmelCase , _UpperCAmelCase ) UpperCamelCase_ , UpperCamelCase_ = jax.random.split(_UpperCAmelCase ) UpperCamelCase_ = {'params': params_rng, 'dropout': dropout_rng} UpperCamelCase_ = self.module.init(_UpperCAmelCase , _UpperCAmelCase )['params'] return random_params def __call__( self , _UpperCAmelCase , _UpperCAmelCase = None , ) -> Optional[int]: UpperCamelCase_ = jnp.transpose(_UpperCAmelCase , (0, 2, 3, 1) ) return self.module.apply( {'params': params or self.params} , jnp.array(_UpperCAmelCase , dtype=jnp.floataa ) , rngs={} , )
23
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class lowerCamelCase_ : '''simple docstring''' @property def lowerCAmelCase_ ( self : Optional[Any] ): return self.get_dummy_input() @property def lowerCAmelCase_ ( self : Union[str, Any] ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'." ) def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : str=False , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : Dict=False , ): SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 32 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = (batch_size, num_channels) + sizes SCREAMING_SNAKE_CASE_ = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {'hidden_states': hidden_states} if include_temb: SCREAMING_SNAKE_CASE_ = 128 SCREAMING_SNAKE_CASE_ = randn_tensor((batch_size, temb_channels) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) if include_res_hidden_states_tuple: SCREAMING_SNAKE_CASE_ = torch.manual_seed(1 ) SCREAMING_SNAKE_CASE_ = (randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase ),) if include_encoder_hidden_states: SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, 32, 32) ).to(_lowerCAmelCase ) if include_skip_sample: SCREAMING_SNAKE_CASE_ = randn_tensor(((batch_size, 3) + sizes) , generator=_lowerCAmelCase , device=_lowerCAmelCase ) return dummy_input def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 128, } if self.block_type == "up": SCREAMING_SNAKE_CASE_ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) SCREAMING_SNAKE_CASE_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) unet_block.to(_lowerCAmelCase ) unet_block.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = unet_block(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] self.assertEqual(output.shape , self.output_shape ) SCREAMING_SNAKE_CASE_ = output[0, -1, -3:, -3:] SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase ).to(_lowerCAmelCase ) assert torch_all_close(output_slice.flatten() , _lowerCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_init_args_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = self.block_class(**_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.train() SCREAMING_SNAKE_CASE_ = model(**_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = output[0] SCREAMING_SNAKE_CASE_ = torch.device(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(output.shape , device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.mse_loss(_lowerCAmelCase , _lowerCAmelCase ) loss.backward()
31
0
'''simple docstring''' UpperCAmelCase_ : Dict = { '''Pillow''': '''Pillow''', '''accelerate''': '''accelerate>=0.11.0''', '''compel''': '''compel==0.1.8''', '''black''': '''black~=23.1''', '''datasets''': '''datasets''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.13.2''', '''requests-mock''': '''requests-mock==1.10.0''', '''importlib_metadata''': '''importlib_metadata''', '''invisible-watermark''': '''invisible-watermark''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2''', '''jaxlib''': '''jaxlib>=0.1.65''', '''Jinja2''': '''Jinja2''', '''k-diffusion''': '''k-diffusion>=0.0.12''', '''torchsde''': '''torchsde''', '''note_seq''': '''note_seq''', '''librosa''': '''librosa''', '''numpy''': '''numpy''', '''omegaconf''': '''omegaconf''', '''parameterized''': '''parameterized''', '''protobuf''': '''protobuf>=3.20.3,<4''', '''pytest''': '''pytest''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''ruff''': '''ruff>=0.0.241''', '''safetensors''': '''safetensors''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''scipy''': '''scipy''', '''onnx''': '''onnx''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''tensorboard''': '''tensorboard''', '''torch''': '''torch>=1.4''', '''torchvision''': '''torchvision''', '''transformers''': '''transformers>=4.25.1''', '''urllib3''': '''urllib3<=2.0.0''', }
24
import operator as op def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> Any: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCAmelCase , __UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { '^': op.pow, '*': op.mul, '/': div, '+': op.add, '-': op.sub, } # operators & their respective operation # print table header print('Symbol'.center(8 ) , 'Action'.center(12 ) , 'Stack' , sep=' | ' ) print('-' * (30 + len(__UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(__UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('push(' + x + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print(''.rjust(8 ) , ('pop(' + a + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' ) stack.append( str(opr[x](int(__UpperCAmelCase ) , int(__UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('push(' + a + x + b + ')').ljust(12 ) , ','.join(__UpperCAmelCase ) , sep=' | ' , ) return int(stack[0] ) if __name__ == "__main__": lowerCamelCase__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
31
0
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def lowerCamelCase__ ( _a = True , *_a , **_a): if not is_tqdm_available(): raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.") SCREAMING_SNAKE_CASE : List[Any] = False if main_process_only: SCREAMING_SNAKE_CASE : Optional[int] = PartialState().local_process_index == 0 return _tqdm(*_a , **_a , disable=_a)
25
def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: assert isinstance(__UpperCAmelCase , __UpperCAmelCase ), f"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE_ = f"The input value of [n={number}] has to be > 0" raise ValueError(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = sylvester(number - 1 ) SCREAMING_SNAKE_CASE_ = num - 1 SCREAMING_SNAKE_CASE_ = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
31
0
'''simple docstring''' from __future__ import annotations def _a ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ) -> None: """simple docstring""" if start is None: __snake_case : Optional[Any] = 0 if end is None: __snake_case : Optional[Any] = len(_lowerCamelCase ) - 1 if start >= end: return __snake_case : Tuple = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: __snake_case , __snake_case : str = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
26
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 lowerCamelCase__ : List[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase__ : Union[str, Any] = TaTokenizerFast lowerCamelCase__ : Dict = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ['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 lowerCamelCase__ : int = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
31
0
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = 'M-CLIP' def __init__( self , snake_case_=1024 , snake_case_=768 , **snake_case_ ): _A = transformerDimSize _A = imageDimSize super().__init__(**snake_case_ ) class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = MCLIPConfig def __init__( self , snake_case_ , *snake_case_ , **snake_case_ ): super().__init__(snake_case_ , *snake_case_ , **snake_case_ ) _A = XLMRobertaModel(snake_case_ ) _A = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = self.transformer(input_ids=snake_case_ , attention_mask=snake_case_ )[0] _A = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(snake_case_ ), embs
27
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_torch def lowerCAmelCase_ ( self : int ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Tuple ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache SCREAMING_SNAKE_CASE_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(_lowerCAmelCase ) BertModel.from_pretrained(_lowerCAmelCase ) BertTokenizer.from_pretrained(_lowerCAmelCase ) pipeline(task='fill-mask' , model=_lowerCAmelCase ) # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : List[str] ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched SCREAMING_SNAKE_CASE_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # next emulate no network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) @require_torch def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import pipeline\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' SCREAMING_SNAKE_CASE_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, mock, run] )] SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' , result.stderr.decode().replace('\n' , '' ) , ) @require_torch def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = '\nfrom transformers import AutoModel\n ' SCREAMING_SNAKE_CASE_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network SCREAMING_SNAKE_CASE_ = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed SCREAMING_SNAKE_CASE_ = self.get_env() SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files SCREAMING_SNAKE_CASE_ = '1' SCREAMING_SNAKE_CASE_ = subprocess.run(_lowerCAmelCase , env=_lowerCAmelCase , check=_lowerCAmelCase , capture_output=_lowerCAmelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('success' , result.stdout.decode() )
31
0
'''simple docstring''' import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process UpperCamelCase_ = logging.getLogger(__name__) def lowercase__( __UpperCamelCase: str ,__UpperCamelCase: str ): """simple docstring""" return (preds == labels).mean() @dataclass class _a : '''simple docstring''' A : str = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class _a : '''simple docstring''' A : str = field(metadata={'''help''': '''The name of the task to train on: ''' + ''', '''.join(processors.keys() )} ) A : str = field(metadata={'''help''': '''Should contain the data files for the task.'''} ) A : int = field( default=128 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) A : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def lowercase__( ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' ,datefmt='%m/%d/%Y %H:%M:%S' ,level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN ,) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' ,training_args.local_rank ,training_args.device ,training_args.n_gpu ,bool(training_args.local_rank != -1 ) ,training_args.fpaa ,) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' ,__UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: SCREAMING_SNAKE_CASE : Optional[Any] = processors[data_args.task_name]() SCREAMING_SNAKE_CASE : List[Any] = processor.get_labels() SCREAMING_SNAKE_CASE : Optional[Any] = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Any = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=__UpperCamelCase ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,) SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool('.ckpt' in model_args.model_name_or_path ) ,config=__UpperCamelCase ,cache_dir=model_args.cache_dir ,) # Get datasets SCREAMING_SNAKE_CASE : List[str] = ( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=__UpperCamelCase ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) SCREAMING_SNAKE_CASE : str = ( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=__UpperCamelCase ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase: EvalPrediction ) -> Dict: SCREAMING_SNAKE_CASE : Any = np.argmax(p.predictions ,axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase ,p.label_ids )} # Data collator SCREAMING_SNAKE_CASE : str = DataCollatorWithPadding(__UpperCamelCase ,pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer SCREAMING_SNAKE_CASE : Union[str, Any] = Trainer( model=__UpperCamelCase ,args=__UpperCamelCase ,train_dataset=__UpperCamelCase ,eval_dataset=__UpperCamelCase ,compute_metrics=__UpperCamelCase ,data_collator=__UpperCamelCase ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation SCREAMING_SNAKE_CASE : List[str] = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) SCREAMING_SNAKE_CASE : Any = trainer.evaluate() SCREAMING_SNAKE_CASE : Any = os.path.join(training_args.output_dir ,'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase ,'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' ,__UpperCamelCase ,__UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def lowercase__( __UpperCamelCase: Tuple ): """simple docstring""" main() if __name__ == "__main__": main()
28
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "M-CLIP" def __init__( self : Tuple , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=768 , **_lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = transformerDimSize SCREAMING_SNAKE_CASE_ = imageDimSize super().__init__(**_lowerCAmelCase ) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = MCLIPConfig def __init__( self : Dict , _lowerCAmelCase : Union[str, Any] , *_lowerCAmelCase : str , **_lowerCAmelCase : str ): super().__init__(_lowerCAmelCase , *_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaModel(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.transformer(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase )[0] SCREAMING_SNAKE_CASE_ = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(_lowerCAmelCase ), embs
31
0
"""simple docstring""" import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class __lowerCamelCase ( enum.Enum ): a__: Any = 0 a__: Tuple = 1 a__: Optional[Any] = 2 @add_end_docstrings(lowerCAmelCase ) class __lowerCamelCase ( lowerCAmelCase ): a__: Tuple = '\n In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The\n voice of Nicholas\'s young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western\n Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision\n and denounces one of the men as a horse thief. Although his father initially slaps him for making such an\n accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of\n the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop,\n begging for his blessing. <eod> </s> <eos>\n ' def __init__( self , *UpperCAmelCase , **UpperCAmelCase ): super().__init__(*UpperCAmelCase , **UpperCAmelCase ) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == '''tf''' else MODEL_FOR_CAUSAL_LM_MAPPING ) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. lowerCamelCase_ = None if self.model.config.prefix is not None: lowerCamelCase_ = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. lowerCamelCase_ = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = self._sanitize_parameters(prefix=UpperCAmelCase , **self._forward_params ) lowerCamelCase_ = {**self._preprocess_params, **preprocess_params} lowerCamelCase_ = {**self._forward_params, **forward_params} def UpperCAmelCase__ ( self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ): lowerCamelCase_ = {} if prefix is not None: lowerCamelCase_ = prefix if prefix: lowerCamelCase_ = self.tokenizer( UpperCAmelCase , padding=UpperCAmelCase , add_special_tokens=UpperCAmelCase , return_tensors=self.framework ) lowerCamelCase_ = prefix_inputs['''input_ids'''].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( f"{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected" ''' [None, \'hole\']''' ) lowerCamelCase_ = handle_long_generation preprocess_params.update(UpperCAmelCase ) lowerCamelCase_ = generate_kwargs lowerCamelCase_ = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_full_text`''' ) if return_tensors is not None: raise ValueError('''`return_full_text` is mutually exclusive with `return_tensors`''' ) lowerCamelCase_ = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_tensors`''' ) lowerCamelCase_ = ReturnType.TENSORS if return_type is not None: lowerCamelCase_ = return_type if clean_up_tokenization_spaces is not None: lowerCamelCase_ = clean_up_tokenization_spaces if stop_sequence is not None: lowerCamelCase_ = self.tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) if len(UpperCAmelCase ) > 1: warnings.warn( '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of''' ''' the stop sequence will be used as the stop sequence string in the interim.''' ) lowerCamelCase_ = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCAmelCase__ ( self , *UpperCAmelCase , **UpperCAmelCase ): # Parse arguments if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({'''add_space_before_punct_symbol''': True} ) return super()._parse_and_tokenize(*UpperCAmelCase , **UpperCAmelCase ) def __call__( self , UpperCAmelCase , **UpperCAmelCase ): return super().__call__(UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase__ ( self , UpperCAmelCase , UpperCAmelCase="" , UpperCAmelCase=None , **UpperCAmelCase ): lowerCamelCase_ = self.tokenizer( prefix + prompt_text , padding=UpperCAmelCase , add_special_tokens=UpperCAmelCase , return_tensors=self.framework ) lowerCamelCase_ = prompt_text if handle_long_generation == "hole": lowerCamelCase_ = inputs['''input_ids'''].shape[-1] if "max_new_tokens" in generate_kwargs: lowerCamelCase_ = generate_kwargs['''max_new_tokens'''] else: lowerCamelCase_ = generate_kwargs.get('''max_length''' , self.model.config.max_length ) - cur_len if new_tokens < 0: raise ValueError('''We cannot infer how many new tokens are expected''' ) if cur_len + new_tokens > self.tokenizer.model_max_length: lowerCamelCase_ = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( '''We cannot use `hole` to handle this generation the number of desired tokens exceeds the''' ''' models max length''' ) lowerCamelCase_ = inputs['''input_ids'''][:, -keep_length:] if "attention_mask" in inputs: lowerCamelCase_ = inputs['''attention_mask'''][:, -keep_length:] return inputs def UpperCAmelCase__ ( self , UpperCAmelCase , **UpperCAmelCase ): lowerCamelCase_ = model_inputs['''input_ids'''] lowerCamelCase_ = model_inputs.get('''attention_mask''' , UpperCAmelCase ) # Allow empty prompts if input_ids.shape[1] == 0: lowerCamelCase_ = None lowerCamelCase_ = None lowerCamelCase_ = 1 else: lowerCamelCase_ = input_ids.shape[0] lowerCamelCase_ = model_inputs.pop('''prompt_text''' ) # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. lowerCamelCase_ = generate_kwargs.pop('''prefix_length''' , 0 ) if prefix_length > 0: lowerCamelCase_ = '''max_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].max_new_tokens is not None ) if not has_max_new_tokens: lowerCamelCase_ = generate_kwargs.get('''max_length''' ) or self.model.config.max_length generate_kwargs["max_length"] += prefix_length lowerCamelCase_ = '''min_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL lowerCamelCase_ = self.model.generate(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) lowerCamelCase_ = generated_sequence.shape[0] if self.framework == "pt": lowerCamelCase_ = generated_sequence.reshape(UpperCAmelCase , out_b // in_b , *generated_sequence.shape[1:] ) elif self.framework == "tf": lowerCamelCase_ = tf.reshape(UpperCAmelCase , (in_b, out_b // in_b, *generated_sequence.shape[1:]) ) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def UpperCAmelCase__ ( self , UpperCAmelCase , UpperCAmelCase=ReturnType.FULL_TEXT , UpperCAmelCase=True ): lowerCamelCase_ = model_outputs['''generated_sequence'''][0] lowerCamelCase_ = model_outputs['''input_ids'''] lowerCamelCase_ = model_outputs['''prompt_text'''] lowerCamelCase_ = generated_sequence.numpy().tolist() lowerCamelCase_ = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: lowerCamelCase_ = {'''generated_token_ids''': sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text lowerCamelCase_ = self.tokenizer.decode( UpperCAmelCase , skip_special_tokens=UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase , ) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: lowerCamelCase_ = 0 else: lowerCamelCase_ = len( self.tokenizer.decode( input_ids[0] , skip_special_tokens=UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase , ) ) if return_type == ReturnType.FULL_TEXT: lowerCamelCase_ = prompt_text + text[prompt_length:] else: lowerCamelCase_ = text[prompt_length:] lowerCamelCase_ = {'''generated_text''': all_text} records.append(UpperCAmelCase ) return records
29
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_lowerCAmelCase ) return image @property def lowerCAmelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def lowerCAmelCase_ ( self : Tuple ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def lowerCAmelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : List[Any] ): def extract(*_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : str ): class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int ): self.pixel_values.to(_lowerCAmelCase ) return self return Out() return extract def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = 'cpu' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowerCAmelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , return_dict=_lowerCAmelCase , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_lowerCAmelCase ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_lowerCAmelCase , scheduler=_lowerCAmelCase , vae=_lowerCAmelCase , text_encoder=_lowerCAmelCase , tokenizer=_lowerCAmelCase , safety_checker=_lowerCAmelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = alt_pipe.to(_lowerCAmelCase ) alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = 'A painting of a squirrel eating a burger' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_lowerCAmelCase , num_inference_steps=2 , output_type='np' , image=_lowerCAmelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg' ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy' ) SCREAMING_SNAKE_CASE_ = 'BAAI/AltDiffusion' SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _lowerCAmelCase , safety_checker=_lowerCAmelCase , ) pipe.to(_lowerCAmelCase ) pipe.set_progress_bar_config(disable=_lowerCAmelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = 'A fantasy landscape, trending on artstation' SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_lowerCAmelCase , image=_lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , generator=_lowerCAmelCase , output_type='np' , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1E-2
31
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class __a( _a ): """simple docstring""" lowerCAmelCase = '''megatron-bert''' def __init__( self ,_SCREAMING_SNAKE_CASE=29_056 ,_SCREAMING_SNAKE_CASE=1_024 ,_SCREAMING_SNAKE_CASE=24 ,_SCREAMING_SNAKE_CASE=16 ,_SCREAMING_SNAKE_CASE=4_096 ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=512 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=0.02 ,_SCREAMING_SNAKE_CASE=1e-12 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE="absolute" ,_SCREAMING_SNAKE_CASE=True ,**_SCREAMING_SNAKE_CASE ,) -> Tuple: super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = vocab_size UpperCAmelCase_ : List[Any] = hidden_size UpperCAmelCase_ : List[str] = num_hidden_layers UpperCAmelCase_ : Dict = num_attention_heads UpperCAmelCase_ : Dict = hidden_act UpperCAmelCase_ : Optional[int] = intermediate_size UpperCAmelCase_ : List[Any] = hidden_dropout_prob UpperCAmelCase_ : Union[str, Any] = attention_probs_dropout_prob UpperCAmelCase_ : Union[str, Any] = max_position_embeddings UpperCAmelCase_ : Tuple = type_vocab_size UpperCAmelCase_ : Optional[Any] = initializer_range UpperCAmelCase_ : Dict = layer_norm_eps UpperCAmelCase_ : Dict = position_embedding_type UpperCAmelCase_ : int = use_cache
30
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 lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { '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 lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "longformer" def __init__( self : Union[str, Any] , _lowerCAmelCase : Union[List[int], int] = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 1 , _lowerCAmelCase : int = 0 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : int = 30_522 , _lowerCAmelCase : int = 768 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 12 , _lowerCAmelCase : int = 3_072 , _lowerCAmelCase : str = "gelu" , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : float = 0.1 , _lowerCAmelCase : int = 512 , _lowerCAmelCase : int = 2 , _lowerCAmelCase : float = 0.02 , _lowerCAmelCase : float = 1E-12 , _lowerCAmelCase : bool = False , **_lowerCAmelCase : Union[str, Any] , ): super().__init__(pad_token_id=_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = attention_window SCREAMING_SNAKE_CASE_ = sep_token_id SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = type_vocab_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = onnx_export class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , _lowerCAmelCase : "PretrainedConfig" , _lowerCAmelCase : str = "default" , _lowerCAmelCase : "List[PatchingSpec]" = None ): super().__init__(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = True @property def lowerCAmelCase_ ( self : Any ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: SCREAMING_SNAKE_CASE_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ] ) @property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = super().outputs if self.task == "default": SCREAMING_SNAKE_CASE_ = {0: 'batch'} return outputs @property def lowerCAmelCase_ ( self : str ): return 1E-4 @property def lowerCAmelCase_ ( self : Optional[Any] ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14 ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : "PreTrainedTokenizerBase" , _lowerCAmelCase : int = -1 , _lowerCAmelCase : int = -1 , _lowerCAmelCase : bool = False , _lowerCAmelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_ = super().generate_dummy_inputs( preprocessor=_lowerCAmelCase , batch_size=_lowerCAmelCase , seq_length=_lowerCAmelCase , is_pair=_lowerCAmelCase , framework=_lowerCAmelCase ) 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 SCREAMING_SNAKE_CASE_ = torch.zeros_like(inputs['input_ids'] ) # make every second token global SCREAMING_SNAKE_CASE_ = 1 return inputs
31
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/config.json", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/config.json", "funnel-transformer/medium-base": "https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json", "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/config.json", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json", "funnel-transformer/xlarge-base": "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json", } class __UpperCamelCase ( A__ ): __A : int = """funnel""" __A : Union[str, Any] = { """hidden_size""": """d_model""", """num_attention_heads""": """n_head""", } def __init__( self , _UpperCamelCase=30522 , _UpperCamelCase=[4, 4, 4] , _UpperCamelCase=None , _UpperCamelCase=2 , _UpperCamelCase=768 , _UpperCamelCase=12 , _UpperCamelCase=64 , _UpperCamelCase=3072 , _UpperCamelCase="gelu_new" , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.0 , _UpperCamelCase=0.1 , _UpperCamelCase=None , _UpperCamelCase=1e-9 , _UpperCamelCase="mean" , _UpperCamelCase="relative_shift" , _UpperCamelCase=True , _UpperCamelCase=True , _UpperCamelCase=True , **_UpperCamelCase , ): _UpperCAmelCase = vocab_size _UpperCAmelCase = block_sizes _UpperCAmelCase = [1] * len(_UpperCamelCase ) if block_repeats is None else block_repeats assert len(_UpperCamelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _UpperCAmelCase = num_decoder_layers _UpperCAmelCase = d_model _UpperCAmelCase = n_head _UpperCAmelCase = d_head _UpperCAmelCase = d_inner _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout _UpperCAmelCase = attention_dropout _UpperCAmelCase = activation_dropout _UpperCAmelCase = initializer_range _UpperCAmelCase = initializer_std _UpperCAmelCase = layer_norm_eps assert pooling_type in [ "mean", "max", ], f'''Got {pooling_type} for `pooling_type` but only \'mean\' and \'max\' are supported.''' _UpperCAmelCase = pooling_type assert attention_type in [ "relative_shift", "factorized", ], f'''Got {attention_type} for `attention_type` but only \'relative_shift\' and \'factorized\' are supported.''' _UpperCAmelCase = attention_type _UpperCAmelCase = separate_cls _UpperCAmelCase = truncate_seq _UpperCAmelCase = pool_q_only super().__init__(**_UpperCamelCase ) @property def UpperCamelCase( self ): return sum(self.block_sizes ) @num_hidden_layers.setter def UpperCamelCase( self , _UpperCamelCase ): raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def UpperCamelCase( self ): return len(self.block_sizes ) @num_blocks.setter def UpperCamelCase( self , _UpperCamelCase ): raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
32
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
0
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer import diffusers from diffusers import ( AutoencoderKL, EulerDiscreteScheduler, StableDiffusionLatentUpscalePipeline, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() def SCREAMING_SNAKE_CASE ( __lowerCAmelCase ) -> List[Any]: snake_case__ = [tensor.shape for tensor in tensor_list] return all(shape == shapes[0] for shape in shapes[1:] ) class __magic_name__ (snake_case_ ,snake_case_ ,snake_case_ ,unittest.TestCase ): '''simple docstring''' __lowercase : Dict = StableDiffusionLatentUpscalePipeline __lowercase : List[str] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { 'height', 'width', 'cross_attention_kwargs', 'negative_prompt_embeds', 'prompt_embeds', } __lowercase : List[Any] = PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'} __lowercase : Any = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS __lowercase : int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __lowercase : List[Any] = frozenset([] ) __lowercase : Any = True @property def SCREAMING_SNAKE_CASE__ ( self:List[str] ): snake_case__ = 1 snake_case__ = 4 snake_case__ = (16, 16) snake_case__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_a ) return image def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] ): torch.manual_seed(0 ) snake_case__ = UNetaDConditionModel( act_fn='''gelu''' , attention_head_dim=8 , norm_num_groups=_a , block_out_channels=[32, 32, 64, 64] , time_cond_proj_dim=1_60 , conv_in_kernel=1 , conv_out_kernel=1 , cross_attention_dim=32 , down_block_types=( '''KDownBlock2D''', '''KCrossAttnDownBlock2D''', '''KCrossAttnDownBlock2D''', '''KCrossAttnDownBlock2D''', ) , in_channels=8 , mid_block_type=_a , only_cross_attention=_a , out_channels=5 , resnet_time_scale_shift='''scale_shift''' , time_embedding_type='''fourier''' , timestep_post_act='''gelu''' , up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''') , ) snake_case__ = AutoencoderKL( block_out_channels=[32, 32, 64, 64] , in_channels=3 , out_channels=3 , down_block_types=[ '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', ] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) snake_case__ = EulerDiscreteScheduler(prediction_type='''sample''' ) snake_case__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , hidden_act='''quick_gelu''' , projection_dim=5_12 , ) snake_case__ = CLIPTextModel(_a ) snake_case__ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) snake_case__ = { '''unet''': model.eval(), '''vae''': vae.eval(), '''scheduler''': scheduler, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, } return components def SCREAMING_SNAKE_CASE__ ( self:List[Any] , _a:Optional[Any] , _a:List[str]=0 ): if str(_a ).startswith('''mps''' ): snake_case__ = torch.manual_seed(_a ) else: snake_case__ = torch.Generator(device=_a ).manual_seed(_a ) snake_case__ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': self.dummy_image.cpu(), '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self:str ): snake_case__ = '''cpu''' snake_case__ = self.get_dummy_components() snake_case__ = self.pipeline_class(**_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) snake_case__ = self.get_dummy_inputs(_a ) snake_case__ = pipe(**_a ).images snake_case__ = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 2_56, 2_56, 3) ) snake_case__ = np.array( [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055] ) snake_case__ = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_a , 1e-3 ) def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] ): super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 ) def SCREAMING_SNAKE_CASE__ ( self:List[Any] ): super().test_cpu_offload_forward_pass(expected_max_diff=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self:str ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self:Any ): super().test_inference_batch_single_identical(expected_max_diff=7e-3 ) def SCREAMING_SNAKE_CASE__ ( self:Tuple ): super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self:Dict ): super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self:str ): super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self:Any ): snake_case__ = [ '''DDIMScheduler''', '''DDPMScheduler''', '''PNDMScheduler''', '''HeunDiscreteScheduler''', '''EulerAncestralDiscreteScheduler''', '''KDPM2DiscreteScheduler''', '''KDPM2AncestralDiscreteScheduler''', '''DPMSolverSDEScheduler''', ] snake_case__ = self.get_dummy_components() snake_case__ = self.pipeline_class(**_a ) # make sure that PNDM does not need warm-up pipe.scheduler.register_to_config(skip_prk_steps=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) snake_case__ = self.get_dummy_inputs(_a ) snake_case__ = 2 snake_case__ = [] for scheduler_enum in KarrasDiffusionSchedulers: if scheduler_enum.name in skip_schedulers: # no sigma schedulers are not supported # no schedulers continue snake_case__ = getattr(_a , scheduler_enum.name ) snake_case__ = scheduler_cls.from_config(pipe.scheduler.config ) snake_case__ = pipe(**_a )[0] outputs.append(_a ) assert check_same_shape(_a ) @require_torch_gpu @slow class __magic_name__ (unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] ): super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self:str ): snake_case__ = torch.manual_seed(33 ) snake_case__ = StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' , torch_dtype=torch.floataa ) pipe.to('''cuda''' ) snake_case__ = StableDiffusionLatentUpscalePipeline.from_pretrained( '''stabilityai/sd-x2-latent-upscaler''' , torch_dtype=torch.floataa ) upscaler.to('''cuda''' ) snake_case__ = '''a photo of an astronaut high resolution, unreal engine, ultra realistic''' snake_case__ = pipe(_a , generator=_a , output_type='''latent''' ).images snake_case__ = upscaler( prompt=_a , image=_a , num_inference_steps=20 , guidance_scale=0 , generator=_a , output_type='''np''' , ).images[0] snake_case__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy''' ) assert np.abs((expected_image - image).mean() ) < 5e-2 def SCREAMING_SNAKE_CASE__ ( self:Optional[int] ): snake_case__ = torch.manual_seed(33 ) snake_case__ = StableDiffusionLatentUpscalePipeline.from_pretrained( '''stabilityai/sd-x2-latent-upscaler''' , torch_dtype=torch.floataa ) upscaler.to('''cuda''' ) snake_case__ = '''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas''' snake_case__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png''' ) snake_case__ = upscaler( prompt=_a , image=_a , num_inference_steps=20 , guidance_scale=0 , generator=_a , output_type='''np''' , ).images[0] snake_case__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy''' ) assert np.abs((expected_image - image).max() ) < 5e-2
33
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "swinv2" lowercase_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , _lowerCAmelCase : Optional[Any]=224 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Tuple=3 , _lowerCAmelCase : Tuple=96 , _lowerCAmelCase : Dict=[2, 2, 6, 2] , _lowerCAmelCase : Optional[Any]=[3, 6, 12, 24] , _lowerCAmelCase : str=7 , _lowerCAmelCase : List[Any]=4.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : List[Any]="gelu" , _lowerCAmelCase : str=False , _lowerCAmelCase : str=0.02 , _lowerCAmelCase : List[Any]=1E-5 , _lowerCAmelCase : str=32 , **_lowerCAmelCase : List[Any] , ): super().__init__(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = len(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ = int(embed_dim * 2 ** (len(_lowerCAmelCase ) - 1) ) SCREAMING_SNAKE_CASE_ = (0, 0, 0, 0)
31
0
"""simple docstring""" from functools import lru_cache @lru_cache def __snake_case ( _lowercase ): """simple docstring""" if num < 0: raise ValueError('''Number should not be negative.''' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
34
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCamelCase__ : Dict = random.Random() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple=1.0 , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Dict=None ) -> Tuple: if rng is None: SCREAMING_SNAKE_CASE_ = global_rng SCREAMING_SNAKE_CASE_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]=7 , _lowerCAmelCase : Union[str, Any]=400 , _lowerCAmelCase : Tuple=2_000 , _lowerCAmelCase : str=1 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[Any]=16_000 , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Any=80 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : List[str]=64 , _lowerCAmelCase : List[Any]="hann_window" , _lowerCAmelCase : Any=80 , _lowerCAmelCase : List[Any]=7_600 , _lowerCAmelCase : List[Any]=1E-10 , _lowerCAmelCase : Optional[Any]=True , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = min_seq_length SCREAMING_SNAKE_CASE_ = max_seq_length SCREAMING_SNAKE_CASE_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ = feature_size SCREAMING_SNAKE_CASE_ = padding_value SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = num_mel_bins SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = win_length SCREAMING_SNAKE_CASE_ = win_function SCREAMING_SNAKE_CASE_ = fmin SCREAMING_SNAKE_CASE_ = fmax SCREAMING_SNAKE_CASE_ = mel_floor SCREAMING_SNAKE_CASE_ = return_attention_mask def lowerCAmelCase_ ( self : Union[str, Any] ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int]=False , _lowerCAmelCase : str=False ): def _flatten(_lowerCAmelCase : Dict ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: SCREAMING_SNAKE_CASE_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[int]=False ): if equal_length: SCREAMING_SNAKE_CASE_ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = SpeechTaFeatureExtractor def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : int ): self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1E-3 ) ) def lowerCAmelCase_ ( self : List[Any] ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE_ = ['longest', 'max_length', 'do_not_pad'] SCREAMING_SNAKE_CASE_ = [None, 1_600, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = feat_extract(_lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='max_length' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=1_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = feat_extract( _lowerCAmelCase , truncation=_lowerCAmelCase , max_length=2_000 , padding='longest' , return_tensors='np' ) SCREAMING_SNAKE_CASE_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def lowerCAmelCase_ ( self : Tuple ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE_ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1E-3 ) ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_lowerCAmelCase ) == len(_lowerCAmelCase ) for x, y in zip(_lowerCAmelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) SCREAMING_SNAKE_CASE_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' )[input_name] SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad(_lowerCAmelCase , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.feat_extract_dict SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE_ = [len(_lowerCAmelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE_ = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE_ = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE_ = feat_extract.pad( _lowerCAmelCase , padding='max_length' , max_length=_lowerCAmelCase , truncation=_lowerCAmelCase , return_tensors='np' ) self.assertIn('attention_mask' , _lowerCAmelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ = ds.sort('id' ).select(range(_lowerCAmelCase ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _lowerCAmelCase , atol=1E-6 ) ) def lowerCAmelCase_ ( self : Optional[int] ): # fmt: off SCREAMING_SNAKE_CASE_ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on SCREAMING_SNAKE_CASE_ = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE_ = feature_extractor(audio_target=_lowerCAmelCase , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _lowerCAmelCase , atol=1E-4 ) )
31
0
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_camembert import CamembertTokenizer else: a_ :Tuple = None a_ :Optional[Any] = logging.get_logger(__name__) a_ :int = {'vocab_file': 'sentencepiece.bpe.model', 'tokenizer_file': 'tokenizer.json'} a_ :List[Any] = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', }, 'tokenizer_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/tokenizer.json', }, } a_ :Tuple = { 'camembert-base': 5_12, } a_ :Dict = '▁' class lowercase ( _UpperCAmelCase ): lowerCamelCase : Tuple = VOCAB_FILES_NAMES lowerCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Any = ['''input_ids''', '''attention_mask'''] lowerCamelCase : Tuple = CamembertTokenizer def __init__( self : int , _lowercase : int=None , _lowercase : List[str]=None , _lowercase : Optional[int]="<s>" , _lowercase : Optional[int]="</s>" , _lowercase : Tuple="</s>" , _lowercase : str="<s>" , _lowercase : Tuple="<unk>" , _lowercase : str="<pad>" , _lowercase : Dict="<mask>" , _lowercase : List[str]=["<s>NOTUSED", "</s>NOTUSED"] , **_lowercase : List[str] , ): # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE__ : List[str] = AddedToken(_lowercase , lstrip=_lowercase , rstrip=_lowercase ) if isinstance(_lowercase , _lowercase ) else mask_token super().__init__( _lowercase , tokenizer_file=_lowercase , bos_token=_lowercase , eos_token=_lowercase , sep_token=_lowercase , cls_token=_lowercase , unk_token=_lowercase , pad_token=_lowercase , mask_token=_lowercase , additional_special_tokens=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = vocab_file SCREAMING_SNAKE_CASE__ : Optional[Any] = False if not self.vocab_file else True def lowercase__ ( self : List[str] , _lowercase : List[int] , _lowercase : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE__ : Union[str, Any] = [self.cls_token_id] SCREAMING_SNAKE_CASE__ : List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowercase__ ( self : Dict , _lowercase : List[int] , _lowercase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = [self.sep_token_id] SCREAMING_SNAKE_CASE__ : str = [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 lowercase__ ( self : Any , _lowercase : str , _lowercase : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(_lowercase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return SCREAMING_SNAKE_CASE__ : Optional[int] = os.path.join( _lowercase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowercase ): copyfile(self.vocab_file , _lowercase ) return (out_vocab_file,)
35
from __future__ import annotations from typing import TypedDict class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = 42 lowercase_ = 42 def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> list[str]: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__UpperCAmelCase ) )] def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> BWTTransformDict: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) SCREAMING_SNAKE_CASE_ = all_rotations(__UpperCAmelCase ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE_ = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__UpperCAmelCase ), } return response def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : int ) -> str: if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: SCREAMING_SNAKE_CASE_ = int(__UpperCAmelCase ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__UpperCAmelCase ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) SCREAMING_SNAKE_CASE_ = [''] * len(__UpperCAmelCase ) for _ in range(len(__UpperCAmelCase ) ): for i in range(len(__UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = 'Provide a string that I will generate its BWT transform: ' lowerCamelCase__ : List[str] = input(entry_msg).strip() lowerCamelCase__ : int = bwt_transform(s) print( f'''Burrows Wheeler transform for string \'{s}\' results ''' f'''in \'{result['bwt_string']}\'''' ) lowerCamelCase__ : Dict = reverse_bwt(result['bwt_string'], result['idx_original_string']) print( f'''Reversing Burrows Wheeler transform for entry \'{result['bwt_string']}\' ''' f'''we get original string \'{original_string}\'''' )
31
0
from queue import PriorityQueue from typing import Any import numpy as np def lowercase ( __A : dict , __A : str , __A : set , __A : set , __A : dict , __A : dict , __A : PriorityQueue , __A : dict , __A : float | int , ) -> float | int: '''simple docstring''' for nxt, d in graph[v]: if nxt in visited_forward: continue snake_case : Any = cst_fwd.get(__A , np.inf ) snake_case : Any = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) snake_case : Dict = new_cost_f snake_case : int = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: snake_case : Optional[Any] = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def lowercase ( __A : str , __A : str , __A : dict , __A : dict ) -> int: '''simple docstring''' snake_case : Optional[int] = -1 snake_case : Union[str, Any] = set() snake_case : Optional[int] = set() snake_case : Union[str, Any] = {source: 0} snake_case : Optional[Any] = {destination: 0} snake_case : Any = {source: None} snake_case : Any = {destination: None} snake_case : PriorityQueue[Any] = PriorityQueue() snake_case : PriorityQueue[Any] = PriorityQueue() snake_case : List[Any] = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): snake_case , snake_case : str = queue_forward.get() visited_forward.add(__A ) snake_case , snake_case : str = queue_backward.get() visited_backward.add(__A ) snake_case : List[Any] = pass_and_relaxation( __A , __A , __A , __A , __A , __A , __A , __A , __A , ) snake_case : Any = pass_and_relaxation( __A , __A , __A , __A , __A , __A , __A , __A , __A , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: snake_case : Any = shortest_distance return shortest_path_distance __lowercase : List[Any] = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } __lowercase : Optional[int] = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
36
class lowerCamelCase_ : '''simple docstring''' def __init__( self : str ): SCREAMING_SNAKE_CASE_ = {} def lowerCAmelCase_ ( self : List[str] ): print(self.vertex ) for i in self.vertex: print(_lowerCAmelCase , ' -> ' , ' -> '.join([str(_lowerCAmelCase ) for j in self.vertex[i]] ) ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_lowerCAmelCase ) else: # else make a new vertex SCREAMING_SNAKE_CASE_ = [to_vertex] def lowerCAmelCase_ ( self : Optional[Any] ): # visited array for storing already visited nodes SCREAMING_SNAKE_CASE_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : int , _lowerCAmelCase : list ): # mark start vertex as visited SCREAMING_SNAKE_CASE_ = True print(_lowerCAmelCase , end=' ' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
31
0
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ConditionalDetrImageProcessor class A__ ( unittest.TestCase ): """simple docstring""" def __init__( self : int , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=7 , lowerCamelCase__ : Optional[Any]=3 , lowerCamelCase__ : Dict=30 , lowerCamelCase__ : Optional[int]=400 , lowerCamelCase__ : str=True , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=True , lowerCamelCase__ : List[str]=[0.5, 0.5, 0.5] , lowerCamelCase__ : Optional[int]=[0.5, 0.5, 0.5] , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : List[str]=1 / 255 , lowerCamelCase__ : str=True , ): # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p a__ : List[str] = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} a__ : Optional[Any] = parent a__ : Union[str, Any] = batch_size a__ : Tuple = num_channels a__ : Union[str, Any] = min_resolution a__ : Union[str, Any] = max_resolution a__ : Union[str, Any] = do_resize a__ : Tuple = size a__ : str = do_normalize a__ : Optional[int] = image_mean a__ : Optional[Any] = image_std a__ : str = do_rescale a__ : Dict = rescale_factor a__ : int = do_pad def _UpperCamelCase( self : str ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def _UpperCamelCase( self : Dict , lowerCamelCase__ : Any , lowerCamelCase__ : Tuple=False ): if not batched: a__ : str = image_inputs[0] if isinstance(lowerCamelCase__ , Image.Image ): a__, a__ : Tuple = image.size else: a__, a__ : int = image.shape[1], image.shape[2] if w < h: a__ : str = int(self.size["shortest_edge"] * h / w ) a__ : int = self.size["shortest_edge"] elif w > h: a__ : int = self.size["shortest_edge"] a__ : Dict = int(self.size["shortest_edge"] * w / h ) else: a__ : Dict = self.size["shortest_edge"] a__ : Tuple = self.size["shortest_edge"] else: a__ : Union[str, Any] = [] for image in image_inputs: a__, a__ : str = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) a__ : int = max(lowerCamelCase__ , key=lambda lowerCamelCase__ : item[0] )[0] a__ : str = max(lowerCamelCase__ , key=lambda lowerCamelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = ConditionalDetrImageProcessor if is_vision_available() else None def _UpperCamelCase( self : Optional[int] ): a__ : Tuple = ConditionalDetrImageProcessingTester(self ) @property def _UpperCamelCase( self : Dict ): return self.image_processor_tester.prepare_image_processor_dict() def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , "image_mean" ) ) self.assertTrue(hasattr(lowerCamelCase__ , "image_std" ) ) self.assertTrue(hasattr(lowerCamelCase__ , "do_normalize" ) ) self.assertTrue(hasattr(lowerCamelCase__ , "do_resize" ) ) self.assertTrue(hasattr(lowerCamelCase__ , "size" ) ) def _UpperCamelCase( self : Tuple ): a__ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , lowerCamelCase__ ) a__ : str = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=lowerCamelCase__ ) self.assertEqual(image_processor.size , {"shortest_edge": 42, "longest_edge": 84} ) self.assertEqual(image_processor.do_pad , lowerCamelCase__ ) def _UpperCamelCase( self : Any ): pass def _UpperCamelCase( self : Optional[Any] ): # Initialize image_processing a__ : int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a__ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input a__ : Any = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values a__, a__ : Optional[Any] = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a__, a__ : Dict = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) a__ : Union[str, Any] = image_processing(lowerCamelCase__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase( self : str ): # Initialize image_processing a__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input a__ : List[str] = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values a__, a__ : Optional[int] = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a__ : Any = image_processing(lowerCamelCase__ , return_tensors="pt" ).pixel_values a__, a__ : Optional[Any] = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase( self : Optional[Any] ): # Initialize image_processing a__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input a__ : Dict = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values a__, a__ : Union[str, Any] = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched a__ : Tuple = image_processing(lowerCamelCase__ , return_tensors="pt" ).pixel_values a__, a__ : Optional[Any] = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def _UpperCamelCase( self : Tuple ): # prepare image and target a__ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: a__ : Tuple = json.loads(f.read() ) a__ : Any = {"image_id": 39_769, "annotations": target} # encode them a__ : List[str] = ConditionalDetrImageProcessor.from_pretrained("microsoft/conditional-detr-resnet-50" ) a__ : str = image_processing(images=lowerCamelCase__ , annotations=lowerCamelCase__ , return_tensors="pt" ) # verify pixel values a__ : Optional[Any] = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , lowerCamelCase__ ) a__ : int = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , lowerCamelCase__ , atol=1E-4 ) ) # verify area a__ : List[Any] = torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , lowerCamelCase__ ) ) # verify boxes a__ : Any = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , lowerCamelCase__ ) a__ : int = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , lowerCamelCase__ , atol=1E-3 ) ) # verify image_id a__ : Any = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , lowerCamelCase__ ) ) # verify is_crowd a__ : Any = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , lowerCamelCase__ ) ) # verify class_labels a__ : int = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , lowerCamelCase__ ) ) # verify orig_size a__ : Union[str, Any] = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , lowerCamelCase__ ) ) # verify size a__ : List[Any] = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Optional[int] ): # prepare image, target and masks_path a__ : Union[str, Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: a__ : int = json.loads(f.read() ) a__ : Any = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} a__ : Dict = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them a__ : str = ConditionalDetrImageProcessor(format="coco_panoptic" ) a__ : Optional[int] = image_processing(images=lowerCamelCase__ , annotations=lowerCamelCase__ , masks_path=lowerCamelCase__ , return_tensors="pt" ) # verify pixel values a__ : int = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , lowerCamelCase__ ) a__ : Dict = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , lowerCamelCase__ , atol=1E-4 ) ) # verify area a__ : Optional[int] = torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , lowerCamelCase__ ) ) # verify boxes a__ : int = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , lowerCamelCase__ ) a__ : Dict = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , lowerCamelCase__ , atol=1E-3 ) ) # verify image_id a__ : Dict = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , lowerCamelCase__ ) ) # verify is_crowd a__ : Dict = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , lowerCamelCase__ ) ) # verify class_labels a__ : str = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , lowerCamelCase__ ) ) # verify masks a__ : Union[str, Any] = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , lowerCamelCase__ ) # verify orig_size a__ : int = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , lowerCamelCase__ ) ) # verify size a__ : Dict = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , lowerCamelCase__ ) )
37
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json', 'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json', 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json', 'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json', } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "funnel" lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self : int , _lowerCAmelCase : Optional[int]=30_522 , _lowerCAmelCase : List[str]=[4, 4, 4] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : Optional[int]=2 , _lowerCAmelCase : int=768 , _lowerCAmelCase : Optional[Any]=12 , _lowerCAmelCase : Optional[Any]=64 , _lowerCAmelCase : Optional[Any]=3_072 , _lowerCAmelCase : List[str]="gelu_new" , _lowerCAmelCase : Any=0.1 , _lowerCAmelCase : int=0.1 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : Dict=None , _lowerCAmelCase : str=1E-9 , _lowerCAmelCase : Any="mean" , _lowerCAmelCase : Union[str, Any]="relative_shift" , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Dict=True , _lowerCAmelCase : Tuple=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = block_sizes SCREAMING_SNAKE_CASE_ = [1] * len(_lowerCAmelCase ) if block_repeats is None else block_repeats assert len(_lowerCAmelCase ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." SCREAMING_SNAKE_CASE_ = num_decoder_layers SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = n_head SCREAMING_SNAKE_CASE_ = d_head SCREAMING_SNAKE_CASE_ = d_inner SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = initializer_std SCREAMING_SNAKE_CASE_ = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported." SCREAMING_SNAKE_CASE_ = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported." SCREAMING_SNAKE_CASE_ = attention_type SCREAMING_SNAKE_CASE_ = separate_cls SCREAMING_SNAKE_CASE_ = truncate_seq SCREAMING_SNAKE_CASE_ = pool_q_only super().__init__(**_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[Any] ): raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.' ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Union[str, Any] ): raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.' )
31
0
'''simple docstring''' def UpperCamelCase__ ( __magic_name__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: '''simple docstring''' snake_case__ : Tuple = set() # Replace all the whitespace in our sentence snake_case__ : List[Any] = input_str.replace(""" """ , """""" ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(__magic_name__ ) == 26 def UpperCamelCase__ ( __magic_name__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: '''simple docstring''' snake_case__ : Optional[Any] = [False] * 26 for char in input_str: if char.islower(): snake_case__ : int = True elif char.isupper(): snake_case__ : Optional[Any] = True return all(__magic_name__ ) def UpperCamelCase__ ( __magic_name__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: '''simple docstring''' return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def UpperCamelCase__ ( ) -> None: '''simple docstring''' from timeit import timeit snake_case__ : Optional[Any] = """from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest""" print(timeit("""is_pangram()""" , setup=__magic_name__ ) ) print(timeit("""is_pangram_faster()""" , setup=__magic_name__ ) ) print(timeit("""is_pangram_fastest()""" , setup=__magic_name__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
38
from __future__ import annotations from collections.abc import Iterator class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None class lowerCamelCase_ : '''simple docstring''' def __init__( self : int , _lowerCAmelCase : Node ): SCREAMING_SNAKE_CASE_ = tree def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Node | None ): if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : Dict ): yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
31
0
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): while a != 0: snake_case_, snake_case_ = b % a, a return b def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if gcd(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) != 1: snake_case_ = F'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(SCREAMING_SNAKE_CASE__ ) snake_case_, snake_case_, snake_case_ = 1, 0, a snake_case_, snake_case_, snake_case_ = 0, 1, m while va != 0: snake_case_ = ua // va snake_case_, snake_case_, snake_case_, snake_case_, snake_case_, snake_case_ = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
39
def UpperCAmelCase_ ( __UpperCAmelCase : list , __UpperCAmelCase : int , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 ) -> int: SCREAMING_SNAKE_CASE_ = right or len(__UpperCAmelCase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__UpperCAmelCase , __UpperCAmelCase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
0
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def UpperCamelCase ( snake_case__ : str ) -> None: UpperCamelCase , UpperCamelCase : Tuple = analyze_text(snake_case__ ) UpperCamelCase : Optional[Any] = list(' ' + ascii_lowercase ) # what is our total sum of probabilities. UpperCamelCase : List[str] = sum(single_char_strings.values() ) # one length string UpperCamelCase : Optional[Any] = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: UpperCamelCase : Tuple = single_char_strings[ch] UpperCamelCase : Any = my_str / all_sum my_fir_sum += prob * math.loga(snake_case__ ) # entropy formula. # print entropy print(F"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string UpperCamelCase : List[str] = sum(two_char_strings.values() ) UpperCamelCase : Union[str, Any] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: UpperCamelCase : List[Any] = cha + cha if sequence in two_char_strings: UpperCamelCase : List[str] = two_char_strings[sequence] UpperCamelCase : Dict = int(snake_case__ ) / all_sum my_sec_sum += prob * math.loga(snake_case__ ) # print second entropy print(F"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(F"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def UpperCamelCase ( snake_case__ : str ) -> tuple[dict, dict]: UpperCamelCase : List[str] = Counter() # type: ignore UpperCamelCase : Any = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(snake_case__ ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def UpperCamelCase ( ) -> Optional[int]: import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
40
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_fnet import FNetTokenizer else: lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase__ : List[str] = { 'vocab_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model', }, 'tokenizer_file': { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json', }, } lowerCamelCase__ : Optional[Any] = { 'google/fnet-base': 512, 'google/fnet-large': 512, } lowerCamelCase__ : List[Any] = '▁' class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "token_type_ids"] lowercase_ = FNetTokenizer def __init__( self : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[str]=False , _lowerCAmelCase : Optional[Any]=True , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="<unk>" , _lowerCAmelCase : Optional[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="<pad>" , _lowerCAmelCase : Optional[int]="[CLS]" , _lowerCAmelCase : Optional[Any]="[MASK]" , **_lowerCAmelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. SCREAMING_SNAKE_CASE_ = ( AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase , normalized=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) else mask_token ) super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , remove_space=_lowerCAmelCase , keep_accents=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCAmelCase ): copyfile(self.vocab_file , _lowerCAmelCase ) return (out_vocab_file,)
31
0