code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class _lowercase : def __init__( self: Tuple , UpperCamelCase__: Optional[int] , UpperCamelCase__: Optional[Any]=13 , UpperCamelCase__: Optional[Any]=2 , UpperCamelCase__: List[str]=24 , UpperCamelCase__: Optional[int]=16 , UpperCamelCase__: Union[str, Any]=True , UpperCamelCase__: Optional[int]=True , UpperCamelCase__: Any=32 , UpperCamelCase__: Tuple=5 , UpperCamelCase__: Union[str, Any]=4 , UpperCamelCase__: str=37 , UpperCamelCase__: Any="gelu" , UpperCamelCase__: Union[str, Any]=0.1 , UpperCamelCase__: Any=0.1 , UpperCamelCase__: str=10 , UpperCamelCase__: int=0.02 , UpperCamelCase__: str=None , UpperCamelCase__: Tuple=2 , UpperCamelCase__: Optional[Any]=2 , ): lowerCamelCase__ : Dict = parent lowerCamelCase__ : List[Any] = batch_size lowerCamelCase__ : List[str] = patch_size lowerCamelCase__ : Union[str, Any] = max_length lowerCamelCase__ : Union[str, Any] = num_mel_bins lowerCamelCase__ : Tuple = is_training lowerCamelCase__ : Union[str, Any] = use_labels lowerCamelCase__ : str = hidden_size lowerCamelCase__ : Dict = num_hidden_layers lowerCamelCase__ : Dict = num_attention_heads lowerCamelCase__ : int = intermediate_size lowerCamelCase__ : Optional[int] = hidden_act lowerCamelCase__ : List[str] = hidden_dropout_prob lowerCamelCase__ : str = attention_probs_dropout_prob lowerCamelCase__ : Union[str, Any] = type_sequence_label_size lowerCamelCase__ : Dict = initializer_range lowerCamelCase__ : Any = scope lowerCamelCase__ : Any = frequency_stride lowerCamelCase__ : Optional[Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) lowerCamelCase__ : Optional[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 lowerCamelCase__ : List[Any] = (self.max_length - self.patch_size) // self.time_stride + 1 lowerCamelCase__ : Dict = frequency_out_dimension * time_out_dimension lowerCamelCase__ : str = num_patches + 2 def lowerCamelCase_ ( self: Optional[int] ): lowerCamelCase__ : List[Any] = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) lowerCamelCase__ : Any = None if self.use_labels: lowerCamelCase__ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase__ : List[str] = self.get_config() return config, input_values, labels def lowerCamelCase_ ( self: Union[str, Any] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def lowerCamelCase_ ( self: Dict , UpperCamelCase__: Dict , UpperCamelCase__: Union[str, Any] , UpperCamelCase__: Tuple ): lowerCamelCase__ : int = ASTModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCamelCase__ : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase_ ( self: Any ): lowerCamelCase__ : Union[str, Any] = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Tuple = config_and_inputs lowerCamelCase__ : Tuple = {"""input_values""": input_values} return config, inputs_dict @require_torch class _lowercase ( _lowercase , _lowercase , unittest.TestCase ): a = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) a = ( {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel} if is_torch_available() else {} ) a = False a = False a = False a = False def lowerCamelCase_ ( self: str , UpperCamelCase__: Tuple , UpperCamelCase__: List[str] , UpperCamelCase__: List[str] , UpperCamelCase__: Optional[int] , UpperCamelCase__: Optional[int] ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def lowerCamelCase_ ( self: str ): lowerCamelCase__ : str = ASTModelTester(self ) lowerCamelCase__ : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def lowerCamelCase_ ( self: Any ): self.config_tester.run_common_tests() @unittest.skip(reason="""AST does not use inputs_embeds""" ) def lowerCamelCase_ ( self: Dict ): pass def lowerCamelCase_ ( self: List[Any] ): lowerCamelCase__ , lowerCamelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Any = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase__ : Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , nn.Linear ) ) def lowerCamelCase_ ( self: Any ): lowerCamelCase__ , lowerCamelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Tuple = model_class(UpperCamelCase__ ) lowerCamelCase__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : Tuple = [*signature.parameters.keys()] lowerCamelCase__ : List[Any] = ["""input_values"""] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def lowerCamelCase_ ( self: str ): lowerCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) @slow def lowerCamelCase_ ( self: List[str] ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : Union[str, Any] = ASTModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE_ () -> List[Any]: lowerCamelCase__ : Optional[Any] = hf_hub_download( repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" , filename="""sample_audio.flac""" , repo_type="""dataset""" ) lowerCamelCase__ , lowerCamelCase__ : List[Any] = torchaudio.load(UpperCamelCase ) return audio, sampling_rate @require_torch @require_torchaudio class _lowercase ( unittest.TestCase ): @cached_property def lowerCamelCase_ ( self: str ): return ( ASTFeatureExtractor.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593""" ) if is_torchaudio_available() else None ) @slow def lowerCamelCase_ ( self: Dict ): lowerCamelCase__ : int = self.default_feature_extractor lowerCamelCase__ : List[Any] = ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593""" ).to(UpperCamelCase__ ) lowerCamelCase__ : Union[str, Any] = self.default_feature_extractor lowerCamelCase__ , lowerCamelCase__ : str = prepare_audio() lowerCamelCase__ : Optional[int] = audio.squeeze().numpy() lowerCamelCase__ : Optional[int] = feature_extractor(UpperCamelCase__ , sampling_rate=UpperCamelCase__ , return_tensors="""pt""" ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): lowerCamelCase__ : Union[str, Any] = model(**UpperCamelCase__ ) # verify the logits lowerCamelCase__ : Tuple = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) lowerCamelCase__ : Optional[int] = torch.tensor([-0.8_760, -7.0_042, -8.6_602] ).to(UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCamelCase__ , atol=1e-4 ) )
41
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class lowercase ( UpperCamelCase__ ): _a = 42 class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a , _a , _a , ) -> List[Any]: super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: if latents is None: _A : str = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) _A : Union[str, Any] = latents.to(_a ) _A : int = latents * scheduler.init_noise_sigma return latents def a__ ( self , _a=0 ) -> Optional[Any]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _A : str = torch.device(F'''cuda:{gpu_id}''' ) _A : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def a__ ( self ) -> List[Any]: if self.device != torch.device("""meta""" ) or not hasattr(self.image_encoder , """_hf_hook""" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , """_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 def a__ ( self , _a , _a , _a , _a , ) -> Tuple: if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): _A : int = torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): _A : Dict = self.image_processor(_a , return_tensors="""pt""" ).pixel_values[0].unsqueeze(0 ) _A : int = image.to(dtype=self.image_encoder.dtype , device=_a ) _A : List[Any] = self.image_encoder(_a )["""last_hidden_state"""] _A : List[Any] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 _A : Dict = image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: _A : str = torch.zeros_like(_a ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _A : List[str] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a = 1 , _a = 25 , _a = None , _a = None , _a = 4.0 , _a = 64 , _a = "pil" , _a = True , ) -> Union[str, Any]: if isinstance(_a , PIL.Image.Image ): _A : List[Any] = 1 elif isinstance(_a , torch.Tensor ): _A : Any = image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): _A : Union[str, Any] = len(_a ) else: raise ValueError( F'''`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}''' ) _A : Optional[int] = self._execution_device _A : Tuple = batch_size * num_images_per_prompt _A : List[Any] = guidance_scale > 1.0 _A : Optional[Any] = self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) _A : Optional[int] = self.scheduler.timesteps _A : List[str] = self.prior.config.num_embeddings _A : int = self.prior.config.embedding_dim _A : Optional[Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim _A : List[Any] = latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance _A : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _A : int = self.scheduler.scale_model_input(_a , _a ) _A : Tuple = self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance _A , _A : Optional[Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: _A , _A : Dict = noise_pred.chunk(2 ) _A : Tuple = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) _A : int = self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) _A : List[str] = [] for i, latent in enumerate(_a ): print() _A : List[str] = self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(_a ) _A : List[Any] = torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}''' ) _A : List[str] = images.cpu().numpy() if output_type == "pil": _A : List[Any] = [self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , """final_offload_hook""" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
26
0
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( __A , __A = None , __A = None ) -> None: if start is None: _snake_case = 0 if end is None: _snake_case = len(__A ) - 1 if start >= end: return _snake_case = (start + end) // 2 slowsort(__A , __A , __A ) slowsort(__A , mid + 1 , __A ) if sequence[end] < sequence[mid]: _snake_case , _snake_case = sequence[mid], sequence[end] slowsort(__A , __A , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
42
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( snake_case_ ): print("""Loading config file...""" ) def flatten_yaml_as_dict(snake_case_,snake_case_="",snake_case_="." ): _A : Union[str, Any] = [] for k, v in d.items(): _A : Optional[int] = parent_key + sep + k if parent_key else k if isinstance(snake_case_,collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(snake_case_,snake_case_,sep=snake_case_ ).items() ) else: items.append((new_key, v) ) return dict(snake_case_ ) _A : List[Any] = argparse.Namespace() with open(snake_case_,"""r""" ) as yaml_file: try: _A : List[Any] = yaml.load(snake_case_,Loader=yaml.FullLoader ) _A : Optional[int] = flatten_yaml_as_dict(snake_case_ ) for k, v in flat_cfg.items(): setattr(snake_case_,snake_case_,snake_case_ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(snake_case_,str(snake_case_ ) ) ) return config def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[Any] = MobileViTVaConfig() _A : Tuple = False # dataset if task_name.startswith("""imagenet1k_""" ): _A : Dict = 1000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : int = 384 else: _A : int = 256 _A : List[str] = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _A : Union[str, Any] = 21000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : str = 384 else: _A : List[Any] = 256 _A : List[str] = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _A : int = 151 _A : int = 512 _A : Optional[int] = """ade20k-id2label.json""" _A : Any = True elif task_name.startswith("""voc_""" ): _A : List[Any] = 21 _A : Dict = 512 _A : Dict = """pascal-voc-id2label.json""" _A : int = True # orig_config _A : Any = load_orig_config_file(snake_case_ ) assert getattr(snake_case_,"""model.classification.name""",-1 ) == "mobilevit_v2", "Invalid model" _A : List[Any] = getattr(snake_case_,"""model.classification.mitv2.width_multiplier""",1.0 ) assert ( getattr(snake_case_,"""model.classification.mitv2.attn_norm_layer""",-1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _A : str = getattr(snake_case_,"""model.classification.activation.name""","""swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _A : Optional[int] = getattr(snake_case_,"""model.segmentation.output_stride""",16 ) if "_deeplabv3" in task_name: _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_rates""",[12, 24, 36] ) _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_out_channels""",512 ) _A : str = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_dropout""",0.1 ) # id2label _A : List[Any] = """huggingface/label-files""" _A : List[Any] = json.load(open(hf_hub_download(snake_case_,snake_case_,repo_type="""dataset""" ),"""r""" ) ) _A : str = {int(snake_case_ ): v for k, v in idalabel.items()} _A : str = idalabel _A : Dict = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Any = dct.pop(snake_case_ ) _A : Union[str, Any] = val def lowerCAmelCase_ ( snake_case_,snake_case_=False ): if base_model: _A : Optional[int] = """""" else: _A : Dict = """mobilevitv2.""" _A : int = [] for k in state_dict.keys(): if k[:8] == "encoder.": _A : Any = k[8:] else: _A : List[str] = k if ".block." in k: _A : Any = k_new.replace(""".block.""",""".""" ) if ".conv." in k: _A : List[Any] = k_new.replace(""".conv.""",""".convolution.""" ) if ".norm." in k: _A : Any = k_new.replace(""".norm.""",""".normalization.""" ) if "conv_1." in k: _A : int = k_new.replace("""conv_1.""",f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.''',f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: _A : Tuple = k_new.replace(""".exp_1x1.""",""".expand_1x1.""" ) if ".red_1x1." in k: _A : Optional[int] = k_new.replace(""".red_1x1.""",""".reduce_1x1.""" ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: _A : Optional[int] = k_new.replace(f'''layer_{i}.0.''',f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: _A : Union[str, Any] = k_new.replace(f'''layer_{i}.1.local_rep.0.''',f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: _A : str = k_new.replace(f'''layer_{i}.1.local_rep.1.''',f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: _A : Optional[int] = [0, 1] elif i == 4: _A : Union[str, Any] = [0, 1, 2, 3] elif i == 5: _A : Optional[Any] = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: _A : Union[str, Any] = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''',f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: _A : List[str] = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''',f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.1.conv_proj.''',f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_attn.0.""","""layernorm_before.""" ) if "pre_norm_attn.1." in k: _A : str = k_new.replace("""pre_norm_attn.1.""","""attention.""" ) if "pre_norm_ffn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_ffn.0.""","""layernorm_after.""" ) if "pre_norm_ffn.1." in k: _A : Dict = k_new.replace("""pre_norm_ffn.1.""","""ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _A : List[str] = k_new.replace("""pre_norm_ffn.3.""","""ffn.conv2.""" ) if "classifier.1." in k: _A : List[str] = k_new.replace("""classifier.1.""","""classifier.""" ) if "seg_head." in k: _A : List[Any] = k_new.replace("""seg_head.""","""segmentation_head.""" ) if ".aspp_layer." in k: _A : List[Any] = k_new.replace(""".aspp_layer.""",""".""" ) if ".aspp_pool." in k: _A : Optional[Any] = k_new.replace(""".aspp_pool.""",""".""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(snake_case_ ) for k in keys_to_ignore: state_dict.pop(snake_case_,snake_case_ ) def lowerCAmelCase_ ( ): _A : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _A : List[Any] = Image.open(requests.get(snake_case_,stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[Any] = get_mobilevitva_config(snake_case_,snake_case_ ) # load original state_dict _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _A : Optional[Any] = MobileViTVaForSemanticSegmentation(snake_case_ ).eval() _A : str = False else: _A : int = MobileViTVaForImageClassification(snake_case_ ).eval() _A : List[Any] = False # remove and rename some keys of load the original model _A : List[Any] = checkpoint remove_unused_keys(snake_case_ ) _A : Optional[Any] = create_rename_keys(snake_case_,base_model=snake_case_ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case_,snake_case_,snake_case_ ) # load modified state_dict model.load_state_dict(snake_case_ ) # Check outputs on an image, prepared by MobileViTImageProcessor _A : str = MobileViTImageProcessor(crop_size=config.image_size,size=config.image_size + 32 ) _A : List[Any] = image_processor(images=prepare_img(),return_tensors="""pt""" ) _A : Optional[Any] = model(**snake_case_ ) # verify classification model if task_name.startswith("""imagenet""" ): _A : List[Any] = outputs.logits _A : Optional[int] = logits.argmax(-1 ).item() print("""Predicted class:""",model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _A : int = torch.tensor([-1.63_36e00, -7.32_04e-02, -5.18_83e-01] ) assert torch.allclose(logits[0, :3],snake_case_,atol=1e-4 ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you'd like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) _snake_case = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
26
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase = logging.get_logger(__name__) __lowercase = { '''SCUT-DLVCLab/lilt-roberta-en-base''': ( '''https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json''' ), } class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : Tuple = """lilt""" def __init__( self , __lowercase=30_522 , __lowercase=768 , __lowercase=12 , __lowercase=12 , __lowercase=3_072 , __lowercase="gelu" , __lowercase=0.1 , __lowercase=0.1 , __lowercase=512 , __lowercase=2 , __lowercase=0.02 , __lowercase=1E-1_2 , __lowercase=0 , __lowercase="absolute" , __lowercase=None , __lowercase=4 , __lowercase=1_024 , **__lowercase , ) -> int: super().__init__(pad_token_id=__lowercase , **__lowercase) __UpperCamelCase :int = vocab_size __UpperCamelCase :int = hidden_size __UpperCamelCase :Tuple = num_hidden_layers __UpperCamelCase :Optional[Any] = num_attention_heads __UpperCamelCase :Tuple = hidden_act __UpperCamelCase :Any = intermediate_size __UpperCamelCase :int = hidden_dropout_prob __UpperCamelCase :str = attention_probs_dropout_prob __UpperCamelCase :Tuple = max_position_embeddings __UpperCamelCase :Tuple = type_vocab_size __UpperCamelCase :Any = initializer_range __UpperCamelCase :List[str] = layer_norm_eps __UpperCamelCase :str = position_embedding_type __UpperCamelCase :List[Any] = classifier_dropout __UpperCamelCase :Optional[Any] = channel_shrink_ratio __UpperCamelCase :int = max_ad_position_embeddings
43
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
0
"""simple docstring""" import logging import random import ray from transformers import RagConfig, RagRetriever, RagTokenizer from transformers.models.rag.retrieval_rag import CustomHFIndex _a : Any = logging.getLogger(__name__) class __A : def __init__( self ): _lowerCAmelCase : Tuple = False def __A ( self , a__ , a__ , a__ , a__ ): if not self.initialized: _lowerCAmelCase : Any = RagRetriever( a__ , question_encoder_tokenizer=a__ , generator_tokenizer=a__ , index=a__ , init_retrieval=a__ , ) _lowerCAmelCase : Optional[int] = True def __A ( self ): self.retriever.index.init_index() def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : Any = self.retriever._main_retrieve(a__ , a__ ) return doc_ids, retrieved_doc_embeds class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__ , a__ , a__ , a__=None ): if index is not None and index.is_initialized() and len(a__ ) > 0: raise ValueError( """When using Ray for distributed fine-tuning, """ """you'll need to provide the paths instead, """ """as the dataset and the index are loaded """ """separately. More info in examples/rag/use_own_knowledge_dataset.py """ ) super().__init__( a__ , question_encoder_tokenizer=a__ , generator_tokenizer=a__ , index=a__ , init_retrieval=a__ , ) _lowerCAmelCase : Union[str, Any] = retrieval_workers if len(self.retrieval_workers ) > 0: ray.get( [ worker.create_rag_retriever.remote(a__ , a__ , a__ , a__ ) for worker in self.retrieval_workers ] ) def __A ( self ): logger.info("""initializing retrieval""" ) if len(self.retrieval_workers ) > 0: ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers] ) else: # Non-distributed training. Load index into this same process. self.index.init_index() def __A ( self , a__ , a__ ): if len(self.retrieval_workers ) > 0: # Select a random retrieval actor. _lowerCAmelCase : List[str] = self.retrieval_workers[random.randint(0 , len(self.retrieval_workers ) - 1 )] _lowerCAmelCase , _lowerCAmelCase : Tuple = ray.get(random_worker.retrieve.remote(a__ , a__ ) ) else: _lowerCAmelCase , _lowerCAmelCase : Any = self._main_retrieve(a__ , a__ ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(a__ ) @classmethod def __A ( cls , a__ , a__=None , **a__ ): return super(a__ , cls ).get_tokenizers(a__ , a__ , **a__ ) @classmethod def __A ( cls , a__ , a__ , a__=None , **a__ ): _lowerCAmelCase : str = kwargs.pop("""config""" , a__ ) or RagConfig.from_pretrained(a__ , **a__ ) _lowerCAmelCase : Tuple = RagTokenizer.from_pretrained(a__ , config=a__ ) _lowerCAmelCase : str = rag_tokenizer.question_encoder _lowerCAmelCase : Union[str, Any] = rag_tokenizer.generator if indexed_dataset is not None: _lowerCAmelCase : Any = """custom""" _lowerCAmelCase : Any = CustomHFIndex(config.retrieval_vector_size , a__ ) else: _lowerCAmelCase : Union[str, Any] = cls._build_index(a__ ) return cls( a__ , question_encoder_tokenizer=a__ , generator_tokenizer=a__ , retrieval_workers=a__ , index=a__ , )
44
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = 1 @register_to_config def __init__( self , _a=2000 , _a=0.1 , _a=20 , _a=1e-3 ) -> List[Any]: _A : Dict = None _A : List[Any] = None _A : Dict = None def a__ ( self , _a , _a = None ) -> Union[str, Any]: _A : Union[str, Any] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def a__ ( self , _a , _a , _a , _a=None ) -> Dict: if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _A : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _A : List[Any] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _A : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): _A : List[Any] = std.unsqueeze(-1 ) _A : int = -score / std # compute _A : Tuple = -1.0 / len(self.timesteps ) _A : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _A : List[str] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _A : Union[str, Any] = beta_t.unsqueeze(-1 ) _A : Tuple = -0.5 * beta_t * x _A : Tuple = torch.sqrt(_a ) _A : Dict = drift - diffusion**2 * score _A : Dict = x + drift * dt # add noise _A : Any = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) _A : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Optional[Any]: return self.config.num_train_timesteps
26
0
"""simple docstring""" from __future__ import annotations import math def lowercase ( lowerCAmelCase__ : int ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( lowerCAmelCase__ : int ) -> list[int]: __a = str(lowerCAmelCase__ ) __a = [n] for i in range(1 , len(lowerCAmelCase__ ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def lowercase ( lowerCAmelCase__ : int ) -> bool: if len(str(lowerCAmelCase__ ) ) > 3: if not is_prime(int(str(lowerCAmelCase__ )[-3:] ) ) or not is_prime(int(str(lowerCAmelCase__ )[:3] ) ): return False return True def lowercase ( lowerCAmelCase__ : int = 11 ) -> list[int]: __a = [] __a = 13 while len(lowerCAmelCase__ ) != count: if validate(lowerCAmelCase__ ): __a = list_truncated_nums(lowerCAmelCase__ ) if all(is_prime(lowerCAmelCase__ ) for i in list_nums ): list_truncated_primes.append(lowerCAmelCase__ ) num += 2 return list_truncated_primes def lowercase ( ) -> int: return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(F'''{sum(compute_truncated_primes(1_1)) = }''')
45
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "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", }, } _snake_case = { "google/fnet-base": 512, "google/fnet-large": 512, } _snake_case = "▁" class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["input_ids", "token_type_ids"] _a = FNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=True , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Optional[int]: # 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. _A : int = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) _A : Optional[int] = do_lower_case _A : List[Any] = remove_space _A : str = keep_accents _A : int = vocab_file _A : int = False if not self.vocab_file else True def a__ ( self , _a , _a = None ) -> List[int]: _A : str = [self.sep_token_id] _A : Dict = [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 a__ ( self , _a , _a = None ) -> List[int]: _A : Any = [self.sep_token_id] _A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : List[str] = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
26
0
"""simple docstring""" from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE__ = "\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n" SCREAMING_SNAKE_CASE__ = "\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric(\"mean_iou\")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {'mean_iou': 0.47750000000000004, 'mean_accuracy': 0.5916666666666666, 'overall_accuracy': 0.5263157894736842, 'per_category_iou': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), 'per_category_accuracy': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n" SCREAMING_SNAKE_CASE__ = "\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}" def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : bool , SCREAMING_SNAKE_CASE : Optional[Dict[int, int]] = None , SCREAMING_SNAKE_CASE : bool = False , ): '''simple docstring''' if label_map is not None: for old_id, new_id in label_map.items(): lowerCAmelCase = new_id # turn into Numpy arrays lowerCAmelCase = np.array(SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.array(SCREAMING_SNAKE_CASE ) if reduce_labels: lowerCAmelCase = 2_55 lowerCAmelCase = label - 1 lowerCAmelCase = 2_55 lowerCAmelCase = label != ignore_index lowerCAmelCase = np.not_equal(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase = pred_label[mask] lowerCAmelCase = np.array(SCREAMING_SNAKE_CASE )[mask] lowerCAmelCase = pred_label[pred_label == label] lowerCAmelCase = np.histogram(SCREAMING_SNAKE_CASE , bins=SCREAMING_SNAKE_CASE , range=(0, num_labels - 1) )[0] lowerCAmelCase = np.histogram(SCREAMING_SNAKE_CASE , bins=SCREAMING_SNAKE_CASE , range=(0, num_labels - 1) )[0] lowerCAmelCase = np.histogram(SCREAMING_SNAKE_CASE , bins=SCREAMING_SNAKE_CASE , range=(0, num_labels - 1) )[0] lowerCAmelCase = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : bool , SCREAMING_SNAKE_CASE : Optional[Dict[int, int]] = None , SCREAMING_SNAKE_CASE : bool = False , ): '''simple docstring''' lowerCAmelCase = np.zeros((num_labels,) , dtype=np.floataa ) lowerCAmelCase = np.zeros((num_labels,) , dtype=np.floataa ) lowerCAmelCase = np.zeros((num_labels,) , dtype=np.floataa ) lowerCAmelCase = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = intersect_and_union( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : bool , SCREAMING_SNAKE_CASE : Optional[int] = None , SCREAMING_SNAKE_CASE : Optional[Dict[int, int]] = None , SCREAMING_SNAKE_CASE : bool = False , ): '''simple docstring''' lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = total_intersect_and_union( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # compute metrics lowerCAmelCase = {} lowerCAmelCase = total_area_intersect.sum() / total_area_label.sum() lowerCAmelCase = total_area_intersect / total_area_union lowerCAmelCase = total_area_intersect / total_area_label lowerCAmelCase = np.nanmean(SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.nanmean(SCREAMING_SNAKE_CASE ) lowerCAmelCase = all_acc lowerCAmelCase = iou lowerCAmelCase = acc if nan_to_num is not None: lowerCAmelCase = {metric: np.nan_to_num(SCREAMING_SNAKE_CASE , nan=SCREAMING_SNAKE_CASE ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def _snake_case ( self ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { """predictions""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), """references""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), } ) , reference_urls=[ """https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py""" ] , ) def _snake_case ( self , lowercase , lowercase , lowercase , lowercase , lowercase = None , lowercase = None , lowercase = False , ) -> Tuple: lowerCAmelCase = mean_iou( results=lowercase , gt_seg_maps=lowercase , num_labels=lowercase , ignore_index=lowercase , nan_to_num=lowercase , label_map=lowercase , reduce_labels=lowercase , ) return iou_result
46
from math import asin, atan, cos, radians, sin, sqrt, tan _snake_case = 6_3_7_8_1_3_7.0 _snake_case = 6_3_5_6_7_5_2.3_1_4_2_4_5 _snake_case = 6378137 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Any = (AXIS_A - AXIS_B) / AXIS_A _A : Optional[int] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : List[str] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : Optional[Any] = radians(snake_case_ ) _A : str = radians(snake_case_ ) # Equation _A : Dict = sin((phi_a - phi_a) / 2 ) _A : List[str] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda _A : Optional[int] = sqrt(sin_sq_phi + (cos(snake_case_ ) * cos(snake_case_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
26
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings lowerCamelCase : List[Any] = r"\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `\" / \"`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `\" // \"`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `\"train\"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `\"compressed\"`)\n The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and\n `\"compressed\"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a \"dummy\" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n" @add_start_docstrings(A__ ) class A__ ( A__ ): A__ = 'rag' A__ = True def __init__( self : Optional[Any] , _a : Union[str, Any]=None , _a : Optional[Any]=True , _a : Any=None , _a : Dict=None , _a : Optional[Any]=None , _a : Tuple=None , _a : List[Any]=None , _a : Optional[Any]=" / " , _a : str=" // " , _a : Tuple=5 , _a : Optional[int]=300 , _a : Optional[Any]=768 , _a : Union[str, Any]=8 , _a : Dict="wiki_dpr" , _a : Tuple="train" , _a : Any="compressed" , _a : Union[str, Any]=None , _a : Optional[int]=None , _a : Optional[int]=False , _a : Any=False , _a : str=0.0 , _a : Optional[int]=True , _a : Optional[int]=False , _a : Optional[int]=False , _a : int=False , _a : Any=True , _a : Union[str, Any]=None , **_a : List[str] , ) -> str: '''simple docstring''' super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _SCREAMING_SNAKE_CASE =kwargs.pop('question_encoder' ) _SCREAMING_SNAKE_CASE =question_encoder_config.pop('model_type' ) _SCREAMING_SNAKE_CASE =kwargs.pop('generator' ) _SCREAMING_SNAKE_CASE =decoder_config.pop('model_type' ) from ..auto.configuration_auto import AutoConfig _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =reduce_loss _SCREAMING_SNAKE_CASE =label_smoothing _SCREAMING_SNAKE_CASE =exclude_bos_score _SCREAMING_SNAKE_CASE =do_marginalize _SCREAMING_SNAKE_CASE =title_sep _SCREAMING_SNAKE_CASE =doc_sep _SCREAMING_SNAKE_CASE =n_docs _SCREAMING_SNAKE_CASE =max_combined_length _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =dataset_split _SCREAMING_SNAKE_CASE =index_name _SCREAMING_SNAKE_CASE =retrieval_vector_size _SCREAMING_SNAKE_CASE =retrieval_batch_size _SCREAMING_SNAKE_CASE =passages_path _SCREAMING_SNAKE_CASE =index_path _SCREAMING_SNAKE_CASE =use_dummy_dataset _SCREAMING_SNAKE_CASE =output_retrieved _SCREAMING_SNAKE_CASE =do_deduplication _SCREAMING_SNAKE_CASE =use_cache if self.forced_eos_token_id is None: _SCREAMING_SNAKE_CASE =getattr(self.generator , 'forced_eos_token_id' , _a ) @classmethod def A ( cls : List[str] , _a : PretrainedConfig , _a : PretrainedConfig , **_a : Tuple ) -> PretrainedConfig: '''simple docstring''' return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def A ( self : int ) -> List[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =copy.deepcopy(self.__dict__ ) _SCREAMING_SNAKE_CASE =self.question_encoder.to_dict() _SCREAMING_SNAKE_CASE =self.generator.to_dict() _SCREAMING_SNAKE_CASE =self.__class__.model_type return output
47
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> Optional[int]: super().__init__(_a ) _A : Union[str, Any] = RobertaEmbeddings(_a ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> str: super().__init__(_a ) _A : Any = config.num_labels _A : Dict = config.num_hidden_layers _A : List[str] = DeeRobertaModel(_a ) _A : int = nn.Dropout(config.hidden_dropout_prob ) _A : int = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_a ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=-1 , _a=False , ) -> Any: _A : Optional[int] = self.num_layers try: _A : List[str] = self.roberta( _a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , ) _A : List[str] = outputs[1] _A : List[str] = self.dropout(_a ) _A : Optional[Any] = self.classifier(_a ) _A : List[Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: _A : List[Any] = e.message _A : Optional[int] = e.exit_layer _A : Optional[int] = outputs[0] if not self.training: _A : int = entropy(_a ) _A : int = [] _A : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression _A : Union[str, Any] = MSELoss() _A : Tuple = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Dict = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits _A : Optional[Any] = [] for highway_exit in outputs[-1]: _A : Tuple = highway_exit[0] if not self.training: highway_logits_all.append(_a ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression _A : List[str] = MSELoss() _A : Optional[int] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Tuple = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_a ) if train_highway: _A : Dict = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: _A : int = (loss,) + outputs if not self.training: _A : Optional[Any] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: _A : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
0
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
48
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" def __init__( self , _a=3_0522 , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __snake_case :Optional[Any] = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case :Optional[int] = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys __snake_case :Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
49
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) if n == 0: return 0 _A : Tuple = float("""-inf""" ) for i in range(1,n + 1 ): _A : str = max( snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_ ) ) return max_revue def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) _A : Dict = [float("""-inf""" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: _A : List[str] = float("""-inf""" ) for i in range(1,n + 1 ): _A : Optional[Any] = max( snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_ ),) _A : Tuple = max_revenue return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. _A : List[Any] = [float("""-inf""" ) for _ in range(n + 1 )] _A : Any = 0 for i in range(1,n + 1 ): _A : Optional[Any] = max_rev[i] for j in range(1,i + 1 ): _A : int = max(snake_case_,prices[j - 1] + max_rev[i - j] ) _A : int = max_revenue_i return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): if n < 0: _A : Optional[Any] = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(snake_case_ ) if n > len(snake_case_ ): _A : Any = ( """Each integral piece of rod must have a corresponding price. """ f'''Got n = {n} but length of prices = {len(snake_case_ )}''' ) raise ValueError(snake_case_ ) def lowerCAmelCase_ ( ): _A : Tuple = [6, 10, 12, 15, 20, 23] _A : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. _A : Any = 36 _A : List[Any] = top_down_cut_rod(snake_case_,snake_case_ ) _A : List[Any] = bottom_up_cut_rod(snake_case_,snake_case_ ) _A : Dict = naive_cut_rod_recursive(snake_case_,snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
0
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase="shi-labs/oneformer_demo" ) -> List[Any]: with open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type='dataset' ) , 'r' ) as f: lowerCamelCase__ : str = json.load(_UpperCAmelCase ) lowerCamelCase__ : Tuple = {} lowerCamelCase__ : List[Any] = [] lowerCamelCase__ : str = [] for key, info in class_info.items(): lowerCamelCase__ : Union[str, Any] = info['name'] class_names.append(info['name'] ) if info["isthing"]: thing_ids.append(int(_UpperCAmelCase ) ) lowerCamelCase__ : Optional[int] = thing_ids lowerCamelCase__ : Union[str, Any] = class_names return metadata class lowerCAmelCase ( unittest.TestCase ): def __init__( self : List[str] , UpperCAmelCase : Optional[int] , UpperCAmelCase : Optional[int]=7 , UpperCAmelCase : Union[str, Any]=3 , UpperCAmelCase : int=30 , UpperCAmelCase : List[str]=400 , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Any=True , UpperCAmelCase : List[Any]=True , UpperCAmelCase : Dict=[0.5, 0.5, 0.5] , UpperCAmelCase : List[str]=[0.5, 0.5, 0.5] , UpperCAmelCase : Optional[Any]=10 , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[int]=255 , UpperCAmelCase : Any="shi-labs/oneformer_demo" , UpperCAmelCase : Any="ade20k_panoptic.json" , UpperCAmelCase : List[Any]=10 , ) -> Union[str, Any]: lowerCamelCase__ : Tuple = parent lowerCamelCase__ : Tuple = batch_size lowerCamelCase__ : str = num_channels lowerCamelCase__ : Union[str, Any] = min_resolution lowerCamelCase__ : int = max_resolution lowerCamelCase__ : Dict = do_resize lowerCamelCase__ : Optional[int] = {'shortest_edge': 32, 'longest_edge': 1333} if size is None else size lowerCamelCase__ : Dict = do_normalize lowerCamelCase__ : Tuple = image_mean lowerCamelCase__ : List[str] = image_std lowerCamelCase__ : Any = class_info_file lowerCamelCase__ : Any = prepare_metadata(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : Optional[int] = num_text lowerCamelCase__ : List[str] = repo_path # for the post_process_functions lowerCamelCase__ : Any = 2 lowerCamelCase__ : str = 10 lowerCamelCase__ : str = 10 lowerCamelCase__ : Any = 3 lowerCamelCase__ : Union[str, Any] = 4 lowerCamelCase__ : Any = num_labels lowerCamelCase__ : str = do_reduce_labels lowerCamelCase__ : str = ignore_index def A_ ( self : Union[str, Any] ) -> Any: return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def A_ ( self : Optional[int] , UpperCAmelCase : List[str] , UpperCAmelCase : List[Any]=False ) -> int: if not batched: lowerCamelCase__ : List[str] = image_inputs[0] if isinstance(UpperCAmelCase , Image.Image ): lowerCamelCase__ , lowerCamelCase__ : Tuple = image.size else: lowerCamelCase__ , lowerCamelCase__ : Dict = image.shape[1], image.shape[2] if w < h: lowerCamelCase__ : Dict = int(self.size['shortest_edge'] * h / w ) lowerCamelCase__ : List[Any] = self.size['shortest_edge'] elif w > h: lowerCamelCase__ : Optional[Any] = self.size['shortest_edge'] lowerCamelCase__ : str = int(self.size['shortest_edge'] * w / h ) else: lowerCamelCase__ : str = self.size['shortest_edge'] lowerCamelCase__ : Union[str, Any] = self.size['shortest_edge'] else: lowerCamelCase__ : Any = [] for image in image_inputs: lowerCamelCase__ , lowerCamelCase__ : List[str] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) lowerCamelCase__ : Optional[Any] = max(UpperCAmelCase , key=lambda UpperCAmelCase : item[0] )[0] lowerCamelCase__ : str = max(UpperCAmelCase , key=lambda UpperCAmelCase : item[1] )[1] return expected_height, expected_width def A_ ( self : Tuple ) -> Tuple: return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string UpperCAmelCase__ = image_processing_class def A_ ( self : Any ) -> int: lowerCamelCase__ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def A_ ( self : str ) -> int: return self.image_processing_tester.prepare_image_processor_dict() def A_ ( self : int ) -> Any: lowerCamelCase__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase , 'image_mean' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'image_std' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_normalize' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_resize' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'size' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'ignore_index' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'class_info_file' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'num_text' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'repo_path' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'metadata' ) ) self.assertTrue(hasattr(UpperCAmelCase , 'do_reduce_labels' ) ) def A_ ( self : str ) -> List[Any]: pass def A_ ( self : Tuple ) -> Union[str, Any]: # Initialize image_processor lowerCamelCase__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCamelCase__ : int = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , Image.Image ) # Test not batched input lowerCamelCase__ : List[str] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values lowerCamelCase__ , lowerCamelCase__ : str = self.image_processing_tester.get_expected_values(UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCamelCase__ , lowerCamelCase__ : Tuple = self.image_processing_tester.get_expected_values(UpperCAmelCase , batched=UpperCAmelCase ) lowerCamelCase__ : List[str] = image_processor( UpperCAmelCase , ['semantic'] * len(UpperCAmelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def A_ ( self : Tuple ) -> str: # Initialize image_processor lowerCamelCase__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCamelCase__ : Union[str, Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCAmelCase , numpify=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , np.ndarray ) # Test not batched input lowerCamelCase__ : List[str] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values lowerCamelCase__ , lowerCamelCase__ : List[str] = self.image_processing_tester.get_expected_values(UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCamelCase__ , lowerCamelCase__ : List[str] = self.image_processing_tester.get_expected_values(UpperCAmelCase , batched=UpperCAmelCase ) lowerCamelCase__ : str = image_processor( UpperCAmelCase , ['semantic'] * len(UpperCAmelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def A_ ( self : Optional[int] ) -> Union[str, Any]: # Initialize image_processor lowerCamelCase__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCamelCase__ : Union[str, Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCAmelCase , torchify=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , torch.Tensor ) # Test not batched input lowerCamelCase__ : Union[str, Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values lowerCamelCase__ , lowerCamelCase__ : str = self.image_processing_tester.get_expected_values(UpperCAmelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCamelCase__ , lowerCamelCase__ : int = self.image_processing_tester.get_expected_values(UpperCAmelCase , batched=UpperCAmelCase ) lowerCamelCase__ : int = image_processor( UpperCAmelCase , ['semantic'] * len(UpperCAmelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def A_ ( self : int , UpperCAmelCase : List[str]=False , UpperCAmelCase : List[Any]=False , UpperCAmelCase : Union[str, Any]="np" ) -> str: lowerCamelCase__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # prepare image and target lowerCamelCase__ : Dict = self.image_processing_tester.num_labels lowerCamelCase__ : List[str] = None lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : str = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCAmelCase ) if with_segmentation_maps: lowerCamelCase__ : Tuple = num_labels if is_instance_map: lowerCamelCase__ : Dict = list(range(UpperCAmelCase ) ) * 2 lowerCamelCase__ : Optional[int] = dict(enumerate(UpperCAmelCase ) ) lowerCamelCase__ : int = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": lowerCamelCase__ : Optional[int] = [Image.fromarray(UpperCAmelCase ) for annotation in annotations] lowerCamelCase__ : List[str] = image_processor( UpperCAmelCase , ['semantic'] * len(UpperCAmelCase ) , UpperCAmelCase , return_tensors='pt' , instance_id_to_semantic_id=UpperCAmelCase , pad_and_return_pixel_mask=UpperCAmelCase , ) return inputs def A_ ( self : str ) -> Any: pass def A_ ( self : Tuple ) -> List[Any]: def common(UpperCAmelCase : Union[str, Any]=False , UpperCAmelCase : Optional[Any]=None ): lowerCamelCase__ : Any = self.comm_get_image_processor_inputs( with_segmentation_maps=UpperCAmelCase , is_instance_map=UpperCAmelCase , segmentation_type=UpperCAmelCase ) lowerCamelCase__ : Tuple = inputs['mask_labels'] lowerCamelCase__ : Union[str, Any] = inputs['class_labels'] lowerCamelCase__ : Optional[Any] = inputs['pixel_values'] lowerCamelCase__ : List[Any] = inputs['text_inputs'] # check the batch_size for mask_label, class_label, text_input in zip(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(UpperCAmelCase ) , self.image_processing_tester.num_text ) common() common(is_instance_map=UpperCAmelCase ) common(is_instance_map=UpperCAmelCase , segmentation_type='pil' ) common(is_instance_map=UpperCAmelCase , segmentation_type='pil' ) def A_ ( self : Optional[int] ) -> Any: lowerCamelCase__ : Dict = np.zeros((20, 50) ) lowerCamelCase__ : List[Any] = 1 lowerCamelCase__ : Dict = 1 lowerCamelCase__ : Optional[int] = 1 lowerCamelCase__ : Union[str, Any] = binary_mask_to_rle(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def A_ ( self : Union[str, Any] ) -> str: lowerCamelCase__ : str = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) lowerCamelCase__ : Tuple = self.image_processing_tester.get_fake_oneformer_outputs() lowerCamelCase__ : Any = fature_extractor.post_process_semantic_segmentation(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) lowerCamelCase__ : Any = [(1, 4) for i in range(self.image_processing_tester.batch_size )] lowerCamelCase__ : Dict = fature_extractor.post_process_semantic_segmentation(UpperCAmelCase , target_sizes=UpperCAmelCase ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def A_ ( self : List[str] ) -> List[str]: lowerCamelCase__ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) lowerCamelCase__ : Union[str, Any] = self.image_processing_tester.get_fake_oneformer_outputs() lowerCamelCase__ : str = image_processor.post_process_instance_segmentation(UpperCAmelCase , threshold=0 ) self.assertTrue(len(UpperCAmelCase ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , UpperCAmelCase ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def A_ ( self : Any ) -> Union[str, Any]: lowerCamelCase__ : int = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) lowerCamelCase__ : int = self.image_processing_tester.get_fake_oneformer_outputs() lowerCamelCase__ : Tuple = image_processor.post_process_panoptic_segmentation(UpperCAmelCase , threshold=0 ) self.assertTrue(len(UpperCAmelCase ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , UpperCAmelCase ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
50
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( snake_case_ = "AAPL" ): _A : str = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' _A : List[Any] = BeautifulSoup(requests.get(snake_case_ ).text,"""html.parser""" ) _A : Union[str, Any] = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""",class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
0
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class __snake_case ( ctypes.Structure ): # _fields is a specific attr expected by ctypes UpperCAmelCase__ : str = [('''size''', ctypes.c_int), ('''visible''', ctypes.c_byte)] def A () -> Dict: """simple docstring""" if os.name == "nt": UpperCAmelCase_ = CursorInfo() UpperCAmelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) UpperCAmelCase_ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write('''\033[?25l''' ) sys.stdout.flush() def A () -> Any: """simple docstring""" if os.name == "nt": UpperCAmelCase_ = CursorInfo() UpperCAmelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) UpperCAmelCase_ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write('''\033[?25h''' ) sys.stdout.flush() @contextmanager def A () -> Dict: """simple docstring""" try: hide_cursor() yield finally: show_cursor()
51
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _a = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def a__ ( self , _a , _a , _a ) -> int: _A : str = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def a__ ( self , _a , _a ) -> Dict: _A : Any = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) _A : List[Any] = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) _A : Optional[int] = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def a__ ( self ) -> List[str]: _A : Any = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility _A : Dict = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) _A : Any = 3 _A : Any = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) _A : Optional[int] = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) _A : Dict = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) _A : Dict = generator.model.config.eos_token_id _A : List[str] = """<pad>""" _A : Dict = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def a__ ( self ) -> int: _A : Optional[Any] = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility _A : str = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
26
0
# coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import sys import transformers __lowerCamelCase : str = """3""" print("""Python version:""", sys.version) print("""transformers version:""", transformers.__version__) try: import torch print("""Torch version:""", torch.__version__) print("""Cuda available:""", torch.cuda.is_available()) print("""Cuda version:""", torch.version.cuda) print("""CuDNN version:""", torch.backends.cudnn.version()) print("""Number of GPUs available:""", torch.cuda.device_count()) print("""NCCL version:""", torch.cuda.nccl.version()) except ImportError: print("""Torch version:""", None) try: import deepspeed print("""DeepSpeed version:""", deepspeed.__version__) except ImportError: print("""DeepSpeed version:""", None) try: import tensorflow as tf print("""TensorFlow version:""", tf.__version__) print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU"""))) print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU"""))) except ImportError: print("""TensorFlow version:""", None)
52
def lowerCAmelCase_ ( snake_case_,snake_case_ ): while b: _A , _A : List[str] = b, a % b return a def lowerCAmelCase_ ( snake_case_,snake_case_ ): return a if b == 0 else euclidean_gcd_recursive(snake_case_,a % b ) def lowerCAmelCase_ ( ): print(f'''euclidean_gcd(3, 5) = {euclidean_gcd(3,5 )}''' ) print(f'''euclidean_gcd(5, 3) = {euclidean_gcd(5,3 )}''' ) print(f'''euclidean_gcd(1, 3) = {euclidean_gcd(1,3 )}''' ) print(f'''euclidean_gcd(3, 6) = {euclidean_gcd(3,6 )}''' ) print(f'''euclidean_gcd(6, 3) = {euclidean_gcd(6,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,5 )}''' ) print(f'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,3 )}''' ) print(f'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,6 )}''' ) print(f'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,3 )}''' ) if __name__ == "__main__": main()
26
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ : List[Any] ={ '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Optional[int] =[ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys a__ : Optional[int] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
53
def lowerCAmelCase_ ( snake_case_ ): if number < 0: raise ValueError("""number must not be negative""" ) return number & (number - 1) == 0 if __name__ == "__main__": import doctest doctest.testmod()
26
0
"""simple docstring""" import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml a__ : List[Any] = NewType('''DataClass''', Any) a__ : str = NewType('''DataClassType''', Any) def UpperCAmelCase__ (lowerCAmelCase_ ): '''simple docstring''' if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f"""Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).""" ) def UpperCAmelCase__ (lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = {str(lowerCAmelCase_ ): choice for choice in choices} return lambda lowerCAmelCase_ : str_to_choice.get(lowerCAmelCase_ , lowerCAmelCase_ ) def UpperCAmelCase__ (*, lowerCAmelCase_ = None , lowerCAmelCase_ = None , lowerCAmelCase_ = dataclasses.MISSING , lowerCAmelCase_ = dataclasses.MISSING , lowerCAmelCase_ = None , **lowerCAmelCase_ , ): '''simple docstring''' if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls __SCREAMING_SNAKE_CASE = {} if aliases is not None: __SCREAMING_SNAKE_CASE = aliases if help is not None: __SCREAMING_SNAKE_CASE = help return dataclasses.field(metadata=lowerCAmelCase_ , default=lowerCAmelCase_ , default_factory=lowerCAmelCase_ , **lowerCAmelCase_ ) class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" snake_case__ : Iterable[DataClassType] def __init__( self : Any , UpperCAmelCase__ : Union[DataClassType, Iterable[DataClassType]] , **UpperCAmelCase__ : Optional[Any] ) -> Optional[int]: # To make the default appear when using --help if "formatter_class" not in kwargs: __SCREAMING_SNAKE_CASE = ArgumentDefaultsHelpFormatter super().__init__(**UpperCAmelCase__ ) if dataclasses.is_dataclass(UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = [dataclass_types] __SCREAMING_SNAKE_CASE = list(UpperCAmelCase__ ) for dtype in self.dataclass_types: self._add_dataclass_arguments(UpperCAmelCase__ ) @staticmethod def UpperCAmelCase_ ( UpperCAmelCase__ : ArgumentParser , UpperCAmelCase__ : dataclasses.Field ) -> Tuple: __SCREAMING_SNAKE_CASE = F"""--{field.name}""" __SCREAMING_SNAKE_CASE = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , UpperCAmelCase__ ): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default" ) __SCREAMING_SNAKE_CASE = kwargs.pop("aliases" , [] ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = [aliases] __SCREAMING_SNAKE_CASE = getattr(field.type , "__origin__" , field.type ) if origin_type is Union or (hasattr(UpperCAmelCase__ , "UnionType" ) and isinstance(UpperCAmelCase__ , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(UpperCAmelCase__ ) not in field.type.__args__ ): raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" " the argument parser only supports one type per argument." F""" Problem encountered in field '{field.name}'.""" ) if type(UpperCAmelCase__ ) not in field.type.__args__: # filter `str` in Union __SCREAMING_SNAKE_CASE = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] __SCREAMING_SNAKE_CASE = getattr(field.type , "__origin__" , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) __SCREAMING_SNAKE_CASE = ( field.type.__args__[0] if isinstance(UpperCAmelCase__ , field.type.__args__[1] ) else field.type.__args__[1] ) __SCREAMING_SNAKE_CASE = getattr(field.type , "__origin__" , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) __SCREAMING_SNAKE_CASE = {} if origin_type is Literal or (isinstance(field.type , UpperCAmelCase__ ) and issubclass(field.type , UpperCAmelCase__ )): if origin_type is Literal: __SCREAMING_SNAKE_CASE = field.type.__args__ else: __SCREAMING_SNAKE_CASE = [x.value for x in field.type] __SCREAMING_SNAKE_CASE = make_choice_type_function(kwargs["choices"] ) if field.default is not dataclasses.MISSING: __SCREAMING_SNAKE_CASE = field.default else: __SCREAMING_SNAKE_CASE = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument __SCREAMING_SNAKE_CASE = copy(UpperCAmelCase__ ) # Hack because type=bool in argparse does not behave as we want. __SCREAMING_SNAKE_CASE = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. __SCREAMING_SNAKE_CASE = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way __SCREAMING_SNAKE_CASE = default # This tells argparse we accept 0 or 1 value after --field_name __SCREAMING_SNAKE_CASE = "?" # This is the value that will get picked if we do --field_name (without value) __SCREAMING_SNAKE_CASE = True elif isclass(UpperCAmelCase__ ) and issubclass(UpperCAmelCase__ , UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = field.type.__args__[0] __SCREAMING_SNAKE_CASE = "+" if field.default_factory is not dataclasses.MISSING: __SCREAMING_SNAKE_CASE = field.default_factory() elif field.default is dataclasses.MISSING: __SCREAMING_SNAKE_CASE = True else: __SCREAMING_SNAKE_CASE = field.type if field.default is not dataclasses.MISSING: __SCREAMING_SNAKE_CASE = field.default elif field.default_factory is not dataclasses.MISSING: __SCREAMING_SNAKE_CASE = field.default_factory() else: __SCREAMING_SNAKE_CASE = True parser.add_argument(UpperCAmelCase__ , *UpperCAmelCase__ , **UpperCAmelCase__ ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): __SCREAMING_SNAKE_CASE = False parser.add_argument(F"""--no_{field.name}""" , action="store_false" , dest=field.name , **UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Tuple , UpperCAmelCase__ : DataClassType ) -> Union[str, Any]: if hasattr(UpperCAmelCase__ , "_argument_group_name" ): __SCREAMING_SNAKE_CASE = self.add_argument_group(dtype._argument_group_name ) else: __SCREAMING_SNAKE_CASE = self try: __SCREAMING_SNAKE_CASE = get_type_hints(UpperCAmelCase__ ) except NameError: raise RuntimeError( F"""Type resolution failed for {dtype}. Try declaring the class in global scope or """ "removing line of `from __future__ import annotations` which opts in Postponed " "Evaluation of Annotations (PEP 563)" ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 1_0) and "unsupported operand type(s) for |" in str(UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = ".".join(map(UpperCAmelCase__ , sys.version_info[:3] ) ) raise RuntimeError( F"""Type resolution failed for {dtype} on Python {python_version}. Try removing """ "line of `from __future__ import annotations` which opts in union types as " "`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To " "support Python versions that lower than 3.10, you need to use " "`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of " "`X | None`." ) from ex raise for field in dataclasses.fields(UpperCAmelCase__ ): if not field.init: continue __SCREAMING_SNAKE_CASE = type_hints[field.name] self._parse_dataclass_field(UpperCAmelCase__ , UpperCAmelCase__ ) def UpperCAmelCase_ ( self : str , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : Any=False , UpperCAmelCase__ : Tuple=True , UpperCAmelCase__ : str=None , UpperCAmelCase__ : Optional[Any]=None , ) -> Tuple[DataClass, ...]: if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): __SCREAMING_SNAKE_CASE = [] if args_filename: args_files.append(Path(UpperCAmelCase__ ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix(".args" ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values __SCREAMING_SNAKE_CASE = ArgumentParser() args_file_parser.add_argument(UpperCAmelCase__ , type=UpperCAmelCase__ , action="append" ) # Use only remaining args for further parsing (remove the args_file_flag) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = args_file_parser.parse_known_args(args=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = vars(UpperCAmelCase__ ).get(args_file_flag.lstrip("-" ) , UpperCAmelCase__ ) if cmd_args_file_paths: args_files.extend([Path(UpperCAmelCase__ ) for p in cmd_args_file_paths] ) __SCREAMING_SNAKE_CASE = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last __SCREAMING_SNAKE_CASE = file_args + args if args is not None else file_args + sys.argv[1:] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.parse_known_args(args=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = [] for dtype in self.dataclass_types: __SCREAMING_SNAKE_CASE = {f.name for f in dataclasses.fields(UpperCAmelCase__ ) if f.init} __SCREAMING_SNAKE_CASE = {k: v for k, v in vars(UpperCAmelCase__ ).items() if k in keys} for k in keys: delattr(UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = dtype(**UpperCAmelCase__ ) outputs.append(UpperCAmelCase__ ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(UpperCAmelCase__ ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F"""Some specified arguments are not used by the HfArgumentParser: {remaining_args}""" ) return (*outputs,) def UpperCAmelCase_ ( self : List[str] , UpperCAmelCase__ : Dict[str, Any] , UpperCAmelCase__ : bool = False ) -> Tuple[DataClass, ...]: __SCREAMING_SNAKE_CASE = set(args.keys() ) __SCREAMING_SNAKE_CASE = [] for dtype in self.dataclass_types: __SCREAMING_SNAKE_CASE = {f.name for f in dataclasses.fields(UpperCAmelCase__ ) if f.init} __SCREAMING_SNAKE_CASE = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) __SCREAMING_SNAKE_CASE = dtype(**UpperCAmelCase__ ) outputs.append(UpperCAmelCase__ ) if not allow_extra_keys and unused_keys: raise ValueError(F"""Some keys are not used by the HfArgumentParser: {sorted(UpperCAmelCase__ )}""" ) return tuple(UpperCAmelCase__ ) def UpperCAmelCase_ ( self : str , UpperCAmelCase__ : str , UpperCAmelCase__ : bool = False ) -> Tuple[DataClass, ...]: with open(Path(UpperCAmelCase__ ) , encoding="utf-8" ) as open_json_file: __SCREAMING_SNAKE_CASE = json.loads(open_json_file.read() ) __SCREAMING_SNAKE_CASE = self.parse_dict(UpperCAmelCase__ , allow_extra_keys=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ ) def UpperCAmelCase_ ( self : List[str] , UpperCAmelCase__ : str , UpperCAmelCase__ : bool = False ) -> Tuple[DataClass, ...]: __SCREAMING_SNAKE_CASE = self.parse_dict(yaml.safe_load(Path(UpperCAmelCase__ ).read_text() ) , allow_extra_keys=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ )
54
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def lowerCAmelCase_ ( snake_case_ ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: _A : str = k.replace(snake_case_,snake_case_ ) if k.startswith("""encoder""" ): _A : Optional[Any] = k.replace(""".attn""",""".self_attn""" ) _A : Dict = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Optional[Any] = k.replace("""norm2""","""final_layer_norm""" ) elif k.startswith("""decoder""" ): _A : str = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Any = k.replace("""norm2""","""encoder_attn_layer_norm""" ) _A : Optional[int] = k.replace("""norm3""","""final_layer_norm""" ) return k def lowerCAmelCase_ ( snake_case_ ): _A : List[Any] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: _A : str = sd.pop(snake_case_ ) _A : Optional[int] = k.replace("""layernorm_embedding""","""layer_norm""" ) assert new_k not in sd _A : Optional[int] = v _snake_case = ["START"] @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) _A : List[Any] = model["""model"""] _A : Optional[Any] = BlenderbotConfig.from_json_file(snake_case_ ) _A : List[str] = BlenderbotForConditionalGeneration(snake_case_ ) _A : Tuple = m.model.state_dict().keys() _A : Any = [] _A : Dict = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue _A : Optional[int] = rename_state_dict_key(snake_case_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: _A : Dict = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(snake_case_ ) m.model.load_state_dict(snake_case_,strict=snake_case_ ) m.half() m.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) _snake_case = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
'''simple docstring''' class snake_case : """simple docstring""" def __init__( self ): """simple docstring""" lowerCamelCase_ = 0 lowerCamelCase_ = 0 lowerCamelCase_ = {} def snake_case ( self , UpperCamelCase ): """simple docstring""" if vertex not in self.adjacency: lowerCamelCase_ = {} self.num_vertices += 1 def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase ): """simple docstring""" self.add_vertex(UpperCamelCase ) self.add_vertex(UpperCamelCase ) if head == tail: return lowerCamelCase_ = weight lowerCamelCase_ = weight def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.get_edges() for edge in edges: lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = edge edges.remove((tail, head, weight) ) for i in range(len(UpperCamelCase ) ): lowerCamelCase_ = list(edges[i] ) edges.sort(key=lambda UpperCamelCase : e[2] ) for i in range(len(UpperCamelCase ) - 1 ): if edges[i][2] >= edges[i + 1][2]: lowerCamelCase_ = edges[i][2] + 1 for edge in edges: lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = edge lowerCamelCase_ = weight lowerCamelCase_ = weight def __str__( self ): """simple docstring""" lowerCamelCase_ = "" for tail in self.adjacency: for head in self.adjacency[tail]: lowerCamelCase_ = self.adjacency[head][tail] string += f'''{head} -> {tail} == {weight}\n''' return string.rstrip("\n" ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def snake_case ( self ): """simple docstring""" return self.adjacency.keys() @staticmethod def snake_case ( UpperCamelCase=None , UpperCamelCase=None ): """simple docstring""" lowerCamelCase_ = Graph() if vertices is None: lowerCamelCase_ = [] if edges is None: lowerCamelCase_ = [] for vertex in vertices: g.add_vertex(UpperCamelCase ) for edge in edges: g.add_edge(*UpperCamelCase ) return g class snake_case : """simple docstring""" def __init__( self ): """simple docstring""" lowerCamelCase_ = {} lowerCamelCase_ = {} def __len__( self ): """simple docstring""" return len(self.parent ) def snake_case ( self , UpperCamelCase ): """simple docstring""" if item in self.parent: return self.find(UpperCamelCase ) lowerCamelCase_ = item lowerCamelCase_ = 0 return item def snake_case ( self , UpperCamelCase ): """simple docstring""" if item not in self.parent: return self.make_set(UpperCamelCase ) if item != self.parent[item]: lowerCamelCase_ = self.find(self.parent[item] ) return self.parent[item] def snake_case ( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = self.find(UpperCamelCase ) lowerCamelCase_ = self.find(UpperCamelCase ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: lowerCamelCase_ = roota return roota if self.rank[roota] < self.rank[roota]: lowerCamelCase_ = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 lowerCamelCase_ = roota return roota return None @staticmethod def snake_case ( UpperCamelCase ): """simple docstring""" lowerCamelCase_ = graph.num_vertices lowerCamelCase_ = Graph.UnionFind() lowerCamelCase_ = [] while num_components > 1: lowerCamelCase_ = {} for vertex in graph.get_vertices(): lowerCamelCase_ = -1 lowerCamelCase_ = graph.get_edges() for edge in edges: lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = edge edges.remove((tail, head, weight) ) for edge in edges: lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = edge lowerCamelCase_ = union_find.find(UpperCamelCase ) lowerCamelCase_ = union_find.find(UpperCamelCase ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: lowerCamelCase_ = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: lowerCamelCase_ = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = cheap_edge[vertex] if union_find.find(UpperCamelCase ) != union_find.find(UpperCamelCase ): union_find.union(UpperCamelCase , UpperCamelCase ) mst_edges.append(cheap_edge[vertex] ) lowerCamelCase_ = num_components - 1 lowerCamelCase_ = Graph.build(edges=UpperCamelCase ) return mst
55
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a = None , _a = None , _a = False , **_a , ) -> int: super().__init__(features=_a , cache_dir=_a , keep_in_memory=_a , **_a ) _A : Optional[int] = Sql( cache_dir=_a , features=_a , sql=_a , con=_a , **_a , ) def a__ ( self ) -> Optional[Any]: _A : Tuple = None _A : int = None _A : Tuple = None _A : Union[str, Any] = None self.builder.download_and_prepare( download_config=_a , download_mode=_a , verification_mode=_a , base_path=_a , ) # Build dataset for splits _A : int = self.builder.as_dataset( split="""train""" , verification_mode=_a , in_memory=self.keep_in_memory ) return dataset class lowercase : def __init__( self , _a , _a , _a , _a = None , _a = None , **_a , ) -> Union[str, Any]: if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) _A : Dict = dataset _A : int = name _A : Union[str, Any] = con _A : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _A : str = num_proc _A : Optional[Any] = to_sql_kwargs def a__ ( self ) -> int: _A : Any = self.to_sql_kwargs.pop("""sql""" , _a ) _A : List[str] = self.to_sql_kwargs.pop("""con""" , _a ) _A : int = self.to_sql_kwargs.pop("""index""" , _a ) _A : List[str] = self._write(index=_a , **self.to_sql_kwargs ) return written def a__ ( self , _a ) -> Optional[int]: _A , _A , _A : List[str] = args _A : int = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs _A : str = query_table( table=self.dataset.data , key=slice(_a , offset + self.batch_size ) , indices=self.dataset._indices , ) _A : Tuple = batch.to_pandas() _A : Union[str, Any] = df.to_sql(self.name , self.con , index=_a , **_a ) return num_rows or len(_a ) def a__ ( self , _a , **_a ) -> int: _A : Any = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: _A , _A : Tuple = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _a , _a )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += num_rows return written
26
0
'''simple docstring''' import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": a : List[str] = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, required=True, help='Path to the checkpoint to convert.' ) parser.add_argument( '--original_config_file', type=str, required=True, help='The YAML config file corresponding to the original architecture.', ) parser.add_argument( '--num_in_channels', default=None, type=int, help='The number of input channels. If `None` number of input channels will be automatically inferred.', ) parser.add_argument( '--image_size', default=512, type=int, help=( 'The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2' ' Base. Use 768 for Stable Diffusion v2.' ), ) parser.add_argument( '--extract_ema', action='store_true', help=( 'Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights' ' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield' ' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.' ), ) parser.add_argument( '--upcast_attention', action='store_true', help=( 'Whether the attention computation should always be upcasted. This is necessary when running stable' ' diffusion 2.1.' ), ) parser.add_argument( '--from_safetensors', action='store_true', help='If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.', ) parser.add_argument( '--to_safetensors', action='store_true', help='Whether to store pipeline in safetensors format or not.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument('--device', type=str, help='Device to use (e.g. cpu, cuda:0, cuda:1, etc.)') def __magic_name__ ( __UpperCAmelCase ) -> Any: '''simple docstring''' if string == "True": return True elif string == "False": return False else: raise ValueError(F"could not parse string as bool {string}" ) parser.add_argument( '--use_linear_projection', help='Override for use linear projection', required=False, type=parse_bool ) parser.add_argument('--cross_attention_dim', help='Override for cross attention_dim', required=False, type=int) a : List[str] = parser.parse_args() a : str = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
56
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class lowercase ( UpperCamelCase__ ): _a = "fnet" def __init__( self , _a=3_2000 , _a=768 , _a=12 , _a=3072 , _a="gelu_new" , _a=0.1 , _a=512 , _a=4 , _a=0.02 , _a=1e-12 , _a=False , _a=512 , _a=3 , _a=1 , _a=2 , **_a , ) -> int: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Any = vocab_size _A : str = max_position_embeddings _A : Optional[Any] = hidden_size _A : List[str] = num_hidden_layers _A : List[str] = intermediate_size _A : List[Any] = hidden_act _A : List[str] = hidden_dropout_prob _A : List[str] = initializer_range _A : List[Any] = type_vocab_size _A : List[Any] = layer_norm_eps _A : List[str] = use_tpu_fourier_optimizations _A : str = tpu_short_seq_length
26
0
"""simple docstring""" import re from filelock import FileLock try: import nltk A : str = True except (ImportError, ModuleNotFoundError): A : Optional[int] = False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' re.sub("<n>" , "" , _UpperCamelCase ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(_UpperCamelCase ) )
57
def lowerCAmelCase_ ( snake_case_ ): if n_term == "": return [] _A : list = [] for temp in range(int(snake_case_ ) ): series.append(f'''1/{temp + 1}''' if series else """1""" ) return series if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
26
0
'''simple docstring''' import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets lowercase_ = datasets.logging.get_logger(__name__) lowercase_ = """\ @inproceedings{bleurt, title={BLEURT: Learning Robust Metrics for Text Generation}, author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh}, booktitle={ACL}, year={2020}, url={https://arxiv.org/abs/2004.04696} } """ lowercase_ = """\ BLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018) and then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune it for your specific application (the latter is expected to perform better). See the project's README at https://github.com/google-research/bleurt#readme for more information. """ lowercase_ = """ BLEURT score. Args: `predictions` (list of str): prediction/candidate sentences `references` (list of str): reference sentences `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None. Returns: 'scores': List of scores. Examples: >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> bleurt = datasets.load_metric(\"bleurt\") >>> results = bleurt.compute(predictions=predictions, references=references) >>> print([round(v, 2) for v in results[\"scores\"]]) [1.03, 1.04] """ lowercase_ = { """bleurt-tiny-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip""", """bleurt-tiny-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip""", """bleurt-base-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip""", """bleurt-base-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip""", """bleurt-large-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip""", """bleurt-large-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip""", """BLEURT-20-D3""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip""", """BLEURT-20-D6""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip""", """BLEURT-20-D12""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip""", """BLEURT-20""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip""", } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): '''simple docstring''' def snake_case_( self ) -> Any: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/google-research/bleurt""" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , codebase_urls=["""https://github.com/google-research/bleurt"""] , reference_urls=["""https://github.com/google-research/bleurt""", """https://arxiv.org/abs/2004.04696"""] , ) def snake_case_( self , A ) -> Optional[Any]: # check that config name specifies a valid BLEURT model if self.config_name == "default": logger.warning( """Using default BLEURT-Base checkpoint for sequence maximum length 128. """ """You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512').""" ) _SCREAMING_SNAKE_CASE = """bleurt-base-128""" if self.config_name.lower() in CHECKPOINT_URLS: _SCREAMING_SNAKE_CASE = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: _SCREAMING_SNAKE_CASE = self.config_name.upper() else: raise KeyError( f'{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}' ) # download the model checkpoint specified by self.config_name and set up the scorer _SCREAMING_SNAKE_CASE = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name] ) _SCREAMING_SNAKE_CASE = score.BleurtScorer(os.path.join(A , A ) ) def snake_case_( self , A , A ) -> int: _SCREAMING_SNAKE_CASE = self.scorer.score(references=A , candidates=A ) return {"scores": scores}
58
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _snake_case = logging.get_logger(__name__) _snake_case = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) _snake_case = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCAmelCase_ ( snake_case_ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A : List[str] = model_type_to_module_name(snake_case_ ) _A : List[Any] = importlib.import_module(f'''.{module_name}''',"""transformers.models""" ) try: return getattr(snake_case_,snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_,"""__name__""",snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A : List[Any] = importlib.import_module("""transformers""" ) if hasattr(snake_case_,snake_case_ ): return getattr(snake_case_,snake_case_ ) return None def lowerCAmelCase_ ( snake_case_,snake_case_ = None,snake_case_ = False,snake_case_ = False,snake_case_ = None,snake_case_ = None,snake_case_ = None,snake_case_ = False,**snake_case_,): _A : Optional[int] = get_file_from_repo( snake_case_,snake_case_,cache_dir=snake_case_,force_download=snake_case_,resume_download=snake_case_,proxies=snake_case_,use_auth_token=snake_case_,revision=snake_case_,local_files_only=snake_case_,) if resolved_config_file is None: logger.info( """Could not locate the feature extractor configuration file, will try to use the model config instead.""" ) return {} with open(snake_case_,encoding="""utf-8""" ) as reader: return json.load(snake_case_ ) class lowercase : def __init__( self ) -> List[Any]: raise EnvironmentError( """AutoFeatureExtractor is designed to be instantiated """ """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(_a ) def a__ ( cls , _a , **_a ) -> Any: _A : Tuple = kwargs.pop("""config""" , _a ) _A : Tuple = kwargs.pop("""trust_remote_code""" , _a ) _A : List[Any] = True _A , _A : Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A : Tuple = config_dict.get("""feature_extractor_type""" , _a ) _A : int = None if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): _A : Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A : int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A : Optional[int] = getattr(_a , """feature_extractor_type""" , _a ) if hasattr(_a , """auto_map""" ) and "AutoFeatureExtractor" in config.auto_map: _A : Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A : Optional[Any] = feature_extractor_class_from_name(_a ) _A : List[Any] = feature_extractor_auto_map is not None _A : Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A : Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A : Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A : str = kwargs.pop("""code_revision""" , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A : Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ ( _a , _a ) -> Optional[int]: FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
26
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __lowerCamelCase = { """configuration_falcon""": ["""FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FalconConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ """FALCON_PRETRAINED_MODEL_ARCHIVE_LIST""", """FalconForCausalLM""", """FalconModel""", """FalconPreTrainedModel""", """FalconForSequenceClassification""", """FalconForTokenClassification""", """FalconForQuestionAnswering""", ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
59
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=False , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Dict: _A : str = parent _A : int = batch_size _A : Optional[int] = num_channels _A : List[Any] = image_size _A : int = min_resolution _A : Optional[int] = max_resolution _A : Any = do_resize _A : List[str] = size if size is not None else {"""height""": 18, """width""": 20} _A : Optional[int] = do_thumbnail _A : str = do_align_axis _A : List[Any] = do_pad _A : Optional[Any] = do_normalize _A : Tuple = image_mean _A : List[str] = image_std def a__ ( self ) -> Optional[int]: return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = DonutImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : List[str] = DonutImageProcessingTester(self ) @property def a__ ( self ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[Any]: _A : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) self.assertTrue(hasattr(_a , """do_thumbnail""" ) ) self.assertTrue(hasattr(_a , """do_align_long_axis""" ) ) self.assertTrue(hasattr(_a , """do_pad""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) def a__ ( self ) -> List[Any]: _A : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) _A : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def a__ ( self ) -> Union[str, Any]: pass @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : Any = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Dict: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : int = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : List[str] = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : str = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
26
0
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class snake_case_( a__ ): __UpperCamelCase = (DPMSolverSinglestepScheduler,) __UpperCamelCase = (('''num_inference_steps''', 25),) def lowerCamelCase__ ( self : Any , **UpperCamelCase_ : List[str] ): lowerCAmelCase : Union[str, Any] = { '''num_train_timesteps''': 1_0_0_0, '''beta_start''': 0.0_001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''solver_order''': 2, '''prediction_type''': '''epsilon''', '''thresholding''': False, '''sample_max_value''': 1.0, '''algorithm_type''': '''dpmsolver++''', '''solver_type''': '''midpoint''', '''lambda_min_clipped''': -float('''inf''' ), '''variance_type''': None, } config.update(**UpperCamelCase_ ) return config def lowerCamelCase__ ( self : List[Any] , UpperCamelCase_ : Optional[Any]=0 , **UpperCamelCase_ : Tuple ): lowerCAmelCase : Dict = dict(self.forward_default_kwargs ) lowerCAmelCase : int = kwargs.pop('''num_inference_steps''' , UpperCamelCase_ ) lowerCAmelCase : int = self.dummy_sample lowerCAmelCase : List[Any] = 0.1 * sample lowerCAmelCase : int = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : str = self.get_scheduler_config(**UpperCamelCase_ ) lowerCAmelCase : Tuple = scheduler_class(**UpperCamelCase_ ) scheduler.set_timesteps(UpperCamelCase_ ) # copy over dummy past residuals lowerCAmelCase : Tuple = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase_ ) lowerCAmelCase : List[Any] = scheduler_class.from_pretrained(UpperCamelCase_ ) new_scheduler.set_timesteps(UpperCamelCase_ ) # copy over dummy past residuals lowerCAmelCase : Any = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase, lowerCAmelCase : List[str] = sample, sample for t in range(UpperCamelCase_ , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase : Optional[Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ).prev_sample lowerCAmelCase : Optional[Any] = new_scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCamelCase__ ( self : Tuple ): pass def lowerCamelCase__ ( self : str , UpperCamelCase_ : str=0 , **UpperCamelCase_ : Union[str, Any] ): lowerCAmelCase : str = dict(self.forward_default_kwargs ) lowerCAmelCase : str = kwargs.pop('''num_inference_steps''' , UpperCamelCase_ ) lowerCAmelCase : Dict = self.dummy_sample lowerCAmelCase : Tuple = 0.1 * sample lowerCAmelCase : str = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : str = self.get_scheduler_config() lowerCAmelCase : Tuple = scheduler_class(**UpperCamelCase_ ) scheduler.set_timesteps(UpperCamelCase_ ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase : List[str] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase_ ) lowerCAmelCase : str = scheduler_class.from_pretrained(UpperCamelCase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(UpperCamelCase_ ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase : Tuple = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase : int = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ).prev_sample lowerCAmelCase : str = new_scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCamelCase__ ( self : Tuple , UpperCamelCase_ : List[Any]=None , **UpperCamelCase_ : List[Any] ): if scheduler is None: lowerCAmelCase : Tuple = self.scheduler_classes[0] lowerCAmelCase : Dict = self.get_scheduler_config(**UpperCamelCase_ ) lowerCAmelCase : Optional[int] = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Optional[int] = self.scheduler_classes[0] lowerCAmelCase : Dict = self.get_scheduler_config(**UpperCamelCase_ ) lowerCAmelCase : Optional[int] = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Optional[int] = 1_0 lowerCAmelCase : Dict = self.dummy_model() lowerCAmelCase : Tuple = self.dummy_sample_deter scheduler.set_timesteps(UpperCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Optional[int] = model(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : List[Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ).prev_sample return sample def lowerCamelCase__ ( self : Optional[Any] ): lowerCAmelCase : str = DPMSolverSinglestepScheduler(**self.get_scheduler_config() ) lowerCAmelCase : Any = 5_0 lowerCAmelCase : Tuple = self.dummy_model() lowerCAmelCase : Dict = self.dummy_sample_deter scheduler.set_timesteps(UpperCamelCase_ ) # make sure that the first t is uneven for i, t in enumerate(scheduler.timesteps[3:] ): lowerCAmelCase : int = model(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : Union[str, Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ).prev_sample lowerCAmelCase : List[Any] = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.2_574 ) < 1E-3 def lowerCamelCase__ ( self : Union[str, Any] ): for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_configs(num_train_timesteps=UpperCamelCase_ ) def lowerCamelCase__ ( self : Optional[int] ): # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase : str = DPMSolverSinglestepScheduler(**self.get_scheduler_config() ) lowerCAmelCase : Tuple = self.full_loop(scheduler=UpperCamelCase_ ) lowerCAmelCase : str = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.2_791 ) < 1E-3 lowerCAmelCase : Any = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : int = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Any = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Any = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase : Union[str, Any] = self.full_loop(scheduler=UpperCamelCase_ ) lowerCAmelCase : Tuple = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.2_791 ) < 1E-3 def lowerCamelCase__ ( self : str ): self.check_over_configs(thresholding=UpperCamelCase_ ) for order in [1, 2, 3]: for solver_type in ["midpoint", "heun"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=UpperCamelCase_ , prediction_type=UpperCamelCase_ , sample_max_value=UpperCamelCase_ , algorithm_type='''dpmsolver++''' , solver_order=UpperCamelCase_ , solver_type=UpperCamelCase_ , ) def lowerCamelCase__ ( self : Tuple ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=UpperCamelCase_ ) def lowerCamelCase__ ( self : Optional[Any] ): for algorithm_type in ["dpmsolver", "dpmsolver++"]: for solver_type in ["midpoint", "heun"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=UpperCamelCase_ , solver_type=UpperCamelCase_ , prediction_type=UpperCamelCase_ , algorithm_type=UpperCamelCase_ , ) lowerCAmelCase : List[Any] = self.full_loop( solver_order=UpperCamelCase_ , solver_type=UpperCamelCase_ , prediction_type=UpperCamelCase_ , algorithm_type=UpperCamelCase_ , ) assert not torch.isnan(UpperCamelCase_ ).any(), "Samples have nan numbers" def lowerCamelCase__ ( self : Any ): self.check_over_configs(lower_order_final=UpperCamelCase_ ) self.check_over_configs(lower_order_final=UpperCamelCase_ ) def lowerCamelCase__ ( self : Dict ): self.check_over_configs(lambda_min_clipped=-float('''inf''' ) ) self.check_over_configs(lambda_min_clipped=-5.1 ) def lowerCamelCase__ ( self : Dict ): self.check_over_configs(variance_type=UpperCamelCase_ ) self.check_over_configs(variance_type='''learned_range''' ) def lowerCamelCase__ ( self : Any ): for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_forward(num_inference_steps=UpperCamelCase_ , time_step=0 ) def lowerCamelCase__ ( self : Any ): lowerCAmelCase : List[Any] = self.full_loop() lowerCAmelCase : Union[str, Any] = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.2_791 ) < 1E-3 def lowerCamelCase__ ( self : str ): lowerCAmelCase : Union[str, Any] = self.full_loop(use_karras_sigmas=UpperCamelCase_ ) lowerCAmelCase : List[str] = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.2_248 ) < 1E-3 def lowerCamelCase__ ( self : Tuple ): lowerCAmelCase : List[Any] = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase : int = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.1_453 ) < 1E-3 def lowerCamelCase__ ( self : List[Any] ): lowerCAmelCase : List[Any] = self.full_loop(prediction_type='''v_prediction''' , use_karras_sigmas=UpperCamelCase_ ) lowerCAmelCase : Dict = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_mean.item() - 0.0_649 ) < 1E-3 def lowerCamelCase__ ( self : Optional[Any] ): lowerCAmelCase : Tuple = self.scheduler_classes[0] lowerCAmelCase : str = self.get_scheduler_config(thresholding=UpperCamelCase_ , dynamic_thresholding_ratio=0 ) lowerCAmelCase : Any = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Dict = 1_0 lowerCAmelCase : Dict = self.dummy_model() lowerCAmelCase : str = self.dummy_sample_deter.half() scheduler.set_timesteps(UpperCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Optional[Any] = model(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : List[Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ).prev_sample assert sample.dtype == torch.floataa
60
from __future__ import annotations import numpy as np def lowerCAmelCase_ ( snake_case_ ): return np.maximum(0,snake_case_ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
26
0
"""simple docstring""" 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, ) _a = logging.get_logger(__name__) # pylint: disable=invalid-name _a = '\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 __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase=8 ): UpperCAmelCase_ : Union[str, Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 UpperCAmelCase_ : Tuple = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class A_ (lowercase__ ): '''simple docstring''' def __init__( self , lowercase_ , lowercase_ , lowercase_ , ): """simple docstring""" super().__init__() self.register_modules( unet=lowercase_ , scheduler=lowercase_ , movq=lowercase_ , ) UpperCAmelCase_ : Union[str, Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" if latents is None: UpperCAmelCase_ : List[Any] = randn_tensor(lowercase_ , generator=lowercase_ , device=lowercase_ , dtype=lowercase_ ) else: if latents.shape != shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) UpperCAmelCase_ : int = latents.to(lowercase_ ) UpperCAmelCase_ : List[Any] = latents * scheduler.init_noise_sigma return latents def UpperCamelCase__ ( self , lowercase_=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) UpperCAmelCase_ : Dict = torch.device(F"""cuda:{gpu_id}""" ) UpperCAmelCase_ : Optional[Any] = [ 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 ): """simple docstring""" 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." ) UpperCAmelCase_ : Tuple = 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) UpperCAmelCase_ : Union[str, Any] = None for cpu_offloaded_model in [self.unet, self.movq]: UpperCAmelCase_ , UpperCAmelCase_ : int = cpu_offload_with_hook(lowercase_ , lowercase_ , prev_module_hook=lowercase_ ) # We'll offload the last model manually. UpperCAmelCase_ : Dict = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCamelCase__ ( self ): """simple docstring""" 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_ = 512 , lowercase_ = 512 , lowercase_ = 100 , lowercase_ = 4.0 , lowercase_ = 1 , lowercase_ = None , lowercase_ = None , lowercase_ = "pil" , lowercase_ = True , ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = self._execution_device UpperCAmelCase_ : Dict = guidance_scale > 1.0 if isinstance(lowercase_ , lowercase_ ): UpperCAmelCase_ : Optional[int] = torch.cat(lowercase_ , dim=0 ) UpperCAmelCase_ : List[str] = image_embeds.shape[0] * num_images_per_prompt if isinstance(lowercase_ , lowercase_ ): UpperCAmelCase_ : Any = torch.cat(lowercase_ , dim=0 ) if do_classifier_free_guidance: UpperCAmelCase_ : Optional[Any] = image_embeds.repeat_interleave(lowercase_ , dim=0 ) UpperCAmelCase_ : Tuple = negative_image_embeds.repeat_interleave(lowercase_ , dim=0 ) UpperCAmelCase_ : Union[str, Any] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=lowercase_ ) self.scheduler.set_timesteps(lowercase_ , device=lowercase_ ) UpperCAmelCase_ : Any = self.scheduler.timesteps UpperCAmelCase_ : List[Any] = self.unet.config.in_channels UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = downscale_height_and_width(lowercase_ , lowercase_ , self.movq_scale_factor ) # create initial latent UpperCAmelCase_ : str = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , lowercase_ , lowercase_ , lowercase_ , self.scheduler , ) for i, t in enumerate(self.progress_bar(lowercase_ ) ): # expand the latents if we are doing classifier free guidance UpperCAmelCase_ : Union[str, Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCAmelCase_ : Union[str, Any] = {"image_embeds": image_embeds} UpperCAmelCase_ : Optional[Any] = self.unet( sample=lowercase_ , timestep=lowercase_ , encoder_hidden_states=lowercase_ , added_cond_kwargs=lowercase_ , return_dict=lowercase_ , )[0] if do_classifier_free_guidance: UpperCAmelCase_ , UpperCAmelCase_ : Dict = noise_pred.split(latents.shape[1] , dim=1 ) UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = noise_pred.chunk(2 ) UpperCAmelCase_ , UpperCAmelCase_ : List[str] = variance_pred.chunk(2 ) UpperCAmelCase_ : Union[str, Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) UpperCAmelCase_ : Union[str, Any] = 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"] ): UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase_ : Dict = self.scheduler.step( lowercase_ , lowercase_ , lowercase_ , generator=lowercase_ , )[0] # post-processing UpperCAmelCase_ : Optional[Any] = 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"]: UpperCAmelCase_ : Any = image * 0.5 + 0.5 UpperCAmelCase_ : Tuple = image.clamp(0 , 1 ) UpperCAmelCase_ : int = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": UpperCAmelCase_ : Tuple = self.numpy_to_pil(lowercase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowercase_ )
61
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) _snake_case = getLogger(__name__) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ = 8,snake_case_ = 1024,snake_case_="val",snake_case_=None,snake_case_=False,snake_case_="summarization",snake_case_=None,snake_case_=1,snake_case_ = None,snake_case_="",**snake_case_,): _A : Dict = str(snake_case_ ) assert local_rank is not None torch.distributed.init_process_group(backend="""nccl""",rank=snake_case_ ) _A : Tuple = Path(snake_case_ ) _A : List[Any] = save_dir.joinpath(f'''rank_{local_rank}_output.json''' ) torch.cuda.set_device(snake_case_ ) _A : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained(snake_case_ ).cuda() if fpaa: _A : Any = model.half() # determine if we need to increase num_beams use_task_specific_params(snake_case_,snake_case_ ) # update config with task specific params _A : str = generate_kwargs.pop("""num_beams""",model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: _A : int = num_return_sequences _A : Optional[Any] = AutoTokenizer.from_pretrained(snake_case_ ) logger.info(f'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. if max_source_length is None: _A : Optional[int] = tokenizer.model_max_length if prefix is None: _A : Tuple = prefix or getattr(model.config,"""prefix""","""""" ) or """""" _A : Optional[int] = SeqaSeqDataset( snake_case_,snake_case_,snake_case_,max_target_length=1024,type_path=snake_case_,n_obs=snake_case_,prefix=snake_case_,**snake_case_,) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. _A : Optional[int] = ds.make_sortish_sampler(snake_case_,distributed=snake_case_,add_extra_examples=snake_case_,shuffle=snake_case_ ) _A : Dict = DataLoader(snake_case_,sampler=snake_case_,batch_size=snake_case_,collate_fn=ds.collate_fn ) _A : Optional[Any] = [] for batch in tqdm(snake_case_ ): _A : Tuple = model.generate( input_ids=batch["""input_ids"""].to(model.device ),attention_mask=batch["""attention_mask"""].to(model.device ),num_return_sequences=snake_case_,num_beams=snake_case_,**snake_case_,) _A : Any = tokenizer.batch_decode(snake_case_,skip_special_tokens=snake_case_,clean_up_tokenization_spaces=snake_case_ ) _A : Dict = batch["""ids"""] if num_return_sequences > 1: _A : Any = chunks(snake_case_,snake_case_ ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(snake_case_ ): results.append({"""pred""": pred, """id""": ids[i].item()} ) save_json(snake_case_,snake_case_ ) return results, sampler.num_replicas def lowerCAmelCase_ ( ): _A : Tuple = argparse.ArgumentParser( epilog="""Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate""" ) parser.add_argument("""--data_dir""",type=snake_case_,help="""like cnn_dm/test.source""" ) parser.add_argument( """--model_name""",type=snake_case_,help="""like facebook/bart-large-cnn,t5-base, etc.""",default="""sshleifer/distilbart-xsum-12-3""",) parser.add_argument("""--save_dir""",type=snake_case_,help="""where to save""",default="""tmp_gen""" ) parser.add_argument("""--max_source_length""",type=snake_case_,default=snake_case_ ) parser.add_argument( """--type_path""",type=snake_case_,default="""test""",help="""which subset to evaluate typically train/val/test""" ) parser.add_argument("""--task""",type=snake_case_,default="""summarization""",help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""",type=snake_case_,default=8,required=snake_case_,help="""batch size""" ) parser.add_argument( """--local_rank""",type=snake_case_,default=-1,required=snake_case_,help="""should be passed by distributed.launch""" ) parser.add_argument( """--n_obs""",type=snake_case_,default=snake_case_,required=snake_case_,help="""How many observations. Defaults to all.""" ) parser.add_argument( """--num_return_sequences""",type=snake_case_,default=1,required=snake_case_,help="""How many sequences to return""" ) parser.add_argument( """--sync_timeout""",type=snake_case_,default=600,required=snake_case_,help="""How long should master process wait for other processes to finish.""",) parser.add_argument("""--src_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument("""--tgt_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument( """--prefix""",type=snake_case_,required=snake_case_,default=snake_case_,help="""will be added to the begininng of src examples""" ) parser.add_argument("""--fp16""",action="""store_true""" ) parser.add_argument("""--debug""",action="""store_true""" ) _A : Union[str, Any] = time.time() _A , _A : List[str] = parser.parse_known_args() _A : List[str] = parse_numeric_n_bool_cl_kwargs(snake_case_ ) if generate_kwargs and args.local_rank <= 0: print(f'''parsed the following generate kwargs: {generate_kwargs}''' ) _A : Dict = Path(args.save_dir + """_tmp""" ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) # this handles locking. _A : int = list(json_save_dir.glob("""rank_*.json""" ) ) if intermediate_files: raise ValueError(f'''Found files at {json_save_dir} please move or remove them.''' ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. _A : Any = {} if args.src_lang is not None: _A : int = args.src_lang if args.tgt_lang is not None: _A : Dict = args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=snake_case_ ) _A , _A : str = eval_data_dir( args.data_dir,snake_case_,args.model_name,type_path=args.type_path,bs=args.bs,fpaa=args.fpaa,task=args.task,local_rank=args.local_rank,n_obs=args.n_obs,max_source_length=args.max_source_length,num_return_sequences=args.num_return_sequences,prefix=args.prefix,dataset_kwargs=snake_case_,**snake_case_,) if args.local_rank <= 0: _A : List[Any] = Path(args.save_dir ) save_dir.mkdir(exist_ok=snake_case_ ) _A : Tuple = gather_results_from_each_node(snake_case_,snake_case_,args.sync_timeout ) _A : Optional[int] = combine_partial_results(snake_case_ ) if args.num_return_sequences > 1: _A : Optional[Any] = save_dir.joinpath("""pseudolabel_results.json""" ) print(f'''Saving aggregated results at {save_path}, intermediate in {json_save_dir}/''' ) save_json(snake_case_,snake_case_ ) return _A : List[str] = Path(args.data_dir ).joinpath(args.type_path + """.target""" ) with open(snake_case_ ) as f: _A : int = [x.rstrip() for x in f.readlines()][: len(snake_case_ )] # Calculate metrics, save metrics, and save _generations.txt _A : Dict = """translation""" in args.task _A : Optional[Any] = calculate_bleu if calc_bleu else calculate_rouge _A : Tuple = """bleu""" if calc_bleu else """rouge""" _A : Dict = score_fn(snake_case_,snake_case_ ) _A : List[Any] = len(snake_case_ ) _A : Optional[int] = time.time() - start_time _A : Dict = round(runtime / metrics["""n_obs"""],4 ) _A : Dict = num_replicas # TODO(@stas00): add whatever metadata to metrics _A : Any = save_dir.joinpath(f'''{args.type_path}_{metric_name}.json''' ) save_json(snake_case_,snake_case_,indent=snake_case_ ) print(snake_case_ ) write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}_generations.txt''' ) ) if args.debug: write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}.target''' ) ) else: shutil.rmtree(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = [] for partial_result in partial_results: records.extend(snake_case_ ) _A : Optional[Any] = sorted(snake_case_,key=lambda snake_case_ : x["id"] ) _A : List[str] = [x["""pred"""] for x in records] return preds def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): # WAIT FOR lots of .json files _A : Optional[Any] = time.time() logger.info("""waiting for all nodes to finish""" ) _A : List[str] = None while (time.time() - start_wait) < timeout: _A : str = list(save_dir.glob("""rank_*.json""" ) ) if len(snake_case_ ) < num_replicas: continue try: # make sure all json files are fully saved _A : List[str] = lmap(snake_case_,snake_case_ ) return json_data except JSONDecodeError: continue else: raise TimeoutError("""Rank 0 gave up on waiting for other processes""" ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
26
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { 's-JoL/Open-Llama-V1': 'https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json', } class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : Any = "open-llama" def __init__( self , A_=100000 , A_=4096 , A_=11008 , A_=32 , A_=32 , A_="silu" , A_=2048 , A_=0.02 , A_=1E-6 , A_=True , A_=0 , A_=1 , A_=2 , A_=False , A_=True , A_=0.1 , A_=0.1 , A_=True , A_=True , A_=None , **A_ , ) -> List[Any]: __UpperCamelCase =vocab_size __UpperCamelCase =max_position_embeddings __UpperCamelCase =hidden_size __UpperCamelCase =intermediate_size __UpperCamelCase =num_hidden_layers __UpperCamelCase =num_attention_heads __UpperCamelCase =hidden_act __UpperCamelCase =initializer_range __UpperCamelCase =rms_norm_eps __UpperCamelCase =use_cache __UpperCamelCase =kwargs.pop( 'use_memorry_efficient_attention' , A_ ) __UpperCamelCase =hidden_dropout_prob __UpperCamelCase =attention_dropout_prob __UpperCamelCase =use_stable_embedding __UpperCamelCase =shared_input_output_embedding __UpperCamelCase =rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ , tie_word_embeddings=A_ , **A_ , ) def _a ( self ) -> List[str]: if self.rope_scaling is None: return if not isinstance(self.rope_scaling , A_ ) or len(self.rope_scaling ) != 2: raise ValueError( '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ' f'got {self.rope_scaling}' ) __UpperCamelCase =self.rope_scaling.get('type' , A_ ) __UpperCamelCase =self.rope_scaling.get('factor' , A_ ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}' ) if rope_scaling_factor is None or not isinstance(A_ , A_ ) or rope_scaling_factor <= 1.0: raise ValueError(f'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}' )
62
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Any: _A : Tuple = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) _A : List[Any] = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" _A : List[str] = model(_a )["""last_hidden_state"""] _A : Union[str, Any] = tf.TensorShape((1, 10, 768) ) self.assertEqual(output.shape , _a ) # compare the actual values for a slice. _A : List[Any] = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
26
0
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def _lowerCamelCase ( lowercase : List[str] ) -> Optional[Any]: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0X4E_00 and cp <= 0X9F_FF) or (cp >= 0X34_00 and cp <= 0X4D_BF) # or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF) # or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F) # or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F) # or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF) # or (cp >= 0XF9_00 and cp <= 0XFA_FF) or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F) # ): # return True return False def _lowerCamelCase ( lowercase : str ) -> Tuple: # word like '180' or '身高' or '神' for char in word: _a = ord(lowercase ) if not _is_chinese_char(lowercase ): return 0 return 1 def _lowerCamelCase ( lowercase : List[str] ) -> List[Any]: _a = set() for token in tokens: _a = len(lowercase ) > 1 and is_chinese(lowercase ) if chinese_word: word_set.add(lowercase ) _a = list(lowercase ) return word_list def _lowerCamelCase ( lowercase : List[str] , lowercase : set() ) -> Dict: if not chinese_word_set: return bert_tokens _a = max([len(lowercase ) for w in chinese_word_set] ) _a = bert_tokens _a , _a = 0, len(lowercase ) while start < end: _a = True if is_chinese(bert_word[start] ): _a = min(end - start , lowercase ) for i in range(lowercase , 1 , -1 ): _a = "".join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): _a = "##" + bert_word[j] _a = start + i _a = False break if single_word: start += 1 return bert_word def _lowerCamelCase ( lowercase : List[str] , lowercase : LTP , lowercase : BertTokenizer ) -> int: _a = [] for i in range(0 , len(lowercase ) , 100 ): _a = ltp_tokenizer.pipeline(lines[i : i + 100] , tasks=["cws"] ).cws _a = [get_chinese_word(lowercase ) for r in res] ltp_res.extend(lowercase ) assert len(lowercase ) == len(lowercase ) _a = [] for i in range(0 , len(lowercase ) , 100 ): _a = bert_tokenizer(lines[i : i + 100] , add_special_tokens=lowercase , truncation=lowercase , max_length=512 ) bert_res.extend(res["input_ids"] ) assert len(lowercase ) == len(lowercase ) _a = [] for input_ids, chinese_word in zip(lowercase , lowercase ): _a = [] for id in input_ids: _a = bert_tokenizer._convert_id_to_token(lowercase ) input_tokens.append(lowercase ) _a = add_sub_symbol(lowercase , lowercase ) _a = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(lowercase ): if token[:2] == "##": _a = token[2:] # save chinese tokens' pos if len(lowercase ) == 1 and _is_chinese_char(ord(lowercase ) ): ref_id.append(lowercase ) ref_ids.append(lowercase ) assert len(lowercase ) == len(lowercase ) return ref_ids def _lowerCamelCase ( lowercase : str ) -> Tuple: # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm) # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp) with open(args.file_name , "r" , encoding="utf-8" ) as f: _a = f.readlines() _a = [line.strip() for line in data if len(lowercase ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' _a = LTP(args.ltp ) # faster in GPU device _a = BertTokenizer.from_pretrained(args.bert ) _a = prepare_ref(lowercase , lowercase , lowercase ) with open(args.save_path , "w" , encoding="utf-8" ) as f: _a = [json.dumps(lowercase ) + "\n" for ref in ref_ids] f.writelines(lowercase ) if __name__ == "__main__": lowerCAmelCase_ : Tuple = argparse.ArgumentParser(description='prepare_chinese_ref') parser.add_argument( '--file_name', required=False, type=str, default='./resources/chinese-demo.txt', help='file need process, same as training data in lm', ) parser.add_argument( '--ltp', required=False, type=str, default='./resources/ltp', help='resources for LTP tokenizer, usually a path', ) parser.add_argument( '--bert', required=False, type=str, default='./resources/robert', help='resources for Bert tokenizer', ) parser.add_argument( '--save_path', required=False, type=str, default='./resources/ref.txt', help='path to save res', ) lowerCAmelCase_ : Tuple = parser.parse_args() main(args)
63
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class lowercase ( UpperCamelCase__ ): _a = 42 class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a , _a , _a , ) -> List[Any]: super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: if latents is None: _A : str = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) _A : Union[str, Any] = latents.to(_a ) _A : int = latents * scheduler.init_noise_sigma return latents def a__ ( self , _a=0 ) -> Optional[Any]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _A : str = torch.device(F'''cuda:{gpu_id}''' ) _A : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def a__ ( self ) -> List[Any]: if self.device != torch.device("""meta""" ) or not hasattr(self.image_encoder , """_hf_hook""" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , """_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 def a__ ( self , _a , _a , _a , _a , ) -> Tuple: if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): _A : int = torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): _A : Dict = self.image_processor(_a , return_tensors="""pt""" ).pixel_values[0].unsqueeze(0 ) _A : int = image.to(dtype=self.image_encoder.dtype , device=_a ) _A : List[Any] = self.image_encoder(_a )["""last_hidden_state"""] _A : List[Any] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 _A : Dict = image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: _A : str = torch.zeros_like(_a ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _A : List[str] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a = 1 , _a = 25 , _a = None , _a = None , _a = 4.0 , _a = 64 , _a = "pil" , _a = True , ) -> Union[str, Any]: if isinstance(_a , PIL.Image.Image ): _A : List[Any] = 1 elif isinstance(_a , torch.Tensor ): _A : Any = image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): _A : Union[str, Any] = len(_a ) else: raise ValueError( F'''`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}''' ) _A : Optional[int] = self._execution_device _A : Tuple = batch_size * num_images_per_prompt _A : List[Any] = guidance_scale > 1.0 _A : Optional[Any] = self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) _A : Optional[int] = self.scheduler.timesteps _A : List[str] = self.prior.config.num_embeddings _A : int = self.prior.config.embedding_dim _A : Optional[Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim _A : List[Any] = latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance _A : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _A : int = self.scheduler.scale_model_input(_a , _a ) _A : Tuple = self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance _A , _A : Optional[Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: _A , _A : Dict = noise_pred.chunk(2 ) _A : Tuple = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) _A : int = self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) _A : List[str] = [] for i, latent in enumerate(_a ): print() _A : List[str] = self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(_a ) _A : List[Any] = torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}''' ) _A : List[str] = images.cpu().numpy() if output_type == "pil": _A : List[Any] = [self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , """final_offload_hook""" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
26
0
"""simple docstring""" import cmath import math def UpperCAmelCase__ (snake_case__ : float , snake_case__ : float , snake_case__ : float , snake_case__ : float ): """simple docstring""" _snake_case : Dict = math.radians(snake_case__ ) _snake_case : Dict = math.radians(snake_case__ ) # Convert voltage and current to rectangular form _snake_case : str = cmath.rect(snake_case__ , snake_case__ ) _snake_case : str = cmath.rect(snake_case__ , snake_case__ ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
64
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( snake_case_ ): print("""Loading config file...""" ) def flatten_yaml_as_dict(snake_case_,snake_case_="",snake_case_="." ): _A : Union[str, Any] = [] for k, v in d.items(): _A : Optional[int] = parent_key + sep + k if parent_key else k if isinstance(snake_case_,collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(snake_case_,snake_case_,sep=snake_case_ ).items() ) else: items.append((new_key, v) ) return dict(snake_case_ ) _A : List[Any] = argparse.Namespace() with open(snake_case_,"""r""" ) as yaml_file: try: _A : List[Any] = yaml.load(snake_case_,Loader=yaml.FullLoader ) _A : Optional[int] = flatten_yaml_as_dict(snake_case_ ) for k, v in flat_cfg.items(): setattr(snake_case_,snake_case_,snake_case_ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(snake_case_,str(snake_case_ ) ) ) return config def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[Any] = MobileViTVaConfig() _A : Tuple = False # dataset if task_name.startswith("""imagenet1k_""" ): _A : Dict = 1000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : int = 384 else: _A : int = 256 _A : List[str] = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _A : Union[str, Any] = 21000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : str = 384 else: _A : List[Any] = 256 _A : List[str] = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _A : int = 151 _A : int = 512 _A : Optional[int] = """ade20k-id2label.json""" _A : Any = True elif task_name.startswith("""voc_""" ): _A : List[Any] = 21 _A : Dict = 512 _A : Dict = """pascal-voc-id2label.json""" _A : int = True # orig_config _A : Any = load_orig_config_file(snake_case_ ) assert getattr(snake_case_,"""model.classification.name""",-1 ) == "mobilevit_v2", "Invalid model" _A : List[Any] = getattr(snake_case_,"""model.classification.mitv2.width_multiplier""",1.0 ) assert ( getattr(snake_case_,"""model.classification.mitv2.attn_norm_layer""",-1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _A : str = getattr(snake_case_,"""model.classification.activation.name""","""swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _A : Optional[int] = getattr(snake_case_,"""model.segmentation.output_stride""",16 ) if "_deeplabv3" in task_name: _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_rates""",[12, 24, 36] ) _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_out_channels""",512 ) _A : str = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_dropout""",0.1 ) # id2label _A : List[Any] = """huggingface/label-files""" _A : List[Any] = json.load(open(hf_hub_download(snake_case_,snake_case_,repo_type="""dataset""" ),"""r""" ) ) _A : str = {int(snake_case_ ): v for k, v in idalabel.items()} _A : str = idalabel _A : Dict = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Any = dct.pop(snake_case_ ) _A : Union[str, Any] = val def lowerCAmelCase_ ( snake_case_,snake_case_=False ): if base_model: _A : Optional[int] = """""" else: _A : Dict = """mobilevitv2.""" _A : int = [] for k in state_dict.keys(): if k[:8] == "encoder.": _A : Any = k[8:] else: _A : List[str] = k if ".block." in k: _A : Any = k_new.replace(""".block.""",""".""" ) if ".conv." in k: _A : List[Any] = k_new.replace(""".conv.""",""".convolution.""" ) if ".norm." in k: _A : Any = k_new.replace(""".norm.""",""".normalization.""" ) if "conv_1." in k: _A : int = k_new.replace("""conv_1.""",f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.''',f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: _A : Tuple = k_new.replace(""".exp_1x1.""",""".expand_1x1.""" ) if ".red_1x1." in k: _A : Optional[int] = k_new.replace(""".red_1x1.""",""".reduce_1x1.""" ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: _A : Optional[int] = k_new.replace(f'''layer_{i}.0.''',f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: _A : Union[str, Any] = k_new.replace(f'''layer_{i}.1.local_rep.0.''',f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: _A : str = k_new.replace(f'''layer_{i}.1.local_rep.1.''',f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: _A : Optional[int] = [0, 1] elif i == 4: _A : Union[str, Any] = [0, 1, 2, 3] elif i == 5: _A : Optional[Any] = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: _A : Union[str, Any] = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''',f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: _A : List[str] = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''',f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.1.conv_proj.''',f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_attn.0.""","""layernorm_before.""" ) if "pre_norm_attn.1." in k: _A : str = k_new.replace("""pre_norm_attn.1.""","""attention.""" ) if "pre_norm_ffn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_ffn.0.""","""layernorm_after.""" ) if "pre_norm_ffn.1." in k: _A : Dict = k_new.replace("""pre_norm_ffn.1.""","""ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _A : List[str] = k_new.replace("""pre_norm_ffn.3.""","""ffn.conv2.""" ) if "classifier.1." in k: _A : List[str] = k_new.replace("""classifier.1.""","""classifier.""" ) if "seg_head." in k: _A : List[Any] = k_new.replace("""seg_head.""","""segmentation_head.""" ) if ".aspp_layer." in k: _A : List[Any] = k_new.replace(""".aspp_layer.""",""".""" ) if ".aspp_pool." in k: _A : Optional[Any] = k_new.replace(""".aspp_pool.""",""".""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(snake_case_ ) for k in keys_to_ignore: state_dict.pop(snake_case_,snake_case_ ) def lowerCAmelCase_ ( ): _A : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _A : List[Any] = Image.open(requests.get(snake_case_,stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[Any] = get_mobilevitva_config(snake_case_,snake_case_ ) # load original state_dict _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _A : Optional[Any] = MobileViTVaForSemanticSegmentation(snake_case_ ).eval() _A : str = False else: _A : int = MobileViTVaForImageClassification(snake_case_ ).eval() _A : List[Any] = False # remove and rename some keys of load the original model _A : List[Any] = checkpoint remove_unused_keys(snake_case_ ) _A : Optional[Any] = create_rename_keys(snake_case_,base_model=snake_case_ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case_,snake_case_,snake_case_ ) # load modified state_dict model.load_state_dict(snake_case_ ) # Check outputs on an image, prepared by MobileViTImageProcessor _A : str = MobileViTImageProcessor(crop_size=config.image_size,size=config.image_size + 32 ) _A : List[Any] = image_processor(images=prepare_img(),return_tensors="""pt""" ) _A : Optional[Any] = model(**snake_case_ ) # verify classification model if task_name.startswith("""imagenet""" ): _A : List[Any] = outputs.logits _A : Optional[int] = logits.argmax(-1 ).item() print("""Predicted class:""",model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _A : int = torch.tensor([-1.63_36e00, -7.32_04e-02, -5.18_83e-01] ) assert torch.allclose(logits[0, :3],snake_case_,atol=1e-4 ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you'd like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) _snake_case = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
26
0
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv('TEST_SAGEMAKER' , 'False' ) ) is not True , reason='Skipping test because should only be run when releasing minor transformers version' , ) @pytest.mark.usefixtures('sm_env' ) @parameterized_class( [ { 'framework': 'pytorch', 'script': 'run_glue.py', 'model_name_or_path': 'distilbert-base-cased', 'instance_type': 'ml.p3.16xlarge', 'results': {'train_runtime': 6_50, 'eval_accuracy': 0.7, 'eval_loss': 0.6}, }, { 'framework': 'pytorch', 'script': 'run_ddp.py', 'model_name_or_path': 'distilbert-base-cased', 'instance_type': 'ml.p3.16xlarge', 'results': {'train_runtime': 6_00, 'eval_accuracy': 0.7, 'eval_loss': 0.6}, }, { 'framework': 'tensorflow', 'script': 'run_tf_dist.py', 'model_name_or_path': 'distilbert-base-cased', 'instance_type': 'ml.p3.16xlarge', 'results': {'train_runtime': 6_00, 'eval_accuracy': 0.6, 'eval_loss': 0.7}, }, ] ) class A ( unittest.TestCase ): def lowercase_ (self : List[str] ) -> Optional[int]: """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=__UpperCAmelCase , ) assert hasattr(self , "env" ) def lowercase_ (self : List[Any] , __UpperCAmelCase : Any ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = f"""{self.env.base_job_name}-{instance_count}-{"ddp" if "ddp" in self.script else "smd"}""" # distributed data settings UpperCAmelCase__ = {"smdistributed": {"dataparallel": {"enabled": True}}} if self.script != "run_ddp.py" else None # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=__UpperCAmelCase , instance_count=__UpperCAmelCase , instance_type=self.instance_type , debugger_hook_config=__UpperCAmelCase , hyperparameters={**self.env.distributed_hyperparameters, "model_name_or_path": self.model_name_or_path} , metric_definitions=self.env.metric_definitions , distribution=__UpperCAmelCase , py_version="py36" , ) def lowercase_ (self : str , __UpperCAmelCase : Tuple ) -> Any: """simple docstring""" TrainingJobAnalytics(__UpperCAmelCase ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def lowercase_ (self : Optional[int] , __UpperCAmelCase : Optional[Any] ) -> List[str]: """simple docstring""" UpperCAmelCase__ = self.create_estimator(__UpperCAmelCase ) # run training estimator.fit() # result dataframe UpperCAmelCase__ = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis UpperCAmelCase__ = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) UpperCAmelCase__ = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping UpperCAmelCase__ = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 9_9_9_9_9_9 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , __UpperCAmelCase )
65
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
0
"""simple docstring""" import math class lowerCamelCase : '''simple docstring''' def __init__( self: List[Any] , snake_case: int=0 ) -> int: # a graph with Node 0,1,...,N-1 snake_case_ :List[str] = n snake_case_ :int = [ [math.inf for j in range(0 , snake_case )] for i in range(0 , snake_case ) ] # adjacency matrix for weight snake_case_ :str = [ [math.inf for j in range(0 , snake_case )] for i in range(0 , snake_case ) ] # dp[i][j] stores minimum distance from i to j def lowerCAmelCase_ ( self: Optional[int] , snake_case: str , snake_case: Optional[Any] , snake_case: str ) -> Tuple: snake_case_ :List[Any] = w def lowerCAmelCase_ ( self: List[str] ) -> str: for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): snake_case_ :Any = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def lowerCAmelCase_ ( self: int , snake_case: List[Any] , snake_case: Optional[Any] ) -> Union[str, Any]: return self.dp[u][v] if __name__ == "__main__": __a = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
66
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = 1 @register_to_config def __init__( self , _a=2000 , _a=0.1 , _a=20 , _a=1e-3 ) -> List[Any]: _A : Dict = None _A : List[Any] = None _A : Dict = None def a__ ( self , _a , _a = None ) -> Union[str, Any]: _A : Union[str, Any] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def a__ ( self , _a , _a , _a , _a=None ) -> Dict: if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _A : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _A : List[Any] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _A : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): _A : List[Any] = std.unsqueeze(-1 ) _A : int = -score / std # compute _A : Tuple = -1.0 / len(self.timesteps ) _A : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _A : List[str] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _A : Union[str, Any] = beta_t.unsqueeze(-1 ) _A : Tuple = -0.5 * beta_t * x _A : Tuple = torch.sqrt(_a ) _A : Dict = drift - diffusion**2 * score _A : Dict = x + drift * dt # add noise _A : Any = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) _A : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Optional[Any]: return self.config.num_train_timesteps
26
0
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
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: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "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", }, } _snake_case = { "google/fnet-base": 512, "google/fnet-large": 512, } _snake_case = "▁" class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["input_ids", "token_type_ids"] _a = FNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=True , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Optional[int]: # 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. _A : int = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) _A : Optional[int] = do_lower_case _A : List[Any] = remove_space _A : str = keep_accents _A : int = vocab_file _A : int = False if not self.vocab_file else True def a__ ( self , _a , _a = None ) -> List[int]: _A : str = [self.sep_token_id] _A : Dict = [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 a__ ( self , _a , _a = None ) -> List[int]: _A : Any = [self.sep_token_id] _A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : List[str] = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
26
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { """vinvino02/glpn-kitti""": """https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json""", # See all GLPN models at https://huggingface.co/models?filter=glpn } class a__ ( snake_case ): """simple docstring""" __lowerCamelCase = 'glpn' def __init__( self , lowercase=3 , lowercase=4 , lowercase=[2, 2, 2, 2] , lowercase=[8, 4, 2, 1] , lowercase=[32, 64, 160, 256] , lowercase=[7, 3, 3, 3] , lowercase=[4, 2, 2, 2] , lowercase=[1, 2, 5, 8] , lowercase=[4, 4, 4, 4] , lowercase="gelu" , lowercase=0.0 , lowercase=0.0 , lowercase=0.02 , lowercase=0.1 , lowercase=1e-6 , lowercase=64 , lowercase=10 , lowercase=-1 , **lowercase , ) -> Dict: '''simple docstring''' super().__init__(**lowercase ) A__ = num_channels A__ = num_encoder_blocks A__ = depths A__ = sr_ratios A__ = hidden_sizes A__ = patch_sizes A__ = strides A__ = mlp_ratios A__ = num_attention_heads A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = drop_path_rate A__ = layer_norm_eps A__ = decoder_hidden_size A__ = max_depth A__ = head_in_index
68
from math import asin, atan, cos, radians, sin, sqrt, tan _snake_case = 6_3_7_8_1_3_7.0 _snake_case = 6_3_5_6_7_5_2.3_1_4_2_4_5 _snake_case = 6378137 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Any = (AXIS_A - AXIS_B) / AXIS_A _A : Optional[int] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : List[str] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : Optional[Any] = radians(snake_case_ ) _A : str = radians(snake_case_ ) # Equation _A : Dict = sin((phi_a - phi_a) / 2 ) _A : List[str] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda _A : Optional[int] = sqrt(sin_sq_phi + (cos(snake_case_ ) * cos(snake_case_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
26
0
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping __UpperCamelCase = tuple[int, int] class UpperCamelCase : def __init__( self, lowerCAmelCase__, lowerCAmelCase__) -> None: snake_case_ = vertices snake_case_ = { (min(lowerCAmelCase__), max(lowerCAmelCase__)): weight for edge, weight in edges.items() } def a_ ( self, lowerCAmelCase__, lowerCAmelCase__) -> None: self.vertices.add(edge[0]) self.vertices.add(edge[1]) snake_case_ = weight def a_ ( self) -> Graph: snake_case_ = Graph({min(self.vertices)}, {}) snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 while len(subgraph.vertices) < len(self.vertices): snake_case_ = max(self.edges.values()) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: snake_case_ = edge snake_case_ = weight subgraph.add_edge(lowerCAmelCase__, lowerCAmelCase__) return subgraph def UpperCAmelCase ( UpperCAmelCase = "p107_network.txt" ) -> int: snake_case_ = os.path.abspath(os.path.dirname(UpperCAmelCase ) ) snake_case_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) snake_case_ = {} snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 with open(UpperCAmelCase ) as f: snake_case_ = f.read().strip().split('\n' ) snake_case_ = [line.split(',' ) for line in data] for edgea in range(1 , len(UpperCAmelCase ) ): for edgea in range(UpperCAmelCase ): if adjaceny_matrix[edgea][edgea] != "-": snake_case_ = int(adjaceny_matrix[edgea][edgea] ) snake_case_ = Graph(set(range(len(UpperCAmelCase ) ) ) , UpperCAmelCase ) snake_case_ = graph.prims_algorithm() snake_case_ = sum(graph.edges.values() ) snake_case_ = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(F"""{solution() = }""")
69
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> Optional[int]: super().__init__(_a ) _A : Union[str, Any] = RobertaEmbeddings(_a ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> str: super().__init__(_a ) _A : Any = config.num_labels _A : Dict = config.num_hidden_layers _A : List[str] = DeeRobertaModel(_a ) _A : int = nn.Dropout(config.hidden_dropout_prob ) _A : int = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_a ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=-1 , _a=False , ) -> Any: _A : Optional[int] = self.num_layers try: _A : List[str] = self.roberta( _a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , ) _A : List[str] = outputs[1] _A : List[str] = self.dropout(_a ) _A : Optional[Any] = self.classifier(_a ) _A : List[Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: _A : List[Any] = e.message _A : Optional[int] = e.exit_layer _A : Optional[int] = outputs[0] if not self.training: _A : int = entropy(_a ) _A : int = [] _A : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression _A : Union[str, Any] = MSELoss() _A : Tuple = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Dict = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits _A : Optional[Any] = [] for highway_exit in outputs[-1]: _A : Tuple = highway_exit[0] if not self.training: highway_logits_all.append(_a ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression _A : List[str] = MSELoss() _A : Optional[int] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Tuple = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_a ) if train_highway: _A : Dict = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: _A : int = (loss,) + outputs if not self.training: _A : Optional[Any] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: _A : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
0
'''simple docstring''' def UpperCamelCase__ ( lowerCAmelCase = 1_00 ): """simple docstring""" _lowerCAmelCase = set() _lowerCAmelCase = 0 _lowerCAmelCase = n + 1 # maximum limit for a in range(2 , lowerCAmelCase ): for b in range(2 , lowerCAmelCase ): _lowerCAmelCase = a**b # calculates the current power collect_powers.add(lowerCAmelCase ) # adds the result to the set return len(lowerCAmelCase ) if __name__ == "__main__": print('''Number of terms ''', solution(int(str(input()).strip())))
70
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" def __init__( self , _a=3_0522 , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
0
import sys import turtle def A ( a_ ,a_ ) -> tuple[float, float]: return (pa[0] + pa[0]) / 2, (pa[1] + pa[1]) / 2 def A ( a_ ,a_ ,a_ ,a_ ,) -> None: my_pen.up() my_pen.goto(vertexa[0] ,vertexa[1] ) my_pen.down() my_pen.goto(vertexa[0] ,vertexa[1] ) my_pen.goto(vertexa[0] ,vertexa[1] ) my_pen.goto(vertexa[0] ,vertexa[1] ) if depth == 0: return triangle(a_ ,get_mid(a_ ,a_ ) ,get_mid(a_ ,a_ ) ,depth - 1 ) triangle(a_ ,get_mid(a_ ,a_ ) ,get_mid(a_ ,a_ ) ,depth - 1 ) triangle(a_ ,get_mid(a_ ,a_ ) ,get_mid(a_ ,a_ ) ,depth - 1 ) if __name__ == "__main__": if len(sys.argv) != 2: raise ValueError( '''Correct format for using this script: ''' '''python fractals.py <int:depth_for_fractal>''' ) A_ :Optional[int] = turtle.Turtle() my_pen.ht() my_pen.speed(5) my_pen.pencolor('''red''') A_ :Tuple = [(-175, -125), (0, 175), (175, -125)] # vertices of triangle triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))
71
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) if n == 0: return 0 _A : Tuple = float("""-inf""" ) for i in range(1,n + 1 ): _A : str = max( snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_ ) ) return max_revue def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) _A : Dict = [float("""-inf""" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: _A : List[str] = float("""-inf""" ) for i in range(1,n + 1 ): _A : Optional[Any] = max( snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_ ),) _A : Tuple = max_revenue return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. _A : List[Any] = [float("""-inf""" ) for _ in range(n + 1 )] _A : Any = 0 for i in range(1,n + 1 ): _A : Optional[Any] = max_rev[i] for j in range(1,i + 1 ): _A : int = max(snake_case_,prices[j - 1] + max_rev[i - j] ) _A : int = max_revenue_i return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): if n < 0: _A : Optional[Any] = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(snake_case_ ) if n > len(snake_case_ ): _A : Any = ( """Each integral piece of rod must have a corresponding price. """ f'''Got n = {n} but length of prices = {len(snake_case_ )}''' ) raise ValueError(snake_case_ ) def lowerCAmelCase_ ( ): _A : Tuple = [6, 10, 12, 15, 20, 23] _A : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. _A : Any = 36 _A : List[Any] = top_down_cut_rod(snake_case_,snake_case_ ) _A : List[Any] = bottom_up_cut_rod(snake_case_,snake_case_ ) _A : Dict = naive_cut_rod_recursive(snake_case_,snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
0
"""simple docstring""" from __future__ import annotations from random import random class __snake_case : def __init__( self : Dict , __lowerCAmelCase : int | None = None ): """simple docstring""" _lowerCamelCase : List[Any] = value _lowerCamelCase : Any = random() _lowerCamelCase : Node | None = None _lowerCamelCase : Node | None = None def __repr__( self : Any ): """simple docstring""" from pprint import pformat if self.left is None and self.right is None: return f'''\'{self.value}: {self.prior:.5}\'''' else: return pformat( {f'''{self.value}: {self.prior:.5}''': (self.left, self.right)} , indent=1 ) def __str__( self : Tuple ): """simple docstring""" _lowerCamelCase : Union[str, Any] = str(self.value ) + ''' ''' _lowerCamelCase : Any = str(self.left or '''''' ) _lowerCamelCase : Any = str(self.right or '''''' ) return value + left + right def snake_case_ ( A_ : Node | None, A_ : int ): '''simple docstring''' if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: _lowerCamelCase , _lowerCamelCase : str = split(root.left, A_ ) return left, root else: _lowerCamelCase , _lowerCamelCase : Dict = split(root.right, A_ ) return root, right def snake_case_ ( A_ : Node | None, A_ : Node | None ): '''simple docstring''' if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: _lowerCamelCase : str = merge(left.right, A_ ) return left else: _lowerCamelCase : str = merge(A_, right.left ) return right def snake_case_ ( A_ : Node | None, A_ : int ): '''simple docstring''' _lowerCamelCase : Union[str, Any] = Node(A_ ) _lowerCamelCase , _lowerCamelCase : List[Any] = split(A_, A_ ) return merge(merge(A_, A_ ), A_ ) def snake_case_ ( A_ : Node | None, A_ : int ): '''simple docstring''' _lowerCamelCase , _lowerCamelCase : Any = split(A_, value - 1 ) _lowerCamelCase , _lowerCamelCase : Any = split(A_, A_ ) return merge(A_, A_ ) def snake_case_ ( A_ : Node | None ): '''simple docstring''' if not root: # None return else: inorder(root.left ) print(root.value, end=''',''' ) inorder(root.right ) def snake_case_ ( A_ : Node | None, A_ : str ): '''simple docstring''' for arg in args.split(): if arg[0] == "+": _lowerCamelCase : Any = insert(A_, int(arg[1:] ) ) elif arg[0] == "-": _lowerCamelCase : Optional[Any] = erase(A_, int(arg[1:] ) ) else: print('''Unknown command''' ) return root def snake_case_ ( ): '''simple docstring''' _lowerCamelCase : Union[str, Any] = None print( '''enter numbers to create a tree, + value to add value into treap, ''' '''- value to erase all nodes with value. \'q\' to quit. ''' ) _lowerCamelCase : Optional[int] = input() while args != "q": _lowerCamelCase : List[str] = interact_treap(A_, A_ ) print(A_ ) _lowerCamelCase : Optional[Any] = input() print('''good by!''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
72
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( snake_case_ = "AAPL" ): _A : str = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' _A : List[Any] = BeautifulSoup(requests.get(snake_case_ ).text,"""html.parser""" ) _A : Union[str, Any] = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""",class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
0
import os import unittest from transformers import FunnelTokenizer, FunnelTokenizerFast from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A_ ( SCREAMING_SNAKE_CASE , unittest.TestCase ): _UpperCAmelCase : Dict = FunnelTokenizer _UpperCAmelCase : List[str] = FunnelTokenizerFast _UpperCAmelCase : str = True _UpperCAmelCase : Optional[int] = True def lowerCAmelCase ( self : Any): super().setUp() __lowerCamelCase : Union[str, Any] = [ '<unk>', '<cls>', '<sep>', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __lowerCamelCase : Optional[Any] = 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 : Union[str, Any] ,**SCREAMING_SNAKE_CASE__ : List[Any]): return FunnelTokenizer.from_pretrained(self.tmpdirname ,**SCREAMING_SNAKE_CASE__) def lowerCAmelCase ( self : List[Any] ,**SCREAMING_SNAKE_CASE__ : List[str]): return FunnelTokenizerFast.from_pretrained(self.tmpdirname ,**SCREAMING_SNAKE_CASE__) def lowerCAmelCase ( self : Optional[int] ,SCREAMING_SNAKE_CASE__ : str): __lowerCamelCase : Dict = 'UNwant\u00E9d,running' __lowerCamelCase : Tuple = 'unwanted, running' return input_text, output_text def lowerCAmelCase ( self : List[Any]): __lowerCamelCase : Tuple = self.tokenizer_class(self.vocab_file) __lowerCamelCase : Any = tokenizer.tokenize('UNwant\u00E9d,running') self.assertListEqual(SCREAMING_SNAKE_CASE__ ,['un', '##want', '##ed', ',', 'runn', '##ing']) self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__) ,[7, 4, 5, 1_0, 8, 9]) def lowerCAmelCase ( self : Tuple): __lowerCamelCase : int = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE__) for tokenizer in tokenizers: __lowerCamelCase : Dict = tokenizer('UNwant\u00E9d,running') __lowerCamelCase : int = len(inputs['input_ids']) - 1 self.assertListEqual(inputs['token_type_ids'] ,[2] + [0] * sentence_len) __lowerCamelCase : Dict = tokenizer('UNwant\u00E9d,running' ,'UNwant\u00E9d,running') self.assertListEqual(inputs['token_type_ids'] ,[2] + [0] * sentence_len + [1] * sentence_len)
73
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _a = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def a__ ( self , _a , _a , _a ) -> int: _A : str = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def a__ ( self , _a , _a ) -> Dict: _A : Any = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) _A : List[Any] = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) _A : Optional[int] = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def a__ ( self ) -> List[str]: _A : Any = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility _A : Dict = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) _A : Any = 3 _A : Any = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) _A : Optional[int] = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) _A : Dict = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) _A : Dict = generator.model.config.eos_token_id _A : List[str] = """<pad>""" _A : Dict = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def a__ ( self ) -> int: _A : Optional[Any] = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility _A : str = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
26
0
"""simple docstring""" from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": _lowercase = input('''Enter image url: ''').strip() print(F"""Downloading image from {url} ...""") _lowercase = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image _lowercase = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] _lowercase = requests.get(image_url).content _lowercase = F"""{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg""" with open(file_name, '''wb''') as fp: fp.write(image_data) print(F"""Done. Image saved to disk as {file_name}.""")
74
def lowerCAmelCase_ ( snake_case_,snake_case_ ): while b: _A , _A : List[str] = b, a % b return a def lowerCAmelCase_ ( snake_case_,snake_case_ ): return a if b == 0 else euclidean_gcd_recursive(snake_case_,a % b ) def lowerCAmelCase_ ( ): print(f'''euclidean_gcd(3, 5) = {euclidean_gcd(3,5 )}''' ) print(f'''euclidean_gcd(5, 3) = {euclidean_gcd(5,3 )}''' ) print(f'''euclidean_gcd(1, 3) = {euclidean_gcd(1,3 )}''' ) print(f'''euclidean_gcd(3, 6) = {euclidean_gcd(3,6 )}''' ) print(f'''euclidean_gcd(6, 3) = {euclidean_gcd(6,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,5 )}''' ) print(f'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,3 )}''' ) print(f'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,6 )}''' ) print(f'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,3 )}''' ) if __name__ == "__main__": main()
26
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) a_ : Optional[int] = {"""configuration_deit""": ["""DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DeiTConfig""", """DeiTOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Any = ["""DeiTFeatureExtractor"""] a_ : Any = ["""DeiTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Union[str, Any] = [ """DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """DeiTForImageClassification""", """DeiTForImageClassificationWithTeacher""", """DeiTForMaskedImageModeling""", """DeiTModel""", """DeiTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Any = [ """TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFDeiTForImageClassification""", """TFDeiTForImageClassificationWithTeacher""", """TFDeiTForMaskedImageModeling""", """TFDeiTModel""", """TFDeiTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
75
def lowerCAmelCase_ ( snake_case_ ): if number < 0: raise ValueError("""number must not be negative""" ) return number & (number - 1) == 0 if __name__ == "__main__": import doctest doctest.testmod()
26
0
import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : Tuple = old_name if "patch_embed" in old_name: SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : str = old_name.split(".") if layer == "0": SCREAMING_SNAKE_CASE : Tuple = old_name.replace("0" , "convolution1") elif layer == "1": SCREAMING_SNAKE_CASE : List[str] = old_name.replace("1" , "batchnorm_before") elif layer == "3": SCREAMING_SNAKE_CASE : str = old_name.replace("3" , "convolution2") else: SCREAMING_SNAKE_CASE : Tuple = old_name.replace("4" , "batchnorm_after") if "network" in old_name and re.search(r"\d\.\d" , _a): SCREAMING_SNAKE_CASE : Union[str, Any] = r"\b\d{2}\b" if bool(re.search(_a , _a)): SCREAMING_SNAKE_CASE : Tuple = re.search(r"\d\.\d\d." , _a).group() else: SCREAMING_SNAKE_CASE : Optional[Any] = re.search(r"\d\.\d." , _a).group() if int(match[0]) < 6: SCREAMING_SNAKE_CASE : Union[str, Any] = old_name.replace(_a , "") SCREAMING_SNAKE_CASE : Union[str, Any] = trimmed_name.replace("network" , match[0] + ".meta4D_layers.blocks." + match[2:-1]) SCREAMING_SNAKE_CASE : List[Any] = "intermediate_stages." + trimmed_name else: SCREAMING_SNAKE_CASE : Dict = old_name.replace(_a , "") if int(match[2]) < num_meta4D_last_stage: SCREAMING_SNAKE_CASE : str = trimmed_name.replace("network" , "meta4D_layers.blocks." + match[2]) else: SCREAMING_SNAKE_CASE : int = str(int(match[2]) - num_meta4D_last_stage) SCREAMING_SNAKE_CASE : Any = trimmed_name.replace("network" , "meta3D_layers.blocks." + layer_index) if "norm1" in old_name: SCREAMING_SNAKE_CASE : str = trimmed_name.replace("norm1" , "layernorm1") elif "norm2" in old_name: SCREAMING_SNAKE_CASE : List[str] = trimmed_name.replace("norm2" , "layernorm2") elif "fc1" in old_name: SCREAMING_SNAKE_CASE : Any = trimmed_name.replace("fc1" , "linear_in") elif "fc2" in old_name: SCREAMING_SNAKE_CASE : Optional[int] = trimmed_name.replace("fc2" , "linear_out") SCREAMING_SNAKE_CASE : List[str] = "last_stage." + trimmed_name elif "network" in old_name and re.search(r".\d." , _a): SCREAMING_SNAKE_CASE : List[str] = old_name.replace("network" , "intermediate_stages") if "fc" in new_name: SCREAMING_SNAKE_CASE : str = new_name.replace("fc" , "convolution") elif ("norm1" in new_name) and ("layernorm1" not in new_name): SCREAMING_SNAKE_CASE : Any = new_name.replace("norm1" , "batchnorm_before") elif ("norm2" in new_name) and ("layernorm2" not in new_name): SCREAMING_SNAKE_CASE : Optional[int] = new_name.replace("norm2" , "batchnorm_after") if "proj" in new_name: SCREAMING_SNAKE_CASE : Any = new_name.replace("proj" , "projection") if "dist_head" in new_name: SCREAMING_SNAKE_CASE : int = new_name.replace("dist_head" , "distillation_classifier") elif "head" in new_name: SCREAMING_SNAKE_CASE : Tuple = new_name.replace("head" , "classifier") elif "patch_embed" in new_name: SCREAMING_SNAKE_CASE : int = "efficientformer." + new_name elif new_name == "norm.weight" or new_name == "norm.bias": SCREAMING_SNAKE_CASE : Tuple = new_name.replace("norm" , "layernorm") SCREAMING_SNAKE_CASE : List[Any] = "efficientformer." + new_name else: SCREAMING_SNAKE_CASE : Optional[Any] = "efficientformer.encoder." + new_name return new_name def lowerCamelCase__ ( _a , _a): for key in checkpoint.copy().keys(): SCREAMING_SNAKE_CASE : List[Any] = checkpoint.pop(_a) SCREAMING_SNAKE_CASE : Dict = val return checkpoint def lowerCamelCase__ ( ): SCREAMING_SNAKE_CASE : int = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE : Optional[Any] = Image.open(requests.get(_a , stream=_a).raw) return image def lowerCamelCase__ ( _a , _a , _a , _a): SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(_a , map_location="cpu")["model"] SCREAMING_SNAKE_CASE : Dict = EfficientFormerConfig.from_json_file(_a) SCREAMING_SNAKE_CASE : List[Any] = EfficientFormerForImageClassificationWithTeacher(_a) SCREAMING_SNAKE_CASE : List[Any] = "_".join(checkpoint_path.split("/")[-1].split(".")[0].split("_")[:-1]) SCREAMING_SNAKE_CASE : Tuple = config.depths[-1] - config.num_metaad_blocks + 1 SCREAMING_SNAKE_CASE : str = convert_torch_checkpoint(_a , _a) model.load_state_dict(_a) model.eval() SCREAMING_SNAKE_CASE : str = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } # prepare image SCREAMING_SNAKE_CASE : Tuple = prepare_img() SCREAMING_SNAKE_CASE : Optional[Any] = 256 SCREAMING_SNAKE_CASE : Any = 224 SCREAMING_SNAKE_CASE : List[str] = EfficientFormerImageProcessor( size={"shortest_edge": image_size} , crop_size={"height": crop_size, "width": crop_size} , resample=pillow_resamplings["bicubic"] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = processor(images=_a , return_tensors="pt").pixel_values # original processing pipeline SCREAMING_SNAKE_CASE : str = Compose( [ Resize(_a , interpolation=pillow_resamplings["bicubic"]), CenterCrop(_a), ToTensor(), Normalize(_a , _a), ]) SCREAMING_SNAKE_CASE : List[str] = image_transforms(_a).unsqueeze(0) assert torch.allclose(_a , _a) SCREAMING_SNAKE_CASE : Optional[Any] = model(_a) SCREAMING_SNAKE_CASE : List[Any] = outputs.logits SCREAMING_SNAKE_CASE : Tuple = (1, 1000) if "l1" in model_name: SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328]) assert torch.allclose(logits[0, :10] , _a , atol=1E-3) assert logits.shape == expected_shape elif "l3" in model_name: SCREAMING_SNAKE_CASE : Any = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127]) assert torch.allclose(logits[0, :10] , _a , atol=1E-3) assert logits.shape == expected_shape elif "l7" in model_name: SCREAMING_SNAKE_CASE : int = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878]) assert logits.shape == expected_shape else: raise ValueError( f"Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7") # Save Checkpoints Path(_a).mkdir(exist_ok=_a) model.save_pretrained(_a) print(f"Checkpoint successfuly converted. Model saved at {pytorch_dump_path}") processor.save_pretrained(_a) print(f"Processor successfuly saved at {pytorch_dump_path}") if push_to_hub: print("Pushing model to the hub...") model.push_to_hub( repo_id=f"Bearnardd/{pytorch_dump_path}" , commit_message="Add model" , use_temp_dir=_a , ) processor.push_to_hub( repo_id=f"Bearnardd/{pytorch_dump_path}" , commit_message="Add image processor" , use_temp_dir=_a , ) if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--pytorch_model_path', default=None, type=str, required=True, help='Path to EfficientFormer pytorch checkpoint.', ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The json file for EfficientFormer model config.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument('--push_to_hub', action='store_true', help='Push model and image processor to the hub') parser.add_argument( '--no-push_to_hub', dest='push_to_hub', action='store_false', help='Do not push model and image processor to the hub', ) parser.set_defaults(push_to_hub=True) a_ = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
76
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def lowerCAmelCase_ ( snake_case_ ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: _A : str = k.replace(snake_case_,snake_case_ ) if k.startswith("""encoder""" ): _A : Optional[Any] = k.replace(""".attn""",""".self_attn""" ) _A : Dict = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Optional[Any] = k.replace("""norm2""","""final_layer_norm""" ) elif k.startswith("""decoder""" ): _A : str = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Any = k.replace("""norm2""","""encoder_attn_layer_norm""" ) _A : Optional[int] = k.replace("""norm3""","""final_layer_norm""" ) return k def lowerCAmelCase_ ( snake_case_ ): _A : List[Any] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: _A : str = sd.pop(snake_case_ ) _A : Optional[int] = k.replace("""layernorm_embedding""","""layer_norm""" ) assert new_k not in sd _A : Optional[int] = v _snake_case = ["START"] @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) _A : List[Any] = model["""model"""] _A : Optional[Any] = BlenderbotConfig.from_json_file(snake_case_ ) _A : List[str] = BlenderbotForConditionalGeneration(snake_case_ ) _A : Tuple = m.model.state_dict().keys() _A : Any = [] _A : Dict = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue _A : Optional[int] = rename_state_dict_key(snake_case_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: _A : Dict = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(snake_case_ ) m.model.load_state_dict(snake_case_,strict=snake_case_ ) m.half() m.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) _snake_case = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
"""simple docstring""" class UpperCAmelCase_ : def __init__( self ) -> List[str]: lowercase__ : Optional[int] = 0 lowercase__ : int = 0 lowercase__ : List[Any] = {} def _UpperCAmelCase ( self , a ) -> int: if vertex not in self.adjacency: lowercase__ : Any = {} self.num_vertices += 1 def _UpperCAmelCase ( self , a , a , a ) -> List[str]: self.add_vertex(a ) self.add_vertex(a ) if head == tail: return lowercase__ : Tuple = weight lowercase__ : List[Any] = weight def _UpperCAmelCase ( self ) -> Union[str, Any]: lowercase__ : Optional[int] = self.get_edges() for edge in edges: lowercase__ , lowercase__ , lowercase__ : str = edge edges.remove((tail, head, weight) ) for i in range(len(a ) ): lowercase__ : Optional[Any] = list(edges[i] ) edges.sort(key=lambda a : e[2] ) for i in range(len(a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: lowercase__ : Union[str, Any] = edges[i][2] + 1 for edge in edges: lowercase__ , lowercase__ , lowercase__ : Union[str, Any] = edge lowercase__ : Dict = weight lowercase__ : Optional[int] = weight def __str__( self ) -> Dict: lowercase__ : Any = '' for tail in self.adjacency: for head in self.adjacency[tail]: lowercase__ : List[Any] = self.adjacency[head][tail] string += f"""{head} -> {tail} == {weight}\n""" return string.rstrip('\n' ) def _UpperCAmelCase ( self ) -> Union[str, Any]: lowercase__ : Tuple = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def _UpperCAmelCase ( self ) -> Optional[int]: return self.adjacency.keys() @staticmethod def _UpperCAmelCase ( a=None , a=None ) -> Dict: lowercase__ : Dict = Graph() if vertices is None: lowercase__ : int = [] if edges is None: lowercase__ : int = [] for vertex in vertices: g.add_vertex(a ) for edge in edges: g.add_edge(*a ) return g class UpperCAmelCase_ : def __init__( self ) -> List[Any]: lowercase__ : Dict = {} lowercase__ : Optional[Any] = {} def __len__( self ) -> Union[str, Any]: return len(self.parent ) def _UpperCAmelCase ( self , a ) -> List[Any]: if item in self.parent: return self.find(a ) lowercase__ : Tuple = item lowercase__ : List[Any] = 0 return item def _UpperCAmelCase ( self , a ) -> Optional[Any]: if item not in self.parent: return self.make_set(a ) if item != self.parent[item]: lowercase__ : Union[str, Any] = self.find(self.parent[item] ) return self.parent[item] def _UpperCAmelCase ( self , a , a ) -> List[str]: lowercase__ : str = self.find(a ) lowercase__ : List[Any] = self.find(a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: lowercase__ : List[Any] = roota return roota if self.rank[roota] < self.rank[roota]: lowercase__ : Optional[Any] = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 lowercase__ : Optional[int] = roota return roota return None @staticmethod def _UpperCAmelCase ( a ) -> List[Any]: lowercase__ : List[Any] = graph.num_vertices lowercase__ : Dict = Graph.UnionFind() lowercase__ : Optional[Any] = [] while num_components > 1: lowercase__ : Any = {} for vertex in graph.get_vertices(): lowercase__ : str = -1 lowercase__ : Dict = graph.get_edges() for edge in edges: lowercase__ , lowercase__ , lowercase__ : Optional[Any] = edge edges.remove((tail, head, weight) ) for edge in edges: lowercase__ , lowercase__ , lowercase__ : Optional[int] = edge lowercase__ : List[str] = union_find.find(a ) lowercase__ : List[Any] = union_find.find(a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: lowercase__ : List[Any] = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: lowercase__ : Optional[Any] = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: lowercase__ , lowercase__ , lowercase__ : Tuple = cheap_edge[vertex] if union_find.find(a ) != union_find.find(a ): union_find.union(a , a ) mst_edges.append(cheap_edge[vertex] ) lowercase__ : List[Any] = num_components - 1 lowercase__ : Optional[int] = Graph.build(edges=a ) return mst
77
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a = None , _a = None , _a = False , **_a , ) -> int: super().__init__(features=_a , cache_dir=_a , keep_in_memory=_a , **_a ) _A : Optional[int] = Sql( cache_dir=_a , features=_a , sql=_a , con=_a , **_a , ) def a__ ( self ) -> Optional[Any]: _A : Tuple = None _A : int = None _A : Tuple = None _A : Union[str, Any] = None self.builder.download_and_prepare( download_config=_a , download_mode=_a , verification_mode=_a , base_path=_a , ) # Build dataset for splits _A : int = self.builder.as_dataset( split="""train""" , verification_mode=_a , in_memory=self.keep_in_memory ) return dataset class lowercase : def __init__( self , _a , _a , _a , _a = None , _a = None , **_a , ) -> Union[str, Any]: if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) _A : Dict = dataset _A : int = name _A : Union[str, Any] = con _A : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _A : str = num_proc _A : Optional[Any] = to_sql_kwargs def a__ ( self ) -> int: _A : Any = self.to_sql_kwargs.pop("""sql""" , _a ) _A : List[str] = self.to_sql_kwargs.pop("""con""" , _a ) _A : int = self.to_sql_kwargs.pop("""index""" , _a ) _A : List[str] = self._write(index=_a , **self.to_sql_kwargs ) return written def a__ ( self , _a ) -> Optional[int]: _A , _A , _A : List[str] = args _A : int = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs _A : str = query_table( table=self.dataset.data , key=slice(_a , offset + self.batch_size ) , indices=self.dataset._indices , ) _A : Tuple = batch.to_pandas() _A : Union[str, Any] = df.to_sql(self.name , self.con , index=_a , **_a ) return num_rows or len(_a ) def a__ ( self , _a , **_a ) -> int: _A : Any = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: _A , _A : Tuple = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _a , _a )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += num_rows return written
26
0
"""simple docstring""" import glob import os import random from string import ascii_lowercase, digits import cva snake_case_ = """""" snake_case_ = """""" snake_case_ = """""" snake_case_ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase ( ): UpperCAmelCase , UpperCAmelCase = get_dataset(lowercase_ , lowercase_ ) print('Processing...' ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(lowercase_ , lowercase_ , lowercase_ ) for index, image in enumerate(lowercase_ ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit('.' , 1 )[0] UpperCAmelCase = F"""{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}""" cva.imwrite(F"""/{file_root}.jpg""" , lowercase_ , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"""Success {index+1}/{len(lowercase_ )} with {file_name}""" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"""{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}""" annos_list.append(lowercase_ ) with open(F"""/{file_root}.txt""" , 'w' ) as outfile: outfile.write('\n'.join(line for line in annos_list ) ) def _lowerCAmelCase ( lowercase_ , lowercase_ ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(lowercase_ , '*.txt' ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit('.' , 1 )[0] with open(lowercase_ ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(lowercase_ , F"""{label_name}.jpg""" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip('\n' ).split(' ' ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(lowercase_ ) labels.append(lowercase_ ) return img_paths, labels def _lowerCAmelCase ( lowercase_ , lowercase_ , lowercase_ = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(lowercase_ ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(lowercase_ ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(lowercase_ ) if flip_type == 1: UpperCAmelCase = cva.flip(lowercase_ , lowercase_ ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(lowercase_ , lowercase_ ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(lowercase_ ) new_imgs_list.append(lowercase_ ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase ( lowercase_ = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(lowercase_ ) for _ in range(lowercase_ ) ) if __name__ == "__main__": main() print("""DONE ✅""")
78
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class lowercase ( UpperCamelCase__ ): _a = "fnet" def __init__( self , _a=3_2000 , _a=768 , _a=12 , _a=3072 , _a="gelu_new" , _a=0.1 , _a=512 , _a=4 , _a=0.02 , _a=1e-12 , _a=False , _a=512 , _a=3 , _a=1 , _a=2 , **_a , ) -> int: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Any = vocab_size _A : str = max_position_embeddings _A : Optional[Any] = hidden_size _A : List[str] = num_hidden_layers _A : List[str] = intermediate_size _A : List[Any] = hidden_act _A : List[str] = hidden_dropout_prob _A : List[str] = initializer_range _A : List[Any] = type_vocab_size _A : List[Any] = layer_norm_eps _A : List[str] = use_tpu_fourier_optimizations _A : str = tpu_short_seq_length
26
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCamelCase_ = { '''configuration_perceiver''': ['''PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PerceiverConfig''', '''PerceiverOnnxConfig'''], '''tokenization_perceiver''': ['''PerceiverTokenizer'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = ['''PerceiverFeatureExtractor'''] lowerCamelCase_ = ['''PerceiverImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PerceiverForImageClassificationConvProcessing''', '''PerceiverForImageClassificationFourier''', '''PerceiverForImageClassificationLearned''', '''PerceiverForMaskedLM''', '''PerceiverForMultimodalAutoencoding''', '''PerceiverForOpticalFlow''', '''PerceiverForSequenceClassification''', '''PerceiverLayer''', '''PerceiverModel''', '''PerceiverPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
79
def lowerCAmelCase_ ( snake_case_ ): if n_term == "": return [] _A : list = [] for temp in range(int(snake_case_ ) ): series.append(f'''1/{temp + 1}''' if series else """1""" ) return series if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
26
0
'''simple docstring''' from __future__ import annotations import math def _UpperCamelCase ( __A , __A ) -> list: '''simple docstring''' if len(__A ) != 2 or len(a[0] ) != 2 or len(__A ) != 2 or len(b[0] ) != 2: raise Exception("Matrices are not 2x2" ) UpperCamelCase__ = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def _UpperCamelCase ( __A , __A ) -> str: '''simple docstring''' return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__A ) ) ] def _UpperCamelCase ( __A , __A ) -> Union[str, Any]: '''simple docstring''' return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__A ) ) ] def _UpperCamelCase ( __A ) -> tuple[list, list, list, list]: '''simple docstring''' if len(__A ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception("Odd matrices are not supported!" ) UpperCamelCase__ = len(__A ) UpperCamelCase__ = matrix_length // 2 UpperCamelCase__ = [[a[i][j] for j in range(__A , __A )] for i in range(__A )] UpperCamelCase__ = [ [a[i][j] for j in range(__A , __A )] for i in range(__A , __A ) ] UpperCamelCase__ = [[a[i][j] for j in range(__A )] for i in range(__A )] UpperCamelCase__ = [[a[i][j] for j in range(__A )] for i in range(__A , __A )] return top_left, top_right, bot_left, bot_right def _UpperCamelCase ( __A ) -> tuple[int, int]: '''simple docstring''' return len(__A ), len(matrix[0] ) def _UpperCamelCase ( __A ) -> None: '''simple docstring''' print("\n".join(str(__A ) for line in matrix ) ) def _UpperCamelCase ( __A , __A ) -> list: '''simple docstring''' if matrix_dimensions(__A ) == (2, 2): return default_matrix_multiplication(__A , __A ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = split_matrix(__A ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = split_matrix(__A ) UpperCamelCase__ = actual_strassen(__A , matrix_subtraction(__A , __A ) ) UpperCamelCase__ = actual_strassen(matrix_addition(__A , __A ) , __A ) UpperCamelCase__ = actual_strassen(matrix_addition(__A , __A ) , __A ) UpperCamelCase__ = actual_strassen(__A , matrix_subtraction(__A , __A ) ) UpperCamelCase__ = actual_strassen(matrix_addition(__A , __A ) , matrix_addition(__A , __A ) ) UpperCamelCase__ = actual_strassen(matrix_subtraction(__A , __A ) , matrix_addition(__A , __A ) ) UpperCamelCase__ = actual_strassen(matrix_subtraction(__A , __A ) , matrix_addition(__A , __A ) ) UpperCamelCase__ = matrix_addition(matrix_subtraction(matrix_addition(__A , __A ) , __A ) , __A ) UpperCamelCase__ = matrix_addition(__A , __A ) UpperCamelCase__ = matrix_addition(__A , __A ) UpperCamelCase__ = matrix_subtraction(matrix_subtraction(matrix_addition(__A , __A ) , __A ) , __A ) # construct the new matrix from our 4 quadrants UpperCamelCase__ = [] for i in range(len(__A ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__A ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def _UpperCamelCase ( __A , __A ) -> list: '''simple docstring''' if matrix_dimensions(__A )[1] != matrix_dimensions(__A )[0]: UpperCamelCase__ = ( "Unable to multiply these matrices, please check the dimensions.\n" F'''Matrix A: {matrixa}\n''' F'''Matrix B: {matrixa}''' ) raise Exception(__A ) UpperCamelCase__ = matrix_dimensions(__A ) UpperCamelCase__ = matrix_dimensions(__A ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] UpperCamelCase__ = max(*__A , *__A ) UpperCamelCase__ = int(math.pow(2 , math.ceil(math.loga(__A ) ) ) ) UpperCamelCase__ = matrixa UpperCamelCase__ = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __A ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __A ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __A ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) UpperCamelCase__ = actual_strassen(__A , __A ) # Removing the additional zeros for i in range(0 , __A ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __A ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": a__ : int = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] a__ : str = [[0, 2, 1, 1], [1_6, 2, 3, 3], [2, 2, 7, 7], [1_3, 1_1, 2_2, 4]] print(strassen(matrixa, matrixa))
80
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _snake_case = logging.get_logger(__name__) _snake_case = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) _snake_case = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCAmelCase_ ( snake_case_ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A : List[str] = model_type_to_module_name(snake_case_ ) _A : List[Any] = importlib.import_module(f'''.{module_name}''',"""transformers.models""" ) try: return getattr(snake_case_,snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_,"""__name__""",snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A : List[Any] = importlib.import_module("""transformers""" ) if hasattr(snake_case_,snake_case_ ): return getattr(snake_case_,snake_case_ ) return None def lowerCAmelCase_ ( snake_case_,snake_case_ = None,snake_case_ = False,snake_case_ = False,snake_case_ = None,snake_case_ = None,snake_case_ = None,snake_case_ = False,**snake_case_,): _A : Optional[int] = get_file_from_repo( snake_case_,snake_case_,cache_dir=snake_case_,force_download=snake_case_,resume_download=snake_case_,proxies=snake_case_,use_auth_token=snake_case_,revision=snake_case_,local_files_only=snake_case_,) if resolved_config_file is None: logger.info( """Could not locate the feature extractor configuration file, will try to use the model config instead.""" ) return {} with open(snake_case_,encoding="""utf-8""" ) as reader: return json.load(snake_case_ ) class lowercase : def __init__( self ) -> List[Any]: raise EnvironmentError( """AutoFeatureExtractor is designed to be instantiated """ """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(_a ) def a__ ( cls , _a , **_a ) -> Any: _A : Tuple = kwargs.pop("""config""" , _a ) _A : Tuple = kwargs.pop("""trust_remote_code""" , _a ) _A : List[Any] = True _A , _A : Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A : Tuple = config_dict.get("""feature_extractor_type""" , _a ) _A : int = None if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): _A : Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A : int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A : Optional[int] = getattr(_a , """feature_extractor_type""" , _a ) if hasattr(_a , """auto_map""" ) and "AutoFeatureExtractor" in config.auto_map: _A : Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A : Optional[Any] = feature_extractor_class_from_name(_a ) _A : List[Any] = feature_extractor_auto_map is not None _A : Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A : Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A : Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A : str = kwargs.pop("""code_revision""" , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A : Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ ( _a , _a ) -> Optional[int]: FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
26
0
"""simple docstring""" lowerCamelCase_ : Any = [ """DownloadConfig""", """DownloadManager""", """DownloadMode""", """StreamingDownloadManager""", ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
81
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=False , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Dict: _A : str = parent _A : int = batch_size _A : Optional[int] = num_channels _A : List[Any] = image_size _A : int = min_resolution _A : Optional[int] = max_resolution _A : Any = do_resize _A : List[str] = size if size is not None else {"""height""": 18, """width""": 20} _A : Optional[int] = do_thumbnail _A : str = do_align_axis _A : List[Any] = do_pad _A : Optional[Any] = do_normalize _A : Tuple = image_mean _A : List[str] = image_std def a__ ( self ) -> Optional[int]: return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = DonutImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : List[str] = DonutImageProcessingTester(self ) @property def a__ ( self ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[Any]: _A : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) self.assertTrue(hasattr(_a , """do_thumbnail""" ) ) self.assertTrue(hasattr(_a , """do_align_long_axis""" ) ) self.assertTrue(hasattr(_a , """do_pad""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) def a__ ( self ) -> List[Any]: _A : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) _A : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def a__ ( self ) -> Union[str, Any]: pass @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : Any = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Dict: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : int = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : List[str] = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : str = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
26
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class __lowerCAmelCase ( unittest.TestCase ): def snake_case ( self ): """simple docstring""" _lowerCAmelCase = tempfile.mkdtemp() _lowerCAmelCase = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """的""", """价""", """格""", """是""", """15""", """便""", """alex""", """##andra""", """,""", """。""", """-""", """t""", """shirt""", ] _lowerCAmelCase = 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] ) ) _lowerCAmelCase = { """do_resize""": True, """size""": {"""height""": 224, """width""": 224}, """do_center_crop""": True, """crop_size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.4814_5466, 0.457_8275, 0.4082_1073], """image_std""": [0.2686_2954, 0.2613_0258, 0.2757_7711], """do_convert_rgb""": True, } _lowerCAmelCase = os.path.join(self.tmpdirname , _snake_case ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_snake_case , _snake_case ) def snake_case ( self , **_snake_case ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_snake_case ) def snake_case ( self , **_snake_case ): """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_snake_case ) def snake_case ( self , **_snake_case ): """simple docstring""" return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **_snake_case ) def snake_case ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] _lowerCAmelCase = [Image.fromarray(np.moveaxis(_snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = self.get_rust_tokenizer() _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) _lowerCAmelCase = ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_snake_case ) _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) _lowerCAmelCase = ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _snake_case ) self.assertIsInstance(processor_fast.tokenizer , _snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _snake_case ) self.assertIsInstance(processor_fast.image_processor , _snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _lowerCAmelCase = self.get_tokenizer(cls_token="""(CLS)""" , sep_token="""(SEP)""" ) _lowerCAmelCase = self.get_image_processor(do_normalize=_snake_case ) _lowerCAmelCase = ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token="""(CLS)""" , sep_token="""(SEP)""" , do_normalize=_snake_case ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _snake_case ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = image_processor(_snake_case , return_tensors="""np""" ) _lowerCAmelCase = processor(images=_snake_case , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) _lowerCAmelCase = """Alexandra,T-shirt的价格是15便士。""" _lowerCAmelCase = processor(text=_snake_case ) _lowerCAmelCase = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) _lowerCAmelCase = """Alexandra,T-shirt的价格是15便士。""" _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = processor(text=_snake_case , images=_snake_case ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) _lowerCAmelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _lowerCAmelCase = processor.batch_decode(_snake_case ) _lowerCAmelCase = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case , _snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = ChineseCLIPProcessor(tokenizer=_snake_case , image_processor=_snake_case ) _lowerCAmelCase = """Alexandra,T-shirt的价格是15便士。""" _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = processor(text=_snake_case , images=_snake_case ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
82
from __future__ import annotations import numpy as np def lowerCAmelCase_ ( snake_case_ ): return np.maximum(0,snake_case_ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
26
0
'''simple docstring''' import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ : Any = logging.get_logger(__name__) snake_case_ : Tuple = {'vocab_file': 'spiece.model'} snake_case_ : Tuple = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } snake_case_ : Union[str, Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class lowercase__ ( lowercase ): lowercase__ = VOCAB_FILES_NAMES lowercase__ = PRETRAINED_VOCAB_FILES_MAP lowercase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ = ["""input_ids""", """attention_mask"""] def __init__( self : List[Any] ,lowerCamelCase__ : str ,lowerCamelCase__ : Tuple=False ,lowerCamelCase__ : List[Any]=False ,lowerCamelCase__ : Any=False ,lowerCamelCase__ : Union[str, Any]=None ,lowerCamelCase__ : Optional[int]=None ,lowerCamelCase__ : Optional[Any]=None ,lowerCamelCase__ : Tuple=None ,lowerCamelCase__ : Optional[Dict[str, Any]] = None ,**lowerCamelCase__ : List[Any] ,): '''simple docstring''' _UpperCamelCase : Any = {} if sp_model_kwargs is None else sp_model_kwargs _UpperCamelCase : int = kwargs.get('name_or_path' ) if name_or_path is None: logger.warning( 'name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,' ' you are testing the model, this can safely be ignored' ) _UpperCamelCase : str = 'None' # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing _UpperCamelCase : Tuple = '<|endoftext|>' if eos_token is None else eos_token _UpperCamelCase : Any = '<unk>' if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: _UpperCamelCase : Union[str, Any] = unk_token if pad_token is None else pad_token _UpperCamelCase : Tuple = eos_token if bos_token is None else bos_token else: _UpperCamelCase : str = '<pad>' if pad_token is None else pad_token _UpperCamelCase : Dict = '<s>' if bos_token is None else bos_token super().__init__( do_lower_case=lowerCamelCase__ ,remove_space=lowerCamelCase__ ,keep_accents=lowerCamelCase__ ,bos_token=lowerCamelCase__ ,eos_token=lowerCamelCase__ ,unk_token=lowerCamelCase__ ,pad_token=lowerCamelCase__ ,sp_model_kwargs=self.sp_model_kwargs ,**lowerCamelCase__ ,) _UpperCamelCase : int = do_lower_case _UpperCamelCase : Tuple = remove_space _UpperCamelCase : int = keep_accents _UpperCamelCase : Union[str, Any] = vocab_file _UpperCamelCase : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase__ ) # Used for whitespace normalization in input texts # fmt : off _UpperCamelCase : List[Any] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '„'} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing _UpperCamelCase : Optional[int] = re.compile( F'[{"".join(map(lowerCamelCase__ ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(127 ,160 ) ) + [160, 173, 8203] ) )}]' ) def __getstate__( self : Any ): '''simple docstring''' _UpperCamelCase : Optional[int] = self.__dict__.copy() _UpperCamelCase : List[str] = None return state def __setstate__( self : Dict ,lowerCamelCase__ : Union[str, Any] ): '''simple docstring''' _UpperCamelCase : Any = d # for backward compatibility if not hasattr(self ,'sp_model_kwargs' ): _UpperCamelCase : List[str] = {} _UpperCamelCase : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : Tuple ): '''simple docstring''' return len(self.sp_model ) def UpperCamelCase_ ( self : List[Any] ,lowerCamelCase__ : str ): '''simple docstring''' _UpperCamelCase : Optional[Any] = self.non_printing_characters_re.sub('' ,lowerCamelCase__ ) # Normalize whitespaces _UpperCamelCase : List[Any] = ''.join([char if char not in self.whitespaces else ' ' for char in text] ) # NFC Unicode normalization _UpperCamelCase : Union[str, Any] = unicodedata.normalize('NFC' ,lowerCamelCase__ ) return text def UpperCamelCase_ ( self : List[str] ,lowerCamelCase__ : str ,**lowerCamelCase__ : Optional[Any] ): '''simple docstring''' _UpperCamelCase : List[str] = self.preprocess_text(lowerCamelCase__ ) return self.sp_model.encode(lowerCamelCase__ ,out_type=lowerCamelCase__ ) def UpperCamelCase_ ( self : Dict ,lowerCamelCase__ : str ): '''simple docstring''' return self.sp_model.PieceToId(lowerCamelCase__ ) def UpperCamelCase_ ( self : Union[str, Any] ,lowerCamelCase__ : int ): '''simple docstring''' return self.sp_model.IdToPiece(lowerCamelCase__ ) @staticmethod def UpperCamelCase_ ( lowerCamelCase__ : str ): '''simple docstring''' return out_string def UpperCamelCase_ ( self : str ,lowerCamelCase__ : List[str] ): '''simple docstring''' _UpperCamelCase : Tuple = [] _UpperCamelCase : Optional[int] = '' _UpperCamelCase : Optional[int] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowerCamelCase__ ) + token _UpperCamelCase : Optional[Any] = True _UpperCamelCase : Union[str, Any] = [] else: current_sub_tokens.append(lowerCamelCase__ ) _UpperCamelCase : str = False out_string += self.sp_model.decode(lowerCamelCase__ ) return out_string def UpperCamelCase_ ( self : Tuple ): '''simple docstring''' _UpperCamelCase : str = {self.convert_ids_to_tokens(lowerCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : str ,lowerCamelCase__ : str ,lowerCamelCase__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(lowerCamelCase__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return _UpperCamelCase : Dict = 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__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,lowerCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase__ ,'wb' ) as fi: _UpperCamelCase : str = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase__ ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,lowerCamelCase__ : Union[str, List[str]] ,lowerCamelCase__ : Union[str, bool] = False ): '''simple docstring''' if isinstance(lowerCamelCase__ ,lowerCamelCase__ ): _UpperCamelCase : List[str] = self.preprocess_text(lowerCamelCase__ ) _UpperCamelCase : Any = self.sp_model.encode(lowerCamelCase__ ) else: _UpperCamelCase : str = [self.preprocess_text(lowerCamelCase__ ) for t in text] _UpperCamelCase : Dict = self.sp_model.encode(lowerCamelCase__ ) if return_tensors is True or return_tensors == "pt": _UpperCamelCase : Dict = torch.tensor(lowerCamelCase__ ) return token_ids def UpperCamelCase_ ( self : List[Any] ,lowerCamelCase__ : Union[int, List[int]] ): '''simple docstring''' return self.sp_model.decode(lowerCamelCase__ ) def UpperCamelCase_ ( self : Dict ,lowerCamelCase__ : "Conversation" ): '''simple docstring''' _UpperCamelCase : List[Any] = [F'User: {text}' if is_user else F'Bot: {text}' for is_user, text in conversation.iter_texts()] _UpperCamelCase : List[Any] = ( F'{self.eos_token}{self.bos_token}' + F'{self.bos_token}'.join(lowerCamelCase__ ) + F'{self.bos_token}Bot:' ) return self.encode(text=lowerCamelCase__ )
83
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) _snake_case = getLogger(__name__) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ = 8,snake_case_ = 1024,snake_case_="val",snake_case_=None,snake_case_=False,snake_case_="summarization",snake_case_=None,snake_case_=1,snake_case_ = None,snake_case_="",**snake_case_,): _A : Dict = str(snake_case_ ) assert local_rank is not None torch.distributed.init_process_group(backend="""nccl""",rank=snake_case_ ) _A : Tuple = Path(snake_case_ ) _A : List[Any] = save_dir.joinpath(f'''rank_{local_rank}_output.json''' ) torch.cuda.set_device(snake_case_ ) _A : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained(snake_case_ ).cuda() if fpaa: _A : Any = model.half() # determine if we need to increase num_beams use_task_specific_params(snake_case_,snake_case_ ) # update config with task specific params _A : str = generate_kwargs.pop("""num_beams""",model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: _A : int = num_return_sequences _A : Optional[Any] = AutoTokenizer.from_pretrained(snake_case_ ) logger.info(f'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. if max_source_length is None: _A : Optional[int] = tokenizer.model_max_length if prefix is None: _A : Tuple = prefix or getattr(model.config,"""prefix""","""""" ) or """""" _A : Optional[int] = SeqaSeqDataset( snake_case_,snake_case_,snake_case_,max_target_length=1024,type_path=snake_case_,n_obs=snake_case_,prefix=snake_case_,**snake_case_,) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. _A : Optional[int] = ds.make_sortish_sampler(snake_case_,distributed=snake_case_,add_extra_examples=snake_case_,shuffle=snake_case_ ) _A : Dict = DataLoader(snake_case_,sampler=snake_case_,batch_size=snake_case_,collate_fn=ds.collate_fn ) _A : Optional[Any] = [] for batch in tqdm(snake_case_ ): _A : Tuple = model.generate( input_ids=batch["""input_ids"""].to(model.device ),attention_mask=batch["""attention_mask"""].to(model.device ),num_return_sequences=snake_case_,num_beams=snake_case_,**snake_case_,) _A : Any = tokenizer.batch_decode(snake_case_,skip_special_tokens=snake_case_,clean_up_tokenization_spaces=snake_case_ ) _A : Dict = batch["""ids"""] if num_return_sequences > 1: _A : Any = chunks(snake_case_,snake_case_ ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(snake_case_ ): results.append({"""pred""": pred, """id""": ids[i].item()} ) save_json(snake_case_,snake_case_ ) return results, sampler.num_replicas def lowerCAmelCase_ ( ): _A : Tuple = argparse.ArgumentParser( epilog="""Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate""" ) parser.add_argument("""--data_dir""",type=snake_case_,help="""like cnn_dm/test.source""" ) parser.add_argument( """--model_name""",type=snake_case_,help="""like facebook/bart-large-cnn,t5-base, etc.""",default="""sshleifer/distilbart-xsum-12-3""",) parser.add_argument("""--save_dir""",type=snake_case_,help="""where to save""",default="""tmp_gen""" ) parser.add_argument("""--max_source_length""",type=snake_case_,default=snake_case_ ) parser.add_argument( """--type_path""",type=snake_case_,default="""test""",help="""which subset to evaluate typically train/val/test""" ) parser.add_argument("""--task""",type=snake_case_,default="""summarization""",help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""",type=snake_case_,default=8,required=snake_case_,help="""batch size""" ) parser.add_argument( """--local_rank""",type=snake_case_,default=-1,required=snake_case_,help="""should be passed by distributed.launch""" ) parser.add_argument( """--n_obs""",type=snake_case_,default=snake_case_,required=snake_case_,help="""How many observations. Defaults to all.""" ) parser.add_argument( """--num_return_sequences""",type=snake_case_,default=1,required=snake_case_,help="""How many sequences to return""" ) parser.add_argument( """--sync_timeout""",type=snake_case_,default=600,required=snake_case_,help="""How long should master process wait for other processes to finish.""",) parser.add_argument("""--src_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument("""--tgt_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument( """--prefix""",type=snake_case_,required=snake_case_,default=snake_case_,help="""will be added to the begininng of src examples""" ) parser.add_argument("""--fp16""",action="""store_true""" ) parser.add_argument("""--debug""",action="""store_true""" ) _A : Union[str, Any] = time.time() _A , _A : List[str] = parser.parse_known_args() _A : List[str] = parse_numeric_n_bool_cl_kwargs(snake_case_ ) if generate_kwargs and args.local_rank <= 0: print(f'''parsed the following generate kwargs: {generate_kwargs}''' ) _A : Dict = Path(args.save_dir + """_tmp""" ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) # this handles locking. _A : int = list(json_save_dir.glob("""rank_*.json""" ) ) if intermediate_files: raise ValueError(f'''Found files at {json_save_dir} please move or remove them.''' ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. _A : Any = {} if args.src_lang is not None: _A : int = args.src_lang if args.tgt_lang is not None: _A : Dict = args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=snake_case_ ) _A , _A : str = eval_data_dir( args.data_dir,snake_case_,args.model_name,type_path=args.type_path,bs=args.bs,fpaa=args.fpaa,task=args.task,local_rank=args.local_rank,n_obs=args.n_obs,max_source_length=args.max_source_length,num_return_sequences=args.num_return_sequences,prefix=args.prefix,dataset_kwargs=snake_case_,**snake_case_,) if args.local_rank <= 0: _A : List[Any] = Path(args.save_dir ) save_dir.mkdir(exist_ok=snake_case_ ) _A : Tuple = gather_results_from_each_node(snake_case_,snake_case_,args.sync_timeout ) _A : Optional[int] = combine_partial_results(snake_case_ ) if args.num_return_sequences > 1: _A : Optional[Any] = save_dir.joinpath("""pseudolabel_results.json""" ) print(f'''Saving aggregated results at {save_path}, intermediate in {json_save_dir}/''' ) save_json(snake_case_,snake_case_ ) return _A : List[str] = Path(args.data_dir ).joinpath(args.type_path + """.target""" ) with open(snake_case_ ) as f: _A : int = [x.rstrip() for x in f.readlines()][: len(snake_case_ )] # Calculate metrics, save metrics, and save _generations.txt _A : Dict = """translation""" in args.task _A : Optional[Any] = calculate_bleu if calc_bleu else calculate_rouge _A : Tuple = """bleu""" if calc_bleu else """rouge""" _A : Dict = score_fn(snake_case_,snake_case_ ) _A : List[Any] = len(snake_case_ ) _A : Optional[int] = time.time() - start_time _A : Dict = round(runtime / metrics["""n_obs"""],4 ) _A : Dict = num_replicas # TODO(@stas00): add whatever metadata to metrics _A : Any = save_dir.joinpath(f'''{args.type_path}_{metric_name}.json''' ) save_json(snake_case_,snake_case_,indent=snake_case_ ) print(snake_case_ ) write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}_generations.txt''' ) ) if args.debug: write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}.target''' ) ) else: shutil.rmtree(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = [] for partial_result in partial_results: records.extend(snake_case_ ) _A : Optional[Any] = sorted(snake_case_,key=lambda snake_case_ : x["id"] ) _A : List[str] = [x["""pred"""] for x in records] return preds def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): # WAIT FOR lots of .json files _A : Optional[Any] = time.time() logger.info("""waiting for all nodes to finish""" ) _A : List[str] = None while (time.time() - start_wait) < timeout: _A : str = list(save_dir.glob("""rank_*.json""" ) ) if len(snake_case_ ) < num_replicas: continue try: # make sure all json files are fully saved _A : List[str] = lmap(snake_case_,snake_case_ ) return json_data except JSONDecodeError: continue else: raise TimeoutError("""Rank 0 gave up on waiting for other processes""" ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
26
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __UpperCAmelCase = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( A__ ): def __init__( self , *__A , **__A ) -> None: warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , __A , ) super().__init__(*__A , **__A )
84
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Any: _A : Tuple = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) _A : List[Any] = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" _A : List[str] = model(_a )["""last_hidden_state"""] _A : Union[str, Any] = tf.TensorShape((1, 10, 768) ) self.assertEqual(output.shape , _a ) # compare the actual values for a slice. _A : List[Any] = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
26
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[int] = { "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 _snake_case ( lowercase_ ): lowerCAmelCase_ : Optional[int] = "funnel" lowerCAmelCase_ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , a__=30_522 , a__=[4, 4, 4] , a__=None , a__=2 , a__=768 , a__=12 , a__=64 , a__=3_072 , a__="gelu_new" , a__=0.1 , a__=0.1 , a__=0.0 , a__=0.1 , a__=None , a__=1e-9 , a__="mean" , a__="relative_shift" , a__=True , a__=True , a__=True , **a__ , ) -> Optional[int]: '''simple docstring''' snake_case_ = vocab_size snake_case_ = block_sizes snake_case_ = [1] * len(a__ ) if block_repeats is None else block_repeats assert len(a__ ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." snake_case_ = num_decoder_layers snake_case_ = d_model snake_case_ = n_head snake_case_ = d_head snake_case_ = d_inner snake_case_ = hidden_act snake_case_ = hidden_dropout snake_case_ = attention_dropout snake_case_ = activation_dropout snake_case_ = initializer_range snake_case_ = initializer_std 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.' 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.' snake_case_ = attention_type snake_case_ = separate_cls snake_case_ = truncate_seq snake_case_ = pool_q_only super().__init__(**a__ ) @property def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase__ ( self , a__ ) -> List[Any]: '''simple docstring''' raise NotImplementedError( "This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`." ) @property def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase__ ( self , a__ ) -> Union[str, Any]: '''simple docstring''' raise NotImplementedError("This model does not support the setting of `num_blocks`. Please set `block_sizes`." )
85
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class lowercase ( UpperCamelCase__ ): _a = 42 class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a , _a , _a , ) -> List[Any]: super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: if latents is None: _A : str = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) _A : Union[str, Any] = latents.to(_a ) _A : int = latents * scheduler.init_noise_sigma return latents def a__ ( self , _a=0 ) -> Optional[Any]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _A : str = torch.device(F'''cuda:{gpu_id}''' ) _A : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def a__ ( self ) -> List[Any]: if self.device != torch.device("""meta""" ) or not hasattr(self.image_encoder , """_hf_hook""" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , """_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 def a__ ( self , _a , _a , _a , _a , ) -> Tuple: if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): _A : int = torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): _A : Dict = self.image_processor(_a , return_tensors="""pt""" ).pixel_values[0].unsqueeze(0 ) _A : int = image.to(dtype=self.image_encoder.dtype , device=_a ) _A : List[Any] = self.image_encoder(_a )["""last_hidden_state"""] _A : List[Any] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 _A : Dict = image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: _A : str = torch.zeros_like(_a ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _A : List[str] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a = 1 , _a = 25 , _a = None , _a = None , _a = 4.0 , _a = 64 , _a = "pil" , _a = True , ) -> Union[str, Any]: if isinstance(_a , PIL.Image.Image ): _A : List[Any] = 1 elif isinstance(_a , torch.Tensor ): _A : Any = image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): _A : Union[str, Any] = len(_a ) else: raise ValueError( F'''`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}''' ) _A : Optional[int] = self._execution_device _A : Tuple = batch_size * num_images_per_prompt _A : List[Any] = guidance_scale > 1.0 _A : Optional[Any] = self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) _A : Optional[int] = self.scheduler.timesteps _A : List[str] = self.prior.config.num_embeddings _A : int = self.prior.config.embedding_dim _A : Optional[Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim _A : List[Any] = latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance _A : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _A : int = self.scheduler.scale_model_input(_a , _a ) _A : Tuple = self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance _A , _A : Optional[Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: _A , _A : Dict = noise_pred.chunk(2 ) _A : Tuple = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) _A : int = self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) _A : List[str] = [] for i, latent in enumerate(_a ): print() _A : List[str] = self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(_a ) _A : List[Any] = torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}''' ) _A : List[str] = images.cpu().numpy() if output_type == "pil": _A : List[Any] = [self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , """final_offload_hook""" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
26
0
"""simple docstring""" from collections.abc import Generator def __lowerCAmelCase (): __lowerCAmelCase , __lowerCAmelCase : List[Any] = 0, 1 while True: __lowerCAmelCase , __lowerCAmelCase : Optional[int] = b, a + b yield b def __lowerCAmelCase (_UpperCamelCase = 1000 ): __lowerCAmelCase : Optional[int] = 1 __lowerCAmelCase : List[str] = fibonacci_generator() while len(str(next(_UpperCamelCase ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
86
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( snake_case_ ): print("""Loading config file...""" ) def flatten_yaml_as_dict(snake_case_,snake_case_="",snake_case_="." ): _A : Union[str, Any] = [] for k, v in d.items(): _A : Optional[int] = parent_key + sep + k if parent_key else k if isinstance(snake_case_,collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(snake_case_,snake_case_,sep=snake_case_ ).items() ) else: items.append((new_key, v) ) return dict(snake_case_ ) _A : List[Any] = argparse.Namespace() with open(snake_case_,"""r""" ) as yaml_file: try: _A : List[Any] = yaml.load(snake_case_,Loader=yaml.FullLoader ) _A : Optional[int] = flatten_yaml_as_dict(snake_case_ ) for k, v in flat_cfg.items(): setattr(snake_case_,snake_case_,snake_case_ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(snake_case_,str(snake_case_ ) ) ) return config def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[Any] = MobileViTVaConfig() _A : Tuple = False # dataset if task_name.startswith("""imagenet1k_""" ): _A : Dict = 1000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : int = 384 else: _A : int = 256 _A : List[str] = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _A : Union[str, Any] = 21000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : str = 384 else: _A : List[Any] = 256 _A : List[str] = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _A : int = 151 _A : int = 512 _A : Optional[int] = """ade20k-id2label.json""" _A : Any = True elif task_name.startswith("""voc_""" ): _A : List[Any] = 21 _A : Dict = 512 _A : Dict = """pascal-voc-id2label.json""" _A : int = True # orig_config _A : Any = load_orig_config_file(snake_case_ ) assert getattr(snake_case_,"""model.classification.name""",-1 ) == "mobilevit_v2", "Invalid model" _A : List[Any] = getattr(snake_case_,"""model.classification.mitv2.width_multiplier""",1.0 ) assert ( getattr(snake_case_,"""model.classification.mitv2.attn_norm_layer""",-1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _A : str = getattr(snake_case_,"""model.classification.activation.name""","""swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _A : Optional[int] = getattr(snake_case_,"""model.segmentation.output_stride""",16 ) if "_deeplabv3" in task_name: _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_rates""",[12, 24, 36] ) _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_out_channels""",512 ) _A : str = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_dropout""",0.1 ) # id2label _A : List[Any] = """huggingface/label-files""" _A : List[Any] = json.load(open(hf_hub_download(snake_case_,snake_case_,repo_type="""dataset""" ),"""r""" ) ) _A : str = {int(snake_case_ ): v for k, v in idalabel.items()} _A : str = idalabel _A : Dict = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Any = dct.pop(snake_case_ ) _A : Union[str, Any] = val def lowerCAmelCase_ ( snake_case_,snake_case_=False ): if base_model: _A : Optional[int] = """""" else: _A : Dict = """mobilevitv2.""" _A : int = [] for k in state_dict.keys(): if k[:8] == "encoder.": _A : Any = k[8:] else: _A : List[str] = k if ".block." in k: _A : Any = k_new.replace(""".block.""",""".""" ) if ".conv." in k: _A : List[Any] = k_new.replace(""".conv.""",""".convolution.""" ) if ".norm." in k: _A : Any = k_new.replace(""".norm.""",""".normalization.""" ) if "conv_1." in k: _A : int = k_new.replace("""conv_1.""",f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.''',f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: _A : Tuple = k_new.replace(""".exp_1x1.""",""".expand_1x1.""" ) if ".red_1x1." in k: _A : Optional[int] = k_new.replace(""".red_1x1.""",""".reduce_1x1.""" ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: _A : Optional[int] = k_new.replace(f'''layer_{i}.0.''',f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: _A : Union[str, Any] = k_new.replace(f'''layer_{i}.1.local_rep.0.''',f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: _A : str = k_new.replace(f'''layer_{i}.1.local_rep.1.''',f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: _A : Optional[int] = [0, 1] elif i == 4: _A : Union[str, Any] = [0, 1, 2, 3] elif i == 5: _A : Optional[Any] = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: _A : Union[str, Any] = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''',f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: _A : List[str] = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''',f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.1.conv_proj.''',f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_attn.0.""","""layernorm_before.""" ) if "pre_norm_attn.1." in k: _A : str = k_new.replace("""pre_norm_attn.1.""","""attention.""" ) if "pre_norm_ffn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_ffn.0.""","""layernorm_after.""" ) if "pre_norm_ffn.1." in k: _A : Dict = k_new.replace("""pre_norm_ffn.1.""","""ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _A : List[str] = k_new.replace("""pre_norm_ffn.3.""","""ffn.conv2.""" ) if "classifier.1." in k: _A : List[str] = k_new.replace("""classifier.1.""","""classifier.""" ) if "seg_head." in k: _A : List[Any] = k_new.replace("""seg_head.""","""segmentation_head.""" ) if ".aspp_layer." in k: _A : List[Any] = k_new.replace(""".aspp_layer.""",""".""" ) if ".aspp_pool." in k: _A : Optional[Any] = k_new.replace(""".aspp_pool.""",""".""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(snake_case_ ) for k in keys_to_ignore: state_dict.pop(snake_case_,snake_case_ ) def lowerCAmelCase_ ( ): _A : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _A : List[Any] = Image.open(requests.get(snake_case_,stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[Any] = get_mobilevitva_config(snake_case_,snake_case_ ) # load original state_dict _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _A : Optional[Any] = MobileViTVaForSemanticSegmentation(snake_case_ ).eval() _A : str = False else: _A : int = MobileViTVaForImageClassification(snake_case_ ).eval() _A : List[Any] = False # remove and rename some keys of load the original model _A : List[Any] = checkpoint remove_unused_keys(snake_case_ ) _A : Optional[Any] = create_rename_keys(snake_case_,base_model=snake_case_ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case_,snake_case_,snake_case_ ) # load modified state_dict model.load_state_dict(snake_case_ ) # Check outputs on an image, prepared by MobileViTImageProcessor _A : str = MobileViTImageProcessor(crop_size=config.image_size,size=config.image_size + 32 ) _A : List[Any] = image_processor(images=prepare_img(),return_tensors="""pt""" ) _A : Optional[Any] = model(**snake_case_ ) # verify classification model if task_name.startswith("""imagenet""" ): _A : List[Any] = outputs.logits _A : Optional[int] = logits.argmax(-1 ).item() print("""Predicted class:""",model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _A : int = torch.tensor([-1.63_36e00, -7.32_04e-02, -5.18_83e-01] ) assert torch.allclose(logits[0, :3],snake_case_,atol=1e-4 ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you'd like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) _snake_case = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
26
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available UpperCamelCase = {'''configuration_speech_encoder_decoder''': ['''SpeechEncoderDecoderConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''SpeechEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxSpeechEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
87
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
0
import argparse from typing import List import evaluate import numpy as np import torch from datasets import DatasetDict, load_dataset # New Code # # We'll be using StratifiedKFold for this example from sklearn.model_selection import StratifiedKFold from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to perform Cross Validation, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __lowerCAmelCase : List[Any] = 16 __lowerCAmelCase : Any = 32 def a__ ( A_, A_, A_, A_, A_ = 16 ): '''simple docstring''' __magic_name__ = AutoTokenizer.from_pretrained("""bert-base-cased""" ) __magic_name__ = DatasetDict( { """train""": dataset["""train"""].select(A_ ), """validation""": dataset["""train"""].select(A_ ), """test""": dataset["""validation"""], } ) def tokenize_function(A_ ): # max_length=None => use the model max length (it's actually the default) __magic_name__ = tokenizer(examples["""sentence1"""], examples["""sentence2"""], truncation=A_, max_length=A_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): __magic_name__ = datasets.map( A_, batched=A_, remove_columns=["""idx""", """sentence1""", """sentence2"""], ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __magic_name__ = tokenized_datasets.rename_column("""label""", """labels""" ) def collate_fn(A_ ): # On TPU it's best to pad everything to the same length or training will be very slow. __magic_name__ = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __magic_name__ = 16 elif accelerator.mixed_precision != "no": __magic_name__ = 8 else: __magic_name__ = None return tokenizer.pad( A_, padding="""longest""", max_length=A_, pad_to_multiple_of=A_, return_tensors="""pt""", ) # Instantiate dataloaders. __magic_name__ = DataLoader( tokenized_datasets["""train"""], shuffle=A_, collate_fn=A_, batch_size=A_ ) __magic_name__ = DataLoader( tokenized_datasets["""validation"""], shuffle=A_, collate_fn=A_, batch_size=A_ ) __magic_name__ = DataLoader( tokenized_datasets["""test"""], shuffle=A_, collate_fn=A_, batch_size=A_ ) return train_dataloader, eval_dataloader, test_dataloader def a__ ( A_, A_ ): '''simple docstring''' __magic_name__ = [] # Download the dataset __magic_name__ = load_dataset("""glue""", """mrpc""" ) # Create our splits __magic_name__ = StratifiedKFold(n_splits=int(args.num_folds ) ) # Initialize accelerator __magic_name__ = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __magic_name__ = config["""lr"""] __magic_name__ = int(config["""num_epochs"""] ) __magic_name__ = int(config["""seed"""] ) __magic_name__ = int(config["""batch_size"""] ) __magic_name__ = evaluate.load("""glue""", """mrpc""" ) # If the batch size is too big we use gradient accumulation __magic_name__ = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: __magic_name__ = batch_size // MAX_GPU_BATCH_SIZE __magic_name__ = MAX_GPU_BATCH_SIZE set_seed(A_ ) # New Code # # Create our folds: __magic_name__ = kfold.split(np.zeros(datasets["""train"""].num_rows ), datasets["""train"""]["""label"""] ) __magic_name__ = [] # Iterate over them for i, (train_idxs, valid_idxs) in enumerate(A_ ): __magic_name__ , __magic_name__ , __magic_name__ = get_fold_dataloaders( A_, A_, A_, A_, ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __magic_name__ = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""", return_dict=A_ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). __magic_name__ = model.to(accelerator.device ) # Instantiate optimizer __magic_name__ = AdamW(params=model.parameters(), lr=A_ ) # Instantiate scheduler __magic_name__ = get_linear_schedule_with_warmup( optimizer=A_, num_warmup_steps=100, num_training_steps=(len(A_ ) * num_epochs) // gradient_accumulation_steps, ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ = accelerator.prepare( A_, A_, A_, A_, A_ ) # Now we train the model for epoch in range(A_ ): model.train() for step, batch in enumerate(A_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __magic_name__ = model(**A_ ) __magic_name__ = outputs.loss __magic_name__ = loss / gradient_accumulation_steps accelerator.backward(A_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(A_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __magic_name__ = model(**A_ ) __magic_name__ = outputs.logits.argmax(dim=-1 ) __magic_name__ , __magic_name__ = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=A_, references=A_, ) __magic_name__ = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''', A_ ) # New Code # # We also run predictions on the test set at the very end __magic_name__ = [] for step, batch in enumerate(A_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __magic_name__ = model(**A_ ) __magic_name__ = outputs.logits __magic_name__ , __magic_name__ = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) fold_predictions.append(predictions.cpu() ) if i == 0: # We need all of the test predictions test_references.append(references.cpu() ) # Use accelerator.print to print only on the main process. test_predictions.append(torch.cat(A_, dim=0 ) ) # We now need to release all our memory and get rid of the current model, optimizer, etc accelerator.free_memory() # New Code # # Finally we check the accuracy of our folded results: __magic_name__ = torch.cat(A_, dim=0 ) __magic_name__ = torch.stack(A_, dim=0 ).sum(dim=0 ).div(int(args.num_folds ) ).argmax(dim=-1 ) __magic_name__ = metric.compute(predictions=A_, references=A_ ) accelerator.print("""Average test metrics from all folds:""", A_ ) def a__ ( ): '''simple docstring''' __magic_name__ = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""", type=A_, default=A_, choices=["""no""", """fp16""", """bf16""", """fp8"""], help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""", ) parser.add_argument("""--cpu""", action="""store_true""", help="""If passed, will train on the CPU.""" ) # New Code # parser.add_argument("""--num_folds""", type=A_, default=3, help="""The number of splits to perform across the dataset""" ) __magic_name__ = parser.parse_args() __magic_name__ = {"""lr""": 2e-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(A_, A_ ) if __name__ == "__main__": main()
88
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = 1 @register_to_config def __init__( self , _a=2000 , _a=0.1 , _a=20 , _a=1e-3 ) -> List[Any]: _A : Dict = None _A : List[Any] = None _A : Dict = None def a__ ( self , _a , _a = None ) -> Union[str, Any]: _A : Union[str, Any] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def a__ ( self , _a , _a , _a , _a=None ) -> Dict: if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _A : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _A : List[Any] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _A : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): _A : List[Any] = std.unsqueeze(-1 ) _A : int = -score / std # compute _A : Tuple = -1.0 / len(self.timesteps ) _A : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _A : List[str] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _A : Union[str, Any] = beta_t.unsqueeze(-1 ) _A : Tuple = -0.5 * beta_t * x _A : Tuple = torch.sqrt(_a ) _A : Dict = drift - diffusion**2 * score _A : Dict = x + drift * dt # add noise _A : Any = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) _A : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Optional[Any]: return self.config.num_train_timesteps
26
0
'''simple docstring''' import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler __lowerCAmelCase = 16 __lowerCAmelCase = 32 def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ = 16 , lowerCAmelCase_ = "bert-base-cased" ) -> str: _a : Optional[int] = AutoTokenizer.from_pretrained(lowerCAmelCase_ ) _a : int = load_dataset('glue' , 'mrpc' ) def tokenize_function(lowerCAmelCase_ ): # max_length=None => use the model max length (it's actually the default) _a : str = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset _a : str = datasets.map( lowerCAmelCase_ , batched=lowerCAmelCase_ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=lowerCAmelCase_ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _a : int = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(lowerCAmelCase_ ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase_ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(lowerCAmelCase_ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. _a : Optional[int] = DataLoader( tokenized_datasets['train'] , shuffle=lowerCAmelCase_ , collate_fn=lowerCAmelCase_ , batch_size=lowerCAmelCase_ ) _a : int = DataLoader( tokenized_datasets['validation'] , shuffle=lowerCAmelCase_ , collate_fn=lowerCAmelCase_ , batch_size=lowerCAmelCase_ ) return train_dataloader, eval_dataloader def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> List[Any]: model.eval() _a : List[str] = 0 for step, batch in enumerate(lowerCAmelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _a : int = model(**lowerCAmelCase_ ) _a : Optional[int] = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times _a , _a : List[str] = accelerator.gather( (predictions, batch['labels']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase_ ) - 1: _a : int = predictions[: len(eval_dataloader.dataset ) - samples_seen] _a : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase_ , references=lowerCAmelCase_ , ) _a : List[Any] = metric.compute() return eval_metric["accuracy"] def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ) -> Any: # Initialize accelerator _a : Optional[int] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _a : List[str] = config['lr'] _a : int = int(config['num_epochs'] ) _a : Union[str, Any] = int(config['seed'] ) _a : Optional[Any] = int(config['batch_size'] ) _a : List[str] = args.model_name_or_path set_seed(lowerCAmelCase_ ) _a , _a : Dict = get_dataloaders(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _a : Tuple = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_ , return_dict=lowerCAmelCase_ ) # Instantiate optimizer _a : List[str] = ( AdamW if accelerator.state.deepspeed_plugin is None or 'optimizer' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) _a : Tuple = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase_ ) if accelerator.state.deepspeed_plugin is not None: _a : int = accelerator.state.deepspeed_plugin.deepspeed_config[ 'gradient_accumulation_steps' ] else: _a : Any = 1 _a : List[Any] = (len(lowerCAmelCase_ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): _a : Any = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase_ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase_ , ) else: _a : int = DummyScheduler(lowerCAmelCase_ , total_num_steps=lowerCAmelCase_ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _a , _a , _a , _a , _a : Dict = accelerator.prepare( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # We need to keep track of how many total steps we have iterated over _a : str = 0 # We also need to keep track of the stating epoch so files are named properly _a : Optional[Any] = 0 _a : List[Any] = evaluate.load('glue' , 'mrpc' ) _a : List[Any] = num_epochs if args.partial_train_epoch is not None: _a : Dict = args.partial_train_epoch if args.resume_from_checkpoint: accelerator.load_state(args.resume_from_checkpoint ) _a : Any = args.resume_from_checkpoint.split('epoch_' )[1] _a : Optional[int] = '' for char in epoch_string: if char.isdigit(): state_epoch_num += char else: break _a : Dict = int(lowerCAmelCase_ ) + 1 _a : Dict = evaluation_loop(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) accelerator.print('resumed checkpoint performance:' , lowerCAmelCase_ ) accelerator.print('resumed checkpoint\'s scheduler\'s lr:' , lr_scheduler.get_lr()[0] ) accelerator.print('resumed optimizers\'s lr:' , optimizer.param_groups[0]['lr'] ) with open(os.path.join(args.output_dir , f"""state_{starting_epoch-1}.json""" ) , 'r' ) as f: _a : str = json.load(lowerCAmelCase_ ) assert resumed_state["accuracy"] == accuracy, "Accuracy mismatch, loading from checkpoint failed" assert ( resumed_state["lr"] == lr_scheduler.get_lr()[0] ), "Scheduler learning rate mismatch, loading from checkpoint failed" assert ( resumed_state["optimizer_lr"] == optimizer.param_groups[0]["lr"] ), "Optimizer learning rate mismatch, loading from checkpoint failed" assert resumed_state["epoch"] == starting_epoch - 1, "Epoch mismatch, loading from checkpoint failed" return # Now we train the model _a : Any = {} for epoch in range(lowerCAmelCase_ , lowerCAmelCase_ ): model.train() for step, batch in enumerate(lowerCAmelCase_ ): _a : Optional[int] = model(**lowerCAmelCase_ ) _a : str = outputs.loss _a : str = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 _a : List[Any] = f"""epoch_{epoch}""" _a : Union[str, Any] = os.path.join(args.output_dir , lowerCAmelCase_ ) accelerator.save_state(lowerCAmelCase_ ) _a : List[str] = evaluation_loop(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) _a : Optional[Any] = accuracy _a : List[Any] = lr_scheduler.get_lr()[0] _a : Any = optimizer.param_groups[0]['lr'] _a : Dict = epoch _a : List[Any] = overall_step accelerator.print(f"""epoch {epoch}:""" , lowerCAmelCase_ ) accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , f"""state_{epoch}.json""" ) , 'w' ) as f: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCamelCase ( ) -> List[Any]: _a : Union[str, Any] = argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=lowerCAmelCase_ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=lowerCAmelCase_ , ) parser.add_argument( '--output_dir' , type=lowerCAmelCase_ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--resume_from_checkpoint' , type=lowerCAmelCase_ , default=lowerCAmelCase_ , help='If the training should continue from a checkpoint folder.' , ) parser.add_argument( '--partial_train_epoch' , type=lowerCAmelCase_ , default=lowerCAmelCase_ , help='If passed, the training will stop after this number of epochs.' , ) parser.add_argument( '--num_epochs' , type=lowerCAmelCase_ , default=2 , help='Number of train epochs.' , ) _a : Tuple = parser.parse_args() _a : Optional[Any] = {'lr': 2E-5, 'num_epochs': args.num_epochs, 'seed': 42, 'batch_size': 16} training_function(lowerCAmelCase_ , lowerCAmelCase_ ) if __name__ == "__main__": main()
89
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: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "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", }, } _snake_case = { "google/fnet-base": 512, "google/fnet-large": 512, } _snake_case = "▁" class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["input_ids", "token_type_ids"] _a = FNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=True , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Optional[int]: # 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. _A : int = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) _A : Optional[int] = do_lower_case _A : List[Any] = remove_space _A : str = keep_accents _A : int = vocab_file _A : int = False if not self.vocab_file else True def a__ ( self , _a , _a = None ) -> List[int]: _A : str = [self.sep_token_id] _A : Dict = [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 a__ ( self , _a , _a = None ) -> List[int]: _A : Any = [self.sep_token_id] _A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : List[str] = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
26
0
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __lowerCAmelCase ( __magic_name__ ): """simple docstring""" snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''CLIPImageProcessor''' snake_case_ = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self , lowerCamelCase__=None , lowerCamelCase__=None , **lowerCamelCase__ ) -> Tuple: '''simple docstring''' __lowerCamelCase = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , lowerCamelCase__ , ) __lowerCamelCase = kwargs.pop('feature_extractor' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(lowerCamelCase__ , lowerCamelCase__ ) def __call__( self , lowerCamelCase__=None , lowerCamelCase__=None , lowerCamelCase__=None , **lowerCamelCase__ ) -> str: '''simple docstring''' if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.' ) if text is not None: __lowerCamelCase = self.tokenizer(lowerCamelCase__ , return_tensors=lowerCamelCase__ , **lowerCamelCase__ ) if images is not None: __lowerCamelCase = self.image_processor(lowerCamelCase__ , return_tensors=lowerCamelCase__ , **lowerCamelCase__ ) if text is not None and images is not None: __lowerCamelCase = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowerCamelCase__ ) , tensor_type=lowerCamelCase__ ) def lowercase_ ( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> List[Any]: '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase__ , **lowerCamelCase__ ) def lowercase_ ( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> int: '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase__ , **lowerCamelCase__ ) @property def lowercase_ ( self ) -> Tuple: '''simple docstring''' __lowerCamelCase = self.tokenizer.model_input_names __lowerCamelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , lowerCamelCase__ , ) return self.image_processor_class @property def lowercase_ ( self ) -> List[Any]: '''simple docstring''' warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , lowerCamelCase__ , ) return self.image_processor
90
from math import asin, atan, cos, radians, sin, sqrt, tan _snake_case = 6_3_7_8_1_3_7.0 _snake_case = 6_3_5_6_7_5_2.3_1_4_2_4_5 _snake_case = 6378137 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Any = (AXIS_A - AXIS_B) / AXIS_A _A : Optional[int] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : List[str] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : Optional[Any] = radians(snake_case_ ) _A : str = radians(snake_case_ ) # Equation _A : Dict = sin((phi_a - phi_a) / 2 ) _A : List[str] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda _A : Optional[int] = sqrt(sin_sq_phi + (cos(snake_case_ ) * cos(snake_case_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
26
0
"""simple docstring""" from __future__ import annotations from collections.abc import Generator def _A () -> Generator[int, None, None]: """simple docstring""" SCREAMING_SNAKE_CASE_ : dict[int, int] = {} SCREAMING_SNAKE_CASE_ : List[Any] = 2 while True: SCREAMING_SNAKE_CASE_ : int = factor_map.pop(__a , __a ) if factor: SCREAMING_SNAKE_CASE_ : Union[str, Any] = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE_ : List[str] = factor else: SCREAMING_SNAKE_CASE_ : List[str] = prime yield prime prime += 1 def _A (__a = 1e10 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = sieve() SCREAMING_SNAKE_CASE_ : Optional[int] = 1 while True: SCREAMING_SNAKE_CASE_ : Union[str, Any] = next(__a ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__a ) n += 2 if __name__ == "__main__": print(solution())
91
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> Optional[int]: super().__init__(_a ) _A : Union[str, Any] = RobertaEmbeddings(_a ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> str: super().__init__(_a ) _A : Any = config.num_labels _A : Dict = config.num_hidden_layers _A : List[str] = DeeRobertaModel(_a ) _A : int = nn.Dropout(config.hidden_dropout_prob ) _A : int = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_a ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=-1 , _a=False , ) -> Any: _A : Optional[int] = self.num_layers try: _A : List[str] = self.roberta( _a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , ) _A : List[str] = outputs[1] _A : List[str] = self.dropout(_a ) _A : Optional[Any] = self.classifier(_a ) _A : List[Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: _A : List[Any] = e.message _A : Optional[int] = e.exit_layer _A : Optional[int] = outputs[0] if not self.training: _A : int = entropy(_a ) _A : int = [] _A : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression _A : Union[str, Any] = MSELoss() _A : Tuple = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Dict = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits _A : Optional[Any] = [] for highway_exit in outputs[-1]: _A : Tuple = highway_exit[0] if not self.training: highway_logits_all.append(_a ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression _A : List[str] = MSELoss() _A : Optional[int] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Tuple = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_a ) if train_highway: _A : Dict = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: _A : int = (loss,) + outputs if not self.training: _A : Optional[Any] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: _A : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
0
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar UpperCamelCase__ = TypeVar("""T""") class a__ ( Generic[T] ): def __init__( self , _A ): """simple docstring""" __lowerCAmelCase = data __lowerCAmelCase = None def __str__( self ): """simple docstring""" return f"""{self.data}""" class a__ ( Generic[T] ): def __init__( self ): """simple docstring""" __lowerCAmelCase = None def __iter__( self ): """simple docstring""" __lowerCAmelCase = self.top while node: yield node.data __lowerCAmelCase = node.next def __str__( self ): """simple docstring""" return "->".join([str(_A ) for item in self] ) def __len__( self ): """simple docstring""" return len(tuple(iter(self ) ) ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" return self.top is None def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" __lowerCAmelCase = Node(_A ) if not self.is_empty(): __lowerCAmelCase = self.top __lowerCAmelCase = node def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _A ) __lowerCAmelCase = self.top __lowerCAmelCase = self.top.next return pop_node.data def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = None if __name__ == "__main__": from doctest import testmod testmod()
92
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" def __init__( self , _a=3_0522 , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _lowercase : Optional[int] = { "configuration_data2vec_audio": ["DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP", "Data2VecAudioConfig"], "configuration_data2vec_text": [ "DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Data2VecTextConfig", "Data2VecTextOnnxConfig", ], "configuration_data2vec_vision": [ "DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP", "Data2VecVisionConfig", "Data2VecVisionOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase : Optional[int] = [ "DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST", "Data2VecAudioForAudioFrameClassification", "Data2VecAudioForCTC", "Data2VecAudioForSequenceClassification", "Data2VecAudioForXVector", "Data2VecAudioModel", "Data2VecAudioPreTrainedModel", ] _lowercase : Dict = [ "DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "Data2VecTextForCausalLM", "Data2VecTextForMaskedLM", "Data2VecTextForMultipleChoice", "Data2VecTextForQuestionAnswering", "Data2VecTextForSequenceClassification", "Data2VecTextForTokenClassification", "Data2VecTextModel", "Data2VecTextPreTrainedModel", ] _lowercase : List[Any] = [ "DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST", "Data2VecVisionForImageClassification", "Data2VecVisionForMaskedImageModeling", "Data2VecVisionForSemanticSegmentation", "Data2VecVisionModel", "Data2VecVisionPreTrainedModel", ] if is_tf_available(): _lowercase : Tuple = [ "TFData2VecVisionForImageClassification", "TFData2VecVisionForSemanticSegmentation", "TFData2VecVisionModel", "TFData2VecVisionPreTrainedModel", ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys _lowercase : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
93
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) if n == 0: return 0 _A : Tuple = float("""-inf""" ) for i in range(1,n + 1 ): _A : str = max( snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_ ) ) return max_revue def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) _A : Dict = [float("""-inf""" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: _A : List[str] = float("""-inf""" ) for i in range(1,n + 1 ): _A : Optional[Any] = max( snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_ ),) _A : Tuple = max_revenue return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. _A : List[Any] = [float("""-inf""" ) for _ in range(n + 1 )] _A : Any = 0 for i in range(1,n + 1 ): _A : Optional[Any] = max_rev[i] for j in range(1,i + 1 ): _A : int = max(snake_case_,prices[j - 1] + max_rev[i - j] ) _A : int = max_revenue_i return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): if n < 0: _A : Optional[Any] = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(snake_case_ ) if n > len(snake_case_ ): _A : Any = ( """Each integral piece of rod must have a corresponding price. """ f'''Got n = {n} but length of prices = {len(snake_case_ )}''' ) raise ValueError(snake_case_ ) def lowerCAmelCase_ ( ): _A : Tuple = [6, 10, 12, 15, 20, 23] _A : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. _A : Any = 36 _A : List[Any] = top_down_cut_rod(snake_case_,snake_case_ ) _A : List[Any] = bottom_up_cut_rod(snake_case_,snake_case_ ) _A : Dict = naive_cut_rod_recursive(snake_case_,snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
0
def __lowerCamelCase ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): """simple docstring""" if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise ValueError('''iterations must be defined as integers''' ) if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not number >= 1: raise ValueError( '''starting number must be and integer and be more than 0''' ) if not iterations >= 1: raise ValueError('''Iterations must be done more than 0 times to play FizzBuzz''' ) a :Optional[Any] = '''''' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(UpperCAmelCase_ ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
94
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( snake_case_ = "AAPL" ): _A : str = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' _A : List[Any] = BeautifulSoup(requests.get(snake_case_ ).text,"""html.parser""" ) _A : Union[str, Any] = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""",class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
0
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase : Tuple = { """configuration_xmod""": [ """XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XmodConfig""", """XmodOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Dict = [ """XMOD_PRETRAINED_MODEL_ARCHIVE_LIST""", """XmodForCausalLM""", """XmodForMaskedLM""", """XmodForMultipleChoice""", """XmodForQuestionAnswering""", """XmodForSequenceClassification""", """XmodForTokenClassification""", """XmodModel""", """XmodPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys UpperCAmelCase : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
95
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _a = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def a__ ( self , _a , _a , _a ) -> int: _A : str = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def a__ ( self , _a , _a ) -> Dict: _A : Any = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) _A : List[Any] = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) _A : Optional[int] = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def a__ ( self ) -> List[str]: _A : Any = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility _A : Dict = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) _A : Any = 3 _A : Any = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) _A : Optional[int] = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) _A : Dict = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) _A : Dict = generator.model.config.eos_token_id _A : List[str] = """<pad>""" _A : Dict = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def a__ ( self ) -> int: _A : Optional[Any] = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility _A : str = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
26
0
"""simple docstring""" # This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny - # all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and # emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files. # The latter is done by `fsmt-make-super-tiny-model.py`. # # It will be used then as "stas/tiny-wmt19-en-ru" from pathlib import Path import json import tempfile from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES lowercase__ = """tiny-wmt19-en-ru""" # Build # borrowed from a test lowercase__ = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] lowercase__ = dict(zip(vocab, range(len(vocab)))) lowercase__ = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] with tempfile.TemporaryDirectory() as tmpdirname: lowercase__ = Path(tmpdirname) lowercase__ = build_dir / VOCAB_FILES_NAMES["""src_vocab_file"""] lowercase__ = build_dir / VOCAB_FILES_NAMES["""tgt_vocab_file"""] lowercase__ = build_dir / VOCAB_FILES_NAMES["""merges_file"""] with open(src_vocab_file, """w""") as fp: fp.write(json.dumps(vocab_tokens)) with open(tgt_vocab_file, """w""") as fp: fp.write(json.dumps(vocab_tokens)) with open(merges_file, """w""") as fp: fp.write("""\n""".join(merges)) lowercase__ = FSMTTokenizer( langs=["""en""", """ru"""], src_vocab_size=len(vocab), tgt_vocab_size=len(vocab), src_vocab_file=src_vocab_file, tgt_vocab_file=tgt_vocab_file, merges_file=merges_file, ) lowercase__ = FSMTConfig( langs=["""ru""", """en"""], src_vocab_size=1000, tgt_vocab_size=1000, d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) lowercase__ = FSMTForConditionalGeneration(config) print(F"num of params {tiny_model.num_parameters()}") # Test lowercase__ = tokenizer(["""Making tiny model"""], return_tensors="""pt""") lowercase__ = tiny_model(**batch) print("""test output:""", len(outputs.logits[0])) # Save tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(F"Generated {mname_tiny}") # Upload # transformers-cli upload tiny-wmt19-en-ru
96
def lowerCAmelCase_ ( snake_case_,snake_case_ ): while b: _A , _A : List[str] = b, a % b return a def lowerCAmelCase_ ( snake_case_,snake_case_ ): return a if b == 0 else euclidean_gcd_recursive(snake_case_,a % b ) def lowerCAmelCase_ ( ): print(f'''euclidean_gcd(3, 5) = {euclidean_gcd(3,5 )}''' ) print(f'''euclidean_gcd(5, 3) = {euclidean_gcd(5,3 )}''' ) print(f'''euclidean_gcd(1, 3) = {euclidean_gcd(1,3 )}''' ) print(f'''euclidean_gcd(3, 6) = {euclidean_gcd(3,6 )}''' ) print(f'''euclidean_gcd(6, 3) = {euclidean_gcd(6,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,5 )}''' ) print(f'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,3 )}''' ) print(f'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,6 )}''' ) print(f'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,3 )}''' ) if __name__ == "__main__": main()
26
0
'''simple docstring''' from collections import defaultdict class lowercase : """simple docstring""" def __init__( self , UpperCamelCase_ , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :List[Any] = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 UpperCamelCase__ :Union[str, Any] = [ [-1 for i in range(total + 1 )] for j in range(2 ** len(UpperCamelCase_ ) ) ] UpperCamelCase__ :str = defaultdict(UpperCamelCase_ ) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 UpperCamelCase__ :Optional[int] = (1 << len(UpperCamelCase_ )) - 1 def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ ): '''simple docstring''' if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement UpperCamelCase__ :str = self.count_ways_until(UpperCamelCase_ , task_no + 1 ) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) , task_no + 1 ) # save the value. UpperCamelCase__ :Optional[int] = total_ways_util return self.dp[mask][task_no] def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' for i in range(len(UpperCamelCase_ ) ): for j in task_performed[i]: self.task[j].append(UpperCamelCase_ ) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 , 1 ) if __name__ == "__main__": __snake_case = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. __snake_case = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
97
def lowerCAmelCase_ ( snake_case_ ): if number < 0: raise ValueError("""number must not be negative""" ) return number & (number - 1) == 0 if __name__ == "__main__": import doctest doctest.testmod()
26
0
"""simple docstring""" import itertools from dataclasses import dataclass from typing import Any, Callable, Dict, List, Optional, Union import pandas as pd import pyarrow as pa import datasets import datasets.config from datasets.features.features import require_storage_cast from datasets.table import table_cast from datasets.utils.py_utils import Literal lowerCAmelCase__ : Tuple = datasets.utils.logging.get_logger(__name__) lowerCAmelCase__ : Any = ['names', 'prefix'] lowerCAmelCase__ : List[Any] = ['warn_bad_lines', 'error_bad_lines', 'mangle_dupe_cols'] lowerCAmelCase__ : List[str] = ['encoding_errors', 'on_bad_lines'] lowerCAmelCase__ : Optional[Any] = ['date_format'] @dataclass class snake_case ( datasets.BuilderConfig ): """simple docstring""" snake_case__ = "," snake_case__ = None snake_case__ = "infer" snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = True snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = False snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = True snake_case__ = True snake_case__ = False snake_case__ = True snake_case__ = None snake_case__ = "." snake_case__ = None snake_case__ = '"' snake_case__ = 0 snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = None snake_case__ = True snake_case__ = True snake_case__ = 0 snake_case__ = True snake_case__ = False snake_case__ = None snake_case__ = 1_00_00 snake_case__ = None snake_case__ = "strict" snake_case__ = "error" snake_case__ = None def __lowerCAmelCase ( self : int ): if self.delimiter is not None: UpperCAmelCase__ = self.delimiter if self.column_names is not None: UpperCAmelCase__ = self.column_names @property def __lowerCAmelCase ( self : Dict ): UpperCAmelCase__ = { 'sep': self.sep, 'header': self.header, 'names': self.names, 'index_col': self.index_col, 'usecols': self.usecols, 'prefix': self.prefix, 'mangle_dupe_cols': self.mangle_dupe_cols, 'engine': self.engine, 'converters': self.converters, 'true_values': self.true_values, 'false_values': self.false_values, 'skipinitialspace': self.skipinitialspace, 'skiprows': self.skiprows, 'nrows': self.nrows, 'na_values': self.na_values, 'keep_default_na': self.keep_default_na, 'na_filter': self.na_filter, 'verbose': self.verbose, 'skip_blank_lines': self.skip_blank_lines, 'thousands': self.thousands, 'decimal': self.decimal, 'lineterminator': self.lineterminator, 'quotechar': self.quotechar, 'quoting': self.quoting, 'escapechar': self.escapechar, 'comment': self.comment, 'encoding': self.encoding, 'dialect': self.dialect, 'error_bad_lines': self.error_bad_lines, 'warn_bad_lines': self.warn_bad_lines, 'skipfooter': self.skipfooter, 'doublequote': self.doublequote, 'memory_map': self.memory_map, 'float_precision': self.float_precision, 'chunksize': self.chunksize, 'encoding_errors': self.encoding_errors, 'on_bad_lines': self.on_bad_lines, 'date_format': self.date_format, } # some kwargs must not be passed if they don't have a default value # some others are deprecated and we can also not pass them if they are the default value for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS: if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig() ,lowerCamelCase__ ): del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 2.0 new arguments if not (datasets.config.PANDAS_VERSION.major >= 2): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 1.3 new arguments if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] return pd_read_csv_kwargs class snake_case ( datasets.ArrowBasedBuilder ): """simple docstring""" snake_case__ = CsvConfig def __lowerCAmelCase ( self : List[str] ): return datasets.DatasetInfo(features=self.config.features ) def __lowerCAmelCase ( self : Any ,lowerCamelCase__ : Optional[Any] ): if not self.config.data_files: raise ValueError(f'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) UpperCAmelCase__ = dl_manager.download_and_extract(self.config.data_files ) if isinstance(lowerCamelCase__ ,(str, list, tuple) ): UpperCAmelCase__ = data_files if isinstance(lowerCamelCase__ ,lowerCamelCase__ ): UpperCAmelCase__ = [files] UpperCAmelCase__ = [dl_manager.iter_files(lowerCamelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={'files': files} )] UpperCAmelCase__ = [] for split_name, files in data_files.items(): if isinstance(lowerCamelCase__ ,lowerCamelCase__ ): UpperCAmelCase__ = [files] UpperCAmelCase__ = [dl_manager.iter_files(lowerCamelCase__ ) for file in files] splits.append(datasets.SplitGenerator(name=lowerCamelCase__ ,gen_kwargs={'files': files} ) ) return splits def __lowerCAmelCase ( self : Union[str, Any] ,lowerCamelCase__ : pa.Table ): if self.config.features is not None: UpperCAmelCase__ = self.config.features.arrow_schema if all(not require_storage_cast(lowerCamelCase__ ) for feature in self.config.features.values() ): # cheaper cast UpperCAmelCase__ = pa.Table.from_arrays([pa_table[field.name] for field in schema] ,schema=lowerCamelCase__ ) else: # more expensive cast; allows str <-> int/float or str to Audio for example UpperCAmelCase__ = table_cast(lowerCamelCase__ ,lowerCamelCase__ ) return pa_table def __lowerCAmelCase ( self : Dict ,lowerCamelCase__ : Any ): UpperCAmelCase__ = self.config.features.arrow_schema if self.config.features else None # dtype allows reading an int column as str UpperCAmelCase__ = ( { name: dtype.to_pandas_dtype() if not require_storage_cast(lowerCamelCase__ ) else object for name, dtype, feature in zip(schema.names ,schema.types ,self.config.features.values() ) } if schema is not None else None ) for file_idx, file in enumerate(itertools.chain.from_iterable(lowerCamelCase__ ) ): UpperCAmelCase__ = pd.read_csv(lowerCamelCase__ ,iterator=lowerCamelCase__ ,dtype=lowerCamelCase__ ,**self.config.pd_read_csv_kwargs ) try: for batch_idx, df in enumerate(lowerCamelCase__ ): UpperCAmelCase__ = pa.Table.from_pandas(lowerCamelCase__ ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(lowerCamelCase__ ) except ValueError as e: logger.error(f'''Failed to read file \'{file}\' with error {type(lowerCamelCase__ )}: {e}''' ) raise
98
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def lowerCAmelCase_ ( snake_case_ ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: _A : str = k.replace(snake_case_,snake_case_ ) if k.startswith("""encoder""" ): _A : Optional[Any] = k.replace(""".attn""",""".self_attn""" ) _A : Dict = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Optional[Any] = k.replace("""norm2""","""final_layer_norm""" ) elif k.startswith("""decoder""" ): _A : str = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Any = k.replace("""norm2""","""encoder_attn_layer_norm""" ) _A : Optional[int] = k.replace("""norm3""","""final_layer_norm""" ) return k def lowerCAmelCase_ ( snake_case_ ): _A : List[Any] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: _A : str = sd.pop(snake_case_ ) _A : Optional[int] = k.replace("""layernorm_embedding""","""layer_norm""" ) assert new_k not in sd _A : Optional[int] = v _snake_case = ["START"] @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) _A : List[Any] = model["""model"""] _A : Optional[Any] = BlenderbotConfig.from_json_file(snake_case_ ) _A : List[str] = BlenderbotForConditionalGeneration(snake_case_ ) _A : Tuple = m.model.state_dict().keys() _A : Any = [] _A : Dict = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue _A : Optional[int] = rename_state_dict_key(snake_case_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: _A : Dict = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(snake_case_ ) m.model.load_state_dict(snake_case_,strict=snake_case_ ) m.half() m.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) _snake_case = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
import logging import os from .state import PartialState class A__ ( logging.LoggerAdapter ): """simple docstring""" @staticmethod def __lowercase ( lowercase) -> Any: '''simple docstring''' a__ : int = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def __lowercase ( self , lowercase , lowercase , *lowercase , **lowercase) -> str: '''simple docstring''' if PartialState._shared_state == {}: raise RuntimeError( 'You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.') a__ : Any = kwargs.pop('main_process_only' , lowercase) a__ : Dict = kwargs.pop('in_order' , lowercase) if self.isEnabledFor(lowercase): if self._should_log(lowercase): a__ , a__ : Dict = self.process(lowercase , lowercase) self.logger.log(lowercase , lowercase , *lowercase , **lowercase) elif in_order: a__ : Dict = PartialState() for i in range(state.num_processes): if i == state.process_index: a__ , a__ : List[str] = self.process(lowercase , lowercase) self.logger.log(lowercase , lowercase , *lowercase , **lowercase) state.wait_for_everyone() def A_ ( A__ , A__ = None ) -> Tuple: if log_level is None: a__ : Dict = os.environ.get('ACCELERATE_LOG_LEVEL' , A__ ) a__ : str = logging.getLogger(A__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(A__ , {} )
99
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a = None , _a = None , _a = False , **_a , ) -> int: super().__init__(features=_a , cache_dir=_a , keep_in_memory=_a , **_a ) _A : Optional[int] = Sql( cache_dir=_a , features=_a , sql=_a , con=_a , **_a , ) def a__ ( self ) -> Optional[Any]: _A : Tuple = None _A : int = None _A : Tuple = None _A : Union[str, Any] = None self.builder.download_and_prepare( download_config=_a , download_mode=_a , verification_mode=_a , base_path=_a , ) # Build dataset for splits _A : int = self.builder.as_dataset( split="""train""" , verification_mode=_a , in_memory=self.keep_in_memory ) return dataset class lowercase : def __init__( self , _a , _a , _a , _a = None , _a = None , **_a , ) -> Union[str, Any]: if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) _A : Dict = dataset _A : int = name _A : Union[str, Any] = con _A : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _A : str = num_proc _A : Optional[Any] = to_sql_kwargs def a__ ( self ) -> int: _A : Any = self.to_sql_kwargs.pop("""sql""" , _a ) _A : List[str] = self.to_sql_kwargs.pop("""con""" , _a ) _A : int = self.to_sql_kwargs.pop("""index""" , _a ) _A : List[str] = self._write(index=_a , **self.to_sql_kwargs ) return written def a__ ( self , _a ) -> Optional[int]: _A , _A , _A : List[str] = args _A : int = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs _A : str = query_table( table=self.dataset.data , key=slice(_a , offset + self.batch_size ) , indices=self.dataset._indices , ) _A : Tuple = batch.to_pandas() _A : Union[str, Any] = df.to_sql(self.name , self.con , index=_a , **_a ) return num_rows or len(_a ) def a__ ( self , _a , **_a ) -> int: _A : Any = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: _A , _A : Tuple = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _a , _a )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += num_rows return written
26
0
"""simple docstring""" import warnings from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __magic_name__ = logging.get_logger(__name__) __magic_name__ = { "nvidia/segformer-b0-finetuned-ade-512-512": ( "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json" ), # See all SegFormer models at https://huggingface.co/models?filter=segformer } class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : List[Any] = '''segformer''' def __init__( self , lowerCAmelCase__=3 , lowerCAmelCase__=4 , lowerCAmelCase__=[2, 2, 2, 2] , lowerCAmelCase__=[8, 4, 2, 1] , lowerCAmelCase__=[3_2, 6_4, 1_6_0, 2_5_6] , lowerCAmelCase__=[7, 3, 3, 3] , lowerCAmelCase__=[4, 2, 2, 2] , lowerCAmelCase__=[1, 2, 5, 8] , lowerCAmelCase__=[4, 4, 4, 4] , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.02 , lowerCAmelCase__=0.1 , lowerCAmelCase__=1E-6 , lowerCAmelCase__=2_5_6 , lowerCAmelCase__=2_5_5 , **lowerCAmelCase__ , ): super().__init__(**lowerCAmelCase__) if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False: warnings.warn( """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be""" """ removed, as the behaviour will default to that of reshape_last_stage = True.""" , lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = num_encoder_blocks __SCREAMING_SNAKE_CASE = depths __SCREAMING_SNAKE_CASE = sr_ratios __SCREAMING_SNAKE_CASE = hidden_sizes __SCREAMING_SNAKE_CASE = patch_sizes __SCREAMING_SNAKE_CASE = strides __SCREAMING_SNAKE_CASE = mlp_ratios __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = classifier_dropout_prob __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = drop_path_rate __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = decoder_hidden_size __SCREAMING_SNAKE_CASE = kwargs.get("""reshape_last_stage""" , lowerCAmelCase__) __SCREAMING_SNAKE_CASE = semantic_loss_ignore_index class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : Optional[int] = version.parse('''1.11''' ) @property def snake_case_ ( self): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ]) @property def snake_case_ ( self): return 1E-4 @property def snake_case_ ( self): return 1_2
100
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class lowercase ( UpperCamelCase__ ): _a = "fnet" def __init__( self , _a=3_2000 , _a=768 , _a=12 , _a=3072 , _a="gelu_new" , _a=0.1 , _a=512 , _a=4 , _a=0.02 , _a=1e-12 , _a=False , _a=512 , _a=3 , _a=1 , _a=2 , **_a , ) -> int: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Any = vocab_size _A : str = max_position_embeddings _A : Optional[Any] = hidden_size _A : List[str] = num_hidden_layers _A : List[str] = intermediate_size _A : List[Any] = hidden_act _A : List[str] = hidden_dropout_prob _A : List[str] = initializer_range _A : List[Any] = type_vocab_size _A : List[Any] = layer_norm_eps _A : List[str] = use_tpu_fourier_optimizations _A : str = tpu_short_seq_length
26
0
def UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): '''simple docstring''' while second != 0: lowercase = first & second first ^= second lowercase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() lowercase__ :List[str] = int(input("Enter the first number: ").strip()) lowercase__ :Optional[Any] = int(input("Enter the second number: ").strip()) print(F'{add(first, second) = }')
101
def lowerCAmelCase_ ( snake_case_ ): if n_term == "": return [] _A : list = [] for temp in range(int(snake_case_ ) ): series.append(f'''1/{temp + 1}''' if series else """1""" ) return series if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
26
0
"""simple docstring""" def lowercase ( _snake_case : list[int] ) ->list[list[int]]: """simple docstring""" __snake_case : Optional[int] = [] if len(_snake_case ) == 1: return [nums.copy()] for _ in range(len(_snake_case ) ): __snake_case : Optional[int] = nums.pop(0 ) __snake_case : Dict = permute(_snake_case ) for perm in permutations: perm.append(_snake_case ) result.extend(_snake_case ) nums.append(_snake_case ) return result def lowercase ( _snake_case : Optional[Any] ) ->Tuple: """simple docstring""" def backtrack(_snake_case : Any ): if start == len(_snake_case ) - 1: output.append(nums[:] ) else: for i in range(_snake_case , len(_snake_case ) ): __snake_case , __snake_case : Dict = nums[i], nums[start] backtrack(start + 1 ) __snake_case , __snake_case : List[str] = nums[i], nums[start] # backtrack __snake_case : Dict = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function SCREAMING_SNAKE_CASE : Union[str, Any] = permutea([1, 2, 3]) print(res) doctest.testmod()
102
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _snake_case = logging.get_logger(__name__) _snake_case = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) _snake_case = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCAmelCase_ ( snake_case_ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A : List[str] = model_type_to_module_name(snake_case_ ) _A : List[Any] = importlib.import_module(f'''.{module_name}''',"""transformers.models""" ) try: return getattr(snake_case_,snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_,"""__name__""",snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A : List[Any] = importlib.import_module("""transformers""" ) if hasattr(snake_case_,snake_case_ ): return getattr(snake_case_,snake_case_ ) return None def lowerCAmelCase_ ( snake_case_,snake_case_ = None,snake_case_ = False,snake_case_ = False,snake_case_ = None,snake_case_ = None,snake_case_ = None,snake_case_ = False,**snake_case_,): _A : Optional[int] = get_file_from_repo( snake_case_,snake_case_,cache_dir=snake_case_,force_download=snake_case_,resume_download=snake_case_,proxies=snake_case_,use_auth_token=snake_case_,revision=snake_case_,local_files_only=snake_case_,) if resolved_config_file is None: logger.info( """Could not locate the feature extractor configuration file, will try to use the model config instead.""" ) return {} with open(snake_case_,encoding="""utf-8""" ) as reader: return json.load(snake_case_ ) class lowercase : def __init__( self ) -> List[Any]: raise EnvironmentError( """AutoFeatureExtractor is designed to be instantiated """ """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(_a ) def a__ ( cls , _a , **_a ) -> Any: _A : Tuple = kwargs.pop("""config""" , _a ) _A : Tuple = kwargs.pop("""trust_remote_code""" , _a ) _A : List[Any] = True _A , _A : Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A : Tuple = config_dict.get("""feature_extractor_type""" , _a ) _A : int = None if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): _A : Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A : int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A : Optional[int] = getattr(_a , """feature_extractor_type""" , _a ) if hasattr(_a , """auto_map""" ) and "AutoFeatureExtractor" in config.auto_map: _A : Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A : Optional[Any] = feature_extractor_class_from_name(_a ) _A : List[Any] = feature_extractor_auto_map is not None _A : Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A : Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A : Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A : str = kwargs.pop("""code_revision""" , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A : Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ ( _a , _a ) -> Optional[int]: FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
26
0
import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def UpperCamelCase( __UpperCamelCase : Dict ): if isinstance(__UpperCamelCase ,collections.abc.Iterable ): return x return (x, x) @require_flax class __snake_case : def UpperCAmelCase__ ( self : Union[str, Any] , A_ : str , A_ : List[str]): pass def UpperCAmelCase__ ( self : int): pass def UpperCAmelCase__ ( self : Optional[int]): pass def UpperCAmelCase__ ( self : Any , A_ : np.ndarray , A_ : np.ndarray , A_ : float): lowerCAmelCase_ : Tuple = np.abs((a - b)).max() self.assertLessEqual(A_ , A_ , F"""Difference between torch and flax is {diff} (>= {tol}).""") def UpperCAmelCase__ ( self : Dict , A_ : List[str] , A_ : Tuple , A_ : Optional[int] , A_ : List[str] , A_ : Optional[int]=None , **A_ : Union[str, Any]): lowerCAmelCase_ : Optional[Any] = VisionTextDualEncoderConfig.from_vision_text_configs(A_ , A_) lowerCAmelCase_ : str = FlaxVisionTextDualEncoderModel(A_) lowerCAmelCase_ : Union[str, Any] = model(input_ids=A_ , pixel_values=A_ , attention_mask=A_) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], config.projection_dim)) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], config.projection_dim)) def UpperCAmelCase__ ( self : List[Any] , A_ : Union[str, Any] , A_ : Optional[int] , A_ : int , A_ : Tuple , A_ : str=None , **A_ : Any): lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = self.get_vision_text_model(A_ , A_) lowerCAmelCase_ : Optional[int] = {'''vision_model''': vision_model, '''text_model''': text_model} lowerCAmelCase_ : Tuple = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**A_) lowerCAmelCase_ : Tuple = model(input_ids=A_ , pixel_values=A_ , attention_mask=A_) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim)) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim)) def UpperCAmelCase__ ( self : Optional[int] , A_ : Optional[int] , A_ : Tuple , A_ : int , A_ : Dict , A_ : Union[str, Any]=None , **A_ : Any): lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = self.get_vision_text_model(A_ , A_) lowerCAmelCase_ : List[Any] = {'''vision_model''': vision_model, '''text_model''': text_model} lowerCAmelCase_ : List[Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**A_) lowerCAmelCase_ : Optional[Any] = model(input_ids=A_ , pixel_values=A_ , attention_mask=A_) lowerCAmelCase_ : int = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A_) lowerCAmelCase_ : str = FlaxVisionTextDualEncoderModel.from_pretrained(A_) lowerCAmelCase_ : Optional[Any] = model(input_ids=A_ , pixel_values=A_ , attention_mask=A_) lowerCAmelCase_ : Dict = after_output[0] lowerCAmelCase_ : List[Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(A_ , 1e-3) def UpperCAmelCase__ ( self : List[str] , A_ : int , A_ : Optional[int] , A_ : List[str] , A_ : Optional[Any] , A_ : int=None , **A_ : Any): lowerCAmelCase_ , lowerCAmelCase_ : Union[str, Any] = self.get_vision_text_model(A_ , A_) lowerCAmelCase_ : Dict = {'''vision_model''': vision_model, '''text_model''': text_model} lowerCAmelCase_ : Union[str, Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**A_) lowerCAmelCase_ : int = model( input_ids=A_ , pixel_values=A_ , attention_mask=A_ , output_attentions=A_) lowerCAmelCase_ : Optional[int] = output.vision_model_output.attentions self.assertEqual(len(A_) , vision_config.num_hidden_layers) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase_ : Tuple = to_atuple(vision_model.config.image_size) lowerCAmelCase_ : Any = to_atuple(vision_model.config.patch_size) lowerCAmelCase_ : str = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCAmelCase_ : Any = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) lowerCAmelCase_ : Optional[Any] = output.text_model_output.attentions self.assertEqual(len(A_) , text_config.num_hidden_layers) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def UpperCAmelCase__ ( self : int , A_ : Any , A_ : Tuple , A_ : Tuple): pt_model.to(A_) pt_model.eval() # prepare inputs lowerCAmelCase_ : int = inputs_dict lowerCAmelCase_ : Union[str, Any] = {k: torch.tensor(v.tolist()) for k, v in flax_inputs.items()} with torch.no_grad(): lowerCAmelCase_ : Any = pt_model(**A_).to_tuple() lowerCAmelCase_ : Dict = fx_model(**A_).to_tuple() self.assertEqual(len(A_) , len(A_) , '''Output lengths differ between Flax and PyTorch''') for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4]): self.assert_almost_equals(A_ , pt_output.numpy() , 4e-2) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(A_) lowerCAmelCase_ : List[Any] = FlaxVisionTextDualEncoderModel.from_pretrained(A_ , from_pt=A_) lowerCAmelCase_ : Optional[Any] = fx_model_loaded(**A_).to_tuple() self.assertEqual(len(A_) , len(A_) , '''Output lengths differ between Flax and PyTorch''') for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4]): self.assert_almost_equals(A_ , pt_output.numpy() , 4e-2) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(A_) lowerCAmelCase_ : Union[str, Any] = VisionTextDualEncoderModel.from_pretrained(A_ , from_flax=A_) pt_model_loaded.to(A_) pt_model_loaded.eval() with torch.no_grad(): lowerCAmelCase_ : Dict = pt_model_loaded(**A_).to_tuple() self.assertEqual(len(A_) , len(A_) , '''Output lengths differ between Flax and PyTorch''') for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4]): self.assert_almost_equals(A_ , pt_output_loaded.numpy() , 4e-2) def UpperCAmelCase__ ( self : Tuple , A_ : Tuple , A_ : Tuple , A_ : str): lowerCAmelCase_ : Dict = VisionTextDualEncoderConfig.from_vision_text_configs(A_ , A_) lowerCAmelCase_ : Any = VisionTextDualEncoderModel(A_) lowerCAmelCase_ : int = FlaxVisionTextDualEncoderModel(A_) lowerCAmelCase_ : Optional[Any] = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , A_) lowerCAmelCase_ : int = fx_state self.check_pt_flax_equivalence(A_ , A_ , A_) def UpperCAmelCase__ ( self : int , A_ : List[str] , A_ : Any , A_ : Optional[Any]): lowerCAmelCase_ : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(A_ , A_) lowerCAmelCase_ : Union[str, Any] = VisionTextDualEncoderModel(A_) lowerCAmelCase_ : Dict = FlaxVisionTextDualEncoderModel(A_) lowerCAmelCase_ : Optional[int] = load_flax_weights_in_pytorch_model(A_ , fx_model.params) self.check_pt_flax_equivalence(A_ , A_ , A_) def UpperCAmelCase__ ( self : int): lowerCAmelCase_ : int = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**A_) def UpperCAmelCase__ ( self : Optional[int]): lowerCAmelCase_ : Optional[int] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**A_) def UpperCAmelCase__ ( self : str): lowerCAmelCase_ : str = self.prepare_config_and_inputs() self.check_save_load(**A_) def UpperCAmelCase__ ( self : List[Any]): lowerCAmelCase_ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**A_) @is_pt_flax_cross_test def UpperCAmelCase__ ( self : Optional[Any]): lowerCAmelCase_ : List[Any] = self.prepare_config_and_inputs() lowerCAmelCase_ : str = config_inputs_dict.pop('''vision_config''') lowerCAmelCase_ : Optional[Any] = config_inputs_dict.pop('''text_config''') lowerCAmelCase_ : Tuple = config_inputs_dict self.check_equivalence_pt_to_flax(A_ , A_ , A_) self.check_equivalence_flax_to_pt(A_ , A_ , A_) @slow def UpperCAmelCase__ ( self : Optional[int]): lowerCAmelCase_ , lowerCAmelCase_ : List[Any] = self.get_pretrained_model_and_inputs() lowerCAmelCase_ : Any = model_a(**A_) lowerCAmelCase_ : int = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(A_) lowerCAmelCase_ : int = FlaxVisionTextDualEncoderModel.from_pretrained(A_) lowerCAmelCase_ : int = model_a(**A_) lowerCAmelCase_ : Optional[int] = after_outputs[0] lowerCAmelCase_ : Optional[Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(A_ , 1e-5) @require_flax class __snake_case ( UpperCamelCase_ ,unittest.TestCase ): def UpperCAmelCase__ ( self : int): lowerCAmelCase_ : int = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( '''hf-internal-testing/tiny-random-vit''' , '''hf-internal-testing/tiny-bert''' , vision_from_pt=A_ , text_from_pt=A_ , ) lowerCAmelCase_ : int = 1_3 lowerCAmelCase_ : int = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) lowerCAmelCase_ : int = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size) lowerCAmelCase_ : str = random_attention_mask([batch_size, 4]) lowerCAmelCase_ : List[Any] = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def UpperCAmelCase__ ( self : Optional[Any] , A_ : Dict , A_ : List[Any]): lowerCAmelCase_ : str = FlaxViTModel(A_) lowerCAmelCase_ : int = FlaxBertModel(A_) return vision_model, text_model def UpperCAmelCase__ ( self : str): lowerCAmelCase_ : int = FlaxViTModelTester(self) lowerCAmelCase_ : Any = FlaxBertModelTester(self) lowerCAmelCase_ : Any = vit_model_tester.prepare_config_and_inputs() lowerCAmelCase_ : int = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase_ , lowerCAmelCase_ : Tuple = vision_config_and_inputs lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class __snake_case ( UpperCamelCase_ ,unittest.TestCase ): def UpperCAmelCase__ ( self : Dict): lowerCAmelCase_ : List[Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( '''hf-internal-testing/tiny-random-clip''' , '''hf-internal-testing/tiny-bert''' , vision_from_pt=A_ , text_from_pt=A_ , ) lowerCAmelCase_ : List[str] = 1_3 lowerCAmelCase_ : Union[str, Any] = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) lowerCAmelCase_ : str = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size) lowerCAmelCase_ : int = random_attention_mask([batch_size, 4]) lowerCAmelCase_ : Tuple = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def UpperCAmelCase__ ( self : int , A_ : List[Any] , A_ : str): lowerCAmelCase_ : List[str] = FlaxCLIPVisionModel(A_) lowerCAmelCase_ : int = FlaxBertModel(A_) return vision_model, text_model def UpperCAmelCase__ ( self : List[Any]): lowerCAmelCase_ : List[str] = FlaxCLIPVisionModelTester(self) lowerCAmelCase_ : Dict = FlaxBertModelTester(self) lowerCAmelCase_ : Any = clip_model_tester.prepare_config_and_inputs() lowerCAmelCase_ : Any = bert_model_tester.prepare_config_and_inputs() lowerCAmelCase_ , lowerCAmelCase_ : Union[str, Any] = vision_config_and_inputs lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : int = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self : Union[str, Any]): lowerCAmelCase_ : int = FlaxVisionTextDualEncoderModel.from_pretrained('''clip-italian/clip-italian''' , logit_scale_init_value=1.0) lowerCAmelCase_ : str = VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian''') lowerCAmelCase_ : str = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') lowerCAmelCase_ : Optional[int] = processor( text=['''una foto di un gatto''', '''una foto di un cane'''] , images=A_ , padding=A_ , return_tensors='''np''') lowerCAmelCase_ : Any = model(**A_) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCAmelCase_ : Any = np.array([[1.228_4727, 0.310_4122]]) self.assertTrue(np.allclose(outputs.logits_per_image , A_ , atol=1e-3))
103
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=False , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Dict: _A : str = parent _A : int = batch_size _A : Optional[int] = num_channels _A : List[Any] = image_size _A : int = min_resolution _A : Optional[int] = max_resolution _A : Any = do_resize _A : List[str] = size if size is not None else {"""height""": 18, """width""": 20} _A : Optional[int] = do_thumbnail _A : str = do_align_axis _A : List[Any] = do_pad _A : Optional[Any] = do_normalize _A : Tuple = image_mean _A : List[str] = image_std def a__ ( self ) -> Optional[int]: return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = DonutImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : List[str] = DonutImageProcessingTester(self ) @property def a__ ( self ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[Any]: _A : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) self.assertTrue(hasattr(_a , """do_thumbnail""" ) ) self.assertTrue(hasattr(_a , """do_align_long_axis""" ) ) self.assertTrue(hasattr(_a , """do_pad""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) def a__ ( self ) -> List[Any]: _A : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) _A : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def a__ ( self ) -> Union[str, Any]: pass @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : Any = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Dict: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : int = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : List[str] = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : str = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
26
0
'''simple docstring''' from __future__ import annotations from math import pi, sqrt def _A ( A__ , A__ ): """simple docstring""" if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
104
from __future__ import annotations import numpy as np def lowerCAmelCase_ ( snake_case_ ): return np.maximum(0,snake_case_ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
26
0
"""simple docstring""" import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() a : Optional[Any] = logging.get_logger(__name__) a : int = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } def _SCREAMING_SNAKE_CASE ( _lowercase : int , _lowercase : int , _lowercase : List[str] , _lowercase : List[str] , _lowercase : Dict ) ->Optional[Any]: '''simple docstring''' for attribute in key.split("." ): a : Dict = getattr(_lowercase , _lowercase ) if weight_type is not None: a : Optional[Any] = getattr(_lowercase , _lowercase ).shape else: a : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": a : List[str] = value elif weight_type == "weight_g": a : int = value elif weight_type == "weight_v": a : int = value elif weight_type == "bias": a : Tuple = value else: a : Union[str, Any] = value logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def _SCREAMING_SNAKE_CASE ( _lowercase : Any , _lowercase : List[str] , _lowercase : int ) ->Any: '''simple docstring''' a : Dict = [] a : int = fairseq_model.state_dict() a : Tuple = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): a : int = False if "conv_layers" in name: load_conv_layer( _lowercase , _lowercase , _lowercase , _lowercase , hf_model.config.feat_extract_norm == "group" , ) a : Optional[Any] = True else: for key, mapped_key in MAPPING.items(): a : Optional[Any] = "hubert." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key if key in name or (key.split("w2v_model." )[-1] == name.split("." )[0] and not is_finetuned): a : List[str] = True if "*" in mapped_key: a : Union[str, Any] = name.split(_lowercase )[0].split("." )[-2] a : str = mapped_key.replace("*" , _lowercase ) if "weight_g" in name: a : Optional[int] = "weight_g" elif "weight_v" in name: a : Optional[Any] = "weight_v" elif "weight" in name: a : Tuple = "weight" elif "bias" in name: a : Tuple = "bias" else: a : Union[str, Any] = None set_recursively(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) continue if not is_used: unused_weights.append(_lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) def _SCREAMING_SNAKE_CASE ( _lowercase : Optional[int] , _lowercase : Optional[Any] , _lowercase : Optional[Any] , _lowercase : str , _lowercase : Optional[Any] ) ->List[Any]: '''simple docstring''' a : List[Any] = full_name.split("conv_layers." )[-1] a : Any = name.split("." ) a : List[str] = int(items[0] ) a : Any = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) a : Tuple = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) a : str = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) a : str = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) a : str = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(_lowercase ) @torch.no_grad() def _SCREAMING_SNAKE_CASE ( _lowercase : Any , _lowercase : Tuple , _lowercase : List[str]=None , _lowercase : Dict=None , _lowercase : int=True ) ->List[Any]: '''simple docstring''' if config_path is not None: a : Tuple = HubertConfig.from_pretrained(_lowercase ) else: a : Any = HubertConfig() if is_finetuned: if dict_path: a : str = Dictionary.load(_lowercase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq a : int = target_dict.pad_index a : Optional[int] = target_dict.bos_index a : Dict = target_dict.eos_index a : Optional[int] = len(target_dict.symbols ) a : List[str] = os.path.join(_lowercase , "vocab.json" ) if not os.path.isdir(_lowercase ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(_lowercase ) ) return os.makedirs(_lowercase , exist_ok=_lowercase ) with open(_lowercase , "w" , encoding="utf-8" ) as vocab_handle: json.dump(target_dict.indices , _lowercase ) a : Optional[int] = WavaVecaCTCTokenizer( _lowercase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=_lowercase , ) a : int = True if config.feat_extract_norm == "layer" else False a : Any = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=_lowercase , return_attention_mask=_lowercase , ) a : str = WavaVecaProcessor(feature_extractor=_lowercase , tokenizer=_lowercase ) processor.save_pretrained(_lowercase ) a : int = HubertForCTC(_lowercase ) else: a : str = HubertModel(_lowercase ) if is_finetuned: a, a, a : List[str] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: a, a, a : int = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) a : Optional[int] = model[0].eval() recursively_load_weights(_lowercase , _lowercase , _lowercase ) hf_wavavec.save_pretrained(_lowercase ) if __name__ == "__main__": a : Tuple = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) a : Optional[Any] = parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
105
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) _snake_case = getLogger(__name__) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ = 8,snake_case_ = 1024,snake_case_="val",snake_case_=None,snake_case_=False,snake_case_="summarization",snake_case_=None,snake_case_=1,snake_case_ = None,snake_case_="",**snake_case_,): _A : Dict = str(snake_case_ ) assert local_rank is not None torch.distributed.init_process_group(backend="""nccl""",rank=snake_case_ ) _A : Tuple = Path(snake_case_ ) _A : List[Any] = save_dir.joinpath(f'''rank_{local_rank}_output.json''' ) torch.cuda.set_device(snake_case_ ) _A : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained(snake_case_ ).cuda() if fpaa: _A : Any = model.half() # determine if we need to increase num_beams use_task_specific_params(snake_case_,snake_case_ ) # update config with task specific params _A : str = generate_kwargs.pop("""num_beams""",model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: _A : int = num_return_sequences _A : Optional[Any] = AutoTokenizer.from_pretrained(snake_case_ ) logger.info(f'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. if max_source_length is None: _A : Optional[int] = tokenizer.model_max_length if prefix is None: _A : Tuple = prefix or getattr(model.config,"""prefix""","""""" ) or """""" _A : Optional[int] = SeqaSeqDataset( snake_case_,snake_case_,snake_case_,max_target_length=1024,type_path=snake_case_,n_obs=snake_case_,prefix=snake_case_,**snake_case_,) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. _A : Optional[int] = ds.make_sortish_sampler(snake_case_,distributed=snake_case_,add_extra_examples=snake_case_,shuffle=snake_case_ ) _A : Dict = DataLoader(snake_case_,sampler=snake_case_,batch_size=snake_case_,collate_fn=ds.collate_fn ) _A : Optional[Any] = [] for batch in tqdm(snake_case_ ): _A : Tuple = model.generate( input_ids=batch["""input_ids"""].to(model.device ),attention_mask=batch["""attention_mask"""].to(model.device ),num_return_sequences=snake_case_,num_beams=snake_case_,**snake_case_,) _A : Any = tokenizer.batch_decode(snake_case_,skip_special_tokens=snake_case_,clean_up_tokenization_spaces=snake_case_ ) _A : Dict = batch["""ids"""] if num_return_sequences > 1: _A : Any = chunks(snake_case_,snake_case_ ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(snake_case_ ): results.append({"""pred""": pred, """id""": ids[i].item()} ) save_json(snake_case_,snake_case_ ) return results, sampler.num_replicas def lowerCAmelCase_ ( ): _A : Tuple = argparse.ArgumentParser( epilog="""Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate""" ) parser.add_argument("""--data_dir""",type=snake_case_,help="""like cnn_dm/test.source""" ) parser.add_argument( """--model_name""",type=snake_case_,help="""like facebook/bart-large-cnn,t5-base, etc.""",default="""sshleifer/distilbart-xsum-12-3""",) parser.add_argument("""--save_dir""",type=snake_case_,help="""where to save""",default="""tmp_gen""" ) parser.add_argument("""--max_source_length""",type=snake_case_,default=snake_case_ ) parser.add_argument( """--type_path""",type=snake_case_,default="""test""",help="""which subset to evaluate typically train/val/test""" ) parser.add_argument("""--task""",type=snake_case_,default="""summarization""",help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""",type=snake_case_,default=8,required=snake_case_,help="""batch size""" ) parser.add_argument( """--local_rank""",type=snake_case_,default=-1,required=snake_case_,help="""should be passed by distributed.launch""" ) parser.add_argument( """--n_obs""",type=snake_case_,default=snake_case_,required=snake_case_,help="""How many observations. Defaults to all.""" ) parser.add_argument( """--num_return_sequences""",type=snake_case_,default=1,required=snake_case_,help="""How many sequences to return""" ) parser.add_argument( """--sync_timeout""",type=snake_case_,default=600,required=snake_case_,help="""How long should master process wait for other processes to finish.""",) parser.add_argument("""--src_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument("""--tgt_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument( """--prefix""",type=snake_case_,required=snake_case_,default=snake_case_,help="""will be added to the begininng of src examples""" ) parser.add_argument("""--fp16""",action="""store_true""" ) parser.add_argument("""--debug""",action="""store_true""" ) _A : Union[str, Any] = time.time() _A , _A : List[str] = parser.parse_known_args() _A : List[str] = parse_numeric_n_bool_cl_kwargs(snake_case_ ) if generate_kwargs and args.local_rank <= 0: print(f'''parsed the following generate kwargs: {generate_kwargs}''' ) _A : Dict = Path(args.save_dir + """_tmp""" ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) # this handles locking. _A : int = list(json_save_dir.glob("""rank_*.json""" ) ) if intermediate_files: raise ValueError(f'''Found files at {json_save_dir} please move or remove them.''' ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. _A : Any = {} if args.src_lang is not None: _A : int = args.src_lang if args.tgt_lang is not None: _A : Dict = args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=snake_case_ ) _A , _A : str = eval_data_dir( args.data_dir,snake_case_,args.model_name,type_path=args.type_path,bs=args.bs,fpaa=args.fpaa,task=args.task,local_rank=args.local_rank,n_obs=args.n_obs,max_source_length=args.max_source_length,num_return_sequences=args.num_return_sequences,prefix=args.prefix,dataset_kwargs=snake_case_,**snake_case_,) if args.local_rank <= 0: _A : List[Any] = Path(args.save_dir ) save_dir.mkdir(exist_ok=snake_case_ ) _A : Tuple = gather_results_from_each_node(snake_case_,snake_case_,args.sync_timeout ) _A : Optional[int] = combine_partial_results(snake_case_ ) if args.num_return_sequences > 1: _A : Optional[Any] = save_dir.joinpath("""pseudolabel_results.json""" ) print(f'''Saving aggregated results at {save_path}, intermediate in {json_save_dir}/''' ) save_json(snake_case_,snake_case_ ) return _A : List[str] = Path(args.data_dir ).joinpath(args.type_path + """.target""" ) with open(snake_case_ ) as f: _A : int = [x.rstrip() for x in f.readlines()][: len(snake_case_ )] # Calculate metrics, save metrics, and save _generations.txt _A : Dict = """translation""" in args.task _A : Optional[Any] = calculate_bleu if calc_bleu else calculate_rouge _A : Tuple = """bleu""" if calc_bleu else """rouge""" _A : Dict = score_fn(snake_case_,snake_case_ ) _A : List[Any] = len(snake_case_ ) _A : Optional[int] = time.time() - start_time _A : Dict = round(runtime / metrics["""n_obs"""],4 ) _A : Dict = num_replicas # TODO(@stas00): add whatever metadata to metrics _A : Any = save_dir.joinpath(f'''{args.type_path}_{metric_name}.json''' ) save_json(snake_case_,snake_case_,indent=snake_case_ ) print(snake_case_ ) write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}_generations.txt''' ) ) if args.debug: write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}.target''' ) ) else: shutil.rmtree(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = [] for partial_result in partial_results: records.extend(snake_case_ ) _A : Optional[Any] = sorted(snake_case_,key=lambda snake_case_ : x["id"] ) _A : List[str] = [x["""pred"""] for x in records] return preds def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): # WAIT FOR lots of .json files _A : Optional[Any] = time.time() logger.info("""waiting for all nodes to finish""" ) _A : List[str] = None while (time.time() - start_wait) < timeout: _A : str = list(save_dir.glob("""rank_*.json""" ) ) if len(snake_case_ ) < num_replicas: continue try: # make sure all json files are fully saved _A : List[str] = lmap(snake_case_,snake_case_ ) return json_data except JSONDecodeError: continue else: raise TimeoutError("""Rank 0 gave up on waiting for other processes""" ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
26
0
"""simple docstring""" from urllib.parse import quote import pytest from datasets.utils.hub import hf_hub_url @pytest.mark.parametrize('''repo_id''' , ['''canonical_dataset_name''', '''org-name/dataset-name'''] ) @pytest.mark.parametrize('''path''' , ['''filename.csv''', '''filename with blanks.csv'''] ) @pytest.mark.parametrize('''revision''' , [None, '''v2'''] ) def __SCREAMING_SNAKE_CASE ( A_ , A_ , A_ ): lowerCAmelCase__ : Tuple = hf_hub_url(repo_id=A_ , path=A_ , revision=A_ ) assert url == f'https://huggingface.co/datasets/{repo_id}/resolve/{revision or "main"}/{quote(A_ )}'
106
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Any: _A : Tuple = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) _A : List[Any] = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" _A : List[str] = model(_a )["""last_hidden_state"""] _A : Union[str, Any] = tf.TensorShape((1, 10, 768) ) self.assertEqual(output.shape , _a ) # compare the actual values for a slice. _A : List[Any] = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
26
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase : Tuple = logging.get_logger(__name__) __lowerCAmelCase : Dict = { 'facebook/vit-mae-base': 'https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class snake_case__ (_UpperCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = """vit_mae""" def __init__( self : Dict , __lowerCamelCase : Union[str, Any]=7_68 , __lowerCamelCase : str=12 , __lowerCamelCase : Dict=12 , __lowerCamelCase : str=30_72 , __lowerCamelCase : List[str]="gelu" , __lowerCamelCase : int=0.0 , __lowerCamelCase : Tuple=0.0 , __lowerCamelCase : List[Any]=0.02 , __lowerCamelCase : List[Any]=1e-12 , __lowerCamelCase : Dict=2_24 , __lowerCamelCase : Optional[Any]=16 , __lowerCamelCase : List[str]=3 , __lowerCamelCase : List[Any]=True , __lowerCamelCase : int=16 , __lowerCamelCase : int=5_12 , __lowerCamelCase : Union[str, Any]=8 , __lowerCamelCase : Any=20_48 , __lowerCamelCase : Tuple=0.75 , __lowerCamelCase : Any=False , **__lowerCamelCase : Tuple , ) -> str: super().__init__(**__lowerCamelCase ) a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = initializer_range a = layer_norm_eps a = image_size a = patch_size a = num_channels a = qkv_bias a = decoder_num_attention_heads a = decoder_hidden_size a = decoder_num_hidden_layers a = decoder_intermediate_size a = mask_ratio a = norm_pix_loss
107
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class lowercase ( UpperCamelCase__ ): _a = 42 class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a , _a , _a , ) -> List[Any]: super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: if latents is None: _A : str = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) _A : Union[str, Any] = latents.to(_a ) _A : int = latents * scheduler.init_noise_sigma return latents def a__ ( self , _a=0 ) -> Optional[Any]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _A : str = torch.device(F'''cuda:{gpu_id}''' ) _A : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def a__ ( self ) -> List[Any]: if self.device != torch.device("""meta""" ) or not hasattr(self.image_encoder , """_hf_hook""" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , """_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 def a__ ( self , _a , _a , _a , _a , ) -> Tuple: if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): _A : int = torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): _A : Dict = self.image_processor(_a , return_tensors="""pt""" ).pixel_values[0].unsqueeze(0 ) _A : int = image.to(dtype=self.image_encoder.dtype , device=_a ) _A : List[Any] = self.image_encoder(_a )["""last_hidden_state"""] _A : List[Any] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 _A : Dict = image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: _A : str = torch.zeros_like(_a ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _A : List[str] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a = 1 , _a = 25 , _a = None , _a = None , _a = 4.0 , _a = 64 , _a = "pil" , _a = True , ) -> Union[str, Any]: if isinstance(_a , PIL.Image.Image ): _A : List[Any] = 1 elif isinstance(_a , torch.Tensor ): _A : Any = image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): _A : Union[str, Any] = len(_a ) else: raise ValueError( F'''`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}''' ) _A : Optional[int] = self._execution_device _A : Tuple = batch_size * num_images_per_prompt _A : List[Any] = guidance_scale > 1.0 _A : Optional[Any] = self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) _A : Optional[int] = self.scheduler.timesteps _A : List[str] = self.prior.config.num_embeddings _A : int = self.prior.config.embedding_dim _A : Optional[Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim _A : List[Any] = latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance _A : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _A : int = self.scheduler.scale_model_input(_a , _a ) _A : Tuple = self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance _A , _A : Optional[Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: _A , _A : Dict = noise_pred.chunk(2 ) _A : Tuple = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) _A : int = self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) _A : List[str] = [] for i, latent in enumerate(_a ): print() _A : List[str] = self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(_a ) _A : List[Any] = torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}''' ) _A : List[str] = images.cpu().numpy() if output_type == "pil": _A : List[Any] = [self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , """final_offload_hook""" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
26
0
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ = '''▁''' lowerCAmelCase__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE__ ( lowercase , unittest.TestCase ): """simple docstring""" a : str =BigBirdTokenizer a : Union[str, Any] =BigBirdTokenizerFast a : Tuple =True a : Any =True def lowercase__ ( self ): """simple docstring""" super().setUp() lowerCAmelCase : str = self.tokenizer_class(snake_case__ , keep_accents=snake_case__ ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "<s>" lowerCAmelCase : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case__ ) , snake_case__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case__ ) , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<unk>" ) self.assertEqual(vocab_keys[1] , "<s>" ) self.assertEqual(vocab_keys[-1] , "[MASK]" ) self.assertEqual(len(snake_case__ ) , 1_004 ) def lowercase__ ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def lowercase__ ( self ): """simple docstring""" if not self.test_rust_tokenizer: return lowerCAmelCase : Tuple = self.get_tokenizer() lowerCAmelCase : Optional[int] = self.get_rust_tokenizer() lowerCAmelCase : Tuple = "I was born in 92000, and this is falsé." lowerCAmelCase : Optional[int] = tokenizer.tokenize(snake_case__ ) lowerCAmelCase : int = rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) lowerCAmelCase : Tuple = tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) lowerCAmelCase : int = rust_tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = self.get_rust_tokenizer() lowerCAmelCase : Tuple = tokenizer.encode(snake_case__ ) lowerCAmelCase : List[Any] = rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = BigBirdTokenizer(snake_case__ , keep_accents=snake_case__ ) lowerCAmelCase : Union[str, Any] = tokenizer.tokenize("This is a test" ) self.assertListEqual(snake_case__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(snake_case__ ) , [285, 46, 10, 170, 382] , ) lowerCAmelCase : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( snake_case__ , [ 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", "é", ".", ] , ) lowerCAmelCase : str = tokenizer.convert_tokens_to_ids(snake_case__ ) self.assertListEqual( snake_case__ , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) lowerCAmelCase : List[str] = tokenizer.convert_ids_to_tokens(snake_case__ ) self.assertListEqual( snake_case__ , [ 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>", ".", ] , ) @cached_property def lowercase__ ( self ): """simple docstring""" return BigBirdTokenizer.from_pretrained("google/bigbird-roberta-base" ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = "Hello World!" lowerCAmelCase : Any = [65, 18_536, 2_260, 101, 66] self.assertListEqual(snake_case__ , self.big_tokenizer.encode(snake_case__ ) ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth" ) # fmt: off lowerCAmelCase : List[str] = [65, 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, 66] # noqa: E231 # fmt: on self.assertListEqual(snake_case__ , self.big_tokenizer.encode(snake_case__ ) ) @require_torch @slow def lowercase__ ( self ): """simple docstring""" import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence lowerCAmelCase : Dict = list(self.big_tokenizer.get_vocab().keys() )[:10] lowerCAmelCase : int = " ".join(snake_case__ ) lowerCAmelCase : Dict = self.big_tokenizer.encode_plus(snake_case__ , return_tensors="pt" , return_token_type_ids=snake_case__ ) lowerCAmelCase : Any = self.big_tokenizer.batch_encode_plus( [sequence + " " + sequence] , return_tensors="pt" , return_token_type_ids=snake_case__ ) lowerCAmelCase : str = BigBirdConfig(attention_type="original_full" ) lowerCAmelCase : Any = BigBirdModel(snake_case__ ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**snake_case__ ) model(**snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = BigBirdTokenizer.from_pretrained("google/bigbird-roberta-base" ) lowerCAmelCase : Union[str, Any] = tokenizer.decode(tokenizer("Paris is the [MASK]." ).input_ids ) self.assertTrue(decoded_text == "[CLS] Paris is the[MASK].[SEP]" ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = {"input_ids": [[65, 39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114, 66], [65, 448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=snake_case__ , model_name="google/bigbird-roberta-base" , revision="215c99f1600e06f83acce68422f2035b2b5c3510" , )
108
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( snake_case_ ): print("""Loading config file...""" ) def flatten_yaml_as_dict(snake_case_,snake_case_="",snake_case_="." ): _A : Union[str, Any] = [] for k, v in d.items(): _A : Optional[int] = parent_key + sep + k if parent_key else k if isinstance(snake_case_,collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(snake_case_,snake_case_,sep=snake_case_ ).items() ) else: items.append((new_key, v) ) return dict(snake_case_ ) _A : List[Any] = argparse.Namespace() with open(snake_case_,"""r""" ) as yaml_file: try: _A : List[Any] = yaml.load(snake_case_,Loader=yaml.FullLoader ) _A : Optional[int] = flatten_yaml_as_dict(snake_case_ ) for k, v in flat_cfg.items(): setattr(snake_case_,snake_case_,snake_case_ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(snake_case_,str(snake_case_ ) ) ) return config def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[Any] = MobileViTVaConfig() _A : Tuple = False # dataset if task_name.startswith("""imagenet1k_""" ): _A : Dict = 1000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : int = 384 else: _A : int = 256 _A : List[str] = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _A : Union[str, Any] = 21000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : str = 384 else: _A : List[Any] = 256 _A : List[str] = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _A : int = 151 _A : int = 512 _A : Optional[int] = """ade20k-id2label.json""" _A : Any = True elif task_name.startswith("""voc_""" ): _A : List[Any] = 21 _A : Dict = 512 _A : Dict = """pascal-voc-id2label.json""" _A : int = True # orig_config _A : Any = load_orig_config_file(snake_case_ ) assert getattr(snake_case_,"""model.classification.name""",-1 ) == "mobilevit_v2", "Invalid model" _A : List[Any] = getattr(snake_case_,"""model.classification.mitv2.width_multiplier""",1.0 ) assert ( getattr(snake_case_,"""model.classification.mitv2.attn_norm_layer""",-1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _A : str = getattr(snake_case_,"""model.classification.activation.name""","""swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _A : Optional[int] = getattr(snake_case_,"""model.segmentation.output_stride""",16 ) if "_deeplabv3" in task_name: _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_rates""",[12, 24, 36] ) _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_out_channels""",512 ) _A : str = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_dropout""",0.1 ) # id2label _A : List[Any] = """huggingface/label-files""" _A : List[Any] = json.load(open(hf_hub_download(snake_case_,snake_case_,repo_type="""dataset""" ),"""r""" ) ) _A : str = {int(snake_case_ ): v for k, v in idalabel.items()} _A : str = idalabel _A : Dict = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Any = dct.pop(snake_case_ ) _A : Union[str, Any] = val def lowerCAmelCase_ ( snake_case_,snake_case_=False ): if base_model: _A : Optional[int] = """""" else: _A : Dict = """mobilevitv2.""" _A : int = [] for k in state_dict.keys(): if k[:8] == "encoder.": _A : Any = k[8:] else: _A : List[str] = k if ".block." in k: _A : Any = k_new.replace(""".block.""",""".""" ) if ".conv." in k: _A : List[Any] = k_new.replace(""".conv.""",""".convolution.""" ) if ".norm." in k: _A : Any = k_new.replace(""".norm.""",""".normalization.""" ) if "conv_1." in k: _A : int = k_new.replace("""conv_1.""",f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.''',f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: _A : Tuple = k_new.replace(""".exp_1x1.""",""".expand_1x1.""" ) if ".red_1x1." in k: _A : Optional[int] = k_new.replace(""".red_1x1.""",""".reduce_1x1.""" ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: _A : Optional[int] = k_new.replace(f'''layer_{i}.0.''',f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: _A : Union[str, Any] = k_new.replace(f'''layer_{i}.1.local_rep.0.''',f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: _A : str = k_new.replace(f'''layer_{i}.1.local_rep.1.''',f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: _A : Optional[int] = [0, 1] elif i == 4: _A : Union[str, Any] = [0, 1, 2, 3] elif i == 5: _A : Optional[Any] = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: _A : Union[str, Any] = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''',f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: _A : List[str] = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''',f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.1.conv_proj.''',f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_attn.0.""","""layernorm_before.""" ) if "pre_norm_attn.1." in k: _A : str = k_new.replace("""pre_norm_attn.1.""","""attention.""" ) if "pre_norm_ffn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_ffn.0.""","""layernorm_after.""" ) if "pre_norm_ffn.1." in k: _A : Dict = k_new.replace("""pre_norm_ffn.1.""","""ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _A : List[str] = k_new.replace("""pre_norm_ffn.3.""","""ffn.conv2.""" ) if "classifier.1." in k: _A : List[str] = k_new.replace("""classifier.1.""","""classifier.""" ) if "seg_head." in k: _A : List[Any] = k_new.replace("""seg_head.""","""segmentation_head.""" ) if ".aspp_layer." in k: _A : List[Any] = k_new.replace(""".aspp_layer.""",""".""" ) if ".aspp_pool." in k: _A : Optional[Any] = k_new.replace(""".aspp_pool.""",""".""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(snake_case_ ) for k in keys_to_ignore: state_dict.pop(snake_case_,snake_case_ ) def lowerCAmelCase_ ( ): _A : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _A : List[Any] = Image.open(requests.get(snake_case_,stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[Any] = get_mobilevitva_config(snake_case_,snake_case_ ) # load original state_dict _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _A : Optional[Any] = MobileViTVaForSemanticSegmentation(snake_case_ ).eval() _A : str = False else: _A : int = MobileViTVaForImageClassification(snake_case_ ).eval() _A : List[Any] = False # remove and rename some keys of load the original model _A : List[Any] = checkpoint remove_unused_keys(snake_case_ ) _A : Optional[Any] = create_rename_keys(snake_case_,base_model=snake_case_ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case_,snake_case_,snake_case_ ) # load modified state_dict model.load_state_dict(snake_case_ ) # Check outputs on an image, prepared by MobileViTImageProcessor _A : str = MobileViTImageProcessor(crop_size=config.image_size,size=config.image_size + 32 ) _A : List[Any] = image_processor(images=prepare_img(),return_tensors="""pt""" ) _A : Optional[Any] = model(**snake_case_ ) # verify classification model if task_name.startswith("""imagenet""" ): _A : List[Any] = outputs.logits _A : Optional[int] = logits.argmax(-1 ).item() print("""Predicted class:""",model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _A : int = torch.tensor([-1.63_36e00, -7.32_04e-02, -5.18_83e-01] ) assert torch.allclose(logits[0, :3],snake_case_,atol=1e-4 ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you'd like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) _snake_case = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
26
0
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=[1, 2, 1] , _SCREAMING_SNAKE_CASE=[2, 2, 4] , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=2.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=8 , ) -> List[str]: '''simple docstring''' UpperCAmelCase : str = parent UpperCAmelCase : str = batch_size UpperCAmelCase : int = image_size UpperCAmelCase : str = patch_size UpperCAmelCase : Any = num_channels UpperCAmelCase : Dict = embed_dim UpperCAmelCase : Any = depths UpperCAmelCase : str = num_heads UpperCAmelCase : Optional[Any] = window_size UpperCAmelCase : int = mlp_ratio UpperCAmelCase : Any = qkv_bias UpperCAmelCase : Union[str, Any] = hidden_dropout_prob UpperCAmelCase : List[Any] = attention_probs_dropout_prob UpperCAmelCase : Any = drop_path_rate UpperCAmelCase : List[Any] = hidden_act UpperCAmelCase : Tuple = use_absolute_embeddings UpperCAmelCase : Optional[Any] = patch_norm UpperCAmelCase : Tuple = layer_norm_eps UpperCAmelCase : List[Any] = initializer_range UpperCAmelCase : Optional[int] = is_training UpperCAmelCase : Any = scope UpperCAmelCase : List[str] = use_labels UpperCAmelCase : Union[str, Any] = type_sequence_label_size UpperCAmelCase : Any = encoder_stride def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' UpperCAmelCase : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase : Optional[Any] = None if self.use_labels: UpperCAmelCase : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Any = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: '''simple docstring''' UpperCAmelCase : Tuple = SwinvaModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCAmelCase : str = model(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) UpperCAmelCase : str = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' UpperCAmelCase : int = SwinvaForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCAmelCase : List[Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images UpperCAmelCase : List[Any] = 1 UpperCAmelCase : Tuple = SwinvaForMaskedImageModeling(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCAmelCase : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase : Union[str, Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] = self.type_sequence_label_size UpperCAmelCase : str = SwinvaForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCAmelCase : Optional[Any] = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Tuple = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[int] = config_and_inputs UpperCAmelCase : Dict = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): __lowerCAmelCase : Tuple = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) __lowerCAmelCase : Union[str, Any] = ( {'feature-extraction': SwinvaModel, 'image-classification': SwinvaForImageClassification} if is_torch_available() else {} ) __lowerCAmelCase : Dict = False __lowerCAmelCase : Optional[Any] = False __lowerCAmelCase : Optional[int] = False __lowerCAmelCase : str = False def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : str = SwinvaModelTester(self ) UpperCAmelCase : Tuple = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , embed_dim=37 ) def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0.""" ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason="""Swinv2 does not use inputs_embeds""" ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : int = model_class(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCAmelCase : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : List[str] = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase : List[str] = [*signature.parameters.keys()] UpperCAmelCase : int = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : List[str] = True for model_class in self.all_model_classes: UpperCAmelCase : str = True UpperCAmelCase : str = False UpperCAmelCase : Dict = True UpperCAmelCase : Dict = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCAmelCase : Union[str, Any] = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : Any = outputs.attentions UpperCAmelCase : Tuple = len(self.model_tester.depths ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) # check that output_attentions also work using config del inputs_dict["output_attentions"] UpperCAmelCase : Any = True UpperCAmelCase : Optional[int] = config.window_size**2 UpperCAmelCase : Dict = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCAmelCase : Optional[int] = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : Dict = outputs.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) UpperCAmelCase : Any = len(_SCREAMING_SNAKE_CASE ) # Check attention is always last and order is fine UpperCAmelCase : int = True UpperCAmelCase : int = True UpperCAmelCase : Union[str, Any] = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCAmelCase : int = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) if hasattr(self.model_tester , """num_hidden_states_types""" ): UpperCAmelCase : List[Any] = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states UpperCAmelCase : List[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(_SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : Optional[int] = outputs.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: '''simple docstring''' UpperCAmelCase : Dict = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCAmelCase : int = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : List[Any] = outputs.hidden_states UpperCAmelCase : Optional[Any] = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) # Swinv2 has a different seq_length UpperCAmelCase : Union[str, Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) UpperCAmelCase : Tuple = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) UpperCAmelCase : List[str] = outputs.reshaped_hidden_states self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Any = reshaped_hidden_states[0].shape UpperCAmelCase : List[str] = ( reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Optional[int] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: UpperCAmelCase : List[str] = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase : Optional[Any] = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : List[str] = 3 UpperCAmelCase : Union[str, Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) UpperCAmelCase : Dict = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) UpperCAmelCase : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) UpperCAmelCase : Dict = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: UpperCAmelCase : List[Any] = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase : Union[str, Any] = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (padded_height, padded_width) ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : Tuple = SwinvaModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Any = _config_zero_init(_SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: UpperCAmelCase : List[str] = model_class(config=_SCREAMING_SNAKE_CASE ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ) if is_vision_available() else None ) @slow def SCREAMING_SNAKE_CASE ( self ) -> str: '''simple docstring''' UpperCAmelCase : Union[str, Any] = SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ).to( _SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[str] = self.default_image_processor UpperCAmelCase : Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) UpperCAmelCase : Tuple = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): UpperCAmelCase : Dict = model(**_SCREAMING_SNAKE_CASE ) # verify the logits UpperCAmelCase : int = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = torch.tensor([-0.3947, -0.4306, 0.0026] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
109
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
0
# XXX: we want transformers master here - in the absense of conftest manipulating sys.path: # hack it in for now: import sys from pathlib import Path lowerCAmelCase = Path(__file__).resolve().parents[3] / 'src' sys.path.insert(1, str(git_repo_path)) import dataclasses # noqa import io # noqa import itertools # noqa import json # noqa import os # noqa import unittest # noqa from copy import deepcopy # noqa from parameterized import parameterized # noqa from transformers import TrainingArguments, is_torch_available # noqa from transformers.deepspeed import is_deepspeed_available # noqa from transformers.file_utils import WEIGHTS_NAME # noqa from transformers.testing_utils import ( # noqa CaptureLogger, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, mockenv_context, require_deepspeed, require_torch_gpu, require_torch_multi_gpu, slow, ) from transformers.trainer_utils import set_seed # noqa set_seed(42) lowerCAmelCase = {'base': 'patrickvonplaten/wav2vec2_tiny_random', 'robust': 'patrickvonplaten/wav2vec2_tiny_random_robust'} lowerCAmelCase = 'zero2' lowerCAmelCase = 'zero3' lowerCAmelCase = [ZEROa, ZEROa] def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = parameterized.to_safe_name('''_'''.join(str(SCREAMING_SNAKE_CASE ) for x in param.args ) ) return f'{func.__name__}_{param_based_name}' # Cartesian-product of zero stages with models to test lowerCAmelCase = list(itertools.product(stages, models.keys())) @slow @require_deepspeed @require_torch_gpu class _a ( UpperCamelCase__ ): @parameterized.expand(UpperCamelCase_ , name_func=UpperCamelCase_ ) def lowerCamelCase_ ( self: str , UpperCamelCase_: List[str] , UpperCamelCase_: Dict ) -> Optional[Any]: """simple docstring""" self.run_and_check( stage=UpperCamelCase_ , model=UpperCamelCase_ , distributed=UpperCamelCase_ , fpaa=UpperCamelCase_ , ) @require_torch_multi_gpu @parameterized.expand(UpperCamelCase_ , name_func=UpperCamelCase_ ) def lowerCamelCase_ ( self: int , UpperCamelCase_: int , UpperCamelCase_: Optional[Any] ) -> List[Any]: """simple docstring""" self.run_and_check( stage=UpperCamelCase_ , model=UpperCamelCase_ , distributed=UpperCamelCase_ , fpaa=UpperCamelCase_ , ) @parameterized.expand(UpperCamelCase_ , name_func=UpperCamelCase_ ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Dict ) -> Dict: """simple docstring""" self.run_and_check( stage=UpperCamelCase_ , model=UpperCamelCase_ , distributed=UpperCamelCase_ , fpaa=UpperCamelCase_ , ) @require_torch_multi_gpu @parameterized.expand(UpperCamelCase_ , name_func=UpperCamelCase_ ) def lowerCamelCase_ ( self: Tuple , UpperCamelCase_: int , UpperCamelCase_: int ) -> str: """simple docstring""" self.run_and_check( stage=UpperCamelCase_ , model=UpperCamelCase_ , distributed=UpperCamelCase_ , fpaa=UpperCamelCase_ , ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase_: Optional[Any] ) -> Tuple: """simple docstring""" pass def lowerCamelCase_ ( self: Optional[Any] , UpperCamelCase_: str , UpperCamelCase_: str , UpperCamelCase_: int = 10 , UpperCamelCase_: bool = True , UpperCamelCase_: bool = True , UpperCamelCase_: bool = True , ) -> List[str]: """simple docstring""" lowercase__ = models[model] lowercase__ = self.run_trainer( stage=UpperCamelCase_ , model_name=UpperCamelCase_ , eval_steps=UpperCamelCase_ , num_train_epochs=1 , distributed=UpperCamelCase_ , fpaa=UpperCamelCase_ , ) self.do_checks(UpperCamelCase_ ) return output_dir def lowerCamelCase_ ( self: List[str] , UpperCamelCase_: str , UpperCamelCase_: str , UpperCamelCase_: int = 10 , UpperCamelCase_: int = 1 , UpperCamelCase_: bool = True , UpperCamelCase_: bool = True , ) -> List[str]: """simple docstring""" lowercase__ = self.get_auto_remove_tmp_dir('''./xxx''' , after=UpperCamelCase_ ) lowercase__ = f'\n --model_name_or_path {model_name}\n --dataset_name hf-internal-testing/librispeech_asr_dummy\n --dataset_config_name clean\n --train_split_name validation\n --validation_split_name validation\n --output_dir {output_dir}\n --num_train_epochs {str(UpperCamelCase_ )}\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 2\n --evaluation_strategy steps\n --learning_rate 5e-4\n --warmup_steps 8\n --orthography timit\n --preprocessing_num_workers 1\n --group_by_length\n --freeze_feature_extractor\n --report_to none\n --save_steps 0\n --eval_steps {eval_steps}\n --report_to none\n '.split() if fpaa: args.extend(['''--fp16'''] ) # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true, # hence the separate config files lowercase__ = f'--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json'.split() lowercase__ = [f'{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py'] lowercase__ = self.get_launcher(UpperCamelCase_ ) lowercase__ = launcher + script + args + ds_args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(UpperCamelCase_ , env=self.get_env() ) return output_dir def lowerCamelCase_ ( self: Tuple , UpperCamelCase_: Union[str, Any]=False ) -> str: """simple docstring""" lowercase__ = min(2 , get_gpu_count() ) if distributed else 1 return f'deepspeed --num_nodes 1 --num_gpus {num_gpus}'.split()
110
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = 1 @register_to_config def __init__( self , _a=2000 , _a=0.1 , _a=20 , _a=1e-3 ) -> List[Any]: _A : Dict = None _A : List[Any] = None _A : Dict = None def a__ ( self , _a , _a = None ) -> Union[str, Any]: _A : Union[str, Any] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def a__ ( self , _a , _a , _a , _a=None ) -> Dict: if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _A : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _A : List[Any] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _A : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): _A : List[Any] = std.unsqueeze(-1 ) _A : int = -score / std # compute _A : Tuple = -1.0 / len(self.timesteps ) _A : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _A : List[str] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _A : Union[str, Any] = beta_t.unsqueeze(-1 ) _A : Tuple = -0.5 * beta_t * x _A : Tuple = torch.sqrt(_a ) _A : Dict = drift - diffusion**2 * score _A : Dict = x + drift * dt # add noise _A : Any = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) _A : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Optional[Any]: return self.config.num_train_timesteps
26
0
"""simple docstring""" import math def __a ( __lowerCamelCase, __lowerCamelCase = 0, __lowerCamelCase = 0 ): UpperCAmelCase_ : Tuple = end or len(snake_case_ ) for i in range(snake_case_, snake_case_ ): UpperCAmelCase_ : List[str] = i UpperCAmelCase_ : Optional[Any] = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: UpperCAmelCase_ : int = array[temp_index - 1] temp_index -= 1 UpperCAmelCase_ : Dict = temp_index_value return array def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): # Max Heap UpperCAmelCase_ : Dict = index UpperCAmelCase_ : Union[str, Any] = 2 * index + 1 # Left Node UpperCAmelCase_ : List[Any] = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: UpperCAmelCase_ : str = left_index if right_index < heap_size and array[largest] < array[right_index]: UpperCAmelCase_ : str = right_index if largest != index: UpperCAmelCase_ : List[Any] = array[largest], array[index] heapify(snake_case_, snake_case_, snake_case_ ) def __a ( __lowerCamelCase ): UpperCAmelCase_ : Any = len(snake_case_ ) for i in range(n // 2, -1, -1 ): heapify(snake_case_, snake_case_, snake_case_ ) for i in range(n - 1, 0, -1 ): UpperCAmelCase_ : Union[str, Any] = array[0], array[i] heapify(snake_case_, 0, snake_case_ ) return array def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): UpperCAmelCase_ : int = low UpperCAmelCase_ : List[Any] = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i UpperCAmelCase_ : str = array[j], array[i] i += 1 def __a ( __lowerCamelCase ): if len(snake_case_ ) == 0: return array UpperCAmelCase_ : Tuple = 2 * math.ceil(math.loga(len(snake_case_ ) ) ) UpperCAmelCase_ : List[str] = 16 return intro_sort(snake_case_, 0, len(snake_case_ ), snake_case_, snake_case_ ) def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): while end - start > size_threshold: if max_depth == 0: return heap_sort(snake_case_ ) max_depth -= 1 UpperCAmelCase_ : Optional[Any] = median_of_a(snake_case_, snake_case_, start + ((end - start) // 2) + 1, end - 1 ) UpperCAmelCase_ : List[Any] = partition(snake_case_, snake_case_, snake_case_, snake_case_ ) intro_sort(snake_case_, snake_case_, snake_case_, snake_case_, snake_case_ ) UpperCAmelCase_ : Optional[Any] = p return insertion_sort(snake_case_, snake_case_, snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() _a = input('Enter numbers separated by a comma : ').strip() _a = [float(item) for item in user_input.split(',')] print(sort(unsorted))
61
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: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "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", }, } _snake_case = { "google/fnet-base": 512, "google/fnet-large": 512, } _snake_case = "▁" class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["input_ids", "token_type_ids"] _a = FNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=True , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Optional[int]: # 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. _A : int = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) _A : Optional[int] = do_lower_case _A : List[Any] = remove_space _A : str = keep_accents _A : int = vocab_file _A : int = False if not self.vocab_file else True def a__ ( self , _a , _a = None ) -> List[int]: _A : str = [self.sep_token_id] _A : Dict = [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 a__ ( self , _a , _a = None ) -> List[int]: _A : Any = [self.sep_token_id] _A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : List[str] = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
26
0
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib a_ :List[Any] = threading.Lock() a_ :str = None a_ :Optional[Any] = { "debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL, } a_ :List[str] = logging.WARNING a_ :str = True def lowercase_ (): snake_case__ : str = os.getenv('TRANSFORMERS_VERBOSITY' , snake_case_ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F'''Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, ''' F'''has to be one of: { ", ".join(log_levels.keys() ) }''' ) return _default_log_level def lowercase_ (): return __name__.split('.' )[0] def lowercase_ (): return logging.getLogger(_get_library_name() ) def lowercase_ (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return snake_case__ : Union[str, Any] = logging.StreamHandler() # Set sys.stderr as stream. snake_case__ : Tuple = sys.stderr.flush # Apply our default configuration to the library root logger. snake_case__ : int = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) snake_case__ : List[str] = False def lowercase_ (): global _default_handler with _lock: if not _default_handler: return snake_case__ : List[str] = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) snake_case__ : Tuple = None def lowercase_ (): return log_levels def lowercase_ (A : Union[str, Any] = None ): if name is None: snake_case__ : Optional[int] = _get_library_name() _configure_library_root_logger() return logging.getLogger(snake_case_ ) def lowercase_ (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def lowercase_ (A : Union[str, Any] ): _configure_library_root_logger() _get_library_root_logger().setLevel(snake_case_ ) def lowercase_ (): return set_verbosity(snake_case_ ) def lowercase_ (): return set_verbosity(snake_case_ ) def lowercase_ (): return set_verbosity(snake_case_ ) def lowercase_ (): return set_verbosity(snake_case_ ) def lowercase_ (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def lowercase_ (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def lowercase_ (A : Dict ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(snake_case_ ) def lowercase_ (A : Dict ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(snake_case_ ) def lowercase_ (): _configure_library_root_logger() snake_case__ : Union[str, Any] = False def lowercase_ (): _configure_library_root_logger() snake_case__ : List[Any] = True def lowercase_ (): snake_case__ : Union[str, Any] = _get_library_root_logger().handlers for handler in handlers: snake_case__ : str = logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s' ) handler.setFormatter(snake_case_ ) def lowercase_ (): snake_case__ : Any = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(snake_case_ ) def lowercase_ (self : str , *A : Any , **A : Union[str, Any] ): snake_case__ : str = os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS' , snake_case_ ) if no_advisory_warnings: return self.warning(*snake_case_ , **snake_case_ ) a_ :Optional[Any] = warning_advice @functools.lru_cache(snake_case_ ) def lowercase_ (self : str , *A : int , **A : int ): self.warning(*snake_case_ , **snake_case_ ) a_ :str = warning_once class snake_case__ : """simple docstring""" def __init__( self : Optional[int], *_snake_case : List[Any], **_snake_case : Tuple ) ->List[Any]: # pylint: disable=unused-argument snake_case__ : List[str] = args[0] if args else None def __iter__( self : List[str] ) ->Optional[Any]: return iter(self._iterator ) def __getattr__( self : List[str], _snake_case : List[Any] ) ->Union[str, Any]: def empty_fn(*_snake_case : Optional[Any], **_snake_case : Tuple ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Union[str, Any] ) ->Tuple: return self def __exit__( self : int, _snake_case : str, _snake_case : List[str], _snake_case : Optional[Any] ) ->List[str]: return class snake_case__ : """simple docstring""" def __call__( self : List[str], *_snake_case : Any, **_snake_case : List[Any] ) ->Union[str, Any]: if _tqdm_active: return tqdm_lib.tqdm(*_a, **_a ) else: return EmptyTqdm(*_a, **_a ) def lowercase_ ( self : List[str], *_snake_case : List[str], **_snake_case : List[str] ) ->Dict: snake_case__ : Optional[Any] = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_a, **_a ) def lowercase_ ( self : Tuple ) ->Union[str, Any]: if _tqdm_active: return tqdm_lib.tqdm.get_lock() a_ :str = _tqdm_cls() def lowercase_ (): global _tqdm_active return bool(_tqdm_active ) def lowercase_ (): global _tqdm_active snake_case__ : Optional[int] = True hf_hub_utils.enable_progress_bars() def lowercase_ (): global _tqdm_active snake_case__ : List[str] = False hf_hub_utils.disable_progress_bars()
277
from math import asin, atan, cos, radians, sin, sqrt, tan _snake_case = 6_3_7_8_1_3_7.0 _snake_case = 6_3_5_6_7_5_2.3_1_4_2_4_5 _snake_case = 6378137 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Any = (AXIS_A - AXIS_B) / AXIS_A _A : Optional[int] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : List[str] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : Optional[Any] = radians(snake_case_ ) _A : str = radians(snake_case_ ) # Equation _A : Dict = sin((phi_a - phi_a) / 2 ) _A : List[str] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda _A : Optional[int] = sqrt(sin_sq_phi + (cos(snake_case_ ) * cos(snake_case_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
26
0
import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def UpperCAmelCase_( a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """_float_tensor""", """decoder.output_projection.weight""", ] for k in ignore_keys: state_dict.pop(snake_case_ , snake_case_ ) def UpperCAmelCase_( a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(snake_case_ , snake_case_ , bias=snake_case_ ) SCREAMING_SNAKE_CASE : str = emb.weight.data return lin_layer def UpperCAmelCase_( a__ , a__="facebook/mbart-large-en-ro" , a__=False , a__=False ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case_ , map_location='''cpu''' )["""model"""] remove_ignore_keys_(snake_case_ ) SCREAMING_SNAKE_CASE : Optional[int] = state_dict["""encoder.embed_tokens.weight"""].shape[0] SCREAMING_SNAKE_CASE : Optional[Any] = MBartConfig.from_pretrained(snake_case_ , vocab_size=snake_case_ ) if mbart_aa and finetuned: SCREAMING_SNAKE_CASE : Union[str, Any] = """relu""" SCREAMING_SNAKE_CASE : str = state_dict["""decoder.embed_tokens.weight"""] SCREAMING_SNAKE_CASE : Optional[Any] = MBartForConditionalGeneration(snake_case_ ) model.model.load_state_dict(snake_case_ ) if finetuned: SCREAMING_SNAKE_CASE : Dict = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": a__ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''fairseq_path''', type=str, help='''bart.large, bart.large.cnn or a path to a model.pt on local filesystem.''' ) parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument( '''--hf_config''', default='''facebook/mbart-large-cc25''', type=str, help='''Which huggingface architecture to use: mbart-large''', ) parser.add_argument('''--mbart_50''', action='''store_true''', help='''whether the model is mMART-50 checkpoint''') parser.add_argument('''--finetuned''', action='''store_true''', help='''whether the model is a fine-tuned checkpoint''') a__ : Tuple = parser.parse_args() a__ : str = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
313
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> Optional[int]: super().__init__(_a ) _A : Union[str, Any] = RobertaEmbeddings(_a ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> str: super().__init__(_a ) _A : Any = config.num_labels _A : Dict = config.num_hidden_layers _A : List[str] = DeeRobertaModel(_a ) _A : int = nn.Dropout(config.hidden_dropout_prob ) _A : int = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_a ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=-1 , _a=False , ) -> Any: _A : Optional[int] = self.num_layers try: _A : List[str] = self.roberta( _a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , ) _A : List[str] = outputs[1] _A : List[str] = self.dropout(_a ) _A : Optional[Any] = self.classifier(_a ) _A : List[Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: _A : List[Any] = e.message _A : Optional[int] = e.exit_layer _A : Optional[int] = outputs[0] if not self.training: _A : int = entropy(_a ) _A : int = [] _A : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression _A : Union[str, Any] = MSELoss() _A : Tuple = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Dict = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits _A : Optional[Any] = [] for highway_exit in outputs[-1]: _A : Tuple = highway_exit[0] if not self.training: highway_logits_all.append(_a ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression _A : List[str] = MSELoss() _A : Optional[int] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Tuple = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_a ) if train_highway: _A : Dict = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: _A : int = (loss,) + outputs if not self.training: _A : Optional[Any] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: _A : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
0
from __future__ import annotations import bisect def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 0 , UpperCAmelCase = -1 ) -> Tuple: """simple docstring""" if hi < 0: lowerCamelCase__ : List[Any] = len(snake_case_ ) while lo < hi: lowerCamelCase__ : str = lo + (hi - lo) // 2 if sorted_collection[mid] < item: lowerCamelCase__ : str = mid + 1 else: lowerCamelCase__ : Tuple = mid return lo def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 0 , UpperCAmelCase = -1 ) -> Optional[Any]: """simple docstring""" if hi < 0: lowerCamelCase__ : List[str] = len(snake_case_ ) while lo < hi: lowerCamelCase__ : Tuple = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: lowerCamelCase__ : Union[str, Any] = mid + 1 else: lowerCamelCase__ : Any = mid return lo def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 0 , UpperCAmelCase = -1 ) -> int: """simple docstring""" sorted_collection.insert(bisect_left(snake_case_ , snake_case_ , snake_case_ , snake_case_ ) , snake_case_ ) def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 0 , UpperCAmelCase = -1 ) -> Optional[Any]: """simple docstring""" sorted_collection.insert(bisect_right(snake_case_ , snake_case_ , snake_case_ , snake_case_ ) , snake_case_ ) def _a ( UpperCAmelCase , UpperCAmelCase ) -> int: """simple docstring""" lowerCamelCase__ : Optional[int] = 0 lowerCamelCase__ : Optional[Any] = len(snake_case_ ) - 1 while left <= right: lowerCamelCase__ : List[str] = left + (right - left) // 2 lowerCamelCase__ : List[Any] = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: lowerCamelCase__ : List[Any] = midpoint - 1 else: lowerCamelCase__ : List[str] = midpoint + 1 return None def _a ( UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: """simple docstring""" lowerCamelCase__ : int = bisect.bisect_left(snake_case_ , snake_case_ ) if index != len(snake_case_ ) and sorted_collection[index] == item: return index return None def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Any: """simple docstring""" if right < left: return None lowerCamelCase__ : List[str] = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(snake_case_ , snake_case_ , snake_case_ , midpoint - 1 ) else: return binary_search_by_recursion(snake_case_ , snake_case_ , midpoint + 1 , snake_case_ ) if __name__ == "__main__": _A : Union[str, Any] = input('Enter numbers separated by comma:\n').strip() _A : List[Any] = sorted(int(item) for item in user_input.split(',')) _A : Tuple = int(input('Enter a single number to be found in the list:\n')) _A : List[str] = binary_search(collection, target) if result is None: print(F'''{target} was not found in {collection}.''') else: print(F'''{target} was found at position {result} in {collection}.''')
142
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" def __init__( self , _a=3_0522 , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
0
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a = get_logger(__name__) __a = Path(__file__).parent / '''model_card_template.md''' __a = uuida().hex __a = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES __a = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES __a = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __lowercase ( _UpperCamelCase = None ) ->Union[str, Any]: """simple docstring""" lowercase : List[Any] = f"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f"""; torch/{_torch_version}""" if is_flax_available(): ua += f"""; jax/{_jax_version}""" ua += f"""; flax/{_flax_version}""" if is_onnx_available(): ua += f"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''', '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(snake_case_, snake_case_ ): ua += "; " + "; ".join(f"""{k}/{v}""" for k, v in user_agent.items() ) elif isinstance(snake_case_, snake_case_ ): ua += "; " + user_agent return ua def __lowercase ( _UpperCamelCase, _UpperCamelCase = None, _UpperCamelCase = None ) ->List[str]: """simple docstring""" if token is None: lowercase : Union[str, Any] = HfFolder.get_token() if organization is None: lowercase : Any = whoami(snake_case_ )["""name"""] return f"""{username}/{model_id}""" else: return f"""{organization}/{model_id}""" def __lowercase ( _UpperCamelCase, _UpperCamelCase ) ->int: """simple docstring""" if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(snake_case_, '''local_rank''' ) and args.local_rank not in [-1, 0]: return lowercase : Any = args.hub_token if hasattr(snake_case_, '''hub_token''' ) else None lowercase : int = get_full_repo_name(snake_case_, token=snake_case_ ) lowercase : Optional[Any] = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''', license='''apache-2.0''', library_name='''diffusers''', tags=[], datasets=args.dataset_name, metrics=[], ), template_path=snake_case_, model_name=snake_case_, repo_name=snake_case_, dataset_name=args.dataset_name if hasattr(snake_case_, '''dataset_name''' ) else None, learning_rate=args.learning_rate, train_batch_size=args.train_batch_size, eval_batch_size=args.eval_batch_size, gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(snake_case_, '''gradient_accumulation_steps''' ) else None ), adam_betaa=args.adam_betaa if hasattr(snake_case_, '''adam_beta1''' ) else None, adam_betaa=args.adam_betaa if hasattr(snake_case_, '''adam_beta2''' ) else None, adam_weight_decay=args.adam_weight_decay if hasattr(snake_case_, '''adam_weight_decay''' ) else None, adam_epsilon=args.adam_epsilon if hasattr(snake_case_, '''adam_epsilon''' ) else None, lr_scheduler=args.lr_scheduler if hasattr(snake_case_, '''lr_scheduler''' ) else None, lr_warmup_steps=args.lr_warmup_steps if hasattr(snake_case_, '''lr_warmup_steps''' ) else None, ema_inv_gamma=args.ema_inv_gamma if hasattr(snake_case_, '''ema_inv_gamma''' ) else None, ema_power=args.ema_power if hasattr(snake_case_, '''ema_power''' ) else None, ema_max_decay=args.ema_max_decay if hasattr(snake_case_, '''ema_max_decay''' ) else None, mixed_precision=args.mixed_precision, ) lowercase : int = os.path.join(args.output_dir, '''README.md''' ) model_card.save(snake_case_ ) def __lowercase ( _UpperCamelCase, _UpperCamelCase = None ) ->Dict: """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash lowercase : int = str(Path(snake_case_ ).as_posix() ) lowercase : List[Any] = re.search(R'''snapshots/([^/]+)/''', snake_case_ ) if search is None: return None lowercase : int = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(snake_case_ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) __a = os.path.join(hf_cache_home, '''diffusers''') def __lowercase ( _UpperCamelCase = None, _UpperCamelCase = None ) ->Union[str, Any]: """simple docstring""" if new_cache_dir is None: lowercase : Union[str, Any] = DIFFUSERS_CACHE if old_cache_dir is None: lowercase : str = old_diffusers_cache lowercase : Any = Path(snake_case_ ).expanduser() lowercase : Tuple = Path(snake_case_ ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): lowercase : Union[str, Any] = new_cache_dir / old_blob_path.relative_to(snake_case_ ) new_blob_path.parent.mkdir(parents=snake_case_, exist_ok=snake_case_ ) os.replace(snake_case_, snake_case_ ) try: os.symlink(snake_case_, snake_case_ ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): __a = 0 else: with open(cache_version_file) as f: try: __a = int(f.read()) except ValueError: __a = 0 if cache_version < 1: __a = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: __a = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'''There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ''' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'''There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ''' '''the directory exists and can be written to.''' ) def __lowercase ( _UpperCamelCase, _UpperCamelCase = None ) ->List[Any]: """simple docstring""" if variant is not None: lowercase : List[str] = weights_name.split('''.''' ) lowercase : Dict = splits[:-1] + [variant] + splits[-1:] lowercase : Any = """.""".join(snake_case_ ) return weights_name def __lowercase ( _UpperCamelCase, *, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase=None, ) ->Any: """simple docstring""" lowercase : Tuple = str(snake_case_ ) if os.path.isfile(snake_case_ ): return pretrained_model_name_or_path elif os.path.isdir(snake_case_ ): if os.path.isfile(os.path.join(snake_case_, snake_case_ ) ): # Load from a PyTorch checkpoint lowercase : Tuple = os.path.join(snake_case_, snake_case_ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(snake_case_, snake_case_, snake_case_ ) ): lowercase : List[Any] = os.path.join(snake_case_, snake_case_, snake_case_ ) return model_file else: raise EnvironmentError( f"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""" ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(snake_case_ ).base_version ) >= version.parse('''0.20.0''' ) ): try: lowercase : List[Any] = hf_hub_download( snake_case_, filename=_add_variant(snake_case_, snake_case_ ), cache_dir=snake_case_, force_download=snake_case_, proxies=snake_case_, resume_download=snake_case_, local_files_only=snake_case_, use_auth_token=snake_case_, user_agent=snake_case_, subfolder=snake_case_, revision=revision or commit_hash, ) warnings.warn( f"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.""", snake_case_, ) return model_file except: # noqa: E722 warnings.warn( f"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(snake_case_, snake_case_ )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(snake_case_, snake_case_ )}\' so that the correct variant file can be added.""", snake_case_, ) try: # 2. Load model file as usual lowercase : Dict = hf_hub_download( snake_case_, filename=snake_case_, cache_dir=snake_case_, force_download=snake_case_, proxies=snake_case_, resume_download=snake_case_, local_files_only=snake_case_, use_auth_token=snake_case_, user_agent=snake_case_, subfolder=snake_case_, revision=revision or commit_hash, ) return model_file except RepositoryNotFoundError: raise EnvironmentError( f"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( f"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' f"""\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.""" ) except EntryNotFoundError: raise EnvironmentError( f"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""" ) except HTTPError as err: raise EnvironmentError( f"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""" ) except ValueError: raise EnvironmentError( f"""We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it""" f""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" f""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( f"""Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' f"""Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory """ f"""containing a file named {weights_name}""" )
337
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) if n == 0: return 0 _A : Tuple = float("""-inf""" ) for i in range(1,n + 1 ): _A : str = max( snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_ ) ) return max_revue def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) _A : Dict = [float("""-inf""" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: _A : List[str] = float("""-inf""" ) for i in range(1,n + 1 ): _A : Optional[Any] = max( snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_ ),) _A : Tuple = max_revenue return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. _A : List[Any] = [float("""-inf""" ) for _ in range(n + 1 )] _A : Any = 0 for i in range(1,n + 1 ): _A : Optional[Any] = max_rev[i] for j in range(1,i + 1 ): _A : int = max(snake_case_,prices[j - 1] + max_rev[i - j] ) _A : int = max_revenue_i return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): if n < 0: _A : Optional[Any] = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(snake_case_ ) if n > len(snake_case_ ): _A : Any = ( """Each integral piece of rod must have a corresponding price. """ f'''Got n = {n} but length of prices = {len(snake_case_ )}''' ) raise ValueError(snake_case_ ) def lowerCAmelCase_ ( ): _A : Tuple = [6, 10, 12, 15, 20, 23] _A : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. _A : Any = 36 _A : List[Any] = top_down_cut_rod(snake_case_,snake_case_ ) _A : List[Any] = bottom_up_cut_rod(snake_case_,snake_case_ ) _A : Dict = naive_cut_rod_recursive(snake_case_,snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
0
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowerCamelCase__ ( _a , _a): assert isinstance(snake_case_ , snake_case_) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True]) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Optional[int] = tmp_path / """cache""" SCREAMING_SNAKE_CASE : Optional[int] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE : Dict = ParquetDatasetReader(snake_case_ , cache_dir=snake_case_ , keep_in_memory=snake_case_).read() _check_parquet_dataset(snake_case_ , snake_case_) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Union[str, Any] = tmp_path / """cache""" SCREAMING_SNAKE_CASE : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} SCREAMING_SNAKE_CASE : Optional[int] = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE : Tuple = ( Features({feature: Value(snake_case_) for feature, dtype in features.items()}) if features is not None else None ) SCREAMING_SNAKE_CASE : Optional[int] = ParquetDatasetReader(snake_case_ , features=snake_case_ , cache_dir=snake_case_).read() _check_parquet_dataset(snake_case_ , snake_case_) @pytest.mark.parametrize("split" , [None, NamedSplit("train"), "train", "test"]) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Dict = tmp_path / """cache""" SCREAMING_SNAKE_CASE : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} SCREAMING_SNAKE_CASE : Optional[int] = ParquetDatasetReader(snake_case_ , cache_dir=snake_case_ , split=snake_case_).read() _check_parquet_dataset(snake_case_ , snake_case_) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list]) def lowerCamelCase__ ( _a , _a , _a): if issubclass(snake_case_ , snake_case_): SCREAMING_SNAKE_CASE : str = parquet_path elif issubclass(snake_case_ , snake_case_): SCREAMING_SNAKE_CASE : List[Any] = [parquet_path] SCREAMING_SNAKE_CASE : Tuple = tmp_path / """cache""" SCREAMING_SNAKE_CASE : List[str] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} SCREAMING_SNAKE_CASE : str = ParquetDatasetReader(snake_case_ , cache_dir=snake_case_).read() _check_parquet_dataset(snake_case_ , snake_case_) def lowerCamelCase__ ( _a , _a , _a=("train",)): assert isinstance(snake_case_ , snake_case_) for split in splits: SCREAMING_SNAKE_CASE : str = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True]) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Optional[Any] = tmp_path / """cache""" SCREAMING_SNAKE_CASE : Any = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE : List[str] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=snake_case_ , keep_in_memory=snake_case_).read() _check_parquet_datasetdict(snake_case_ , snake_case_) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def lowerCamelCase__ ( _a , _a , _a): SCREAMING_SNAKE_CASE : Union[str, Any] = tmp_path / """cache""" SCREAMING_SNAKE_CASE : Any = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} SCREAMING_SNAKE_CASE : str = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE : List[Any] = ( Features({feature: Value(snake_case_) for feature, dtype in features.items()}) if features is not None else None ) SCREAMING_SNAKE_CASE : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=snake_case_ , cache_dir=snake_case_).read() _check_parquet_datasetdict(snake_case_ , snake_case_) @pytest.mark.parametrize("split" , [None, NamedSplit("train"), "train", "test"]) def lowerCamelCase__ ( _a , _a , _a): if split: SCREAMING_SNAKE_CASE : List[str] = {split: parquet_path} else: SCREAMING_SNAKE_CASE : List[Any] = """train""" SCREAMING_SNAKE_CASE : Union[str, Any] = {"""train""": parquet_path, """test""": parquet_path} SCREAMING_SNAKE_CASE : str = tmp_path / """cache""" SCREAMING_SNAKE_CASE : Dict = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} SCREAMING_SNAKE_CASE : Union[str, Any] = ParquetDatasetReader(snake_case_ , cache_dir=snake_case_).read() _check_parquet_datasetdict(snake_case_ , snake_case_ , splits=list(path.keys())) assert all(dataset[split].split == split for split in path.keys()) def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : Dict = ParquetDatasetWriter(snake_case_ , tmp_path / "foo.parquet") assert writer.write() > 0 SCREAMING_SNAKE_CASE : List[str] = pq.ParquetFile(tmp_path / "foo.parquet") SCREAMING_SNAKE_CASE : Dict = pf.read() assert dataset.data.table == output_table def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : int = str(shared_datadir / "test_image_rgb.jpg") SCREAMING_SNAKE_CASE : Any = {"""image""": [image_path]} SCREAMING_SNAKE_CASE : Union[str, Any] = Features({"image": Image()}) SCREAMING_SNAKE_CASE : Union[str, Any] = Dataset.from_dict(snake_case_ , features=snake_case_) SCREAMING_SNAKE_CASE : str = ParquetDatasetWriter(snake_case_ , tmp_path / "foo.parquet") assert writer.write() > 0 SCREAMING_SNAKE_CASE : int = Dataset.from_parquet(str(tmp_path / "foo.parquet")) assert dataset.features == reloaded_dataset.features SCREAMING_SNAKE_CASE : int = ParquetDatasetReader(str(tmp_path / "foo.parquet") , streaming=snake_case_).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32")}), None), (Features({"image": Image(), "foo": Value("int32")}), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio())}), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowerCamelCase__ ( _a , _a): assert get_writer_batch_size(snake_case_) == expected
76
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( snake_case_ = "AAPL" ): _A : str = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' _A : List[Any] = BeautifulSoup(requests.get(snake_case_ ).text,"""html.parser""" ) _A : Union[str, Any] = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""",class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
0
'''simple docstring''' def snake_case__ ( ) -> Union[str, Any]: '''simple docstring''' return [ a * b * (1000 - a - b) for a in range(1 , 999 ) for b in range(snake_case_ , 999 ) if (a * a + b * b == (1000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f'{solution() = }')
272
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _a = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def a__ ( self , _a , _a , _a ) -> int: _A : str = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def a__ ( self , _a , _a ) -> Dict: _A : Any = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) _A : List[Any] = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) _A : Optional[int] = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def a__ ( self ) -> List[str]: _A : Any = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility _A : Dict = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) _A : Any = 3 _A : Any = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) _A : Optional[int] = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) _A : Dict = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) _A : Dict = generator.model.config.eos_token_id _A : List[str] = """<pad>""" _A : Dict = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def a__ ( self ) -> int: _A : Optional[Any] = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility _A : str = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
26
0
import math def lowerCamelCase__ ( a = 1_00 ) -> Tuple: _A: Optional[Any] = sum(i * i for i in range(1 , n + 1 ) ) _A: Optional[Any] = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F"""{solution() = }""")
121
def lowerCAmelCase_ ( snake_case_,snake_case_ ): while b: _A , _A : List[str] = b, a % b return a def lowerCAmelCase_ ( snake_case_,snake_case_ ): return a if b == 0 else euclidean_gcd_recursive(snake_case_,a % b ) def lowerCAmelCase_ ( ): print(f'''euclidean_gcd(3, 5) = {euclidean_gcd(3,5 )}''' ) print(f'''euclidean_gcd(5, 3) = {euclidean_gcd(5,3 )}''' ) print(f'''euclidean_gcd(1, 3) = {euclidean_gcd(1,3 )}''' ) print(f'''euclidean_gcd(3, 6) = {euclidean_gcd(3,6 )}''' ) print(f'''euclidean_gcd(6, 3) = {euclidean_gcd(6,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,5 )}''' ) print(f'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,3 )}''' ) print(f'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,6 )}''' ) print(f'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,3 )}''' ) if __name__ == "__main__": main()
26
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging snake_case__ : Optional[Any] = logging.get_logger(__name__) snake_case__ : Dict = { 'xlm-mlm-en-2048': 'https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json', 'xlm-mlm-ende-1024': 'https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json', 'xlm-mlm-enfr-1024': 'https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json', 'xlm-mlm-enro-1024': 'https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json', 'xlm-mlm-tlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json', 'xlm-mlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json', 'xlm-clm-enfr-1024': 'https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json', 'xlm-clm-ende-1024': 'https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json', 'xlm-mlm-17-1280': 'https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json', 'xlm-mlm-100-1280': 'https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json', } class A_ ( UpperCamelCase__ ): lowerCAmelCase__ = """xlm""" lowerCAmelCase__ = { """hidden_size""": """emb_dim""", """num_attention_heads""": """n_heads""", """num_hidden_layers""": """n_layers""", """n_words""": """vocab_size""", # For backward compatibility } def __init__(self :Optional[Any] , _UpperCamelCase :List[Any]=3_0145 , _UpperCamelCase :Optional[Any]=2048 , _UpperCamelCase :Any=12 , _UpperCamelCase :int=16 , _UpperCamelCase :Optional[int]=0.1 , _UpperCamelCase :Union[str, Any]=0.1 , _UpperCamelCase :Dict=True , _UpperCamelCase :Optional[int]=False , _UpperCamelCase :Optional[int]=False , _UpperCamelCase :int=False , _UpperCamelCase :Union[str, Any]=1 , _UpperCamelCase :List[Any]=True , _UpperCamelCase :Dict=512 , _UpperCamelCase :List[Any]=2048**-0.5 , _UpperCamelCase :str=1e-12 , _UpperCamelCase :List[str]=0.0_2 , _UpperCamelCase :Optional[Any]=0 , _UpperCamelCase :int=1 , _UpperCamelCase :Union[str, Any]=2 , _UpperCamelCase :Tuple=3 , _UpperCamelCase :Union[str, Any]=5 , _UpperCamelCase :Union[str, Any]=True , _UpperCamelCase :List[Any]="first" , _UpperCamelCase :str=True , _UpperCamelCase :Dict=None , _UpperCamelCase :int=True , _UpperCamelCase :Dict=0.1 , _UpperCamelCase :int=5 , _UpperCamelCase :Union[str, Any]=5 , _UpperCamelCase :Any=0 , _UpperCamelCase :Optional[int]=0 , _UpperCamelCase :Optional[Any]=2 , _UpperCamelCase :Dict=0 , **_UpperCamelCase :Union[str, Any] , )-> Optional[int]: __A = vocab_size __A = emb_dim __A = n_layers __A = n_heads __A = dropout __A = attention_dropout __A = gelu_activation __A = sinusoidal_embeddings __A = causal __A = asm __A = n_langs __A = use_lang_emb __A = layer_norm_eps __A = bos_index __A = eos_index __A = pad_index __A = unk_index __A = mask_index __A = is_encoder __A = max_position_embeddings __A = embed_init_std __A = init_std __A = summary_type __A = summary_use_proj __A = summary_activation __A = summary_proj_to_labels __A = summary_first_dropout __A = start_n_top __A = end_n_top __A = mask_token_id __A = lang_id if "n_words" in kwargs: __A = kwargs["""n_words"""] super().__init__(pad_token_id=_a , bos_token_id=_a , **_a ) class A_ ( UpperCamelCase__ ): @property def _lowerCAmelCase (self :int )-> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": __A = {0: """batch""", 1: """choice""", 2: """sequence"""} else: __A = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
117
def lowerCAmelCase_ ( snake_case_ ): if number < 0: raise ValueError("""number must not be negative""" ) return number & (number - 1) == 0 if __name__ == "__main__": import doctest doctest.testmod()
26
0
"""simple docstring""" class _lowerCamelCase : def __init__( self : List[str] ) -> List[str]: """simple docstring""" lowerCAmelCase__ : List[Any] = {} def _lowerCAmelCase ( self : Tuple ) -> None: """simple docstring""" print(self.vertex ) for i in self.vertex: print(_a , """ -> """ , """ -> """.join([str(_a ) for j in self.vertex[i]] ) ) def _lowerCAmelCase ( self : Any , UpperCamelCase : List[str] , UpperCamelCase : str ) -> None: """simple docstring""" # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(_a ) else: # else make a new vertex lowerCAmelCase__ : Dict = [to_vertex] def _lowerCAmelCase ( self : Any ) -> None: """simple docstring""" # visited array for storing already visited nodes lowerCAmelCase__ : List[Any] = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(_a , _a ) def _lowerCAmelCase ( self : int , UpperCamelCase : int , UpperCamelCase : Tuple ) -> None: """simple docstring""" # mark start vertex as visited lowerCAmelCase__ : Tuple = True print(_a , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(_a , _a ) if __name__ == "__main__": _A = 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
242
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def lowerCAmelCase_ ( snake_case_ ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: _A : str = k.replace(snake_case_,snake_case_ ) if k.startswith("""encoder""" ): _A : Optional[Any] = k.replace(""".attn""",""".self_attn""" ) _A : Dict = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Optional[Any] = k.replace("""norm2""","""final_layer_norm""" ) elif k.startswith("""decoder""" ): _A : str = k.replace("""norm1""","""self_attn_layer_norm""" ) _A : Any = k.replace("""norm2""","""encoder_attn_layer_norm""" ) _A : Optional[int] = k.replace("""norm3""","""final_layer_norm""" ) return k def lowerCAmelCase_ ( snake_case_ ): _A : List[Any] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: _A : str = sd.pop(snake_case_ ) _A : Optional[int] = k.replace("""layernorm_embedding""","""layer_norm""" ) assert new_k not in sd _A : Optional[int] = v _snake_case = ["START"] @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) _A : List[Any] = model["""model"""] _A : Optional[Any] = BlenderbotConfig.from_json_file(snake_case_ ) _A : List[str] = BlenderbotForConditionalGeneration(snake_case_ ) _A : Tuple = m.model.state_dict().keys() _A : Any = [] _A : Dict = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue _A : Optional[int] = rename_state_dict_key(snake_case_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: _A : Dict = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(snake_case_ ) m.model.load_state_dict(snake_case_,strict=snake_case_ ) m.half() m.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) _snake_case = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
from __future__ import annotations def __a ( SCREAMING_SNAKE_CASE ) -> Any: '''simple docstring''' create_state_space_tree(snake_case_ , [] , 0 , [0 for i in range(len(snake_case_ ) )] ) def __a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , ) -> int: '''simple docstring''' if index == len(snake_case_ ): print(snake_case_ ) return for i in range(len(snake_case_ ) ): if not index_used[i]: current_sequence.append(sequence[i] ) __UpperCAmelCase = True create_state_space_tree(snake_case_ , snake_case_ , index + 1 , snake_case_ ) current_sequence.pop() __UpperCAmelCase = False A_ : List[str] = [3, 1, 2, 4] generate_all_permutations(sequence) A_ : Tuple = ['A', 'B', 'C'] generate_all_permutations(sequence_a)
333
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a = None , _a = None , _a = False , **_a , ) -> int: super().__init__(features=_a , cache_dir=_a , keep_in_memory=_a , **_a ) _A : Optional[int] = Sql( cache_dir=_a , features=_a , sql=_a , con=_a , **_a , ) def a__ ( self ) -> Optional[Any]: _A : Tuple = None _A : int = None _A : Tuple = None _A : Union[str, Any] = None self.builder.download_and_prepare( download_config=_a , download_mode=_a , verification_mode=_a , base_path=_a , ) # Build dataset for splits _A : int = self.builder.as_dataset( split="""train""" , verification_mode=_a , in_memory=self.keep_in_memory ) return dataset class lowercase : def __init__( self , _a , _a , _a , _a = None , _a = None , **_a , ) -> Union[str, Any]: if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) _A : Dict = dataset _A : int = name _A : Union[str, Any] = con _A : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _A : str = num_proc _A : Optional[Any] = to_sql_kwargs def a__ ( self ) -> int: _A : Any = self.to_sql_kwargs.pop("""sql""" , _a ) _A : List[str] = self.to_sql_kwargs.pop("""con""" , _a ) _A : int = self.to_sql_kwargs.pop("""index""" , _a ) _A : List[str] = self._write(index=_a , **self.to_sql_kwargs ) return written def a__ ( self , _a ) -> Optional[int]: _A , _A , _A : List[str] = args _A : int = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs _A : str = query_table( table=self.dataset.data , key=slice(_a , offset + self.batch_size ) , indices=self.dataset._indices , ) _A : Tuple = batch.to_pandas() _A : Union[str, Any] = df.to_sql(self.name , self.con , index=_a , **_a ) return num_rows or len(_a ) def a__ ( self , _a , **_a ) -> int: _A : Any = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: _A , _A : Tuple = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _a , _a )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating SQL from Arrow format""" , ): written += num_rows return written
26
0
"""simple docstring""" import os import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from huggingface_hub.file_download import http_get from requests.exceptions import HTTPError from transformers import ( AlbertTokenizer, AutoTokenizer, BertTokenizer, BertTokenizerFast, GPTaTokenizerFast, is_tokenizers_available, ) from transformers.testing_utils import TOKEN, USER, is_staging_test, require_tokenizers from transformers.tokenization_utils import Trie sys.path.append(str(Path(__file__).parent.parent / 'utils')) from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" # A mock response for an HTTP head request to emulate server down UpperCAmelCase_ : Optional[int] = mock.Mock() UpperCAmelCase_ : Optional[Any] = 500 UpperCAmelCase_ : Dict = {} UpperCAmelCase_ : Union[str, Any] = HTTPError UpperCAmelCase_ : List[Any] = {} # Download this model to make sure it's in the cache. UpperCAmelCase_ : int = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request" , return_value=_a ) as mock_head: UpperCAmelCase_ : List[Any] = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) # This check we did call the fake head request mock_head.assert_called() @require_tokenizers def UpperCamelCase__ ( self ): """simple docstring""" # A mock response for an HTTP head request to emulate server down UpperCAmelCase_ : str = mock.Mock() UpperCAmelCase_ : Any = 500 UpperCAmelCase_ : Optional[int] = {} UpperCAmelCase_ : List[str] = HTTPError UpperCAmelCase_ : int = {} # Download this model to make sure it's in the cache. UpperCAmelCase_ : int = GPTaTokenizerFast.from_pretrained("gpt2" ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request" , return_value=_a ) as mock_head: UpperCAmelCase_ : str = GPTaTokenizerFast.from_pretrained("gpt2" ) # This check we did call the fake head request mock_head.assert_called() def UpperCamelCase__ ( self ): """simple docstring""" # This test is for deprecated behavior and can be removed in v5 try: UpperCAmelCase_ : Tuple = tempfile.mktemp() with open(_a , "wb" ) as f: http_get("https://huggingface.co/albert-base-v1/resolve/main/spiece.model" , _a ) UpperCAmelCase_ : str = AlbertTokenizer.from_pretrained(_a ) finally: os.remove(_a ) # Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in # the current folder and have the right name. if os.path.isfile("tokenizer.json" ): # We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it. return try: with open("tokenizer.json" , "wb" ) as f: http_get("https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json" , _a ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) # The tiny random BERT has a vocab size of 1024, tiny gpt2 as a vocab size of 1000 self.assertEqual(tokenizer.vocab_size , 1000 ) # Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file. finally: os.remove("tokenizer.json" ) def UpperCamelCase__ ( self ): """simple docstring""" # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase_ : Dict = AlbertTokenizer.from_pretrained("https://huggingface.co/albert-base-v1/resolve/main/spiece.model" ) @is_staging_test class A_ (unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[Any] = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""] @classmethod def UpperCamelCase__ ( cls ): """simple docstring""" UpperCAmelCase_ : int = TOKEN HfFolder.save_token(_a ) @classmethod def UpperCamelCase__ ( cls ): """simple docstring""" try: delete_repo(token=cls._token , repo_id="test-tokenizer" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-tokenizer-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-tokenizer" ) except HTTPError: pass def UpperCamelCase__ ( self ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : Optional[Any] = os.path.join(_a , "vocab.txt" ) with open(_a , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) UpperCAmelCase_ : Optional[Any] = BertTokenizer(_a ) tokenizer.push_to_hub("test-tokenizer" , use_auth_token=self._token ) UpperCAmelCase_ : List[Any] = BertTokenizer.from_pretrained(F"""{USER}/test-tokenizer""" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) # Reset repo delete_repo(token=self._token , repo_id="test-tokenizer" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_a , repo_id="test-tokenizer" , push_to_hub=_a , use_auth_token=self._token ) UpperCAmelCase_ : List[str] = BertTokenizer.from_pretrained(F"""{USER}/test-tokenizer""" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) def UpperCamelCase__ ( self ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : Optional[int] = os.path.join(_a , "vocab.txt" ) with open(_a , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) UpperCAmelCase_ : Tuple = BertTokenizer(_a ) tokenizer.push_to_hub("valid_org/test-tokenizer-org" , use_auth_token=self._token ) UpperCAmelCase_ : List[Any] = BertTokenizer.from_pretrained("valid_org/test-tokenizer-org" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-tokenizer-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained( _a , repo_id="valid_org/test-tokenizer-org" , push_to_hub=_a , use_auth_token=self._token ) UpperCAmelCase_ : Dict = BertTokenizer.from_pretrained("valid_org/test-tokenizer-org" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) @require_tokenizers def UpperCamelCase__ ( self ): """simple docstring""" CustomTokenizer.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : Union[str, Any] = os.path.join(_a , "vocab.txt" ) with open(_a , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) UpperCAmelCase_ : str = CustomTokenizer(_a ) # No fast custom tokenizer tokenizer.push_to_hub("test-dynamic-tokenizer" , use_auth_token=self._token ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(F"""{USER}/test-dynamic-tokenizer""" , trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , "CustomTokenizer" ) # Fast and slow custom tokenizer CustomTokenizerFast.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : Any = os.path.join(_a , "vocab.txt" ) with open(_a , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens] ) ) UpperCAmelCase_ : Dict = BertTokenizerFast.from_pretrained(_a ) bert_tokenizer.save_pretrained(_a ) UpperCAmelCase_ : Dict = CustomTokenizerFast.from_pretrained(_a ) tokenizer.push_to_hub("test-dynamic-tokenizer" , use_auth_token=self._token ) UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(F"""{USER}/test-dynamic-tokenizer""" , trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , "CustomTokenizerFast" ) UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( F"""{USER}/test-dynamic-tokenizer""" , use_fast=_a , trust_remote_code=_a ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , "CustomTokenizer" ) class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = Trie() trie.add("Hello 友達" ) self.assertEqual(trie.data , {"H": {"e": {"l": {"l": {"o": {" ": {"友": {"達": {"": 1}}}}}}}}} ) trie.add("Hello" ) trie.data self.assertEqual(trie.data , {"H": {"e": {"l": {"l": {"o": {"": 1, " ": {"友": {"達": {"": 1}}}}}}}}} ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = Trie() self.assertEqual(trie.split("[CLS] This is a extra_id_100" ) , ["[CLS] This is a extra_id_100"] ) trie.add("[CLS]" ) trie.add("extra_id_1" ) trie.add("extra_id_100" ) self.assertEqual(trie.split("[CLS] This is a extra_id_100" ) , ["[CLS]", " This is a ", "extra_id_100"] ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = Trie() trie.add("A" ) self.assertEqual(trie.split("ABC" ) , ["A", "BC"] ) self.assertEqual(trie.split("BCA" ) , ["BC", "A"] ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = Trie() trie.add("TOKEN]" ) trie.add("[SPECIAL_TOKEN]" ) self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]" ) , ["This is something ", "[SPECIAL_TOKEN]"] ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = Trie() trie.add("A" ) trie.add("P" ) trie.add("[SPECIAL_TOKEN]" ) self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]" ) , ["This is something ", "[SPECIAL_TOKEN]"] ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = Trie() trie.add("AB" ) trie.add("B" ) trie.add("C" ) self.assertEqual(trie.split("ABC" ) , ["AB", "C"] ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = Trie() trie.add("ABC" ) trie.add("B" ) trie.add("CD" ) self.assertEqual(trie.split("ABCD" ) , ["ABC", "D"] ) def UpperCamelCase__ ( self ): """simple docstring""" # Even if the offsets are wrong, we necessarily output correct string # parts. UpperCAmelCase_ : Tuple = Trie() UpperCAmelCase_ : int = trie.cut_text("ABC" , [0, 0, 2, 1, 2, 3] ) self.assertEqual(_a , ["AB", "C"] )
61
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class lowercase ( UpperCamelCase__ ): _a = "fnet" def __init__( self , _a=3_2000 , _a=768 , _a=12 , _a=3072 , _a="gelu_new" , _a=0.1 , _a=512 , _a=4 , _a=0.02 , _a=1e-12 , _a=False , _a=512 , _a=3 , _a=1 , _a=2 , **_a , ) -> int: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Any = vocab_size _A : str = max_position_embeddings _A : Optional[Any] = hidden_size _A : List[str] = num_hidden_layers _A : List[str] = intermediate_size _A : List[Any] = hidden_act _A : List[str] = hidden_dropout_prob _A : List[str] = initializer_range _A : List[Any] = type_vocab_size _A : List[Any] = layer_norm_eps _A : List[str] = use_tpu_fourier_optimizations _A : str = tpu_short_seq_length
26
0
import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class snake_case__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = IFImgaImgSuperResolutionPipeline _SCREAMING_SNAKE_CASE = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""} _SCREAMING_SNAKE_CASE = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""} ) _SCREAMING_SNAKE_CASE = PipelineTesterMixin.required_optional_params - {"""latents"""} def lowercase_ ( self : Tuple ) ->List[Any]: return self._get_superresolution_dummy_components() def lowercase_ ( self : List[Any], _snake_case : List[Any], _snake_case : Any=0 ) ->Any: if str(_a ).startswith('mps' ): snake_case__ : Tuple = torch.manual_seed(_a ) else: snake_case__ : Tuple = torch.Generator(device=_a ).manual_seed(_a ) snake_case__ : List[Any] = floats_tensor((1, 3, 3_2, 3_2), rng=random.Random(_a ) ).to(_a ) snake_case__ : Dict = floats_tensor((1, 3, 1_6, 1_6), rng=random.Random(_a ) ).to(_a ) snake_case__ : Optional[int] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available(), reason='XFormers attention is only available with CUDA and `xformers` installed', ) def lowercase_ ( self : Any ) ->Optional[int]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def lowercase_ ( self : Any ) ->List[Any]: self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda', reason='float16 requires CUDA' ) def lowercase_ ( self : Optional[Any] ) ->Any: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def lowercase_ ( self : int ) ->Union[str, Any]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def lowercase_ ( self : Dict ) ->List[Any]: self._test_save_load_local() def lowercase_ ( self : List[str] ) ->str: self._test_inference_batch_single_identical( expected_max_diff=1e-2, )
277
def lowerCAmelCase_ ( snake_case_ ): if n_term == "": return [] _A : list = [] for temp in range(int(snake_case_ ) ): series.append(f'''1/{temp + 1}''' if series else """1""" ) return series if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
26
0
def UpperCAmelCase_( a__ = 600_851_475_143 ): """simple docstring""" try: SCREAMING_SNAKE_CASE : List[str] = int(snake_case_ ) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''' ) if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''' ) SCREAMING_SNAKE_CASE : Optional[Any] = 2 SCREAMING_SNAKE_CASE : str = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 SCREAMING_SNAKE_CASE : List[Any] = i while n % i == 0: SCREAMING_SNAKE_CASE : Union[str, Any] = n // i i += 1 return int(snake_case_ ) if __name__ == "__main__": print(F"{solution() = }")
313
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _snake_case = logging.get_logger(__name__) _snake_case = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) _snake_case = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCAmelCase_ ( snake_case_ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A : List[str] = model_type_to_module_name(snake_case_ ) _A : List[Any] = importlib.import_module(f'''.{module_name}''',"""transformers.models""" ) try: return getattr(snake_case_,snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_,"""__name__""",snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A : List[Any] = importlib.import_module("""transformers""" ) if hasattr(snake_case_,snake_case_ ): return getattr(snake_case_,snake_case_ ) return None def lowerCAmelCase_ ( snake_case_,snake_case_ = None,snake_case_ = False,snake_case_ = False,snake_case_ = None,snake_case_ = None,snake_case_ = None,snake_case_ = False,**snake_case_,): _A : Optional[int] = get_file_from_repo( snake_case_,snake_case_,cache_dir=snake_case_,force_download=snake_case_,resume_download=snake_case_,proxies=snake_case_,use_auth_token=snake_case_,revision=snake_case_,local_files_only=snake_case_,) if resolved_config_file is None: logger.info( """Could not locate the feature extractor configuration file, will try to use the model config instead.""" ) return {} with open(snake_case_,encoding="""utf-8""" ) as reader: return json.load(snake_case_ ) class lowercase : def __init__( self ) -> List[Any]: raise EnvironmentError( """AutoFeatureExtractor is designed to be instantiated """ """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(_a ) def a__ ( cls , _a , **_a ) -> Any: _A : Tuple = kwargs.pop("""config""" , _a ) _A : Tuple = kwargs.pop("""trust_remote_code""" , _a ) _A : List[Any] = True _A , _A : Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A : Tuple = config_dict.get("""feature_extractor_type""" , _a ) _A : int = None if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): _A : Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A : int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A : Optional[int] = getattr(_a , """feature_extractor_type""" , _a ) if hasattr(_a , """auto_map""" ) and "AutoFeatureExtractor" in config.auto_map: _A : Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A : Optional[Any] = feature_extractor_class_from_name(_a ) _A : List[Any] = feature_extractor_auto_map is not None _A : Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A : Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A : Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A : str = kwargs.pop("""code_revision""" , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A : Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ ( _a , _a ) -> Optional[int]: FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
26
0
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _A : Optional[int] = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ): _UpperCAmelCase : str = "ernie_m" _UpperCAmelCase : Dict = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : List[Any] , A : int = 2_5_0_0_0_2 , A : List[str] = 7_6_8 , A : Optional[Any] = 1_2 , A : Union[str, Any] = 1_2 , A : Dict = 3_0_7_2 , A : List[Any] = "gelu" , A : Dict = 0.1 , A : List[str] = 0.1 , A : Optional[Any] = 5_1_4 , A : Optional[Any] = 0.02 , A : str = 1 , A : int = 1e-05 , A : Union[str, Any]=None , A : str=False , A : Dict=0.0 , **A : str , ) ->str: super().__init__(pad_token_id=_a , **_a ) lowerCamelCase__ : Union[str, Any] = vocab_size lowerCamelCase__ : Any = hidden_size lowerCamelCase__ : Union[str, Any] = num_hidden_layers lowerCamelCase__ : Optional[Any] = num_attention_heads lowerCamelCase__ : Dict = intermediate_size lowerCamelCase__ : Dict = hidden_act lowerCamelCase__ : Optional[Any] = hidden_dropout_prob lowerCamelCase__ : int = attention_probs_dropout_prob lowerCamelCase__ : Tuple = max_position_embeddings lowerCamelCase__ : Union[str, Any] = initializer_range lowerCamelCase__ : List[Any] = layer_norm_eps lowerCamelCase__ : Tuple = classifier_dropout lowerCamelCase__ : Union[str, Any] = is_decoder lowerCamelCase__ : List[Any] = act_dropout
142
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=False , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Dict: _A : str = parent _A : int = batch_size _A : Optional[int] = num_channels _A : List[Any] = image_size _A : int = min_resolution _A : Optional[int] = max_resolution _A : Any = do_resize _A : List[str] = size if size is not None else {"""height""": 18, """width""": 20} _A : Optional[int] = do_thumbnail _A : str = do_align_axis _A : List[Any] = do_pad _A : Optional[Any] = do_normalize _A : Tuple = image_mean _A : List[str] = image_std def a__ ( self ) -> Optional[int]: return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = DonutImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : List[str] = DonutImageProcessingTester(self ) @property def a__ ( self ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Optional[Any]: _A : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) self.assertTrue(hasattr(_a , """do_thumbnail""" ) ) self.assertTrue(hasattr(_a , """do_align_long_axis""" ) ) self.assertTrue(hasattr(_a , """do_pad""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) def a__ ( self ) -> List[Any]: _A : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) _A : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def a__ ( self ) -> Union[str, Any]: pass @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : Any = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Dict: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : int = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : List[str] = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _A : str = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
26
0
def __lowercase ( _UpperCamelCase, _UpperCamelCase ) ->Union[str, Any]: """simple docstring""" _enforce_args(snake_case_, snake_case_ ) if n == 0: return 0 lowercase : Tuple = float('''-inf''' ) for i in range(1, n + 1 ): lowercase : str = max( snake_case_, prices[i - 1] + naive_cut_rod_recursive(n - i, snake_case_ ) ) return max_revue def __lowercase ( _UpperCamelCase, _UpperCamelCase ) ->Any: """simple docstring""" _enforce_args(snake_case_, snake_case_ ) lowercase : Dict = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_, snake_case_, snake_case_ ) def __lowercase ( _UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) ->str: """simple docstring""" if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: lowercase : List[str] = float('''-inf''' ) for i in range(1, n + 1 ): lowercase : Optional[Any] = max( snake_case_, prices[i - 1] + _top_down_cut_rod_recursive(n - i, snake_case_, snake_case_ ), ) lowercase : Tuple = max_revenue return max_rev[n] def __lowercase ( _UpperCamelCase, _UpperCamelCase ) ->List[str]: """simple docstring""" _enforce_args(snake_case_, snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. lowercase : List[Any] = [float('''-inf''' ) for _ in range(n + 1 )] lowercase : Any = 0 for i in range(1, n + 1 ): lowercase : Optional[Any] = max_rev[i] for j in range(1, i + 1 ): lowercase : int = max(snake_case_, prices[j - 1] + max_rev[i - j] ) lowercase : int = max_revenue_i return max_rev[n] def __lowercase ( _UpperCamelCase, _UpperCamelCase ) ->List[str]: """simple docstring""" if n < 0: lowercase : Optional[Any] = f"""n must be greater than or equal to 0. Got n = {n}""" raise ValueError(snake_case_ ) if n > len(snake_case_ ): lowercase : Any = ( """Each integral piece of rod must have a corresponding price. """ f"""Got n = {n} but length of prices = {len(snake_case_ )}""" ) raise ValueError(snake_case_ ) def __lowercase ( ) ->List[Any]: """simple docstring""" lowercase : Tuple = [6, 10, 12, 15, 20, 23] lowercase : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. lowercase : Any = 36 lowercase : List[Any] = top_down_cut_rod(snake_case_, snake_case_ ) lowercase : List[Any] = bottom_up_cut_rod(snake_case_, snake_case_ ) lowercase : Dict = naive_cut_rod_recursive(snake_case_, snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
337
from __future__ import annotations import numpy as np def lowerCAmelCase_ ( snake_case_ ): return np.maximum(0,snake_case_ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
26
0
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : Tuple = analyze_text(snake_case_) SCREAMING_SNAKE_CASE : List[str] = list(" " + ascii_lowercase) # what is our total sum of probabilities. SCREAMING_SNAKE_CASE : Dict = sum(single_char_strings.values()) # one length string SCREAMING_SNAKE_CASE : Union[str, 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: SCREAMING_SNAKE_CASE : List[str] = single_char_strings[ch] SCREAMING_SNAKE_CASE : Optional[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 SCREAMING_SNAKE_CASE : str = sum(two_char_strings.values()) SCREAMING_SNAKE_CASE : Union[str, Any] = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: SCREAMING_SNAKE_CASE : Union[str, Any] = cha + cha if sequence in two_char_strings: SCREAMING_SNAKE_CASE : List[Any] = two_char_strings[sequence] SCREAMING_SNAKE_CASE : str = 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 lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : List[str] = Counter() # type: ignore SCREAMING_SNAKE_CASE : Optional[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 lowerCamelCase__ ( ): 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()
76
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) _snake_case = getLogger(__name__) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ = 8,snake_case_ = 1024,snake_case_="val",snake_case_=None,snake_case_=False,snake_case_="summarization",snake_case_=None,snake_case_=1,snake_case_ = None,snake_case_="",**snake_case_,): _A : Dict = str(snake_case_ ) assert local_rank is not None torch.distributed.init_process_group(backend="""nccl""",rank=snake_case_ ) _A : Tuple = Path(snake_case_ ) _A : List[Any] = save_dir.joinpath(f'''rank_{local_rank}_output.json''' ) torch.cuda.set_device(snake_case_ ) _A : Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained(snake_case_ ).cuda() if fpaa: _A : Any = model.half() # determine if we need to increase num_beams use_task_specific_params(snake_case_,snake_case_ ) # update config with task specific params _A : str = generate_kwargs.pop("""num_beams""",model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: _A : int = num_return_sequences _A : Optional[Any] = AutoTokenizer.from_pretrained(snake_case_ ) logger.info(f'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. if max_source_length is None: _A : Optional[int] = tokenizer.model_max_length if prefix is None: _A : Tuple = prefix or getattr(model.config,"""prefix""","""""" ) or """""" _A : Optional[int] = SeqaSeqDataset( snake_case_,snake_case_,snake_case_,max_target_length=1024,type_path=snake_case_,n_obs=snake_case_,prefix=snake_case_,**snake_case_,) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. _A : Optional[int] = ds.make_sortish_sampler(snake_case_,distributed=snake_case_,add_extra_examples=snake_case_,shuffle=snake_case_ ) _A : Dict = DataLoader(snake_case_,sampler=snake_case_,batch_size=snake_case_,collate_fn=ds.collate_fn ) _A : Optional[Any] = [] for batch in tqdm(snake_case_ ): _A : Tuple = model.generate( input_ids=batch["""input_ids"""].to(model.device ),attention_mask=batch["""attention_mask"""].to(model.device ),num_return_sequences=snake_case_,num_beams=snake_case_,**snake_case_,) _A : Any = tokenizer.batch_decode(snake_case_,skip_special_tokens=snake_case_,clean_up_tokenization_spaces=snake_case_ ) _A : Dict = batch["""ids"""] if num_return_sequences > 1: _A : Any = chunks(snake_case_,snake_case_ ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(snake_case_ ): results.append({"""pred""": pred, """id""": ids[i].item()} ) save_json(snake_case_,snake_case_ ) return results, sampler.num_replicas def lowerCAmelCase_ ( ): _A : Tuple = argparse.ArgumentParser( epilog="""Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate""" ) parser.add_argument("""--data_dir""",type=snake_case_,help="""like cnn_dm/test.source""" ) parser.add_argument( """--model_name""",type=snake_case_,help="""like facebook/bart-large-cnn,t5-base, etc.""",default="""sshleifer/distilbart-xsum-12-3""",) parser.add_argument("""--save_dir""",type=snake_case_,help="""where to save""",default="""tmp_gen""" ) parser.add_argument("""--max_source_length""",type=snake_case_,default=snake_case_ ) parser.add_argument( """--type_path""",type=snake_case_,default="""test""",help="""which subset to evaluate typically train/val/test""" ) parser.add_argument("""--task""",type=snake_case_,default="""summarization""",help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""",type=snake_case_,default=8,required=snake_case_,help="""batch size""" ) parser.add_argument( """--local_rank""",type=snake_case_,default=-1,required=snake_case_,help="""should be passed by distributed.launch""" ) parser.add_argument( """--n_obs""",type=snake_case_,default=snake_case_,required=snake_case_,help="""How many observations. Defaults to all.""" ) parser.add_argument( """--num_return_sequences""",type=snake_case_,default=1,required=snake_case_,help="""How many sequences to return""" ) parser.add_argument( """--sync_timeout""",type=snake_case_,default=600,required=snake_case_,help="""How long should master process wait for other processes to finish.""",) parser.add_argument("""--src_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument("""--tgt_lang""",type=snake_case_,default=snake_case_,required=snake_case_ ) parser.add_argument( """--prefix""",type=snake_case_,required=snake_case_,default=snake_case_,help="""will be added to the begininng of src examples""" ) parser.add_argument("""--fp16""",action="""store_true""" ) parser.add_argument("""--debug""",action="""store_true""" ) _A : Union[str, Any] = time.time() _A , _A : List[str] = parser.parse_known_args() _A : List[str] = parse_numeric_n_bool_cl_kwargs(snake_case_ ) if generate_kwargs and args.local_rank <= 0: print(f'''parsed the following generate kwargs: {generate_kwargs}''' ) _A : Dict = Path(args.save_dir + """_tmp""" ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) # this handles locking. _A : int = list(json_save_dir.glob("""rank_*.json""" ) ) if intermediate_files: raise ValueError(f'''Found files at {json_save_dir} please move or remove them.''' ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. _A : Any = {} if args.src_lang is not None: _A : int = args.src_lang if args.tgt_lang is not None: _A : Dict = args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=snake_case_ ) _A , _A : str = eval_data_dir( args.data_dir,snake_case_,args.model_name,type_path=args.type_path,bs=args.bs,fpaa=args.fpaa,task=args.task,local_rank=args.local_rank,n_obs=args.n_obs,max_source_length=args.max_source_length,num_return_sequences=args.num_return_sequences,prefix=args.prefix,dataset_kwargs=snake_case_,**snake_case_,) if args.local_rank <= 0: _A : List[Any] = Path(args.save_dir ) save_dir.mkdir(exist_ok=snake_case_ ) _A : Tuple = gather_results_from_each_node(snake_case_,snake_case_,args.sync_timeout ) _A : Optional[int] = combine_partial_results(snake_case_ ) if args.num_return_sequences > 1: _A : Optional[Any] = save_dir.joinpath("""pseudolabel_results.json""" ) print(f'''Saving aggregated results at {save_path}, intermediate in {json_save_dir}/''' ) save_json(snake_case_,snake_case_ ) return _A : List[str] = Path(args.data_dir ).joinpath(args.type_path + """.target""" ) with open(snake_case_ ) as f: _A : int = [x.rstrip() for x in f.readlines()][: len(snake_case_ )] # Calculate metrics, save metrics, and save _generations.txt _A : Dict = """translation""" in args.task _A : Optional[Any] = calculate_bleu if calc_bleu else calculate_rouge _A : Tuple = """bleu""" if calc_bleu else """rouge""" _A : Dict = score_fn(snake_case_,snake_case_ ) _A : List[Any] = len(snake_case_ ) _A : Optional[int] = time.time() - start_time _A : Dict = round(runtime / metrics["""n_obs"""],4 ) _A : Dict = num_replicas # TODO(@stas00): add whatever metadata to metrics _A : Any = save_dir.joinpath(f'''{args.type_path}_{metric_name}.json''' ) save_json(snake_case_,snake_case_,indent=snake_case_ ) print(snake_case_ ) write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}_generations.txt''' ) ) if args.debug: write_txt_file(snake_case_,save_dir.joinpath(f'''{args.type_path}.target''' ) ) else: shutil.rmtree(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = [] for partial_result in partial_results: records.extend(snake_case_ ) _A : Optional[Any] = sorted(snake_case_,key=lambda snake_case_ : x["id"] ) _A : List[str] = [x["""pred"""] for x in records] return preds def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): # WAIT FOR lots of .json files _A : Optional[Any] = time.time() logger.info("""waiting for all nodes to finish""" ) _A : List[str] = None while (time.time() - start_wait) < timeout: _A : str = list(save_dir.glob("""rank_*.json""" ) ) if len(snake_case_ ) < num_replicas: continue try: # make sure all json files are fully saved _A : List[str] = lmap(snake_case_,snake_case_ ) return json_data except JSONDecodeError: continue else: raise TimeoutError("""Rank 0 gave up on waiting for other processes""" ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
26
0
'''simple docstring''' # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib __lowercase = get_logger() __lowercase = None class a__( TensorFormatter[Mapping, '''jax.Array''', Mapping] ): '''simple docstring''' def __init__( self , __lowerCAmelCase=None , __lowerCAmelCase=None , **__lowerCAmelCase): """simple docstring""" super().__init__(features=_a) import jax from jaxlib.xla_client import Device if isinstance(_a , _a): raise ValueError( f"Expected {device} to be a `str` not {type(_a)}, as `jaxlib.xla_extension.Device` " """is not serializable neither with `pickle` nor with `dill`. Instead you can surround """ """the device with `str()` to get its string identifier that will be internally mapped """ """to the actual `jaxlib.xla_extension.Device`.""") lowerCAmelCase = device if isinstance(_a , _a) else str(jax.devices()[0]) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys()): logger.warning( f"Device with string identifier {self.device} not listed among the available " f"devices: {list(DEVICE_MAPPING.keys())}, so falling back to the default " f"device: {str(jax.devices()[0])}.") lowerCAmelCase = str(jax.devices()[0]) lowerCAmelCase = jnp_array_kwargs @staticmethod def a_ ( ): """simple docstring""" import jax return {str(_a): device for device in jax.devices()} def a_ ( self , __lowerCAmelCase): """simple docstring""" import jax import jax.numpy as jnp if isinstance(_a , _a) and column: if all( isinstance(_a , jax.Array) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column): return jnp.stack(_a , axis=0) return column def a_ ( self , __lowerCAmelCase): """simple docstring""" import jax import jax.numpy as jnp if isinstance(_a , (str, bytes, type(_a))): return value elif isinstance(_a , (np.character, np.ndarray)) and np.issubdtype(value.dtype , np.character): return value.tolist() lowerCAmelCase = {} if isinstance(_a , (np.number, np.ndarray)) and np.issubdtype(value.dtype , np.integer): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: lowerCAmelCase = {"""dtype""": jnp.intaa} else: lowerCAmelCase = {"""dtype""": jnp.intaa} elif isinstance(_a , (np.number, np.ndarray)) and np.issubdtype(value.dtype , np.floating): lowerCAmelCase = {"""dtype""": jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(_a , PIL.Image.Image): lowerCAmelCase = np.asarray(_a) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: lowerCAmelCase = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device]): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(_a , **{**default_dtype, **self.jnp_array_kwargs}) def a_ ( self , __lowerCAmelCase): """simple docstring""" import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(_a , torch.Tensor): return self._tensorize(data_struct.detach().cpu().numpy()[()]) if hasattr(_a , """__array__""") and not isinstance(_a , jax.Array): lowerCAmelCase = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(_a , np.ndarray): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(_a) for substruct in data_struct]) elif isinstance(_a , (list, tuple)): return self._consolidate([self.recursive_tensorize(_a) for substruct in data_struct]) return self._tensorize(_a) def a_ ( self , __lowerCAmelCase): """simple docstring""" return map_nested(self._recursive_tensorize , _a , map_list=_a) def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = self.numpy_arrow_extractor().extract_row(_a) lowerCAmelCase = self.python_features_decoder.decode_row(_a) return self.recursive_tensorize(_a) def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = self.numpy_arrow_extractor().extract_column(_a) lowerCAmelCase = self.python_features_decoder.decode_column(_a , pa_table.column_names[0]) lowerCAmelCase = self.recursive_tensorize(_a) lowerCAmelCase = self._consolidate(_a) return column def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = self.numpy_arrow_extractor().extract_batch(_a) lowerCAmelCase = self.python_features_decoder.decode_batch(_a) lowerCAmelCase = self.recursive_tensorize(_a) for column_name in batch: lowerCAmelCase = self._consolidate(batch[column_name]) return batch
272
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Any: _A : Tuple = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) _A : List[Any] = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" _A : List[str] = model(_a )["""last_hidden_state"""] _A : Union[str, Any] = tf.TensorShape((1, 10, 768) ) self.assertEqual(output.shape , _a ) # compare the actual values for a slice. _A : List[Any] = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
26
0
import random def lowerCamelCase__ ( a ) -> Any: _A: Optional[Any] = num - 1 _A: Optional[int] = 0 while s % 2 == 0: _A: str = s // 2 t += 1 for _ in range(5 ): _A: Tuple = random.randrange(2 , num - 1 ) _A: Dict = pow(snake_case_ , snake_case_ , snake_case_ ) if v != 1: _A: int = 0 while v != (num - 1): if i == t - 1: return False else: _A: int = i + 1 _A: Dict = (v**2) % num return True def lowerCamelCase__ ( a ) -> List[Any]: if num < 2: return False _A: int = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 1_01, 1_03, 1_07, 1_09, 1_13, 1_27, 1_31, 1_37, 1_39, 1_49, 1_51, 1_57, 1_63, 1_67, 1_73, 1_79, 1_81, 1_91, 1_93, 1_97, 1_99, 2_11, 2_23, 2_27, 2_29, 2_33, 2_39, 2_41, 2_51, 2_57, 2_63, 2_69, 2_71, 2_77, 2_81, 2_83, 2_93, 3_07, 3_11, 3_13, 3_17, 3_31, 3_37, 3_47, 3_49, 3_53, 3_59, 3_67, 3_73, 3_79, 3_83, 3_89, 3_97, 4_01, 4_09, 4_19, 4_21, 4_31, 4_33, 4_39, 4_43, 4_49, 4_57, 4_61, 4_63, 4_67, 4_79, 4_87, 4_91, 4_99, 5_03, 5_09, 5_21, 5_23, 5_41, 5_47, 5_57, 5_63, 5_69, 5_71, 5_77, 5_87, 5_93, 5_99, 6_01, 6_07, 6_13, 6_17, 6_19, 6_31, 6_41, 6_43, 6_47, 6_53, 6_59, 6_61, 6_73, 6_77, 6_83, 6_91, 7_01, 7_09, 7_19, 7_27, 7_33, 7_39, 7_43, 7_51, 7_57, 7_61, 7_69, 7_73, 7_87, 7_97, 8_09, 8_11, 8_21, 8_23, 8_27, 8_29, 8_39, 8_53, 8_57, 8_59, 8_63, 8_77, 8_81, 8_83, 8_87, 9_07, 9_11, 9_19, 9_29, 9_37, 9_41, 9_47, 9_53, 9_67, 9_71, 9_77, 9_83, 9_91, 9_97, ] if num in low_primes: return True for prime in low_primes: if (num % prime) == 0: return False return rabin_miller(snake_case_ ) def lowerCamelCase__ ( a = 10_24 ) -> int: while True: _A: List[Any] = random.randrange(2 ** (keysize - 1) , 2 ** (keysize) ) if is_prime_low_num(snake_case_ ): return num if __name__ == "__main__": UpperCAmelCase__ : List[str] = generate_large_prime() print(('Prime number:', num)) print(('is_prime_low_num:', is_prime_low_num(num)))
121
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class lowercase ( UpperCamelCase__ ): _a = 42 class lowercase ( UpperCamelCase__ ): def __init__( self , _a , _a , _a , _a , _a , ) -> List[Any]: super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: if latents is None: _A : str = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) _A : Union[str, Any] = latents.to(_a ) _A : int = latents * scheduler.init_noise_sigma return latents def a__ ( self , _a=0 ) -> Optional[Any]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _A : str = torch.device(F'''cuda:{gpu_id}''' ) _A : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def a__ ( self ) -> List[Any]: if self.device != torch.device("""meta""" ) or not hasattr(self.image_encoder , """_hf_hook""" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , """_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 def a__ ( self , _a , _a , _a , _a , ) -> Tuple: if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): _A : int = torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): _A : Dict = self.image_processor(_a , return_tensors="""pt""" ).pixel_values[0].unsqueeze(0 ) _A : int = image.to(dtype=self.image_encoder.dtype , device=_a ) _A : List[Any] = self.image_encoder(_a )["""last_hidden_state"""] _A : List[Any] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 _A : Dict = image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: _A : str = torch.zeros_like(_a ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _A : List[str] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a = 1 , _a = 25 , _a = None , _a = None , _a = 4.0 , _a = 64 , _a = "pil" , _a = True , ) -> Union[str, Any]: if isinstance(_a , PIL.Image.Image ): _A : List[Any] = 1 elif isinstance(_a , torch.Tensor ): _A : Any = image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): _A : Union[str, Any] = len(_a ) else: raise ValueError( F'''`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}''' ) _A : Optional[int] = self._execution_device _A : Tuple = batch_size * num_images_per_prompt _A : List[Any] = guidance_scale > 1.0 _A : Optional[Any] = self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) _A : Optional[int] = self.scheduler.timesteps _A : List[str] = self.prior.config.num_embeddings _A : int = self.prior.config.embedding_dim _A : Optional[Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim _A : List[Any] = latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance _A : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _A : int = self.scheduler.scale_model_input(_a , _a ) _A : Tuple = self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance _A , _A : Optional[Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: _A , _A : Dict = noise_pred.chunk(2 ) _A : Tuple = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) _A : int = self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) _A : List[str] = [] for i, latent in enumerate(_a ): print() _A : List[str] = self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(_a ) _A : List[Any] = torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}''' ) _A : List[str] = images.cpu().numpy() if output_type == "pil": _A : List[Any] = [self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , """final_offload_hook""" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
26
0
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin snake_case__ : Optional[Any] = '▁' snake_case__ : Any = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece class A_ ( UpperCamelCase__ , unittest.TestCase ): lowerCAmelCase__ = BertGenerationTokenizer lowerCAmelCase__ = False lowerCAmelCase__ = True def _lowerCAmelCase (self :Union[str, Any] )-> str: super().setUp() __A = BertGenerationTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCAmelCase (self :Optional[int] )-> Union[str, Any]: __A = """<s>""" __A = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def _lowerCAmelCase (self :Union[str, Any] )-> List[Any]: __A = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(_a ) , 1002 ) def _lowerCAmelCase (self :str )-> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCAmelCase (self :Optional[int] )-> Union[str, Any]: __A = BertGenerationTokenizer(_a , keep_accents=_a ) __A = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [285, 46, 10, 170, 382] , ) __A = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) __A = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __A = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def _lowerCAmelCase (self :List[str] )-> Tuple: return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def _lowerCAmelCase (self :List[Any] )-> List[Any]: __A = """Hello World!""" __A = [1_8536, 2260, 101] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def _lowerCAmelCase (self :str )-> List[str]: __A = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) __A = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 3_4324, 497, 391, 408, 1_1342, 1244, 385, 100, 938, 985, 456, 574, 362, 1_2597, 3200, 3129, 1172, ] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @require_torch @slow def _lowerCAmelCase (self :int )-> Optional[Any]: import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence __A = list(self.big_tokenizer.get_vocab().keys() )[:10] __A = """ """.join(_a ) __A = self.big_tokenizer.encode_plus(_a , return_tensors='''pt''' , return_token_type_ids=_a ) __A = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_a ) __A = BertGenerationConfig() __A = BertGenerationEncoder(_a ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_a ) model(**_a ) @slow def _lowerCAmelCase (self :List[Any] )-> Optional[Any]: # fmt: off __A = {"""input_ids""": [[3_9286, 458, 3_6335, 2001, 456, 1_3073, 1_3266, 455, 113, 7746, 1741, 1_1157, 391, 1_3073, 1_3266, 455, 113, 3967, 3_5412, 113, 4936, 109, 3870, 2377, 113, 3_0084, 4_5720, 458, 134, 1_7496, 112, 503, 1_1672, 113, 118, 112, 5665, 1_3347, 3_8687, 112, 1496, 3_1389, 112, 3268, 4_7264, 134, 962, 112, 1_6377, 8035, 2_3130, 430, 1_2169, 1_5518, 2_8592, 458, 146, 4_1697, 109, 391, 1_2169, 1_5518, 1_6689, 458, 146, 4_1358, 109, 452, 726, 4034, 111, 763, 3_5412, 5082, 388, 1903, 111, 9051, 391, 2870, 4_8918, 1900, 1123, 550, 998, 112, 9586, 1_5985, 455, 391, 410, 2_2955, 3_7636, 114], [448, 1_7496, 419, 3663, 385, 763, 113, 2_7533, 2870, 3283, 1_3043, 1639, 2_4713, 523, 656, 2_4013, 1_8550, 2521, 517, 2_7014, 2_1244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 1_1786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 2_1932, 1_8146, 726, 363, 1_7032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
117
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) def lowerCAmelCase_ ( snake_case_ ): print("""Loading config file...""" ) def flatten_yaml_as_dict(snake_case_,snake_case_="",snake_case_="." ): _A : Union[str, Any] = [] for k, v in d.items(): _A : Optional[int] = parent_key + sep + k if parent_key else k if isinstance(snake_case_,collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(snake_case_,snake_case_,sep=snake_case_ ).items() ) else: items.append((new_key, v) ) return dict(snake_case_ ) _A : List[Any] = argparse.Namespace() with open(snake_case_,"""r""" ) as yaml_file: try: _A : List[Any] = yaml.load(snake_case_,Loader=yaml.FullLoader ) _A : Optional[int] = flatten_yaml_as_dict(snake_case_ ) for k, v in flat_cfg.items(): setattr(snake_case_,snake_case_,snake_case_ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(snake_case_,str(snake_case_ ) ) ) return config def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[Any] = MobileViTVaConfig() _A : Tuple = False # dataset if task_name.startswith("""imagenet1k_""" ): _A : Dict = 1000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : int = 384 else: _A : int = 256 _A : List[str] = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _A : Union[str, Any] = 21000 if int(task_name.strip().split("""_""" )[-1] ) == 384: _A : str = 384 else: _A : List[Any] = 256 _A : List[str] = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _A : int = 151 _A : int = 512 _A : Optional[int] = """ade20k-id2label.json""" _A : Any = True elif task_name.startswith("""voc_""" ): _A : List[Any] = 21 _A : Dict = 512 _A : Dict = """pascal-voc-id2label.json""" _A : int = True # orig_config _A : Any = load_orig_config_file(snake_case_ ) assert getattr(snake_case_,"""model.classification.name""",-1 ) == "mobilevit_v2", "Invalid model" _A : List[Any] = getattr(snake_case_,"""model.classification.mitv2.width_multiplier""",1.0 ) assert ( getattr(snake_case_,"""model.classification.mitv2.attn_norm_layer""",-1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _A : str = getattr(snake_case_,"""model.classification.activation.name""","""swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _A : Optional[int] = getattr(snake_case_,"""model.segmentation.output_stride""",16 ) if "_deeplabv3" in task_name: _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_rates""",[12, 24, 36] ) _A : int = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_out_channels""",512 ) _A : str = getattr(snake_case_,"""model.segmentation.deeplabv3.aspp_dropout""",0.1 ) # id2label _A : List[Any] = """huggingface/label-files""" _A : List[Any] = json.load(open(hf_hub_download(snake_case_,snake_case_,repo_type="""dataset""" ),"""r""" ) ) _A : str = {int(snake_case_ ): v for k, v in idalabel.items()} _A : str = idalabel _A : Dict = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Any = dct.pop(snake_case_ ) _A : Union[str, Any] = val def lowerCAmelCase_ ( snake_case_,snake_case_=False ): if base_model: _A : Optional[int] = """""" else: _A : Dict = """mobilevitv2.""" _A : int = [] for k in state_dict.keys(): if k[:8] == "encoder.": _A : Any = k[8:] else: _A : List[str] = k if ".block." in k: _A : Any = k_new.replace(""".block.""",""".""" ) if ".conv." in k: _A : List[Any] = k_new.replace(""".conv.""",""".convolution.""" ) if ".norm." in k: _A : Any = k_new.replace(""".norm.""",""".normalization.""" ) if "conv_1." in k: _A : int = k_new.replace("""conv_1.""",f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.''',f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: _A : Tuple = k_new.replace(""".exp_1x1.""",""".expand_1x1.""" ) if ".red_1x1." in k: _A : Optional[int] = k_new.replace(""".red_1x1.""",""".reduce_1x1.""" ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: _A : Optional[int] = k_new.replace(f'''layer_{i}.0.''',f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: _A : Union[str, Any] = k_new.replace(f'''layer_{i}.1.local_rep.0.''',f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: _A : str = k_new.replace(f'''layer_{i}.1.local_rep.1.''',f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: _A : Optional[int] = [0, 1] elif i == 4: _A : Union[str, Any] = [0, 1, 2, 3] elif i == 5: _A : Optional[Any] = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: _A : Union[str, Any] = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''',f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: _A : List[str] = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''',f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: _A : Optional[Any] = k_new.replace(f'''layer_{i}.1.conv_proj.''',f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_attn.0.""","""layernorm_before.""" ) if "pre_norm_attn.1." in k: _A : str = k_new.replace("""pre_norm_attn.1.""","""attention.""" ) if "pre_norm_ffn.0." in k: _A : Optional[Any] = k_new.replace("""pre_norm_ffn.0.""","""layernorm_after.""" ) if "pre_norm_ffn.1." in k: _A : Dict = k_new.replace("""pre_norm_ffn.1.""","""ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _A : List[str] = k_new.replace("""pre_norm_ffn.3.""","""ffn.conv2.""" ) if "classifier.1." in k: _A : List[str] = k_new.replace("""classifier.1.""","""classifier.""" ) if "seg_head." in k: _A : List[Any] = k_new.replace("""seg_head.""","""segmentation_head.""" ) if ".aspp_layer." in k: _A : List[Any] = k_new.replace(""".aspp_layer.""",""".""" ) if ".aspp_pool." in k: _A : Optional[Any] = k_new.replace(""".aspp_pool.""",""".""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(snake_case_ ) for k in keys_to_ignore: state_dict.pop(snake_case_,snake_case_ ) def lowerCAmelCase_ ( ): _A : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _A : List[Any] = Image.open(requests.get(snake_case_,stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[Any] = get_mobilevitva_config(snake_case_,snake_case_ ) # load original state_dict _A : Tuple = torch.load(snake_case_,map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _A : Optional[Any] = MobileViTVaForSemanticSegmentation(snake_case_ ).eval() _A : str = False else: _A : int = MobileViTVaForImageClassification(snake_case_ ).eval() _A : List[Any] = False # remove and rename some keys of load the original model _A : List[Any] = checkpoint remove_unused_keys(snake_case_ ) _A : Optional[Any] = create_rename_keys(snake_case_,base_model=snake_case_ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case_,snake_case_,snake_case_ ) # load modified state_dict model.load_state_dict(snake_case_ ) # Check outputs on an image, prepared by MobileViTImageProcessor _A : str = MobileViTImageProcessor(crop_size=config.image_size,size=config.image_size + 32 ) _A : List[Any] = image_processor(images=prepare_img(),return_tensors="""pt""" ) _A : Optional[Any] = model(**snake_case_ ) # verify classification model if task_name.startswith("""imagenet""" ): _A : List[Any] = outputs.logits _A : Optional[int] = logits.argmax(-1 ).item() print("""Predicted class:""",model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _A : int = torch.tensor([-1.63_36e00, -7.32_04e-02, -5.18_83e-01] ) assert torch.allclose(logits[0, :3],snake_case_,atol=1e-4 ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you'd like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) _snake_case = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
26
0
"""simple docstring""" 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 ( UpperCamelCase__ ): _lowerCamelCase :Tuple = ["image_processor", "tokenizer"] _lowerCamelCase :Union[str, Any] = "BlipImageProcessor" _lowerCamelCase :str = ("BertTokenizer", "BertTokenizerFast") def __init__( self : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : List[str] ) -> int: """simple docstring""" lowerCAmelCase__ : Union[str, Any] = False super().__init__(_a , _a ) lowerCAmelCase__ : int = self.image_processor def __call__( self : Union[str, Any] , UpperCamelCase : str = None , UpperCamelCase : str = None , UpperCamelCase : Any = True , UpperCamelCase : Any = False , UpperCamelCase : str = None , UpperCamelCase : str = None , UpperCamelCase : Any = 0 , UpperCamelCase : List[Any] = None , UpperCamelCase : List[Any] = None , UpperCamelCase : str = False , UpperCamelCase : List[Any] = False , UpperCamelCase : Union[str, Any] = False , UpperCamelCase : Dict = False , UpperCamelCase : Dict = False , UpperCamelCase : Dict = True , UpperCamelCase : Any = None , **UpperCamelCase : Tuple , ) -> BatchEncoding: """simple docstring""" 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: lowerCAmelCase__ : Dict = self.tokenizer lowerCAmelCase__ : Optional[int] = self.tokenizer( text=_a , add_special_tokens=_a , padding=_a , truncation=_a , max_length=_a , stride=_a , pad_to_multiple_of=_a , return_attention_mask=_a , return_overflowing_tokens=_a , return_special_tokens_mask=_a , return_offsets_mapping=_a , return_token_type_ids=_a , return_length=_a , verbose=_a , return_tensors=_a , **_a , ) return text_encoding # add pixel_values lowerCAmelCase__ : Any = self.image_processor(_a , return_tensors=_a ) if text is not None: lowerCAmelCase__ : List[str] = self.tokenizer( text=_a , add_special_tokens=_a , padding=_a , truncation=_a , max_length=_a , stride=_a , pad_to_multiple_of=_a , return_attention_mask=_a , return_overflowing_tokens=_a , return_special_tokens_mask=_a , return_offsets_mapping=_a , return_token_type_ids=_a , return_length=_a , verbose=_a , return_tensors=_a , **_a , ) else: lowerCAmelCase__ : Dict = None if text_encoding is not None: encoding_image_processor.update(_a ) return encoding_image_processor def _lowerCAmelCase ( self : List[Any] , *UpperCamelCase : List[Any] , **UpperCamelCase : List[str] ) -> int: """simple docstring""" return self.tokenizer.batch_decode(*_a , **_a ) def _lowerCAmelCase ( self : Any , *UpperCamelCase : List[Any] , **UpperCamelCase : Optional[int] ) -> Optional[int]: """simple docstring""" return self.tokenizer.decode(*_a , **_a ) @property def _lowerCAmelCase ( self : int ) -> int: """simple docstring""" lowerCAmelCase__ : Dict = self.tokenizer.model_input_names lowerCAmelCase__ : Tuple = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
242
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
0
from __future__ import annotations import math def __a ( SCREAMING_SNAKE_CASE ) -> Union[str, Any]: '''simple docstring''' if num <= 0: __UpperCAmelCase = f'''{num}: Invalid input, please enter a positive integer.''' raise ValueError(snake_case_ ) __UpperCAmelCase = [True] * (num + 1) __UpperCAmelCase = [] __UpperCAmelCase = 2 __UpperCAmelCase = int(math.sqrt(snake_case_ ) ) while start <= end: # If start is a prime if sieve[start] is True: prime.append(snake_case_ ) # Set multiples of start be False for i in range(start * start , num + 1 , snake_case_ ): if sieve[i] is True: __UpperCAmelCase = False start += 1 for j in range(end + 1 , num + 1 ): if sieve[j] is True: prime.append(snake_case_ ) return prime if __name__ == "__main__": print(prime_sieve(int(input('Enter a positive integer: ').strip())))
333
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = 1 @register_to_config def __init__( self , _a=2000 , _a=0.1 , _a=20 , _a=1e-3 ) -> List[Any]: _A : Dict = None _A : List[Any] = None _A : Dict = None def a__ ( self , _a , _a = None ) -> Union[str, Any]: _A : Union[str, Any] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def a__ ( self , _a , _a , _a , _a=None ) -> Dict: if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _A : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _A : List[Any] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _A : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): _A : List[Any] = std.unsqueeze(-1 ) _A : int = -score / std # compute _A : Tuple = -1.0 / len(self.timesteps ) _A : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _A : List[str] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _A : Union[str, Any] = beta_t.unsqueeze(-1 ) _A : Tuple = -0.5 * beta_t * x _A : Tuple = torch.sqrt(_a ) _A : Dict = drift - diffusion**2 * score _A : Dict = x + drift * dt # add noise _A : Any = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) _A : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ) -> Optional[Any]: return self.config.num_train_timesteps
26
0
"""simple docstring""" from ...processing_utils import ProcessorMixin class A_ (UpperCamelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = """WhisperFeatureExtractor""" SCREAMING_SNAKE_CASE__ : Any = """WhisperTokenizer""" def __init__( self , lowercase_ , lowercase_ ): """simple docstring""" super().__init__(_a , _a ) UpperCAmelCase_ : Dict = self.feature_extractor UpperCAmelCase_ : List[Any] = False def UpperCamelCase__ ( self , lowercase_=None , lowercase_=None , lowercase_=True ): """simple docstring""" return self.tokenizer.get_decoder_prompt_ids(task=_a , language=_a , no_timestamps=_a ) def __call__( self , *lowercase_ , **lowercase_ ): """simple docstring""" # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_a , **_a ) UpperCAmelCase_ : Union[str, Any] = kwargs.pop("audio" , _a ) UpperCAmelCase_ : Tuple = kwargs.pop("sampling_rate" , _a ) UpperCAmelCase_ : int = kwargs.pop("text" , _a ) if len(_a ) > 0: UpperCAmelCase_ : Dict = args[0] UpperCAmelCase_ : int = args[1:] if audio is None and text is None: raise ValueError("You need to specify either an `audio` or `text` input to process." ) if audio is not None: UpperCAmelCase_ : Optional[int] = self.feature_extractor(_a , *_a , sampling_rate=_a , **_a ) if text is not None: UpperCAmelCase_ : int = self.tokenizer(_a , **_a ) if text is None: return inputs elif audio is None: return encodings else: UpperCAmelCase_ : List[Any] = encodings["""input_ids"""] return inputs def UpperCamelCase__ ( self , *lowercase_ , **lowercase_ ): """simple docstring""" return self.tokenizer.batch_decode(*_a , **_a ) def UpperCamelCase__ ( self , *lowercase_ , **lowercase_ ): """simple docstring""" return self.tokenizer.decode(*_a , **_a ) def UpperCamelCase__ ( self , lowercase_ , lowercase_="np" ): """simple docstring""" return self.tokenizer.get_prompt_ids(_a , return_tensors=_a )
61
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: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "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", }, } _snake_case = { "google/fnet-base": 512, "google/fnet-large": 512, } _snake_case = "▁" class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["input_ids", "token_type_ids"] _a = FNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=True , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Optional[int]: # 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. _A : int = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) _A : Optional[int] = do_lower_case _A : List[Any] = remove_space _A : str = keep_accents _A : int = vocab_file _A : int = False if not self.vocab_file else True def a__ ( self , _a , _a = None ) -> List[int]: _A : str = [self.sep_token_id] _A : Dict = [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 a__ ( self , _a , _a = None ) -> List[int]: _A : Any = [self.sep_token_id] _A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : List[str] = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
26
0
a_ :Optional[int] = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" a_ :int = [{"type": "code", "content": INSTALL_CONTENT}] a_ :List[Any] = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
277
from math import asin, atan, cos, radians, sin, sqrt, tan _snake_case = 6_3_7_8_1_3_7.0 _snake_case = 6_3_5_6_7_5_2.3_1_4_2_4_5 _snake_case = 6378137 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Any = (AXIS_A - AXIS_B) / AXIS_A _A : Optional[int] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : List[str] = atan((1 - flattening) * tan(radians(snake_case_ ) ) ) _A : Optional[Any] = radians(snake_case_ ) _A : str = radians(snake_case_ ) # Equation _A : Dict = sin((phi_a - phi_a) / 2 ) _A : List[str] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda _A : Optional[int] = sqrt(sin_sq_phi + (cos(snake_case_ ) * cos(snake_case_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
26
0
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class a_ : """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase=2 , _lowerCamelCase=3 , _lowerCamelCase=4 , _lowerCamelCase=2 , _lowerCamelCase=7 , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=99 , _lowerCamelCase=36 , _lowerCamelCase=2 , _lowerCamelCase=4 , _lowerCamelCase=37 , _lowerCamelCase="gelu" , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=512 , _lowerCamelCase=16 , _lowerCamelCase=2 , _lowerCamelCase=0.0_2 , _lowerCamelCase=6 , _lowerCamelCase=6 , _lowerCamelCase=3 , _lowerCamelCase=4 , _lowerCamelCase=None , _lowerCamelCase=1000 , ) ->int: SCREAMING_SNAKE_CASE : Tuple = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_channels SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Tuple = patch_size SCREAMING_SNAKE_CASE : Union[str, Any] = is_training SCREAMING_SNAKE_CASE : List[Any] = use_input_mask SCREAMING_SNAKE_CASE : Tuple = use_token_type_ids SCREAMING_SNAKE_CASE : Optional[int] = use_labels SCREAMING_SNAKE_CASE : Any = vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Tuple = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[int] = initializer_range SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Dict = shape_size SCREAMING_SNAKE_CASE : Tuple = num_labels SCREAMING_SNAKE_CASE : str = num_choices SCREAMING_SNAKE_CASE : str = scope SCREAMING_SNAKE_CASE : Optional[Any] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : Union[str, Any] = text_seq_length SCREAMING_SNAKE_CASE : Optional[Any] = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : List[str] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self ) ->str: SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) SCREAMING_SNAKE_CASE : Dict = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : Union[str, Any] = bbox[i, j, 3] SCREAMING_SNAKE_CASE : Optional[int] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Dict = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Union[str, Any] = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Dict = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Union[str, Any] = tmp_coordinate SCREAMING_SNAKE_CASE : Any = tf.constant(_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : List[str] = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = None SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_labels: SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaConfig( 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 , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->Optional[int]: SCREAMING_SNAKE_CASE : str = TFLayoutLMvaModel(config=_a ) # text + image SCREAMING_SNAKE_CASE : List[str] = model(_a , pixel_values=_a , training=_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = model( _a , bbox=_a , pixel_values=_a , attention_mask=_a , token_type_ids=_a , training=_a , ) SCREAMING_SNAKE_CASE : int = model(_a , bbox=_a , pixel_values=_a , training=_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : Dict = model(_a , training=_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : Union[str, Any] = model({'''pixel_values''': pixel_values} , training=_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->int: SCREAMING_SNAKE_CASE : str = self.num_labels SCREAMING_SNAKE_CASE : List[str] = TFLayoutLMvaForSequenceClassification(config=_a ) SCREAMING_SNAKE_CASE : int = model( _a , bbox=_a , pixel_values=_a , attention_mask=_a , token_type_ids=_a , labels=_a , training=_a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->Any: SCREAMING_SNAKE_CASE : str = self.num_labels SCREAMING_SNAKE_CASE : Any = TFLayoutLMvaForTokenClassification(config=_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = model( _a , bbox=_a , pixel_values=_a , attention_mask=_a , token_type_ids=_a , labels=_a , training=_a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->Optional[int]: SCREAMING_SNAKE_CASE : int = 2 SCREAMING_SNAKE_CASE : Dict = TFLayoutLMvaForQuestionAnswering(config=_a ) SCREAMING_SNAKE_CASE : Tuple = model( _a , bbox=_a , pixel_values=_a , attention_mask=_a , token_type_ids=_a , start_positions=_a , end_positions=_a , training=_a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self ) ->Union[str, Any]: SCREAMING_SNAKE_CASE : int = self.prepare_config_and_inputs() (SCREAMING_SNAKE_CASE) : Any = config_and_inputs SCREAMING_SNAKE_CASE : List[Any] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class a_ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) __SCREAMING_SNAKE_CASE : List[Any] = ( {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel} if is_tf_available() else {} ) __SCREAMING_SNAKE_CASE : List[str] = False __SCREAMING_SNAKE_CASE : Optional[int] = False __SCREAMING_SNAKE_CASE : int = False def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->Optional[int]: return True def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) ->dict: SCREAMING_SNAKE_CASE : int = copy.deepcopy(_a ) if model_class in get_values(_a ): SCREAMING_SNAKE_CASE : List[Any] = { k: tf.tile(tf.expand_dims(_a , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(_a , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(_a ): SCREAMING_SNAKE_CASE : List[str] = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_a ): SCREAMING_SNAKE_CASE : Dict = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) SCREAMING_SNAKE_CASE : List[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_a ): SCREAMING_SNAKE_CASE : str = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_a ): SCREAMING_SNAKE_CASE : Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __lowerCAmelCase ( self ) ->List[str]: SCREAMING_SNAKE_CASE : List[str] = TFLayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : Optional[Any] = ConfigTester(self , config_class=_a , hidden_size=37 ) def __lowerCAmelCase ( self ) ->Optional[Any]: self.config_tester.run_common_tests() def __lowerCAmelCase ( self ) ->List[str]: SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(_a ) if getattr(_a , '''hf_compute_loss''' , _a ): # The number of elements in the loss should be the same as the number of elements in the label SCREAMING_SNAKE_CASE : int = self._prepare_for_class(inputs_dict.copy() , _a , return_labels=_a ) SCREAMING_SNAKE_CASE : int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=_a )[0] ] SCREAMING_SNAKE_CASE : int = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs SCREAMING_SNAKE_CASE : List[str] = self._prepare_for_class(inputs_dict.copy() , _a , return_labels=_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = prepared_for_class.pop('''input_ids''' ) SCREAMING_SNAKE_CASE : Tuple = model(_a , **_a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions SCREAMING_SNAKE_CASE : Tuple = self._prepare_for_class(inputs_dict.copy() , _a , return_labels=_a ) SCREAMING_SNAKE_CASE : Any = prepared_for_class.pop('''input_ids''' ) if "labels" in prepared_for_class: SCREAMING_SNAKE_CASE : int = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: SCREAMING_SNAKE_CASE : Tuple = -100 SCREAMING_SNAKE_CASE : Dict = tf.convert_to_tensor(_a ) SCREAMING_SNAKE_CASE : Any = model(_a , **_a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict SCREAMING_SNAKE_CASE : List[str] = self._prepare_for_class(inputs_dict.copy() , _a , return_labels=_a ) SCREAMING_SNAKE_CASE : int = model(_a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple SCREAMING_SNAKE_CASE : List[str] = self._prepare_for_class(inputs_dict.copy() , _a , return_labels=_a ) # Get keys that were added with the _prepare_for_class function SCREAMING_SNAKE_CASE : str = prepared_for_class.keys() - inputs_dict.keys() SCREAMING_SNAKE_CASE : Dict = inspect.signature(model.call ).parameters SCREAMING_SNAKE_CASE : Dict = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple SCREAMING_SNAKE_CASE : Dict = {0: """input_ids"""} for label_key in label_keys: SCREAMING_SNAKE_CASE : str = signature_names.index(_a ) SCREAMING_SNAKE_CASE : int = label_key SCREAMING_SNAKE_CASE : Dict = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple SCREAMING_SNAKE_CASE : List[str] = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: SCREAMING_SNAKE_CASE : Tuple = prepared_for_class[value] SCREAMING_SNAKE_CASE : int = tuple(_a ) # Send to model SCREAMING_SNAKE_CASE : List[str] = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __lowerCAmelCase ( self ) ->Optional[int]: ( SCREAMING_SNAKE_CASE ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(_a , _a , _a , _a , _a , _a ) def __lowerCAmelCase ( self ) ->Any: ( SCREAMING_SNAKE_CASE ) : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : Optional[Any] = type self.model_tester.create_and_check_model(_a , _a , _a , _a , _a , _a ) def __lowerCAmelCase ( self ) ->Tuple: ( SCREAMING_SNAKE_CASE ) : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( _a , _a , _a , _a , _a , _a , _a ) def __lowerCAmelCase ( self ) ->Tuple: ( SCREAMING_SNAKE_CASE ) : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( _a , _a , _a , _a , _a , _a , _a ) def __lowerCAmelCase ( self ) ->List[str]: ( SCREAMING_SNAKE_CASE ) : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( _a , _a , _a , _a , _a , _a , _a ) @slow def __lowerCAmelCase ( self ) ->str: for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = TFLayoutLMvaModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def UpperCAmelCase_( ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf class a_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowerCAmelCase ( self ) ->Tuple: return LayoutLMvaImageProcessor(apply_ocr=_a ) if is_vision_available() else None @slow def __lowerCAmelCase ( self ) ->Union[str, Any]: SCREAMING_SNAKE_CASE : Optional[int] = TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ) SCREAMING_SNAKE_CASE : Tuple = self.default_image_processor SCREAMING_SNAKE_CASE : int = prepare_img() SCREAMING_SNAKE_CASE : List[Any] = image_processor(images=_a , return_tensors='''tf''' ).pixel_values SCREAMING_SNAKE_CASE : str = tf.constant([[1, 2]] ) SCREAMING_SNAKE_CASE : int = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model(input_ids=_a , bbox=_a , pixel_values=_a , training=_a ) # verify the logits SCREAMING_SNAKE_CASE : int = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , _a ) SCREAMING_SNAKE_CASE : List[Any] = tf.constant( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _a , atol=1e-4 ) )
313
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> Optional[int]: super().__init__(_a ) _A : Union[str, Any] = RobertaEmbeddings(_a ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ",UpperCamelCase__,) class lowercase ( UpperCamelCase__ ): _a = RobertaConfig _a = "roberta" def __init__( self , _a ) -> str: super().__init__(_a ) _A : Any = config.num_labels _A : Dict = config.num_hidden_layers _A : List[str] = DeeRobertaModel(_a ) _A : int = nn.Dropout(config.hidden_dropout_prob ) _A : int = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_a ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=-1 , _a=False , ) -> Any: _A : Optional[int] = self.num_layers try: _A : List[str] = self.roberta( _a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , ) _A : List[str] = outputs[1] _A : List[str] = self.dropout(_a ) _A : Optional[Any] = self.classifier(_a ) _A : List[Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: _A : List[Any] = e.message _A : Optional[int] = e.exit_layer _A : Optional[int] = outputs[0] if not self.training: _A : int = entropy(_a ) _A : int = [] _A : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression _A : Union[str, Any] = MSELoss() _A : Tuple = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Dict = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits _A : Optional[Any] = [] for highway_exit in outputs[-1]: _A : Tuple = highway_exit[0] if not self.training: highway_logits_all.append(_a ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression _A : List[str] = MSELoss() _A : Optional[int] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: _A : List[Any] = CrossEntropyLoss() _A : Tuple = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_a ) if train_highway: _A : Dict = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: _A : int = (loss,) + outputs if not self.training: _A : Optional[Any] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: _A : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
26
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) _A : Tuple = { 'configuration_deberta': ['DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DebertaConfig', 'DebertaOnnxConfig'], 'tokenization_deberta': ['DebertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : Union[str, Any] = ['DebertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : Optional[int] = [ 'DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'DebertaForMaskedLM', 'DebertaForQuestionAnswering', 'DebertaForSequenceClassification', 'DebertaForTokenClassification', 'DebertaModel', 'DebertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : Dict = [ 'TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDebertaForMaskedLM', 'TFDebertaForQuestionAnswering', 'TFDebertaForSequenceClassification', 'TFDebertaForTokenClassification', 'TFDebertaModel', 'TFDebertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys _A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
142
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" def __init__( self , _a=3_0522 , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
0
from math import isclose, sqrt def __lowercase ( _UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) ->Optional[Any]: """simple docstring""" lowercase : str = point_y / 4 / point_x lowercase : Any = 2 * normal_gradient / (1 + normal_gradient * normal_gradient) lowercase : Dict = (1 - normal_gradient * normal_gradient) / ( 1 + normal_gradient * normal_gradient ) lowercase : Optional[int] = (sa - ca * incoming_gradient) / (ca + sa * incoming_gradient) # to find the next point, solve the simultaeneous equations: # y^2 + 4x^2 = 100 # y - b = m * (x - a) # ==> A x^2 + B x + C = 0 lowercase : List[Any] = outgoing_gradient**2 + 4 lowercase : Union[str, Any] = 2 * outgoing_gradient * (point_y - outgoing_gradient * point_x) lowercase : List[str] = (point_y - outgoing_gradient * point_x) ** 2 - 100 lowercase : str = ( -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) lowercase : int = ( -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) # two solutions, one of which is our input point lowercase : Optional[int] = x_minus if isclose(snake_case_, snake_case_ ) else x_plus lowercase : str = point_y + outgoing_gradient * (next_x - point_x) return next_x, next_y, outgoing_gradient def __lowercase ( _UpperCamelCase = 1.4, _UpperCamelCase = -9.6 ) ->List[Any]: """simple docstring""" lowercase : int = 0 lowercase : float = first_x_coord lowercase : float = first_y_coord lowercase : float = (1_0.1 - point_y) / (0.0 - point_x) while not (-0.0_1 <= point_x <= 0.0_1 and point_y > 0): lowercase : Tuple = next_point(snake_case_, snake_case_, snake_case_ ) num_reflections += 1 return num_reflections if __name__ == "__main__": print(F'''{solution() = }''')
337
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) if n == 0: return 0 _A : Tuple = float("""-inf""" ) for i in range(1,n + 1 ): _A : str = max( snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_ ) ) return max_revue def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) _A : Dict = [float("""-inf""" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: _A : List[str] = float("""-inf""" ) for i in range(1,n + 1 ): _A : Optional[Any] = max( snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_ ),) _A : Tuple = max_revenue return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _enforce_args(snake_case_,snake_case_ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. _A : List[Any] = [float("""-inf""" ) for _ in range(n + 1 )] _A : Any = 0 for i in range(1,n + 1 ): _A : Optional[Any] = max_rev[i] for j in range(1,i + 1 ): _A : int = max(snake_case_,prices[j - 1] + max_rev[i - j] ) _A : int = max_revenue_i return max_rev[n] def lowerCAmelCase_ ( snake_case_,snake_case_ ): if n < 0: _A : Optional[Any] = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(snake_case_ ) if n > len(snake_case_ ): _A : Any = ( """Each integral piece of rod must have a corresponding price. """ f'''Got n = {n} but length of prices = {len(snake_case_ )}''' ) raise ValueError(snake_case_ ) def lowerCAmelCase_ ( ): _A : Tuple = [6, 10, 12, 15, 20, 23] _A : List[Any] = len(snake_case_ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. _A : Any = 36 _A : List[Any] = top_down_cut_rod(snake_case_,snake_case_ ) _A : List[Any] = bottom_up_cut_rod(snake_case_,snake_case_ ) _A : Dict = naive_cut_rod_recursive(snake_case_,snake_case_ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
26
0
from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { 'huggingface/time-series-transformer-tourism-monthly': ( 'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json' ), # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer } class _UpperCamelCase ( UpperCamelCase__ ): '''simple docstring''' lowerCamelCase__ ='time_series_transformer' lowerCamelCase__ ={ 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self : List[Any] , a : List[Any] = None , a : Union[str, Any] = None , a : Any = "student_t" , a : List[str] = "nll" , a : str = 1 , a : Any = [1, 2, 3, 4, 5, 6, 7] , a : int = "mean" , a : int = 0 , a : List[Any] = 0 , a : Tuple = 0 , a : Optional[Any] = 0 , a : List[str] = None , a : List[Any] = None , a : int = 32 , a : List[Any] = 32 , a : Dict = 2 , a : Optional[int] = 2 , a : List[Any] = 2 , a : int = 2 , a : Optional[int] = True , a : str = "gelu" , a : str = 64 , a : Union[str, Any] = 0.1 , a : int = 0.1 , a : str = 0.1 , a : Optional[int] = 0.1 , a : str = 0.1 , a : List[Any] = 100 , a : List[str] = 0.02 , a : Union[str, Any]=True , **a : Dict , ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE : Any = prediction_length SCREAMING_SNAKE_CASE : List[Any] = context_length or prediction_length SCREAMING_SNAKE_CASE : Dict = distribution_output SCREAMING_SNAKE_CASE : List[str] = loss SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Optional[Any] = num_time_features SCREAMING_SNAKE_CASE : Optional[int] = lags_sequence SCREAMING_SNAKE_CASE : Optional[int] = scaling SCREAMING_SNAKE_CASE : Optional[Any] = num_dynamic_real_features SCREAMING_SNAKE_CASE : str = num_static_real_features SCREAMING_SNAKE_CASE : Union[str, Any] = num_static_categorical_features if cardinality and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) SCREAMING_SNAKE_CASE : List[str] = cardinality else: SCREAMING_SNAKE_CASE : int = [0] if embedding_dimension and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) SCREAMING_SNAKE_CASE : Tuple = embedding_dimension else: SCREAMING_SNAKE_CASE : Union[str, Any] = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] SCREAMING_SNAKE_CASE : List[Any] = num_parallel_samples # Transformer architecture configuration SCREAMING_SNAKE_CASE : Optional[Any] = input_size * len(_a ) + self._number_of_features SCREAMING_SNAKE_CASE : Optional[int] = d_model SCREAMING_SNAKE_CASE : List[str] = encoder_attention_heads SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : Any = encoder_ffn_dim SCREAMING_SNAKE_CASE : Dict = decoder_ffn_dim SCREAMING_SNAKE_CASE : int = encoder_layers SCREAMING_SNAKE_CASE : Dict = decoder_layers SCREAMING_SNAKE_CASE : int = dropout SCREAMING_SNAKE_CASE : Tuple = attention_dropout SCREAMING_SNAKE_CASE : List[str] = activation_dropout SCREAMING_SNAKE_CASE : Optional[int] = encoder_layerdrop SCREAMING_SNAKE_CASE : int = decoder_layerdrop SCREAMING_SNAKE_CASE : Optional[int] = activation_function SCREAMING_SNAKE_CASE : Dict = init_std SCREAMING_SNAKE_CASE : List[str] = use_cache super().__init__(is_encoder_decoder=_a , **_a ) @property def __UpperCamelCase ( self : str ) -> int: """simple docstring""" return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
76
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( snake_case_ = "AAPL" ): _A : str = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' _A : List[Any] = BeautifulSoup(requests.get(snake_case_ ).text,"""html.parser""" ) _A : Union[str, Any] = """My(6px) Pos(r) smartphone_Mt(6px)""" return soup.find("""div""",class_=class_ ).find("""span""" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"""Current {symbol:<4} stock price is {stock_price(symbol):>8}""")
26
0
'''simple docstring''' import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() __lowercase = logging.get_logger(__name__) def snake_case__ ( _A: str , _A: Any ) -> Optional[int]: '''simple docstring''' lowerCAmelCase = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (f"encoder.deit.blocks.{i}.norm1.weight", f"encoder.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"encoder.deit.blocks.{i}.norm1.bias", f"encoder.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (f"encoder.deit.blocks.{i}.attn.proj.weight", f"encoder.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (f"encoder.deit.blocks.{i}.attn.proj.bias", f"encoder.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append( (f"encoder.deit.blocks.{i}.norm2.weight", f"encoder.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"encoder.deit.blocks.{i}.norm2.bias", f"encoder.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append( (f"encoder.deit.blocks.{i}.mlp.fc1.weight", f"encoder.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append( (f"encoder.deit.blocks.{i}.mlp.fc1.bias", f"encoder.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append( (f"encoder.deit.blocks.{i}.mlp.fc2.weight", f"encoder.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"encoder.deit.blocks.{i}.mlp.fc2.bias", f"encoder.encoder.layer.{i}.output.dense.bias") ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ("""encoder.deit.cls_token""", """encoder.embeddings.cls_token"""), ("""encoder.deit.pos_embed""", """encoder.embeddings.position_embeddings"""), ("""encoder.deit.patch_embed.proj.weight""", """encoder.embeddings.patch_embeddings.projection.weight"""), ("""encoder.deit.patch_embed.proj.bias""", """encoder.embeddings.patch_embeddings.projection.bias"""), ("""encoder.deit.norm.weight""", """encoder.layernorm.weight"""), ("""encoder.deit.norm.bias""", """encoder.layernorm.bias"""), ] ) return rename_keys def snake_case__ ( _A: Dict , _A: List[Any] ) -> Any: '''simple docstring''' for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) lowerCAmelCase = state_dict.pop(f"encoder.deit.blocks.{i}.attn.qkv.weight" ) lowerCAmelCase = in_proj_weight[ : encoder_config.hidden_size, : ] lowerCAmelCase = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] lowerCAmelCase = in_proj_weight[ -encoder_config.hidden_size :, : ] def snake_case__ ( _A: Optional[Any] , _A: int , _A: int ) -> Union[str, Any]: '''simple docstring''' lowerCAmelCase = dct.pop(snake_case_ ) lowerCAmelCase = val def snake_case__ ( _A: List[Any] ) -> Tuple: '''simple docstring''' if "handwritten" in checkpoint_url: lowerCAmelCase = """https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg""" # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: lowerCAmelCase = """https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg""" lowerCAmelCase = Image.open(requests.get(snake_case_ , stream=snake_case_ ).raw ).convert("""RGB""" ) return im @torch.no_grad() def snake_case__ ( _A: List[Any] , _A: Any ) -> Union[str, Any]: '''simple docstring''' lowerCAmelCase = ViTConfig(image_size=384 , qkv_bias=snake_case_ ) lowerCAmelCase = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: lowerCAmelCase = 768 elif "large" in checkpoint_url: # use ViT-large encoder lowerCAmelCase = 1024 lowerCAmelCase = 4096 lowerCAmelCase = 24 lowerCAmelCase = 16 lowerCAmelCase = 1024 else: raise ValueError("""Should either find 'base' or 'large' in checkpoint URL""" ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: lowerCAmelCase = False lowerCAmelCase = """relu""" lowerCAmelCase = 1024 lowerCAmelCase = True lowerCAmelCase = False lowerCAmelCase = False # load HuggingFace model lowerCAmelCase = ViTModel(snake_case_ , add_pooling_layer=snake_case_ ) lowerCAmelCase = TrOCRForCausalLM(snake_case_ ) lowerCAmelCase = VisionEncoderDecoderModel(encoder=snake_case_ , decoder=snake_case_ ) model.eval() # load state_dict of original model, rename some keys lowerCAmelCase = torch.hub.load_state_dict_from_url(snake_case_ , map_location="""cpu""" , check_hash=snake_case_ )["""model"""] lowerCAmelCase = create_rename_keys(snake_case_ , snake_case_ ) for src, dest in rename_keys: rename_key(snake_case_ , snake_case_ , snake_case_ ) read_in_q_k_v(snake_case_ , snake_case_ ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): lowerCAmelCase = state_dict.pop(snake_case_ ) if key.startswith("""decoder""" ) and "output_projection" not in key: lowerCAmelCase = val else: lowerCAmelCase = val # load state dict model.load_state_dict(snake_case_ ) # Check outputs on an image lowerCAmelCase = ViTImageProcessor(size=encoder_config.image_size ) lowerCAmelCase = RobertaTokenizer.from_pretrained("""roberta-large""" ) lowerCAmelCase = TrOCRProcessor(snake_case_ , snake_case_ ) lowerCAmelCase = processor(images=prepare_img(snake_case_ ) , return_tensors="""pt""" ).pixel_values # verify logits lowerCAmelCase = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) lowerCAmelCase = model(pixel_values=snake_case_ , decoder_input_ids=snake_case_ ) lowerCAmelCase = outputs.logits lowerCAmelCase = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: lowerCAmelCase = torch.tensor( [-1.4_502, -4.6_683, -0.5_347, -2.9_291, 9.1_435, -3.0_571, 8.9_764, 1.7_560, 8.7_358, -1.5_311] ) elif "trocr-large-handwritten" in checkpoint_url: lowerCAmelCase = torch.tensor( [-2.6_437, -1.3_129, -2.2_596, -5.3_455, 6.3_539, 1.7_604, 5.4_991, 1.4_702, 5.6_113, 2.0_170] ) elif "trocr-base-printed" in checkpoint_url: lowerCAmelCase = torch.tensor( [-5.6_816, -5.8_388, 1.1_398, -6.9_034, 6.8_505, -2.4_393, 1.2_284, -1.0_232, -1.9_661, -3.9_210] ) elif "trocr-large-printed" in checkpoint_url: lowerCAmelCase = torch.tensor( [-6.0_162, -7.0_959, 4.4_155, -5.1_063, 7.0_468, -3.1_631, 2.6_466, -0.3_081, -0.8_106, -1.7_535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , snake_case_ , atol=1e-3 ), "First elements of logits not as expected" Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(snake_case_ ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(snake_case_ ) if __name__ == "__main__": __lowercase = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) __lowercase = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
272
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _a = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def a__ ( self , _a , _a , _a ) -> int: _A : str = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def a__ ( self , _a , _a ) -> Dict: _A : Any = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) _A : List[Any] = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) _A : Optional[int] = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def a__ ( self ) -> List[str]: _A : Any = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility _A : Dict = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) _A : Any = 3 _A : Any = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) _A : Optional[int] = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) _A : Dict = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) _A : Dict = generator.model.config.eos_token_id _A : List[str] = """<pad>""" _A : Dict = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def a__ ( self ) -> int: _A : Optional[Any] = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility _A : str = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
26
0
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) UpperCAmelCase__ : str = logging.get_logger(__name__) UpperCAmelCase__ : str = OrderedDict( [ ('audio-spectrogram-transformer', 'ASTFeatureExtractor'), ('beit', 'BeitFeatureExtractor'), ('chinese_clip', 'ChineseCLIPFeatureExtractor'), ('clap', 'ClapFeatureExtractor'), ('clip', 'CLIPFeatureExtractor'), ('clipseg', 'ViTFeatureExtractor'), ('conditional_detr', 'ConditionalDetrFeatureExtractor'), ('convnext', 'ConvNextFeatureExtractor'), ('cvt', 'ConvNextFeatureExtractor'), ('data2vec-audio', 'Wav2Vec2FeatureExtractor'), ('data2vec-vision', 'BeitFeatureExtractor'), ('deformable_detr', 'DeformableDetrFeatureExtractor'), ('deit', 'DeiTFeatureExtractor'), ('detr', 'DetrFeatureExtractor'), ('dinat', 'ViTFeatureExtractor'), ('donut-swin', 'DonutFeatureExtractor'), ('dpt', 'DPTFeatureExtractor'), ('encodec', 'EncodecFeatureExtractor'), ('flava', 'FlavaFeatureExtractor'), ('glpn', 'GLPNFeatureExtractor'), ('groupvit', 'CLIPFeatureExtractor'), ('hubert', 'Wav2Vec2FeatureExtractor'), ('imagegpt', 'ImageGPTFeatureExtractor'), ('layoutlmv2', 'LayoutLMv2FeatureExtractor'), ('layoutlmv3', 'LayoutLMv3FeatureExtractor'), ('levit', 'LevitFeatureExtractor'), ('maskformer', 'MaskFormerFeatureExtractor'), ('mctct', 'MCTCTFeatureExtractor'), ('mobilenet_v1', 'MobileNetV1FeatureExtractor'), ('mobilenet_v2', 'MobileNetV2FeatureExtractor'), ('mobilevit', 'MobileViTFeatureExtractor'), ('nat', 'ViTFeatureExtractor'), ('owlvit', 'OwlViTFeatureExtractor'), ('perceiver', 'PerceiverFeatureExtractor'), ('poolformer', 'PoolFormerFeatureExtractor'), ('regnet', 'ConvNextFeatureExtractor'), ('resnet', 'ConvNextFeatureExtractor'), ('segformer', 'SegformerFeatureExtractor'), ('sew', 'Wav2Vec2FeatureExtractor'), ('sew-d', 'Wav2Vec2FeatureExtractor'), ('speech_to_text', 'Speech2TextFeatureExtractor'), ('speecht5', 'SpeechT5FeatureExtractor'), ('swiftformer', 'ViTFeatureExtractor'), ('swin', 'ViTFeatureExtractor'), ('swinv2', 'ViTFeatureExtractor'), ('table-transformer', 'DetrFeatureExtractor'), ('timesformer', 'VideoMAEFeatureExtractor'), ('tvlt', 'TvltFeatureExtractor'), ('unispeech', 'Wav2Vec2FeatureExtractor'), ('unispeech-sat', 'Wav2Vec2FeatureExtractor'), ('van', 'ConvNextFeatureExtractor'), ('videomae', 'VideoMAEFeatureExtractor'), ('vilt', 'ViltFeatureExtractor'), ('vit', 'ViTFeatureExtractor'), ('vit_mae', 'ViTFeatureExtractor'), ('vit_msn', 'ViTFeatureExtractor'), ('wav2vec2', 'Wav2Vec2FeatureExtractor'), ('wav2vec2-conformer', 'Wav2Vec2FeatureExtractor'), ('wavlm', 'Wav2Vec2FeatureExtractor'), ('whisper', 'WhisperFeatureExtractor'), ('xclip', 'CLIPFeatureExtractor'), ('yolos', 'YolosFeatureExtractor'), ] ) UpperCAmelCase__ : List[str] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCamelCase__ ( a ) -> int: for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A: List[str] = model_type_to_module_name(snake_case_ ) _A: List[Any] = importlib.import_module(f""".{module_name}""" , '''transformers.models''' ) try: return getattr(snake_case_ , snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_ , '''__name__''' , snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A: List[Any] = importlib.import_module('''transformers''' ) if hasattr(snake_case_ , snake_case_ ): return getattr(snake_case_ , snake_case_ ) return None def lowerCamelCase__ ( a , a = None , a = False , a = False , a = None , a = None , a = None , a = False , **a , ) -> Optional[Any]: _A: Optional[int] = get_file_from_repo( snake_case_ , snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , resume_download=snake_case_ , proxies=snake_case_ , use_auth_token=snake_case_ , revision=snake_case_ , local_files_only=snake_case_ , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(snake_case_ , encoding='''utf-8''' ) as reader: return json.load(snake_case_ ) class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[Any] ): """simple docstring""" raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(_a ) def __magic_name__ ( cls : Union[str, Any] , lowerCAmelCase_ : List[Any] , **lowerCAmelCase_ : List[Any] ): """simple docstring""" _A: Tuple = kwargs.pop('''config''' , _a ) _A: Tuple = kwargs.pop('''trust_remote_code''' , _a ) _A: List[Any] = True _A: Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A: Tuple = config_dict.get('''feature_extractor_type''' , _a ) _A: int = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): _A: Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A: int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A: Optional[int] = getattr(_a , '''feature_extractor_type''' , _a ) if hasattr(_a , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: _A: Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A: Optional[Any] = feature_extractor_class_from_name(_a ) _A: List[Any] = feature_extractor_auto_map is not None _A: Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A: Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A: Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A: str = kwargs.pop('''code_revision''' , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A: Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F"""Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a """ F"""`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __magic_name__ ( lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Dict ): """simple docstring""" FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
121
def lowerCAmelCase_ ( snake_case_,snake_case_ ): while b: _A , _A : List[str] = b, a % b return a def lowerCAmelCase_ ( snake_case_,snake_case_ ): return a if b == 0 else euclidean_gcd_recursive(snake_case_,a % b ) def lowerCAmelCase_ ( ): print(f'''euclidean_gcd(3, 5) = {euclidean_gcd(3,5 )}''' ) print(f'''euclidean_gcd(5, 3) = {euclidean_gcd(5,3 )}''' ) print(f'''euclidean_gcd(1, 3) = {euclidean_gcd(1,3 )}''' ) print(f'''euclidean_gcd(3, 6) = {euclidean_gcd(3,6 )}''' ) print(f'''euclidean_gcd(6, 3) = {euclidean_gcd(6,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,5 )}''' ) print(f'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,3 )}''' ) print(f'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,3 )}''' ) print(f'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,6 )}''' ) print(f'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,3 )}''' ) if __name__ == "__main__": main()
26
0