code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase = logging.get_logger(__name__) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=False ): """simple docstring""" lowercase__ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'blocks.{i}.norm1.weight', f'vit.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((f'blocks.{i}.norm1.bias', f'vit.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append((f'blocks.{i}.attn.proj.weight', f'vit.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append((f'blocks.{i}.attn.proj.bias', f'vit.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append((f'blocks.{i}.norm2.weight', f'vit.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((f'blocks.{i}.norm2.bias', f'vit.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append((f'blocks.{i}.mlp.fc1.weight', f'vit.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append((f'blocks.{i}.mlp.fc1.bias', f'vit.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append((f'blocks.{i}.mlp.fc2.weight', f'vit.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((f'blocks.{i}.mlp.fc2.bias', f'vit.encoder.layer.{i}.output.dense.bias') ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" lowercase__ = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=False ): """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: lowercase__ = '''''' else: lowercase__ = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowercase__ = state_dict.pop(f'blocks.{i}.attn.qkv.weight' ) lowercase__ = state_dict.pop(f'blocks.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict lowercase__ = in_proj_weight[ : config.hidden_size, : ] lowercase__ = in_proj_bias[: config.hidden_size] lowercase__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowercase__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowercase__ = in_proj_weight[ -config.hidden_size :, : ] lowercase__ = in_proj_bias[-config.hidden_size :] def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = dct.pop(SCREAMING_SNAKE_CASE ) lowercase__ = val def _a ( ): """simple docstring""" lowercase__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowercase__ = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=True ): """simple docstring""" lowercase__ = ViTConfig() # patch_size if model_name[-1] == "8": lowercase__ = 8 # set labels if required if not base_model: lowercase__ = 10_00 lowercase__ = '''huggingface/label-files''' lowercase__ = '''imagenet-1k-id2label.json''' lowercase__ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) , '''r''' ) ) lowercase__ = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: lowercase__ = 3_84 lowercase__ = 15_36 lowercase__ = 12 lowercase__ = 6 # load original model from torch hub lowercase__ = torch.hub.load('''facebookresearch/dino:main''' , SCREAMING_SNAKE_CASE ) original_model.eval() # load state_dict of original model, remove and rename some keys lowercase__ = original_model.state_dict() if base_model: remove_classification_head_(SCREAMING_SNAKE_CASE ) lowercase__ = create_rename_keys(SCREAMING_SNAKE_CASE , base_model=SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) read_in_q_k_v(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # load HuggingFace model if base_model: lowercase__ = ViTModel(SCREAMING_SNAKE_CASE , add_pooling_layer=SCREAMING_SNAKE_CASE ).eval() else: lowercase__ = ViTForImageClassification(SCREAMING_SNAKE_CASE ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE ) # Check outputs on an image, prepared by ViTImageProcessor lowercase__ = ViTImageProcessor() lowercase__ = image_processor(images=prepare_img() , return_tensors='''pt''' ) lowercase__ = encoding['''pixel_values'''] lowercase__ = model(SCREAMING_SNAKE_CASE ) if base_model: lowercase__ = original_model(SCREAMING_SNAKE_CASE ) assert torch.allclose(SCREAMING_SNAKE_CASE , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: lowercase__ = original_model(SCREAMING_SNAKE_CASE ) assert logits.shape == outputs.logits.shape assert torch.allclose(SCREAMING_SNAKE_CASE , outputs.logits , atol=1E-3 ) Path(SCREAMING_SNAKE_CASE ).mkdir(exist_ok=SCREAMING_SNAKE_CASE ) print(f'Saving model {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(SCREAMING_SNAKE_CASE ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='dino_vitb16', type=str, help='Name of the model trained with DINO you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--base_model', action='store_true', help='Whether to only convert the base model (no projection head weights).', ) parser.set_defaults(base_model=True) lowerCAmelCase = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
110
from __future__ import annotations from PIL import Image # Define glider example UpperCAmelCase_ = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example UpperCAmelCase_ = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def lowerCamelCase__ ( A__ : list[list[int]] ): '''simple docstring''' __lowerCamelCase = [] for i in range(len(A__ ) ): __lowerCamelCase = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours __lowerCamelCase = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(A__ ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(A__ ) - 1: neighbour_count += cells[i + 1][j] if i < len(A__ ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. __lowerCamelCase = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(A__ ) return next_generation def lowerCamelCase__ ( A__ : list[list[int]] , A__ : int ): '''simple docstring''' __lowerCamelCase = [] for _ in range(A__ ): # Create output image __lowerCamelCase = Image.new("""RGB""" , (len(cells[0] ), len(A__ )) ) __lowerCamelCase = img.load() # Save cells to image for x in range(len(A__ ) ): for y in range(len(cells[0] ) ): __lowerCamelCase = 255 - cells[y][x] * 255 __lowerCamelCase = (colour, colour, colour) # Save image images.append(A__ ) __lowerCamelCase = new_generation(A__ ) return images if __name__ == "__main__": UpperCAmelCase_ = generate_images(GLIDER, 16) images[0].save('out.gif', save_all=True, append_images=images[1:])
12
0
"""simple docstring""" import mpmath # for roots of unity import numpy as np class UpperCAmelCase_ : def __init__( self : List[Any] , __UpperCamelCase : List[str]=None , __UpperCamelCase : int=None ) -> Any: # Input as list _UpperCamelCase = list(poly_a or [0] )[:] _UpperCamelCase = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() _UpperCamelCase = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() _UpperCamelCase = len(self.polyB ) # Add 0 to make lengths equal a power of 2 _UpperCamelCase = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform _UpperCamelCase = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product _UpperCamelCase = self.__multiply() def _UpperCamelCase ( self : Union[str, Any] , __UpperCamelCase : List[str] ) -> Dict: _UpperCamelCase = [[x] for x in self.polyA] if which == '''A''' else [[x] for x in self.polyB] # Corner case if len(__UpperCamelCase ) <= 1: return dft[0] # _UpperCamelCase = self.c_max_length // 2 while next_ncol > 0: _UpperCamelCase = [[] for i in range(__UpperCamelCase )] _UpperCamelCase = self.root**next_ncol # First half of next step _UpperCamelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__UpperCamelCase ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step _UpperCamelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__UpperCamelCase ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update _UpperCamelCase = new_dft _UpperCamelCase = next_ncol // 2 return dft[0] def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: _UpperCamelCase = self.__dft('''A''' ) _UpperCamelCase = self.__dft('''B''' ) _UpperCamelCase = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT _UpperCamelCase = 2 while next_ncol <= self.c_max_length: _UpperCamelCase = [[] for i in range(__UpperCamelCase )] _UpperCamelCase = self.root ** (next_ncol // 2) _UpperCamelCase = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update _UpperCamelCase = new_inverse_c next_ncol *= 2 # Unpack _UpperCamelCase = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1J for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self : Any ) -> Optional[Any]: _UpperCamelCase = '''A = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.polyA[: self.len_A] ) ) _UpperCamelCase = '''B = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.polyB[: self.len_B] ) ) _UpperCamelCase = '''A*B = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.product ) ) return F'''{a}\n{b}\n{c}''' # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
54
"""simple docstring""" import unittest from huggingface_hub import hf_hub_download from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor from transformers.pipelines import VideoClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_decord, require_tf, require_torch, require_torch_or_tf, require_vision, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf @require_vision @require_decord class UpperCAmelCase_ ( unittest.TestCase): snake_case__ = MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _UpperCamelCase ( self : Any , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Any , __UpperCamelCase : List[Any] ) -> Optional[Any]: _UpperCamelCase = hf_hub_download( repo_id='''nateraw/video-demo''' , filename='''archery.mp4''' , repo_type='''dataset''' ) _UpperCamelCase = VideoClassificationPipeline(model=__UpperCamelCase , image_processor=__UpperCamelCase , top_k=2 ) _UpperCamelCase = [ example_video_filepath, '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''', ] return video_classifier, examples def _UpperCamelCase ( self : Union[str, Any] , __UpperCamelCase : Tuple , __UpperCamelCase : List[Any] ) -> str: for example in examples: _UpperCamelCase = video_classifier(__UpperCamelCase ) self.assertEqual( __UpperCamelCase , [ {'''score''': ANY(__UpperCamelCase ), '''label''': ANY(__UpperCamelCase )}, {'''score''': ANY(__UpperCamelCase ), '''label''': ANY(__UpperCamelCase )}, ] , ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[Any]: _UpperCamelCase = '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification''' _UpperCamelCase = VideoMAEFeatureExtractor( size={'''shortest_edge''': 10} , crop_size={'''height''': 10, '''width''': 10} ) _UpperCamelCase = pipeline( '''video-classification''' , model=__UpperCamelCase , feature_extractor=__UpperCamelCase , frame_sampling_rate=4 ) _UpperCamelCase = hf_hub_download(repo_id='''nateraw/video-demo''' , filename='''archery.mp4''' , repo_type='''dataset''' ) _UpperCamelCase = video_classifier(__UpperCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__UpperCamelCase , decimals=4 ) , [{'''score''': 0.5_1_9_9, '''label''': '''LABEL_0'''}, {'''score''': 0.4_8_0_1, '''label''': '''LABEL_1'''}] , ) _UpperCamelCase = video_classifier( [ video_file_path, video_file_path, ] , top_k=2 , ) self.assertEqual( nested_simplify(__UpperCamelCase , decimals=4 ) , [ [{'''score''': 0.5_1_9_9, '''label''': '''LABEL_0'''}, {'''score''': 0.4_8_0_1, '''label''': '''LABEL_1'''}], [{'''score''': 0.5_1_9_9, '''label''': '''LABEL_0'''}, {'''score''': 0.4_8_0_1, '''label''': '''LABEL_1'''}], ] , ) @require_tf def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: pass
54
1
"""simple docstring""" # This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/ import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, ControlNetModel, DDIMScheduler, StableDiffusionControlNetImgaImgPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, ) enable_full_determinism() class _UpperCAmelCase ( __snake_case, __snake_case, __snake_case, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ =StableDiffusionControlNetImgaImgPipeline lowerCamelCase__ =TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'} lowerCamelCase__ =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCamelCase__ =IMAGE_TO_IMAGE_IMAGE_PARAMS.union({'control_image'} ) lowerCamelCase__ =IMAGE_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' torch.manual_seed(0 ) __snake_case : List[Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) torch.manual_seed(0 ) __snake_case : Optional[Any] = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) torch.manual_seed(0 ) __snake_case : Any = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=a_ , set_alpha_to_one=a_ , ) torch.manual_seed(0 ) __snake_case : Any = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __snake_case : List[str] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) __snake_case : Any = CLIPTextModel(a_ ) __snake_case : List[str] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __snake_case : Tuple = { '''unet''': unet, '''controlnet''': controlnet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE (self , a_ , a_=0 ): '''simple docstring''' if str(a_ ).startswith('''mps''' ): __snake_case : int = torch.manual_seed(a_ ) else: __snake_case : int = torch.Generator(device=a_ ).manual_seed(a_ ) __snake_case : List[str] = 2 __snake_case : List[str] = randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=a_ , device=torch.device(a_ ) , ) __snake_case : Any = floats_tensor(control_image.shape , rng=random.Random(a_ ) ).to(a_ ) __snake_case : str = image.cpu().permute(0 , 2 , 3 , 1 )[0] __snake_case : int = Image.fromarray(np.uinta(a_ ) ).convert('''RGB''' ).resize((64, 64) ) __snake_case : Any = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''image''': image, '''control_image''': control_image, } return inputs def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) class _UpperCAmelCase ( __snake_case, __snake_case, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ =StableDiffusionControlNetImgaImgPipeline lowerCamelCase__ =TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'} lowerCamelCase__ =TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCamelCase__ =frozenset([] ) # TO_DO: add image_params once refactored VaeImageProcessor.preprocess def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' torch.manual_seed(0 ) __snake_case : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) torch.manual_seed(0 ) def init_weights(a_ ): if isinstance(a_ , torch.nn.Convad ): torch.nn.init.normal(m.weight ) m.bias.data.fill_(1.0 ) __snake_case : Dict = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(a_ ) torch.manual_seed(0 ) __snake_case : Any = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(a_ ) torch.manual_seed(0 ) __snake_case : List[Any] = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=a_ , set_alpha_to_one=a_ , ) torch.manual_seed(0 ) __snake_case : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __snake_case : Optional[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) __snake_case : Dict = CLIPTextModel(a_ ) __snake_case : List[str] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __snake_case : Any = MultiControlNetModel([controlneta, controlneta] ) __snake_case : Any = { '''unet''': unet, '''controlnet''': controlnet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE (self , a_ , a_=0 ): '''simple docstring''' if str(a_ ).startswith('''mps''' ): __snake_case : Dict = torch.manual_seed(a_ ) else: __snake_case : Optional[int] = torch.Generator(device=a_ ).manual_seed(a_ ) __snake_case : Union[str, Any] = 2 __snake_case : Any = [ randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=a_ , device=torch.device(a_ ) , ), randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=a_ , device=torch.device(a_ ) , ), ] __snake_case : List[str] = floats_tensor(control_image[0].shape , rng=random.Random(a_ ) ).to(a_ ) __snake_case : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] __snake_case : Optional[Any] = Image.fromarray(np.uinta(a_ ) ).convert('''RGB''' ).resize((64, 64) ) __snake_case : Any = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''image''': image, '''control_image''': control_image, } return inputs def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = self.get_dummy_components() __snake_case : List[Any] = self.pipeline_class(**a_ ) pipe.to(a_ ) __snake_case : List[Any] = 10.0 __snake_case : List[str] = 4 __snake_case : Optional[Any] = self.get_dummy_inputs(a_ ) __snake_case : int = steps __snake_case : Union[str, Any] = scale __snake_case : str = pipe(**a_ )[0] __snake_case : int = self.get_dummy_inputs(a_ ) __snake_case : Tuple = steps __snake_case : Any = scale __snake_case : str = pipe(**a_ , control_guidance_start=0.1 , control_guidance_end=0.2 )[0] __snake_case : str = self.get_dummy_inputs(a_ ) __snake_case : Union[str, Any] = steps __snake_case : Tuple = scale __snake_case : List[str] = pipe(**a_ , control_guidance_start=[0.1, 0.3] , control_guidance_end=[0.2, 0.7] )[0] __snake_case : int = self.get_dummy_inputs(a_ ) __snake_case : Tuple = steps __snake_case : List[Any] = scale __snake_case : str = pipe(**a_ , control_guidance_start=0.4 , control_guidance_end=[0.5, 0.8] )[0] # make sure that all outputs are different assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = self.get_dummy_components() __snake_case : Union[str, Any] = self.pipeline_class(**a_ ) pipe.to(a_ ) pipe.set_progress_bar_config(disable=a_ ) with tempfile.TemporaryDirectory() as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(a_ ) except NotImplementedError: pass @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Any = ControlNetModel.from_pretrained('''lllyasviel/sd-controlnet-canny''' ) __snake_case : Union[str, Any] = StableDiffusionControlNetImgaImgPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , safety_checker=a_ , controlnet=a_ ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=a_ ) __snake_case : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) __snake_case : List[Any] = '''evil space-punk bird''' __snake_case : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png''' ).resize((5_12, 5_12) ) __snake_case : List[str] = load_image( '''https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png''' ).resize((5_12, 5_12) ) __snake_case : Any = pipe( a_ , a_ , control_image=a_ , generator=a_ , output_type='''np''' , num_inference_steps=50 , strength=0.6 , ) __snake_case : List[str] = output.images[0] assert image.shape == (5_12, 5_12, 3) __snake_case : Union[str, Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy''' ) assert np.abs(expected_image - image ).max() < 9E-2
102
"""simple docstring""" import argparse import collections import torch from flax import traverse_util from tax import checkpoints from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def _snake_case ( lowercase__ : Dict , lowercase__ : Dict , lowercase__ : str , lowercase__ : Tuple="attention" ) -> str: '''simple docstring''' lowerCAmelCase_ :Tuple = params[f"""{prefix}/layers_{i}/{layer_name}/key/kernel"""] lowerCAmelCase_ :Union[str, Any] = params[f"""{prefix}/layers_{i}/{layer_name}/out/kernel"""] lowerCAmelCase_ :Any = params[f"""{prefix}/layers_{i}/{layer_name}/query/kernel"""] lowerCAmelCase_ :Optional[int] = params[f"""{prefix}/layers_{i}/{layer_name}/value/kernel"""] return k, o, q, v def _snake_case ( lowercase__ : Optional[Any] , lowercase__ : Any , lowercase__ : int , lowercase__ : Any=False ) -> int: '''simple docstring''' if split_mlp_wi: lowerCAmelCase_ :Tuple = params[f"""{prefix}/layers_{i}/mlp/wi_0/kernel"""] lowerCAmelCase_ :List[str] = params[f"""{prefix}/layers_{i}/mlp/wi_1/kernel"""] lowerCAmelCase_ :Tuple = (wi_a, wi_a) else: lowerCAmelCase_ :List[Any] = params[f"""{prefix}/layers_{i}/mlp/wi/kernel"""] lowerCAmelCase_ :Dict = params[f"""{prefix}/layers_{i}/mlp/wo/kernel"""] return wi, wo def _snake_case ( lowercase__ : Any , lowercase__ : Dict , lowercase__ : Union[str, Any] , lowercase__ : Optional[int] ) -> Tuple: '''simple docstring''' return params[f"""{prefix}/layers_{i}/{layer_name}/scale"""] def _snake_case ( lowercase__ : dict , *, lowercase__ : int , lowercase__ : bool ) -> List[Any]: '''simple docstring''' lowerCAmelCase_ :Tuple = traverse_util.flatten_dict(variables["""target"""] ) lowerCAmelCase_ :Tuple = {"""/""".join(lowercase__ ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi lowerCAmelCase_ :Any = """encoder/layers_0/mlp/wi_0/kernel""" in old print("""Split MLP:""" , lowercase__ ) lowerCAmelCase_ :List[Any] = collections.OrderedDict() # Shared embeddings. lowerCAmelCase_ :Optional[int] = old["""token_embedder/embedding"""] # Encoder. for i in range(lowercase__ ): # Block i, layer 0 (Self Attention). lowerCAmelCase_ :int = tax_layer_norm_lookup(lowercase__ , lowercase__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ :str = tax_attention_lookup(lowercase__ , lowercase__ , """encoder""" , """attention""" ) lowerCAmelCase_ :Optional[Any] = layer_norm lowerCAmelCase_ :Any = k.T lowerCAmelCase_ :Tuple = o.T lowerCAmelCase_ :Tuple = q.T lowerCAmelCase_ :str = v.T # Block i, layer 1 (MLP). lowerCAmelCase_ :Dict = tax_layer_norm_lookup(lowercase__ , lowercase__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ :Any = tax_mlp_lookup(lowercase__ , lowercase__ , """encoder""" , lowercase__ ) lowerCAmelCase_ :Union[str, Any] = layer_norm if split_mlp_wi: lowerCAmelCase_ :List[Any] = wi[0].T lowerCAmelCase_ :Dict = wi[1].T else: lowerCAmelCase_ :int = wi.T lowerCAmelCase_ :List[str] = wo.T lowerCAmelCase_ :Tuple = old[ """encoder/relpos_bias/rel_embedding""" ].T lowerCAmelCase_ :List[str] = old["""encoder/encoder_norm/scale"""] if not is_encoder_only: # Decoder. for i in range(lowercase__ ): # Block i, layer 0 (Self Attention). lowerCAmelCase_ :Optional[Any] = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ :List[Any] = tax_attention_lookup(lowercase__ , lowercase__ , """decoder""" , """self_attention""" ) lowerCAmelCase_ :List[Any] = layer_norm lowerCAmelCase_ :List[str] = k.T lowerCAmelCase_ :Any = o.T lowerCAmelCase_ :Any = q.T lowerCAmelCase_ :Dict = v.T # Block i, layer 1 (Cross Attention). lowerCAmelCase_ :int = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ :Tuple = tax_attention_lookup(lowercase__ , lowercase__ , """decoder""" , """encoder_decoder_attention""" ) lowerCAmelCase_ :Optional[int] = layer_norm lowerCAmelCase_ :str = k.T lowerCAmelCase_ :Tuple = o.T lowerCAmelCase_ :Any = q.T lowerCAmelCase_ :int = v.T # Block i, layer 2 (MLP). lowerCAmelCase_ :Any = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ :Dict = tax_mlp_lookup(lowercase__ , lowercase__ , """decoder""" , lowercase__ ) lowerCAmelCase_ :List[Any] = layer_norm if split_mlp_wi: lowerCAmelCase_ :Any = wi[0].T lowerCAmelCase_ :Any = wi[1].T else: lowerCAmelCase_ :Tuple = wi.T lowerCAmelCase_ :List[str] = wo.T lowerCAmelCase_ :Optional[Any] = old["""decoder/decoder_norm/scale"""] lowerCAmelCase_ :Optional[Any] = old[ """decoder/relpos_bias/rel_embedding""" ].T # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: lowerCAmelCase_ :Tuple = old["""decoder/logits_dense/kernel"""].T return new def _snake_case ( lowercase__ : Union[str, Any] , lowercase__ : bool ) -> Union[str, Any]: '''simple docstring''' lowerCAmelCase_ :Optional[int] = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: lowerCAmelCase_ :Optional[int] = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCAmelCase_ :Tuple = state_dict["""shared.weight"""] if "lm_head.weight" not in state_dict: # For old 1.0 models. print("""Using shared word embeddings as lm_head.""" ) lowerCAmelCase_ :Any = state_dict["""shared.weight"""] return state_dict def _snake_case ( lowercase__ : Union[str, Any] , lowercase__ : str , lowercase__ : List[Any] , lowercase__ : Optional[Any] ) -> Optional[int]: '''simple docstring''' lowerCAmelCase_ :List[Any] = checkpoints.load_tax_checkpoint(lowercase__ ) lowerCAmelCase_ :Optional[int] = convert_tax_to_pytorch(lowercase__ , num_layers=config.num_layers , is_encoder_only=lowercase__ ) lowerCAmelCase_ :Union[str, Any] = make_state_dict(lowercase__ , lowercase__ ) model.load_state_dict(lowercase__ , strict=lowercase__ ) def _snake_case ( lowercase__ : List[Any] , lowercase__ : Optional[Any] , lowercase__ : str , lowercase__ : bool = False ) -> Any: '''simple docstring''' lowerCAmelCase_ :Any = TaConfig.from_json_file(lowercase__ ) print(f"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: lowerCAmelCase_ :List[Any] = TaEncoderModel(lowercase__ ) else: lowerCAmelCase_ :List[str] = TaForConditionalGeneration(lowercase__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) # Save pytorch-model print(f"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(lowercase__ ) # Verify that we can load the checkpoint. model.from_pretrained(lowercase__ ) print("""Done""" ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser(description='Converts a native T5X checkpoint into a PyTorch checkpoint.') # Required parameters parser.add_argument( '--t5x_checkpoint_path', default=None, type=str, required=True, help='Path to the T5X checkpoint.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--is_encoder_only', action='store_true', help='Check if the model is encoder-decoder model', default=False ) __UpperCAmelCase = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only )
84
0
import collections import inspect import unittest from transformers import FocalNetConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, ) from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class A : """simple docstring""" def __init__( self : Any,lowercase_ : Tuple,lowercase_ : str=1_3,lowercase_ : Optional[int]=3_2,lowercase_ : Any=2,lowercase_ : Dict=3,lowercase_ : Optional[Any]=1_6,lowercase_ : List[str]=[3_2, 6_4, 1_2_8],lowercase_ : str=[1, 2, 1],lowercase_ : Optional[Any]=[2, 2, 4],lowercase_ : Tuple=2,lowercase_ : List[Any]=2.0,lowercase_ : Tuple=True,lowercase_ : Optional[int]=0.0,lowercase_ : str=0.0,lowercase_ : str=0.1,lowercase_ : str="gelu",lowercase_ : Union[str, Any]=False,lowercase_ : Tuple=True,lowercase_ : Union[str, Any]=0.02,lowercase_ : Optional[Any]=1E-5,lowercase_ : str=True,lowercase_ : Union[str, Any]=None,lowercase_ : int=True,lowercase_ : List[str]=1_0,lowercase_ : Dict=8,lowercase_ : Optional[int]=["stage1", "stage2"],lowercase_ : Union[str, Any]=[1, 2],)-> Union[str, Any]: '''simple docstring''' A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = embed_dim A__ = hidden_sizes A__ = depths A__ = num_heads A__ = window_size A__ = mlp_ratio A__ = qkv_bias A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = drop_path_rate A__ = hidden_act A__ = use_absolute_embeddings A__ = patch_norm A__ = layer_norm_eps A__ = initializer_range A__ = is_training A__ = scope A__ = use_labels A__ = type_sequence_label_size A__ = encoder_stride A__ = out_features A__ = out_indices def snake_case__ ( self : Tuple )-> str: '''simple docstring''' A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size],self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def snake_case__ ( self : int )-> Dict: '''simple docstring''' return FocalNetConfig( image_size=self.image_size,patch_size=self.patch_size,num_channels=self.num_channels,embed_dim=self.embed_dim,hidden_sizes=self.hidden_sizes,depths=self.depths,num_heads=self.num_heads,window_size=self.window_size,mlp_ratio=self.mlp_ratio,qkv_bias=self.qkv_bias,hidden_dropout_prob=self.hidden_dropout_prob,attention_probs_dropout_prob=self.attention_probs_dropout_prob,drop_path_rate=self.drop_path_rate,hidden_act=self.hidden_act,use_absolute_embeddings=self.use_absolute_embeddings,path_norm=self.patch_norm,layer_norm_eps=self.layer_norm_eps,initializer_range=self.initializer_range,encoder_stride=self.encoder_stride,out_features=self.out_features,out_indices=self.out_indices,) def snake_case__ ( self : Union[str, Any],lowercase_ : str,lowercase_ : str,lowercase_ : Optional[Any] )-> Dict: '''simple docstring''' A__ = FocalNetModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = model(UpperCamelCase__ ) A__ = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) A__ = 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 snake_case__ ( self : List[Any],lowercase_ : Optional[Any],lowercase_ : int,lowercase_ : Dict )-> Dict: '''simple docstring''' A__ = FocalNetBackbone(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = model(UpperCamelCase__ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ),len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ),[self.batch_size, self.image_size, 8, 8] ) # verify channels self.parent.assertEqual(len(model.channels ),len(config.out_features ) ) self.parent.assertListEqual(model.channels,config.hidden_sizes[:-1] ) # verify backbone works with out_features=None A__ = None A__ = FocalNetBackbone(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = model(UpperCamelCase__ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ),1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ),[self.batch_size, self.image_size * 2, 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ),1 ) self.parent.assertListEqual(model.channels,[config.hidden_sizes[-1]] ) def snake_case__ ( self : Tuple,lowercase_ : Any,lowercase_ : List[str],lowercase_ : List[Any] )-> int: '''simple docstring''' A__ = FocalNetForMaskedImageModeling(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = model(UpperCamelCase__ ) self.parent.assertEqual( result.reconstruction.shape,(self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A__ = 1 A__ = FocalNetForMaskedImageModeling(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.reconstruction.shape,(self.batch_size, 1, self.image_size, self.image_size) ) def snake_case__ ( self : List[str],lowercase_ : Dict,lowercase_ : Union[str, Any],lowercase_ : Union[str, Any] )-> Any: '''simple docstring''' A__ = self.type_sequence_label_size A__ = FocalNetForImageClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = model(UpperCamelCase__,labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images A__ = 1 A__ = FocalNetForImageClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size) ) def snake_case__ ( self : Dict )-> Optional[int]: '''simple docstring''' A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class A ( __a , __a , unittest.TestCase ): """simple docstring""" lowerCamelCase = ( ( FocalNetModel, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetBackbone, ) if is_torch_available() else () ) lowerCamelCase = ( {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification} if is_torch_available() else {} ) lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False def snake_case__ ( self : int )-> List[str]: '''simple docstring''' A__ = FocalNetModelTester(self ) A__ = ConfigTester(self,config_class=UpperCamelCase__,embed_dim=3_7,has_text_modality=UpperCamelCase__ ) def snake_case__ ( self : Tuple )-> Optional[int]: '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def snake_case__ ( self : str )-> int: '''simple docstring''' return def snake_case__ ( self : Any )-> Optional[int]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def snake_case__ ( self : str )-> Any: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*UpperCamelCase__ ) def snake_case__ ( self : Dict )-> Optional[Any]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__ ) def snake_case__ ( self : Dict )-> List[str]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ ) @unittest.skip(reason='FocalNet does not use inputs_embeds' ) def snake_case__ ( self : Tuple )-> List[Any]: '''simple docstring''' pass @unittest.skip(reason='FocalNet does not use feedforward chunking' ) def snake_case__ ( self : Dict )-> Any: '''simple docstring''' pass def snake_case__ ( self : List[str] )-> Union[str, Any]: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: A__ = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings(),(nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__,nn.Linear ) ) def snake_case__ ( self : str )-> Tuple: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: A__ = model_class(UpperCamelCase__ ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1],UpperCamelCase__ ) def snake_case__ ( self : List[str],lowercase_ : Any,lowercase_ : Any,lowercase_ : Dict,lowercase_ : Union[str, Any] )-> Dict: '''simple docstring''' A__ = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCamelCase__,UpperCamelCase__ ) ) A__ = outputs.hidden_states A__ = getattr( self.model_tester,'expected_num_hidden_layers',len(self.model_tester.depths ) + 1 ) self.assertEqual(len(UpperCamelCase__ ),UpperCamelCase__ ) # FocalNet has a different seq_length A__ = ( config.patch_size if isinstance(config.patch_size,collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A__ = (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],) A__ = outputs.reshaped_hidden_states self.assertEqual(len(UpperCamelCase__ ),UpperCamelCase__ ) A__ , A__ , A__ , A__ = reshaped_hidden_states[0].shape A__ = ( reshaped_hidden_states[0].view(UpperCamelCase__,UpperCamelCase__,height * width ).permute(0,2,1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ),[num_patches, self.model_tester.embed_dim],) def snake_case__ ( self : Dict )-> int: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size,collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes[:-1]: A__ = True self.check_hidden_states_output(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True self.check_hidden_states_output(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,UpperCamelCase__ ) def snake_case__ ( self : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = ( 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) ) A__ = ( config.patch_size if isinstance(config.patch_size,collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A__ = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) A__ = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes[:-1]: A__ = True self.check_hidden_states_output(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,(padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True self.check_hidden_states_output(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,(padded_height, padded_width) ) @slow def snake_case__ ( self : Union[str, Any] )-> List[Any]: '''simple docstring''' for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = FocalNetModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def snake_case__ ( self : int )-> Any: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(UpperCamelCase__ ) for model_class in self.all_model_classes: A__ = model_class(config=UpperCamelCase__ ) for name, param in model.named_parameters(): if "embeddings" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item(),[0.0, 1.0],msg=F'Parameter {name} of model {model_class} seems not properly initialized',) @require_vision @require_torch class A ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case__ ( self : str )-> Dict: '''simple docstring''' return AutoImageProcessor.from_pretrained('microsoft/focalnet-tiny' ) if is_vision_available() else None @slow def snake_case__ ( self : List[Any] )-> str: '''simple docstring''' A__ = FocalNetForImageClassification.from_pretrained('microsoft/focalnet-tiny' ).to(UpperCamelCase__ ) A__ = self.default_image_processor A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) A__ = image_processor(images=UpperCamelCase__,return_tensors='pt' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ = model(**UpperCamelCase__ ) # verify the logits A__ = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape,UpperCamelCase__ ) A__ = torch.tensor([0.2_166, -0.4_368, 0.2_191] ).to(UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3],UpperCamelCase__,atol=1E-4 ) ) self.assertTrue(outputs.logits.argmax(dim=-1 ).item(),2_8_1 ) @require_torch class A ( __a , unittest.TestCase ): """simple docstring""" lowerCamelCase = (FocalNetBackbone,) if is_torch_available() else () lowerCamelCase = FocalNetConfig lowerCamelCase = False def snake_case__ ( self : Optional[int] )-> Dict: '''simple docstring''' A__ = FocalNetModelTester(self )
369
import numpy as np from transformers import Pipeline def _snake_case( SCREAMING_SNAKE_CASE__ : int ) -> int: '''simple docstring''' A__ = np.max(SCREAMING_SNAKE_CASE__ , axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) A__ = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) class A ( _UpperCAmelCase ): """simple docstring""" def snake_case__ ( self : Dict,**lowercase_ : Tuple )-> Tuple: '''simple docstring''' A__ = {} if "second_text" in kwargs: A__ = kwargs['second_text'] return preprocess_kwargs, {}, {} def snake_case__ ( self : List[Any],lowercase_ : int,lowercase_ : Optional[int]=None )-> List[str]: '''simple docstring''' return self.tokenizer(lowercase_,text_pair=lowercase_,return_tensors=self.framework ) def snake_case__ ( self : str,lowercase_ : Dict )-> List[str]: '''simple docstring''' return self.model(**lowercase_ ) def snake_case__ ( self : Dict,lowercase_ : Optional[int] )-> Dict: '''simple docstring''' A__ = model_outputs.logits[0].numpy() A__ = softmax(lowercase_ ) A__ = np.argmax(lowercase_ ) A__ = self.model.config.idalabel[best_class] A__ = probabilities[best_class].item() A__ = logits.tolist() return {"label": label, "score": score, "logits": logits}
282
0
from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging __lowerCamelCase : Optional[int] = logging.get_logger(__name__) __lowerCamelCase : Tuple = { '''deepmind/language-perceiver''': '''https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json''', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class a__ ( A__ ): A = 'perceiver' def __init__( self : List[Any],_A : Tuple=256,_A : str=1280,_A : List[Any]=768,_A : Union[str, Any]=1,_A : Union[str, Any]=26,_A : List[str]=8,_A : List[Any]=8,_A : List[Any]=None,_A : List[Any]=None,_A : Union[str, Any]="kv",_A : Any=1,_A : int=1,_A : Dict="gelu",_A : Any=0.1,_A : int=0.02,_A : int=1E-12,_A : Any=True,_A : Optional[Any]=262,_A : List[Any]=2048,_A : str=56,_A : Optional[int]=[368, 496],_A : Dict=16,_A : Tuple=1920,_A : List[Any]=16,_A : str=[1, 16, 224, 224],**_A : Optional[Any],): """simple docstring""" super().__init__(**_A ) SCREAMING_SNAKE_CASE_ : Dict = num_latents SCREAMING_SNAKE_CASE_ : List[Any] = d_latents SCREAMING_SNAKE_CASE_ : Union[str, Any] = d_model SCREAMING_SNAKE_CASE_ : Optional[int] = num_blocks SCREAMING_SNAKE_CASE_ : List[Any] = num_self_attends_per_block SCREAMING_SNAKE_CASE_ : Tuple = num_self_attention_heads SCREAMING_SNAKE_CASE_ : List[str] = num_cross_attention_heads SCREAMING_SNAKE_CASE_ : List[Any] = qk_channels SCREAMING_SNAKE_CASE_ : Any = v_channels SCREAMING_SNAKE_CASE_ : Any = cross_attention_shape_for_attention SCREAMING_SNAKE_CASE_ : List[str] = self_attention_widening_factor SCREAMING_SNAKE_CASE_ : Any = cross_attention_widening_factor SCREAMING_SNAKE_CASE_ : List[Any] = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Any = initializer_range SCREAMING_SNAKE_CASE_ : List[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_ : Tuple = use_query_residual # masked language modeling attributes SCREAMING_SNAKE_CASE_ : List[str] = vocab_size SCREAMING_SNAKE_CASE_ : Union[str, Any] = max_position_embeddings # image classification attributes SCREAMING_SNAKE_CASE_ : Dict = image_size # flow attributes SCREAMING_SNAKE_CASE_ : List[Any] = train_size # multimodal autoencoding attributes SCREAMING_SNAKE_CASE_ : str = num_frames SCREAMING_SNAKE_CASE_ : Any = audio_samples_per_frame SCREAMING_SNAKE_CASE_ : Tuple = samples_per_patch SCREAMING_SNAKE_CASE_ : Optional[Any] = output_shape class a__ ( A__ ): @property def __UpperCamelCase ( self : List[str] ): """simple docstring""" if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ : List[str] = {0: "batch", 1: "choice", 2: "sequence"} else: SCREAMING_SNAKE_CASE_ : str = {0: "batch", 1: "sequence"} return OrderedDict( [ ("inputs", dynamic_axis), ("attention_mask", dynamic_axis), ] ) @property def __UpperCamelCase ( self : List[Any] ): """simple docstring""" return 1E-4 def __UpperCamelCase ( self : List[str],_A : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"],_A : int = -1,_A : int = -1,_A : int = -1,_A : bool = False,_A : Optional[TensorType] = None,_A : int = 3,_A : int = 40,_A : int = 40,): """simple docstring""" if isinstance(_A,_A ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ : Tuple = compute_effective_axis_dimension( _A,fixed_dimension=OnnxConfig.default_fixed_batch,num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ : Tuple = preprocessor.num_special_tokens_to_add(_A ) SCREAMING_SNAKE_CASE_ : Any = compute_effective_axis_dimension( _A,fixed_dimension=OnnxConfig.default_fixed_sequence,num_token_to_add=_A ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ : Optional[Any] = [" ".join(["a"] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ : str = dict(preprocessor(_A,return_tensors=_A ) ) SCREAMING_SNAKE_CASE_ : List[str] = inputs.pop("input_ids" ) return inputs elif isinstance(_A,_A ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ : Any = compute_effective_axis_dimension(_A,fixed_dimension=OnnxConfig.default_fixed_batch ) SCREAMING_SNAKE_CASE_ : Optional[int] = self._generate_dummy_images(_A,_A,_A,_A ) SCREAMING_SNAKE_CASE_ : Any = dict(preprocessor(images=_A,return_tensors=_A ) ) SCREAMING_SNAKE_CASE_ : Any = inputs.pop("pixel_values" ) return inputs else: raise ValueError( "Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor." )
18
'''simple docstring''' import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging __lowerCAmelCase = ( """https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py""" ) __lowerCAmelCase = logging.get_logger(__name__) # pylint: disable=invalid-name def UpperCAmelCase_ (): """simple docstring""" _a : Optional[int] = 'https://pypi.org/pypi/diffusers/json' _a : int = json.loads(request.urlopen(__a ).read() )['releases'].keys() return sorted(__a , key=lambda __a : version.Version(__a ) ) def UpperCAmelCase_ (): """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(__a ) os.makedirs(__a , exist_ok=__a ) _a : str = Path(__a ) / '__init__.py' if not init_path.exists(): init_path.touch() def UpperCAmelCase_ (__a : Union[str, os.PathLike] ): """simple docstring""" init_hf_modules() _a : Dict = Path(__a ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(__a , exist_ok=__a ) _a : Optional[int] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def UpperCAmelCase_ (__a : str ): """simple docstring""" with open(__a , 'r' , encoding='utf-8' ) as f: _a : int = f.read() # Imports of the form `import .xxx` _a : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , __a , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , __a , flags=re.MULTILINE ) # Unique-ify return list(set(__a ) ) def UpperCAmelCase_ (__a : Any ): """simple docstring""" _a : Optional[int] = False _a : Optional[int] = [module_file] _a : List[str] = [] # Let's recurse through all relative imports while not no_change: _a : str = [] for f in files_to_check: new_imports.extend(get_relative_imports(__a ) ) _a : Union[str, Any] = Path(__a ).parent _a : str = [str(module_path / m ) for m in new_imports] _a : Tuple = [f for f in new_import_files if f not in all_relative_imports] _a : Dict = [f"""{f}.py""" for f in new_import_files] _a : List[str] = len(__a ) == 0 all_relative_imports.extend(__a ) return all_relative_imports def UpperCAmelCase_ (__a : Tuple ): """simple docstring""" with open(__a , 'r' , encoding='utf-8' ) as f: _a : Dict = f.read() # Imports of the form `import xxx` _a : Optional[int] = re.findall('^\s*import\s+(\S+)\s*$' , __a , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , __a , flags=re.MULTILINE ) # Only keep the top-level module _a : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all _a : Optional[int] = list(set(__a ) ) _a : List[str] = [] for imp in imports: try: importlib.import_module(__a ) except ImportError: missing_packages.append(__a ) if len(__a ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' f"""{', '.join(__a )}. Run `pip install {' '.join(__a )}`""" ) return get_relative_imports(__a ) def UpperCAmelCase_ (__a : Any , __a : str ): """simple docstring""" _a : Any = module_path.replace(os.path.sep , '.' ) _a : Union[str, Any] = importlib.import_module(__a ) if class_name is None: return find_pipeline_class(__a ) return getattr(__a , __a ) def UpperCAmelCase_ (__a : Optional[int] ): """simple docstring""" from ..pipelines import DiffusionPipeline _a : List[str] = dict(inspect.getmembers(__a , inspect.isclass ) ) _a : str = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , __a ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( f"""Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:""" f""" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in""" f""" {loaded_module}.""" ) _a : Any = cls return pipeline_class def UpperCAmelCase_ (__a : Union[str, os.PathLike] , __a : str , __a : Optional[Union[str, os.PathLike]] = None , __a : bool = False , __a : bool = False , __a : Optional[Dict[str, str]] = None , __a : Optional[Union[bool, str]] = None , __a : Optional[str] = None , __a : bool = False , ): """simple docstring""" _a : str = str(__a ) _a : Optional[Any] = os.path.join(__a , __a ) if os.path.isfile(__a ): _a : Tuple = module_file_or_url _a : Optional[Any] = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: _a : int = get_diffusers_versions() # cut ".dev0" _a : Any = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: _a : Any = latest_version if latest_version[1:] in available_versions else 'main' logger.info(f"""Defaulting to latest_version: {revision}.""" ) elif revision in available_versions: _a : Any = f"""v{revision}""" elif revision == "main": _a : Optional[int] = revision else: raise ValueError( f"""`custom_revision`: {revision} does not exist. Please make sure to choose one of""" f""" {', '.join(available_versions + ['main'] )}.""" ) # community pipeline on GitHub _a : Tuple = COMMUNITY_PIPELINES_URL.format(revision=__a , pipeline=__a ) try: _a : Any = cached_download( __a , cache_dir=__a , force_download=__a , proxies=__a , resume_download=__a , local_files_only=__a , use_auth_token=__a , ) _a : List[Any] = 'git' _a : Any = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(f"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise else: try: # Load from URL or cache if already cached _a : Optional[Any] = hf_hub_download( __a , __a , cache_dir=__a , force_download=__a , proxies=__a , resume_download=__a , local_files_only=__a , use_auth_token=__a , ) _a : List[Any] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(f"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise # Check we have all the requirements in our environment _a : Optional[int] = check_imports(__a ) # Now we move the module inside our cached dynamic modules. _a : Optional[Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(__a ) _a : Any = Path(__a ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(__a , submodule_path / module_file ) for module_needed in modules_needed: _a : Dict = f"""{module_needed}.py""" shutil.copy(os.path.join(__a , __a ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(__a , __a ): _a : Optional[Any] = use_auth_token elif use_auth_token is True: _a : List[Any] = HfFolder.get_token() else: _a : Dict = None _a : int = model_info(__a , revision=__a , token=__a ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. _a : Optional[int] = submodule_path / commit_hash _a : str = full_submodule + os.path.sep + commit_hash create_dynamic_module(__a ) if not (submodule_path / module_file).exists(): shutil.copy(__a , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( __a , f"""{module_needed}.py""" , cache_dir=__a , force_download=__a , resume_download=__a , proxies=__a , use_auth_token=__a , revision=__a , local_files_only=__a , ) return os.path.join(__a , __a ) def UpperCAmelCase_ (__a : Union[str, os.PathLike] , __a : str , __a : Optional[str] = None , __a : Optional[Union[str, os.PathLike]] = None , __a : bool = False , __a : bool = False , __a : Optional[Dict[str, str]] = None , __a : Optional[Union[bool, str]] = None , __a : Optional[str] = None , __a : bool = False , **__a : str , ): """simple docstring""" _a : Dict = get_cached_module_file( __a , __a , cache_dir=__a , force_download=__a , resume_download=__a , proxies=__a , use_auth_token=__a , revision=__a , local_files_only=__a , ) return get_class_in_module(__a , final_module.replace('.py' , '' ) )
271
0
import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed lowerCAmelCase : Any = """true""" def A_ ( _UpperCAmelCase , _UpperCAmelCase=82 , _UpperCAmelCase=16 ): set_seed(42 ) SCREAMING_SNAKE_CASE_: Union[str, Any] = RegressionModel() SCREAMING_SNAKE_CASE_: Optional[int] = deepcopy(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = RegressionDataset(length=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = DataLoader(_UpperCAmelCase , batch_size=_UpperCAmelCase ) model.to(accelerator.device ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = accelerator.prepare(_UpperCAmelCase , _UpperCAmelCase ) return model, ddp_model, dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: Union[str, Any] = AutoTokenizer.from_pretrained("hf-internal-testing/mrpc-bert-base-cased" ) SCREAMING_SNAKE_CASE_: Optional[int] = load_dataset("glue" , "mrpc" , split="validation" ) def tokenize_function(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) return outputs with accelerator.main_process_first(): SCREAMING_SNAKE_CASE_: List[str] = dataset.map( _UpperCAmelCase , batched=_UpperCAmelCase , remove_columns=["idx", "sentence1", "sentence2"] , ) SCREAMING_SNAKE_CASE_: Tuple = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): if use_longest: return tokenizer.pad(_UpperCAmelCase , padding="longest" , return_tensors="pt" ) return tokenizer.pad(_UpperCAmelCase , padding="max_length" , max_length=1_28 , return_tensors="pt" ) return DataLoader(_UpperCAmelCase , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=16 ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = Accelerator(dispatch_batches=_UpperCAmelCase , split_batches=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = get_dataloader(_UpperCAmelCase , not dispatch_batches ) SCREAMING_SNAKE_CASE_: str = AutoModelForSequenceClassification.from_pretrained( "hf-internal-testing/mrpc-bert-base-cased" , return_dict=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = accelerator.prepare(_UpperCAmelCase , _UpperCAmelCase ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[Any] = [] for batch in dataloader: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = batch.values() with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = [], [] for logit, targ in logits_and_targets: logits.append(_UpperCAmelCase ) targs.append(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = torch.cat(_UpperCAmelCase ), torch.cat(_UpperCAmelCase ) return logits, targs def A_ ( _UpperCAmelCase , _UpperCAmelCase=82 , _UpperCAmelCase=False , _UpperCAmelCase=False , _UpperCAmelCase=16 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = get_basic_setup(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = generate_predictions(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) assert ( len(_UpperCAmelCase ) == num_samples ), f"Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(_UpperCAmelCase )}" def A_ ( _UpperCAmelCase = False , _UpperCAmelCase = False ): SCREAMING_SNAKE_CASE_: int = evaluate.load("glue" , "mrpc" ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = get_mrpc_setup(_UpperCAmelCase , _UpperCAmelCase ) # First do baseline SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = setup["no"] model.to(_UpperCAmelCase ) model.eval() for batch in dataloader: batch.to(_UpperCAmelCase ) with torch.inference_mode(): SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=_UpperCAmelCase , references=batch["labels"] ) SCREAMING_SNAKE_CASE_: List[str] = metric.compute() # Then do distributed SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = setup["ddp"] model.eval() for batch in dataloader: with torch.inference_mode(): SCREAMING_SNAKE_CASE_: Optional[Any] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_: Optional[Any] = batch["labels"] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=_UpperCAmelCase , references=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f"Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n" def A_ ( ): SCREAMING_SNAKE_CASE_: Tuple = Accelerator(split_batches=_UpperCAmelCase , dispatch_batches=_UpperCAmelCase ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print("**Testing gather_for_metrics**" ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f"With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`" ) test_mrpc(_UpperCAmelCase , _UpperCAmelCase ) accelerator.state._reset_state() if accelerator.is_local_main_process: print("**Test torch metrics**" ) for split_batches in [True, False]: for dispatch_batches in [True, False]: SCREAMING_SNAKE_CASE_: Any = Accelerator(split_batches=_UpperCAmelCase , dispatch_batches=_UpperCAmelCase ) if accelerator.is_local_main_process: print(f"With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99" ) test_torch_metrics(_UpperCAmelCase , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print("**Test last batch is not dropped when perfectly divisible**" ) SCREAMING_SNAKE_CASE_: Union[str, Any] = Accelerator() test_torch_metrics(_UpperCAmelCase , 5_12 ) accelerator.state._reset_state() def A_ ( _UpperCAmelCase ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
127
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Dict = StableDiffusionInpaintPipeline _UpperCAmelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS _UpperCAmelCase : List[str] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS _UpperCAmelCase : Tuple = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess _UpperCAmelCase : Optional[int] = frozenset([] ) def _SCREAMING_SNAKE_CASE ( self : int): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Optional[Any] = PNDMScheduler(skip_prk_steps=lowerCAmelCase__) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="gelu" , projection_dim=512 , ) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") SCREAMING_SNAKE_CASE_: List[str] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[int]=0): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched SCREAMING_SNAKE_CASE_: Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCAmelCase__)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = image.cpu().permute(0 , 2 , 3 , 1)[0] SCREAMING_SNAKE_CASE_: Tuple = Image.fromarray(np.uinta(lowerCAmelCase__)).convert("RGB").resize((64, 64)) SCREAMING_SNAKE_CASE_: List[str] = Image.fromarray(np.uinta(image + 4)).convert("RGB").resize((64, 64)) if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Tuple = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = { "prompt": "A painting of a squirrel eating a burger", "image": init_image, "mask_image": mask_image, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: int = self.get_dummy_components() SCREAMING_SNAKE_CASE_: int = StableDiffusionInpaintPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Tuple = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : List[str]): super().test_inference_batch_single_identical(expected_max_diff=3E-3) @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : str): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Any = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-inpaint/init_image.png") SCREAMING_SNAKE_CASE_: int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png") SCREAMING_SNAKE_CASE_: Optional[int] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint" "/yellow_cat_sitting_on_a_park_bench.npy") SCREAMING_SNAKE_CASE_: List[str] = "stabilityai/stable-diffusion-2-inpainting" SCREAMING_SNAKE_CASE_: Any = StableDiffusionInpaintPipeline.from_pretrained(lowerCAmelCase__ , safety_checker=lowerCAmelCase__) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: str = "Face of a yellow cat, high resolution, sitting on a park bench" SCREAMING_SNAKE_CASE_: Optional[int] = torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = pipe( prompt=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image).max() < 9E-3 def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-inpaint/init_image.png") SCREAMING_SNAKE_CASE_: List[Any] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png") SCREAMING_SNAKE_CASE_: Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint" "/yellow_cat_sitting_on_a_park_bench_fp16.npy") SCREAMING_SNAKE_CASE_: str = "stabilityai/stable-diffusion-2-inpainting" SCREAMING_SNAKE_CASE_: Dict = StableDiffusionInpaintPipeline.from_pretrained( lowerCAmelCase__ , torch_dtype=torch.floataa , safety_checker=lowerCAmelCase__ , ) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: List[str] = "Face of a yellow cat, high resolution, sitting on a park bench" SCREAMING_SNAKE_CASE_: Tuple = torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Dict = pipe( prompt=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: Any = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image).max() < 5E-1 def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE_: Tuple = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-inpaint/init_image.png") SCREAMING_SNAKE_CASE_: Union[str, Any] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png") SCREAMING_SNAKE_CASE_: List[str] = "stabilityai/stable-diffusion-2-inpainting" SCREAMING_SNAKE_CASE_: Tuple = PNDMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: Any = StableDiffusionInpaintPipeline.from_pretrained( lowerCAmelCase__ , safety_checker=lowerCAmelCase__ , scheduler=lowerCAmelCase__ , torch_dtype=torch.floataa , ) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing(1) pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE_: Any = "Face of a yellow cat, high resolution, sitting on a park bench" SCREAMING_SNAKE_CASE_: Any = torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = pipe( prompt=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
127
1
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class A_ : '''simple docstring''' def lowerCAmelCase_ (self ) -> Optional[Any]: torch.manual_seed(0 ) __UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) __UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) __UpperCAmelCase = UNetaDConditionModel( sample_size=32 , layers_per_block=1 , block_out_channels=[32, 64] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) __UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , thresholding=lowercase__ , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) __UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def lowerCAmelCase_ (self ) -> Union[str, Any]: torch.manual_seed(0 ) __UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) __UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) __UpperCAmelCase = UNetaDConditionModel( sample_size=32 , layers_per_block=[1, 2] , block_out_channels=[32, 64] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.414 , time_embedding_act_fn='''gelu''' , time_embedding_dim=32 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) __UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , thresholding=lowercase__ , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) __UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , ) torch.manual_seed(0 ) __UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def lowerCAmelCase_ (self ) -> Any: __UpperCAmelCase = self.get_dummy_components() __UpperCAmelCase = self.pipeline_class(**lowercase__ ) pipe.to(lowercase__ ) pipe.set_progress_bar_config(disable=lowercase__ ) __UpperCAmelCase = self.get_dummy_inputs(lowercase__ ) __UpperCAmelCase = inputs['''prompt'''] __UpperCAmelCase = inputs['''generator'''] __UpperCAmelCase = inputs['''num_inference_steps'''] __UpperCAmelCase = inputs['''output_type'''] if "image" in inputs: __UpperCAmelCase = inputs['''image'''] else: __UpperCAmelCase = None if "mask_image" in inputs: __UpperCAmelCase = inputs['''mask_image'''] else: __UpperCAmelCase = None if "original_image" in inputs: __UpperCAmelCase = inputs['''original_image'''] else: __UpperCAmelCase = None __UpperCAmelCase , __UpperCAmelCase = pipe.encode_prompt(lowercase__ ) # inputs with prompt converted to embeddings __UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: __UpperCAmelCase = image if mask_image is not None: __UpperCAmelCase = mask_image if original_image is not None: __UpperCAmelCase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(lowercase__ , lowercase__ , lowercase__ ) __UpperCAmelCase = pipe(**lowercase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowercase__ ) __UpperCAmelCase = self.pipeline_class.from_pretrained(lowercase__ ) pipe_loaded.to(lowercase__ ) pipe_loaded.set_progress_bar_config(disable=lowercase__ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(lowercase__ , lowercase__ ) is None , F'''`{optional_component}` did not stay set to None after loading.''' , ) __UpperCAmelCase = self.get_dummy_inputs(lowercase__ ) __UpperCAmelCase = inputs['''generator'''] __UpperCAmelCase = inputs['''num_inference_steps'''] __UpperCAmelCase = inputs['''output_type'''] # inputs with prompt converted to embeddings __UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: __UpperCAmelCase = image if mask_image is not None: __UpperCAmelCase = mask_image if original_image is not None: __UpperCAmelCase = original_image __UpperCAmelCase = pipe_loaded(**lowercase__ )[0] __UpperCAmelCase = np.abs(to_np(lowercase__ ) - to_np(lowercase__ ) ).max() self.assertLess(lowercase__ , 1E-4 ) def lowerCAmelCase_ (self ) -> Tuple: __UpperCAmelCase = self.get_dummy_components() __UpperCAmelCase = self.pipeline_class(**lowercase__ ) pipe.to(lowercase__ ) pipe.set_progress_bar_config(disable=lowercase__ ) __UpperCAmelCase = self.get_dummy_inputs(lowercase__ ) __UpperCAmelCase = pipe(**lowercase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowercase__ ) __UpperCAmelCase = self.pipeline_class.from_pretrained(lowercase__ ) pipe_loaded.to(lowercase__ ) pipe_loaded.set_progress_bar_config(disable=lowercase__ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests __UpperCAmelCase = self.get_dummy_inputs(lowercase__ ) __UpperCAmelCase = pipe_loaded(**lowercase__ )[0] __UpperCAmelCase = np.abs(to_np(lowercase__ ) - to_np(lowercase__ ) ).max() self.assertLess(lowercase__ , 1E-4 )
333
from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES A_ : str = logging.get_logger(__name__) A_ : str = OrderedDict( [ # Base model mapping ('albert', 'FlaxAlbertModel'), ('bart', 'FlaxBartModel'), ('beit', 'FlaxBeitModel'), ('bert', 'FlaxBertModel'), ('big_bird', 'FlaxBigBirdModel'), ('blenderbot', 'FlaxBlenderbotModel'), ('blenderbot-small', 'FlaxBlenderbotSmallModel'), ('clip', 'FlaxCLIPModel'), ('distilbert', 'FlaxDistilBertModel'), ('electra', 'FlaxElectraModel'), ('gpt-sw3', 'FlaxGPT2Model'), ('gpt2', 'FlaxGPT2Model'), ('gpt_neo', 'FlaxGPTNeoModel'), ('gptj', 'FlaxGPTJModel'), ('longt5', 'FlaxLongT5Model'), ('marian', 'FlaxMarianModel'), ('mbart', 'FlaxMBartModel'), ('mt5', 'FlaxMT5Model'), ('opt', 'FlaxOPTModel'), ('pegasus', 'FlaxPegasusModel'), ('regnet', 'FlaxRegNetModel'), ('resnet', 'FlaxResNetModel'), ('roberta', 'FlaxRobertaModel'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormModel'), ('roformer', 'FlaxRoFormerModel'), ('t5', 'FlaxT5Model'), ('vision-text-dual-encoder', 'FlaxVisionTextDualEncoderModel'), ('vit', 'FlaxViTModel'), ('wav2vec2', 'FlaxWav2Vec2Model'), ('whisper', 'FlaxWhisperModel'), ('xglm', 'FlaxXGLMModel'), ('xlm-roberta', 'FlaxXLMRobertaModel'), ] ) A_ : Optional[int] = OrderedDict( [ # Model for pre-training mapping ('albert', 'FlaxAlbertForPreTraining'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForPreTraining'), ('big_bird', 'FlaxBigBirdForPreTraining'), ('electra', 'FlaxElectraForPreTraining'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('t5', 'FlaxT5ForConditionalGeneration'), ('wav2vec2', 'FlaxWav2Vec2ForPreTraining'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) A_ : Union[str, Any] = OrderedDict( [ # Model for Masked LM mapping ('albert', 'FlaxAlbertForMaskedLM'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForMaskedLM'), ('big_bird', 'FlaxBigBirdForMaskedLM'), ('distilbert', 'FlaxDistilBertForMaskedLM'), ('electra', 'FlaxElectraForMaskedLM'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) A_ : Dict = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ('bart', 'FlaxBartForConditionalGeneration'), ('blenderbot', 'FlaxBlenderbotForConditionalGeneration'), ('blenderbot-small', 'FlaxBlenderbotSmallForConditionalGeneration'), ('encoder-decoder', 'FlaxEncoderDecoderModel'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('marian', 'FlaxMarianMTModel'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('pegasus', 'FlaxPegasusForConditionalGeneration'), ('t5', 'FlaxT5ForConditionalGeneration'), ] ) A_ : Optional[int] = OrderedDict( [ # Model for Image-classsification ('beit', 'FlaxBeitForImageClassification'), ('regnet', 'FlaxRegNetForImageClassification'), ('resnet', 'FlaxResNetForImageClassification'), ('vit', 'FlaxViTForImageClassification'), ] ) A_ : Dict = OrderedDict( [ ('vision-encoder-decoder', 'FlaxVisionEncoderDecoderModel'), ] ) A_ : List[str] = OrderedDict( [ # Model for Causal LM mapping ('bart', 'FlaxBartForCausalLM'), ('bert', 'FlaxBertForCausalLM'), ('big_bird', 'FlaxBigBirdForCausalLM'), ('electra', 'FlaxElectraForCausalLM'), ('gpt-sw3', 'FlaxGPT2LMHeadModel'), ('gpt2', 'FlaxGPT2LMHeadModel'), ('gpt_neo', 'FlaxGPTNeoForCausalLM'), ('gptj', 'FlaxGPTJForCausalLM'), ('opt', 'FlaxOPTForCausalLM'), ('roberta', 'FlaxRobertaForCausalLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForCausalLM'), ('xglm', 'FlaxXGLMForCausalLM'), ('xlm-roberta', 'FlaxXLMRobertaForCausalLM'), ] ) A_ : Tuple = OrderedDict( [ # Model for Sequence Classification mapping ('albert', 'FlaxAlbertForSequenceClassification'), ('bart', 'FlaxBartForSequenceClassification'), ('bert', 'FlaxBertForSequenceClassification'), ('big_bird', 'FlaxBigBirdForSequenceClassification'), ('distilbert', 'FlaxDistilBertForSequenceClassification'), ('electra', 'FlaxElectraForSequenceClassification'), ('mbart', 'FlaxMBartForSequenceClassification'), ('roberta', 'FlaxRobertaForSequenceClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForSequenceClassification'), ('roformer', 'FlaxRoFormerForSequenceClassification'), ('xlm-roberta', 'FlaxXLMRobertaForSequenceClassification'), ] ) A_ : Optional[int] = OrderedDict( [ # Model for Question Answering mapping ('albert', 'FlaxAlbertForQuestionAnswering'), ('bart', 'FlaxBartForQuestionAnswering'), ('bert', 'FlaxBertForQuestionAnswering'), ('big_bird', 'FlaxBigBirdForQuestionAnswering'), ('distilbert', 'FlaxDistilBertForQuestionAnswering'), ('electra', 'FlaxElectraForQuestionAnswering'), ('mbart', 'FlaxMBartForQuestionAnswering'), ('roberta', 'FlaxRobertaForQuestionAnswering'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForQuestionAnswering'), ('roformer', 'FlaxRoFormerForQuestionAnswering'), ('xlm-roberta', 'FlaxXLMRobertaForQuestionAnswering'), ] ) A_ : int = OrderedDict( [ # Model for Token Classification mapping ('albert', 'FlaxAlbertForTokenClassification'), ('bert', 'FlaxBertForTokenClassification'), ('big_bird', 'FlaxBigBirdForTokenClassification'), ('distilbert', 'FlaxDistilBertForTokenClassification'), ('electra', 'FlaxElectraForTokenClassification'), ('roberta', 'FlaxRobertaForTokenClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForTokenClassification'), ('roformer', 'FlaxRoFormerForTokenClassification'), ('xlm-roberta', 'FlaxXLMRobertaForTokenClassification'), ] ) A_ : Tuple = OrderedDict( [ # Model for Multiple Choice mapping ('albert', 'FlaxAlbertForMultipleChoice'), ('bert', 'FlaxBertForMultipleChoice'), ('big_bird', 'FlaxBigBirdForMultipleChoice'), ('distilbert', 'FlaxDistilBertForMultipleChoice'), ('electra', 'FlaxElectraForMultipleChoice'), ('roberta', 'FlaxRobertaForMultipleChoice'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMultipleChoice'), ('roformer', 'FlaxRoFormerForMultipleChoice'), ('xlm-roberta', 'FlaxXLMRobertaForMultipleChoice'), ] ) A_ : Tuple = OrderedDict( [ ('bert', 'FlaxBertForNextSentencePrediction'), ] ) A_ : int = OrderedDict( [ ('speech-encoder-decoder', 'FlaxSpeechEncoderDecoderModel'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ] ) A_ : Tuple = OrderedDict( [ ('whisper', 'FlaxWhisperForAudioClassification'), ] ) A_ : Optional[int] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) A_ : int = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) A_ : Dict = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) A_ : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) A_ : Union[str, Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) A_ : Dict = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) A_ : Any = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) A_ : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) A_ : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) A_ : Optional[int] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) A_ : int = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) A_ : Optional[int] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) A_ : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) A_ : List[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_MAPPING A_ : Tuple = auto_class_update(FlaxAutoModel) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_PRETRAINING_MAPPING A_ : str = auto_class_update(FlaxAutoModelForPreTraining, head_doc='pretraining') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING A_ : Optional[Any] = auto_class_update(FlaxAutoModelForCausalLM, head_doc='causal language modeling') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_MASKED_LM_MAPPING A_ : List[str] = auto_class_update(FlaxAutoModelForMaskedLM, head_doc='masked language modeling') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING A_ : Union[str, Any] = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc='sequence-to-sequence language modeling', checkpoint_for_example='t5-base' ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING A_ : Tuple = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc='sequence classification' ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING A_ : Any = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc='question answering') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING A_ : Dict = auto_class_update( FlaxAutoModelForTokenClassification, head_doc='token classification' ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING A_ : Any = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc='multiple choice') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING A_ : Tuple = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc='next sentence prediction' ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING A_ : int = auto_class_update( FlaxAutoModelForImageClassification, head_doc='image classification' ) class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING A_ : Tuple = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc='vision-to-text modeling') class A_ ( _BaseAutoModelClass ): '''simple docstring''' a__ = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING A_ : Optional[int] = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc='sequence-to-sequence speech-to-text modeling' )
333
1
def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> str: '''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''' ) snake_case_ = '''''' 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()
366
'''simple docstring''' import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class a ( _lowerCamelCase , unittest.TestCase ): snake_case_ = FlaxAutoencoderKL @property def A_ ( self : List[Any] ): snake_case_ = 4 snake_case_ = 3 snake_case_ = (32, 32) snake_case_ = jax.random.PRNGKey(0 ) snake_case_ = jax.random.uniform(lowercase_ , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def A_ ( self : Tuple ): snake_case_ = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } snake_case_ = self.dummy_input return init_dict, inputs_dict
72
0
from math import factorial, radians def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ = 1_8 , lowerCamelCase__ = 1_0 ) -> float: __lowerCamelCase : Optional[Any] = angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0) # Converting from degrees to radians __lowerCamelCase : Any = radians(lowerCamelCase__ ) __lowerCamelCase : List[Any] = angle_in_radians __lowerCamelCase : Dict = 3 __lowerCamelCase : List[Any] = -1 for _ in range(lowerCamelCase__ ): result += (b * (angle_in_radians**a)) / factorial(lowerCamelCase__ ) __lowerCamelCase : List[Any] = -b # One positive term and the next will be negative and so on... a += 2 # Increased by 2 for every term. return round(lowerCamelCase__ , lowerCamelCase__ ) if __name__ == "__main__": __import__("""doctest""").testmod()
73
"""simple docstring""" import argparse import collections import json import os import re import string import sys import numpy as np UpperCAmelCase : Union[str, Any] = re.compile(r"\b(a|an|the)\b", re.UNICODE) UpperCAmelCase : Optional[Any] = None def _SCREAMING_SNAKE_CASE () -> List[Any]: '''simple docstring''' lowercase_ = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__lowerCAmelCase , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__lowerCAmelCase , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> str: '''simple docstring''' lowercase_ = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowercase_ = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> Optional[int]: '''simple docstring''' def remove_articles(__lowerCAmelCase ): return ARTICLES_REGEX.sub(""" """ , __lowerCAmelCase ) def white_space_fix(__lowerCAmelCase ): return " ".join(text.split() ) def remove_punc(__lowerCAmelCase ): lowercase_ = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__lowerCAmelCase ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__lowerCAmelCase ) ) ) ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> List[Any]: '''simple docstring''' if not s: return [] return normalize_answer(__lowerCAmelCase ).split() def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__lowerCAmelCase ) == normalize_answer(__lowerCAmelCase ) ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> List[str]: '''simple docstring''' lowercase_ = get_tokens(__lowerCAmelCase ) lowercase_ = get_tokens(__lowerCAmelCase ) lowercase_ = collections.Counter(__lowerCAmelCase ) & collections.Counter(__lowerCAmelCase ) lowercase_ = sum(common.values() ) if len(__lowerCAmelCase ) == 0 or len(__lowerCAmelCase ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 lowercase_ = 1.0 * num_same / len(__lowerCAmelCase ) lowercase_ = 1.0 * num_same / len(__lowerCAmelCase ) lowercase_ = (2 * precision * recall) / (precision + recall) return fa def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> Any: '''simple docstring''' lowercase_ = {} lowercase_ = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowercase_ = qa["""id"""] lowercase_ = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__lowerCAmelCase )] if not gold_answers: # For unanswerable questions, only correct answer is empty string lowercase_ = [""""""] if qid not in preds: print(F'''Missing prediction for {qid}''' ) continue lowercase_ = preds[qid] # Take max over all gold answers lowercase_ = max(compute_exact(__lowerCAmelCase , __lowerCAmelCase ) for a in gold_answers ) lowercase_ = max(compute_fa(__lowerCAmelCase , __lowerCAmelCase ) for a in gold_answers ) return exact_scores, fa_scores def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Any: '''simple docstring''' lowercase_ = {} for qid, s in scores.items(): lowercase_ = na_probs[qid] > na_prob_thresh if pred_na: lowercase_ = float(not qid_to_has_ans[qid] ) else: lowercase_ = s return new_scores def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None ) -> List[str]: '''simple docstring''' if not qid_list: lowercase_ = len(__lowerCAmelCase ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: lowercase_ = len(__lowerCAmelCase ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Any: '''simple docstring''' for k in new_eval: lowercase_ = new_eval[k] def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> List[str]: '''simple docstring''' plt.step(__lowerCAmelCase , __lowerCAmelCase , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__lowerCAmelCase , __lowerCAmelCase , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__lowerCAmelCase ) plt.savefig(__lowerCAmelCase ) plt.clf() def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None ) -> List[Any]: '''simple docstring''' lowercase_ = sorted(__lowerCAmelCase , key=lambda __lowerCAmelCase : na_probs[k] ) lowercase_ = 0.0 lowercase_ = 1.0 lowercase_ = 0.0 lowercase_ = [1.0] lowercase_ = [0.0] lowercase_ = 0.0 for i, qid in enumerate(__lowerCAmelCase ): if qid_to_has_ans[qid]: true_pos += scores[qid] lowercase_ = true_pos / float(i + 1 ) lowercase_ = true_pos / float(__lowerCAmelCase ) if i == len(__lowerCAmelCase ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__lowerCAmelCase ) recalls.append(__lowerCAmelCase ) if out_image: plot_pr_curve(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return {"ap": 100.0 * avg_prec} def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Dict: '''simple docstring''' if out_image_dir and not os.path.exists(__lowerCAmelCase ): os.makedirs(__lowerCAmelCase ) lowercase_ = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return lowercase_ = make_precision_recall_eval( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , out_image=os.path.join(__lowerCAmelCase , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) lowercase_ = make_precision_recall_eval( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , out_image=os.path.join(__lowerCAmelCase , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) lowercase_ = {k: float(__lowerCAmelCase ) for k, v in qid_to_has_ans.items()} lowercase_ = make_precision_recall_eval( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , out_image=os.path.join(__lowerCAmelCase , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__lowerCAmelCase , __lowerCAmelCase , """pr_exact""" ) merge_eval(__lowerCAmelCase , __lowerCAmelCase , """pr_f1""" ) merge_eval(__lowerCAmelCase , __lowerCAmelCase , """pr_oracle""" ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> List[str]: '''simple docstring''' if not qid_list: return lowercase_ = [na_probs[k] for k in qid_list] lowercase_ = np.ones_like(__lowerCAmelCase ) / float(len(__lowerCAmelCase ) ) plt.hist(__lowerCAmelCase , weights=__lowerCAmelCase , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(F'''Histogram of no-answer probability: {name}''' ) plt.savefig(os.path.join(__lowerCAmelCase , F'''na_prob_hist_{name}.png''' ) ) plt.clf() def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Union[str, Any]: '''simple docstring''' lowercase_ = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) lowercase_ = num_no_ans lowercase_ = cur_score lowercase_ = 0.0 lowercase_ = sorted(__lowerCAmelCase , key=lambda __lowerCAmelCase : na_probs[k] ) for i, qid in enumerate(__lowerCAmelCase ): if qid not in scores: continue if qid_to_has_ans[qid]: lowercase_ = scores[qid] else: if preds[qid]: lowercase_ = -1 else: lowercase_ = 0 cur_score += diff if cur_score > best_score: lowercase_ = cur_score lowercase_ = na_probs[qid] return 100.0 * best_score / len(__lowerCAmelCase ), best_thresh def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> List[Any]: '''simple docstring''' lowercase_ , lowercase_ = find_best_thresh(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) lowercase_ , lowercase_ = find_best_thresh(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) lowercase_ = best_exact lowercase_ = exact_thresh lowercase_ = best_fa lowercase_ = fa_thresh def _SCREAMING_SNAKE_CASE () -> int: '''simple docstring''' with open(OPTS.data_file ) as f: lowercase_ = json.load(__lowerCAmelCase ) lowercase_ = dataset_json["""data"""] with open(OPTS.pred_file ) as f: lowercase_ = json.load(__lowerCAmelCase ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: lowercase_ = json.load(__lowerCAmelCase ) else: lowercase_ = {k: 0.0 for k in preds} lowercase_ = make_qid_to_has_ans(__lowerCAmelCase ) # maps qid to True/False lowercase_ = [k for k, v in qid_to_has_ans.items() if v] lowercase_ = [k for k, v in qid_to_has_ans.items() if not v] lowercase_ , lowercase_ = get_raw_scores(__lowerCAmelCase , __lowerCAmelCase ) lowercase_ = apply_no_ans_threshold(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , OPTS.na_prob_thresh ) lowercase_ = apply_no_ans_threshold(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , OPTS.na_prob_thresh ) lowercase_ = make_eval_dict(__lowerCAmelCase , __lowerCAmelCase ) if has_ans_qids: lowercase_ = make_eval_dict(__lowerCAmelCase , __lowerCAmelCase , qid_list=__lowerCAmelCase ) merge_eval(__lowerCAmelCase , __lowerCAmelCase , """HasAns""" ) if no_ans_qids: lowercase_ = make_eval_dict(__lowerCAmelCase , __lowerCAmelCase , qid_list=__lowerCAmelCase ) merge_eval(__lowerCAmelCase , __lowerCAmelCase , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , OPTS.out_image_dir ) histogram_na_prob(__lowerCAmelCase , __lowerCAmelCase , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__lowerCAmelCase , __lowerCAmelCase , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__lowerCAmelCase , __lowerCAmelCase ) else: print(json.dumps(__lowerCAmelCase , indent=2 ) ) if __name__ == "__main__": UpperCAmelCase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt main()
136
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case :int = logging.get_logger(__name__) __snake_case :Union[str, Any] = { "microsoft/git-base": "https://huggingface.co/microsoft/git-base/resolve/main/config.json", } class _A ( UpperCAmelCase__ ): UpperCamelCase__ : Union[str, Any] = 'git_vision_model' def __init__( self : int , __SCREAMING_SNAKE_CASE : List[Any]=768 , __SCREAMING_SNAKE_CASE : str=3_072 , __SCREAMING_SNAKE_CASE : Optional[Any]=12 , __SCREAMING_SNAKE_CASE : List[Any]=12 , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : Dict=224 , __SCREAMING_SNAKE_CASE : Union[str, Any]=16 , __SCREAMING_SNAKE_CASE : Union[str, Any]="quick_gelu" , __SCREAMING_SNAKE_CASE : Dict=1E-5 , __SCREAMING_SNAKE_CASE : Any=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=0.02 , **__SCREAMING_SNAKE_CASE : List[str] , ): '''simple docstring''' super().__init__(**_SCREAMING_SNAKE_CASE) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = num_channels __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act @classmethod def _lowerCamelCase ( cls : Dict , __SCREAMING_SNAKE_CASE : List[Any] , **__SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE) __a = cls.get_config_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) # get the vision config dict if we are loading from GITConfig if config_dict.get('''model_type''') == "git": __a = config_dict["""vision_config"""] if "model_type" in config_dict and hasattr(cls , '''model_type''') and config_dict["model_type"] != cls.model_type: logger.warning( F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.') return cls.from_dict(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) class _A ( UpperCAmelCase__ ): UpperCamelCase__ : Dict = 'git' def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : Tuple=30_522 , __SCREAMING_SNAKE_CASE : Union[str, Any]=768 , __SCREAMING_SNAKE_CASE : Union[str, Any]=6 , __SCREAMING_SNAKE_CASE : Tuple=12 , __SCREAMING_SNAKE_CASE : str=3_072 , __SCREAMING_SNAKE_CASE : Optional[int]="gelu" , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=1_024 , __SCREAMING_SNAKE_CASE : List[Any]=0.02 , __SCREAMING_SNAKE_CASE : Tuple=1E-12 , __SCREAMING_SNAKE_CASE : List[str]=0 , __SCREAMING_SNAKE_CASE : int="absolute" , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Any=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=101 , __SCREAMING_SNAKE_CASE : Any=102 , __SCREAMING_SNAKE_CASE : str=None , **__SCREAMING_SNAKE_CASE : List[str] , ): '''simple docstring''' super().__init__(bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , pad_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the GitVisionConfig with default values.''') __a = GitVisionConfig(**_SCREAMING_SNAKE_CASE) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = use_cache __a = tie_word_embeddings __a = num_image_with_embedding __a = bos_token_id __a = eos_token_id def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = copy.deepcopy(self.__dict__) __a = self.vision_config.to_dict() __a = self.__class__.model_type return output
360
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING __snake_case :Dict = logging.get_logger(__name__) @add_end_docstrings(__UpperCAmelCase ) class _A ( __UpperCAmelCase ): def __init__( self : int , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : str): '''simple docstring''' super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) requires_backends(self , '''vision''') self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING) def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : Tuple=None): '''simple docstring''' __a = {} __a = {} if prompt is not None: __a = prompt if generate_kwargs is not None: __a = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: __a = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,''' ''' please use only one''') __a = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Any , __SCREAMING_SNAKE_CASE : Union[str, List[str], "Image.Image", List["Image.Image"]] , **__SCREAMING_SNAKE_CASE : Any): '''simple docstring''' return super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[Any]=None): '''simple docstring''' __a = load_image(__SCREAMING_SNAKE_CASE) if prompt is not None: if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): raise ValueError( F'Received an invalid text input, got - {type(__SCREAMING_SNAKE_CASE)} - but expected a single string. ' '''Note also that one single text can be provided for conditional image to text generation.''') __a = self.model.config.model_type if model_type == "git": __a = self.image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors=self.framework) __a = self.tokenizer(text=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE).input_ids __a = [self.tokenizer.cls_token_id] + input_ids __a = torch.tensor(__SCREAMING_SNAKE_CASE).unsqueeze(0) model_inputs.update({'''input_ids''': input_ids}) elif model_type == "pix2struct": __a = self.image_processor(images=__SCREAMING_SNAKE_CASE , header_text=__SCREAMING_SNAKE_CASE , return_tensors=self.framework) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation __a = self.image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors=self.framework) __a = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework) model_inputs.update(__SCREAMING_SNAKE_CASE) else: raise ValueError(F'Model type {model_type} does not support conditional text generation') else: __a = self.image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors=self.framework) if self.model.config.model_type == "git" and prompt is None: __a = None return model_inputs def _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=None): '''simple docstring''' if ( "input_ids" in model_inputs and isinstance(model_inputs['''input_ids'''] , __SCREAMING_SNAKE_CASE) and all(x is None for x in model_inputs['''input_ids''']) ): __a = None if generate_kwargs is None: __a = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. __a = model_inputs.pop(self.model.main_input_name) __a = self.model.generate(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) return model_outputs def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = [] for output_ids in model_outputs: __a = { '''generated_text''': self.tokenizer.decode( __SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE , ) } records.append(__SCREAMING_SNAKE_CASE) return records
131
0
def lowercase_ (A : int = 1_0_0_0_0_0_0 ): snake_case__ : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 , limit + 1 ): if phi[i] == i - 1: for j in range(2 * i , limit + 1 , A ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
277
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() a_ :str = logging.get_logger(__name__) def lowercase_ (A : str ): snake_case__ : Tuple = SwinConfig.from_pretrained( 'microsoft/swin-tiny-patch4-window7-224' , out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) snake_case__ : List[Any] = MaskFormerConfig(backbone_config=A ) snake_case__ : Union[str, Any] = 'huggingface/label-files' if "ade20k-full" in model_name: # this should be ok snake_case__ : Dict = 8_4_7 snake_case__ : List[str] = 'maskformer-ade20k-full-id2label.json' elif "ade" in model_name: # this should be ok snake_case__ : Union[str, Any] = 1_5_0 snake_case__ : Any = 'ade20k-id2label.json' elif "coco-stuff" in model_name: # this should be ok snake_case__ : List[str] = 1_7_1 snake_case__ : Union[str, Any] = 'maskformer-coco-stuff-id2label.json' elif "coco" in model_name: # TODO snake_case__ : Dict = 1_3_3 snake_case__ : str = 'coco-panoptic-id2label.json' elif "cityscapes" in model_name: # this should be ok snake_case__ : List[str] = 1_9 snake_case__ : Union[str, Any] = 'cityscapes-id2label.json' elif "vistas" in model_name: # this should be ok snake_case__ : Tuple = 6_5 snake_case__ : List[str] = 'mapillary-vistas-id2label.json' snake_case__ : Dict = json.load(open(hf_hub_download(A , A , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[str] = {int(A ): v for k, v in idalabel.items()} return config def lowercase_ (A : Any ): snake_case__ : Optional[int] = [] # stem # fmt: off rename_keys.append(('backbone.patch_embed.proj.weight', 'model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('backbone.patch_embed.proj.bias', 'model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias') ) rename_keys.append(('backbone.patch_embed.norm.weight', 'model.pixel_level_module.encoder.model.embeddings.norm.weight') ) rename_keys.append(('backbone.patch_embed.norm.bias', 'model.pixel_level_module.encoder.model.embeddings.norm.bias') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_index''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((F'''backbone.layers.{i}.downsample.reduction.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append((F'''backbone.norm{i}.weight''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.weight''') ) rename_keys.append((F'''backbone.norm{i}.bias''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.bias''') ) # FPN rename_keys.append(('sem_seg_head.layer_4.weight', 'model.pixel_level_module.decoder.fpn.stem.0.weight') ) rename_keys.append(('sem_seg_head.layer_4.norm.weight', 'model.pixel_level_module.decoder.fpn.stem.1.weight') ) rename_keys.append(('sem_seg_head.layer_4.norm.bias', 'model.pixel_level_module.decoder.fpn.stem.1.bias') ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F'''sem_seg_head.adapter_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias''') ) rename_keys.append(('sem_seg_head.mask_features.weight', 'model.pixel_level_module.decoder.mask_projection.weight') ) rename_keys.append(('sem_seg_head.mask_features.bias', 'model.pixel_level_module.decoder.mask_projection.bias') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias''') ) # cross-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias''') ) # MLP 1 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc1.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc1.bias''') ) # MLP 2 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc2.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc2.bias''') ) # layernorm 1 (self-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias''') ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias''') ) # layernorm 3 (final layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias''') ) rename_keys.append(('sem_seg_head.predictor.transformer.decoder.norm.weight', 'model.transformer_module.decoder.layernorm.weight') ) rename_keys.append(('sem_seg_head.predictor.transformer.decoder.norm.bias', 'model.transformer_module.decoder.layernorm.bias') ) # heads on top rename_keys.append(('sem_seg_head.predictor.query_embed.weight', 'model.transformer_module.queries_embedder.weight') ) rename_keys.append(('sem_seg_head.predictor.input_proj.weight', 'model.transformer_module.input_projection.weight') ) rename_keys.append(('sem_seg_head.predictor.input_proj.bias', 'model.transformer_module.input_projection.bias') ) rename_keys.append(('sem_seg_head.predictor.class_embed.weight', 'class_predictor.weight') ) rename_keys.append(('sem_seg_head.predictor.class_embed.bias', 'class_predictor.bias') ) for i in range(3 ): rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.weight''', F'''mask_embedder.{i}.0.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.bias''', F'''mask_embedder.{i}.0.bias''') ) # fmt: on return rename_keys def lowercase_ (A : Tuple , A : Tuple , A : Optional[Any] ): snake_case__ : Optional[int] = dct.pop(A ) snake_case__ : Union[str, Any] = val def lowercase_ (A : Optional[Any] , A : Tuple ): snake_case__ : Optional[int] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): snake_case__ : Optional[int] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.weight''' ) snake_case__ : Tuple = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict snake_case__ : str = in_proj_weight[:dim, :] snake_case__ : int = in_proj_bias[: dim] snake_case__ : List[Any] = in_proj_weight[ dim : dim * 2, : ] snake_case__ : List[str] = in_proj_bias[ dim : dim * 2 ] snake_case__ : List[Any] = in_proj_weight[ -dim :, : ] snake_case__ : Dict = in_proj_bias[-dim :] # fmt: on def lowercase_ (A : List[str] , A : List[Any] ): # fmt: off snake_case__ : str = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) snake_case__ : List[Any] = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight''' ) snake_case__ : int = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Any = in_proj_weight[: hidden_size, :] snake_case__ : Tuple = in_proj_bias[:config.hidden_size] snake_case__ : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] snake_case__ : Dict = in_proj_bias[hidden_size : hidden_size * 2] snake_case__ : Any = in_proj_weight[-hidden_size :, :] snake_case__ : int = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) snake_case__ : List[Any] = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight''' ) snake_case__ : List[str] = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[int] = in_proj_weight[: hidden_size, :] snake_case__ : Optional[Any] = in_proj_bias[:config.hidden_size] snake_case__ : int = in_proj_weight[hidden_size : hidden_size * 2, :] snake_case__ : List[str] = in_proj_bias[hidden_size : hidden_size * 2] snake_case__ : List[str] = in_proj_weight[-hidden_size :, :] snake_case__ : str = in_proj_bias[-hidden_size :] # fmt: on def lowercase_ (): snake_case__ : Any = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : int = Image.open(requests.get(A , stream=A ).raw ) return im @torch.no_grad() def lowercase_ (A : str , A : str , A : str , A : bool = False ): snake_case__ : Optional[int] = get_maskformer_config(A ) # load original state_dict with open(A , 'rb' ) as f: snake_case__ : List[Any] = pickle.load(A ) snake_case__ : Optional[int] = data['model'] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys snake_case__ : List[str] = create_rename_keys(A ) for src, dest in rename_keys: rename_key(A , A , A ) read_in_swin_q_k_v(A , config.backbone_config ) read_in_decoder_q_k_v(A , A ) # update to torch tensors for key, value in state_dict.items(): snake_case__ : int = torch.from_numpy(A ) # load 🤗 model snake_case__ : str = MaskFormerForInstanceSegmentation(A ) model.eval() for name, param in model.named_parameters(): print(A , param.shape ) snake_case__ , snake_case__ : Union[str, Any] = model.load_state_dict(A , strict=A ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(A ) == 0, F'''Unexpected keys: {unexpected_keys}''' # verify results snake_case__ : Optional[Any] = prepare_img() if "vistas" in model_name: snake_case__ : int = 6_5 elif "cityscapes" in model_name: snake_case__ : Dict = 6_5_5_3_5 else: snake_case__ : Tuple = 2_5_5 snake_case__ : Optional[int] = True if 'ade' in model_name else False snake_case__ : Dict = MaskFormerImageProcessor(ignore_index=A , reduce_labels=A ) snake_case__ : Any = image_processor(A , return_tensors='pt' ) snake_case__ : Any = model(**A ) print('Logits:' , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": snake_case__ : Tuple = torch.tensor( [[3.6353, -4.4770, -2.6065], [0.5081, -4.2394, -3.5343], [2.1909, -5.0353, -1.9323]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , A , atol=1e-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F'''Saving model and image processor to {pytorch_dump_folder_path}''' ) Path(A ).mkdir(exist_ok=A ) model.save_pretrained(A ) image_processor.save_pretrained(A ) if push_to_hub: print('Pushing model and image processor to the hub...' ) model.push_to_hub(F'''nielsr/{model_name}''' ) image_processor.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": a_ :Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="maskformer-swin-tiny-ade", type=str, help=("Name of the MaskFormer model you'd like to convert",), ) parser.add_argument( "--checkpoint_path", default="/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl", type=str, help="Path to the original state dict (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) a_ :Dict = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
277
1
from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "google/efficientnet-b7": "https://huggingface.co/google/efficientnet-b7/resolve/main/config.json", } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = '''efficientnet''' def __init__( self : Dict , __UpperCAmelCase : int = 3 , __UpperCAmelCase : int = 600 , __UpperCAmelCase : float = 2.0 , __UpperCAmelCase : float = 3.1 , __UpperCAmelCase : int = 8 , __UpperCAmelCase : List[int] = [3, 3, 5, 3, 5, 5, 3] , __UpperCAmelCase : List[int] = [32, 16, 24, 40, 80, 112, 192] , __UpperCAmelCase : List[int] = [16, 24, 40, 80, 112, 192, 320] , __UpperCAmelCase : List[int] = [] , __UpperCAmelCase : List[int] = [1, 2, 2, 2, 1, 2, 1] , __UpperCAmelCase : List[int] = [1, 2, 2, 3, 3, 4, 1] , __UpperCAmelCase : List[int] = [1, 6, 6, 6, 6, 6, 6] , __UpperCAmelCase : float = 0.25 , __UpperCAmelCase : str = "swish" , __UpperCAmelCase : int = 2_560 , __UpperCAmelCase : str = "mean" , __UpperCAmelCase : float = 0.02 , __UpperCAmelCase : float = 0.001 , __UpperCAmelCase : float = 0.99 , __UpperCAmelCase : float = 0.5 , __UpperCAmelCase : float = 0.2 , **__UpperCAmelCase : List[str] , ) ->Any: """simple docstring""" super().__init__(**__UpperCAmelCase ) a = num_channels a = image_size a = width_coefficient a = depth_coefficient a = depth_divisor a = kernel_sizes a = in_channels a = out_channels a = depthwise_padding a = strides a = num_block_repeats a = expand_ratios a = squeeze_expansion_ratio a = hidden_act a = hidden_dim a = pooling_type a = initializer_range a = batch_norm_eps a = batch_norm_momentum a = dropout_rate a = drop_connect_rate a = sum(__UpperCAmelCase ) * 4 class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = version.parse('''1.11''' ) @property def __lowerCAmelCase ( self : List[Any] ) ->Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self : Any ) ->float: """simple docstring""" return 1e-5
26
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(a ) return explored UpperCAmelCase__ = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], "F": ["C", "E", "G"], "G": ["F"], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, "A"))
26
1
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def UpperCamelCase ( ): '''simple docstring''' lowercase = ArgumentParser('''Transformers CLI tool''' , usage='''transformers-cli <command> [<args>]''' ) lowercase = parser.add_subparsers(help='''transformers-cli command helpers''' ) # Register commands ConvertCommand.register_subcommand(lowerCAmelCase__ ) DownloadCommand.register_subcommand(lowerCAmelCase__ ) EnvironmentCommand.register_subcommand(lowerCAmelCase__ ) RunCommand.register_subcommand(lowerCAmelCase__ ) ServeCommand.register_subcommand(lowerCAmelCase__ ) UserCommands.register_subcommand(lowerCAmelCase__ ) AddNewModelCommand.register_subcommand(lowerCAmelCase__ ) AddNewModelLikeCommand.register_subcommand(lowerCAmelCase__ ) LfsCommands.register_subcommand(lowerCAmelCase__ ) PTtoTFCommand.register_subcommand(lowerCAmelCase__ ) # Let's go lowercase = parser.parse_args() if not hasattr(lowerCAmelCase__ , '''func''' ): parser.print_help() exit(1 ) # Run lowercase = args.func(lowerCAmelCase__ ) service.run() if __name__ == "__main__": main()
101
import unittest from transformers import MobileBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertModel, ) class UpperCamelCase__ : def __init__(self : List[Any] , snake_case_ : int , snake_case_ : List[str]=1_3 , snake_case_ : Tuple=7 , snake_case_ : List[Any]=True , snake_case_ : List[Any]=True , snake_case_ : Dict=True , snake_case_ : Optional[int]=True , snake_case_ : str=9_9 , snake_case_ : Dict=6_4 , snake_case_ : Any=3_2 , snake_case_ : str=5 , snake_case_ : int=4 , snake_case_ : List[Any]=3_7 , snake_case_ : Any="gelu" , snake_case_ : Dict=0.1 , snake_case_ : List[str]=0.1 , snake_case_ : str=5_1_2 , snake_case_ : Any=1_6 , snake_case_ : str=2 , snake_case_ : int=0.02 , snake_case_ : Union[str, Any]=3 , snake_case_ : Optional[Any]=4 , snake_case_ : List[Any]=None , ): __a : Any = parent __a : Optional[int] = batch_size __a : Any = seq_length __a : int = is_training __a : Optional[int] = use_input_mask __a : List[Any] = use_token_type_ids __a : Dict = use_labels __a : Tuple = vocab_size __a : str = hidden_size __a : List[Any] = embedding_size __a : List[Any] = num_hidden_layers __a : str = num_attention_heads __a : str = intermediate_size __a : Union[str, Any] = hidden_act __a : Optional[Any] = hidden_dropout_prob __a : Tuple = attention_probs_dropout_prob __a : Union[str, Any] = max_position_embeddings __a : Any = type_vocab_size __a : int = type_sequence_label_size __a : int = initializer_range __a : int = num_labels __a : Union[str, Any] = num_choices __a : Dict = scope def lowerCAmelCase (self : str ): __a : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a : List[Any] = None if self.use_input_mask: __a : Dict = random_attention_mask([self.batch_size, self.seq_length] ) __a : Optional[Any] = None if self.use_token_type_ids: __a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a : Dict = None __a : List[str] = None __a : Optional[Any] = None if self.use_labels: __a : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a : Union[str, Any] = ids_tensor([self.batch_size] , self.num_choices ) __a : Optional[int] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase (self : int ): return MobileBertConfig( 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 , embedding_size=self.embedding_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=snake_case_ , initializer_range=self.initializer_range , ) def lowerCAmelCase (self : str , snake_case_ : Tuple , snake_case_ : List[str] , snake_case_ : int , snake_case_ : int , snake_case_ : Any , snake_case_ : List[Any] , snake_case_ : Any ): __a : Any = MobileBertModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : List[str] = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ ) __a : Optional[Any] = model(snake_case_ , token_type_ids=snake_case_ ) __a : Optional[Any] = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase (self : Any , snake_case_ : Dict , snake_case_ : Optional[Any] , snake_case_ : Tuple , snake_case_ : Dict , snake_case_ : Tuple , snake_case_ : str , snake_case_ : List[Any] ): __a : str = MobileBertForMaskedLM(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : Tuple = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase (self : Tuple , snake_case_ : Any , snake_case_ : Dict , snake_case_ : List[Any] , snake_case_ : Any , snake_case_ : Union[str, Any] , snake_case_ : Optional[int] , snake_case_ : Dict ): __a : Optional[Any] = MobileBertForNextSentencePrediction(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : int = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def lowerCAmelCase (self : Any , snake_case_ : Dict , snake_case_ : Optional[int] , snake_case_ : Any , snake_case_ : Any , snake_case_ : Union[str, Any] , snake_case_ : Dict , snake_case_ : Optional[Any] ): __a : str = MobileBertForPreTraining(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : Union[str, Any] = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ , next_sentence_label=snake_case_ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def lowerCAmelCase (self : Dict , snake_case_ : Tuple , snake_case_ : Any , snake_case_ : Dict , snake_case_ : int , snake_case_ : int , snake_case_ : str , snake_case_ : str ): __a : str = MobileBertForQuestionAnswering(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : Optional[Any] = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , start_positions=snake_case_ , end_positions=snake_case_ , ) 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 : Optional[int] , snake_case_ : Any , snake_case_ : List[str] , snake_case_ : List[Any] , snake_case_ : str , snake_case_ : Any , snake_case_ : Tuple , snake_case_ : Optional[int] ): __a : Any = self.num_labels __a : Union[str, Any] = MobileBertForSequenceClassification(snake_case_ ) model.to(snake_case_ ) model.eval() __a : Tuple = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase (self : List[Any] , snake_case_ : Optional[int] , snake_case_ : str , snake_case_ : Dict , snake_case_ : List[Any] , snake_case_ : Optional[Any] , snake_case_ : Dict , snake_case_ : Optional[int] ): __a : Union[str, Any] = self.num_labels __a : str = MobileBertForTokenClassification(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : Any = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase (self : Union[str, Any] , snake_case_ : List[str] , snake_case_ : Any , snake_case_ : Optional[Any] , snake_case_ : Optional[Any] , snake_case_ : Any , snake_case_ : Dict , snake_case_ : Union[str, Any] ): __a : Union[str, Any] = self.num_choices __a : List[str] = MobileBertForMultipleChoice(config=snake_case_ ) model.to(snake_case_ ) model.eval() __a : List[Any] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __a : int = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __a : str = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __a : Any = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase (self : Optional[Any] ): __a : Optional[Any] = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : int = config_and_inputs __a : Union[str, Any] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class UpperCamelCase__ ( __lowercase ,__lowercase ,unittest.TestCase ): _SCREAMING_SNAKE_CASE : str = ( ( MobileBertModel, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, ) if is_torch_available() else () ) _SCREAMING_SNAKE_CASE : Any = ( { "feature-extraction": MobileBertModel, "fill-mask": MobileBertForMaskedLM, "question-answering": MobileBertForQuestionAnswering, "text-classification": MobileBertForSequenceClassification, "token-classification": MobileBertForTokenClassification, "zero-shot": MobileBertForSequenceClassification, } if is_torch_available() else {} ) _SCREAMING_SNAKE_CASE : Union[str, Any] = True def lowerCAmelCase (self : str , snake_case_ : Tuple , snake_case_ : List[Any] , snake_case_ : Union[str, Any]=False ): __a : List[str] = super()._prepare_for_class(snake_case_ , snake_case_ , return_labels=snake_case_ ) if return_labels: if model_class in get_values(snake_case_ ): __a : Optional[int] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=snake_case_ ) __a : int = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case_ ) return inputs_dict def lowerCAmelCase (self : Tuple ): __a : List[Any] = MobileBertModelTester(self ) __a : int = ConfigTester(self , config_class=snake_case_ , hidden_size=3_7 ) def lowerCAmelCase (self : Union[str, Any] ): self.config_tester.run_common_tests() def lowerCAmelCase (self : Optional[Any] ): __a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*snake_case_ ) def lowerCAmelCase (self : str ): __a : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case_ ) def lowerCAmelCase (self : Tuple ): __a : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case_ ) def lowerCAmelCase (self : Dict ): __a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case_ ) def lowerCAmelCase (self : int ): __a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case_ ) def lowerCAmelCase (self : List[Any] ): __a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case_ ) def lowerCAmelCase (self : int ): __a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case_ ) def lowerCAmelCase (self : Tuple ): __a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_ ) def __UpperCamelCase ( lowerCAmelCase__ : str ): return torch.tensor( lowerCAmelCase__ , dtype=torch.long , device=lowerCAmelCase__ , ) lowercase__ =1e-3 @require_torch @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( unittest.TestCase ): @slow def lowerCAmelCase (self : Any ): __a : Dict = MobileBertModel.from_pretrained('''google/mobilebert-uncased''' ).to(snake_case_ ) __a : Tuple = _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]] ) with torch.no_grad(): __a : str = model(snake_case_ )[0] __a : List[Any] = torch.Size((1, 9, 5_1_2) ) self.assertEqual(output.shape , snake_case_ ) __a : Union[str, Any] = torch.tensor( [ [ [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05], [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00], [2.604_7359E00, 1.567_7652E00, -1.732_4188E-01], ] ] , device=snake_case_ , ) # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a # ~1 difference, it's therefore not a good idea to measure using addition. # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE __a : List[str] = torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE ) __a : Any = torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE ) self.assertTrue(lower_bound and upper_bound )
216
0
from cva import destroyAllWindows, imread, imshow, waitKey def lowerCamelCase__ ( a ) -> Optional[int]: # getting number of pixels in the image _A , _A: Any = img.shape[0], img.shape[1] # converting each pixel's color to its negative for i in range(a ): for j in range(a ): _A: int = [2_55, 2_55, 2_55] - img[i][j] return img if __name__ == "__main__": # read original image UpperCAmelCase__ : Union[str, Any] = imread('image_data/lena.jpg', 1) # convert to its negative UpperCAmelCase__ : str = convert_to_negative(img) # show result image imshow('negative of original image', img) waitKey(0) destroyAllWindows()
301
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 UpperCAmelCase ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' __UpperCamelCase : Optional[int] = ['''image_processor''', '''tokenizer'''] __UpperCamelCase : Optional[Any] = '''BlipImageProcessor''' __UpperCamelCase : int = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[Any] ): """simple docstring""" _A: Optional[Any] = False super().__init__(lowerCAmelCase_ , lowerCAmelCase_ ) _A: List[Any] = self.image_processor def __call__( self : Optional[Any] , lowerCAmelCase_ : ImageInput = None , lowerCAmelCase_ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Union[bool, str, PaddingStrategy] = False , lowerCAmelCase_ : Union[bool, str, TruncationStrategy] = None , lowerCAmelCase_ : Optional[int] = None , lowerCAmelCase_ : int = 0 , lowerCAmelCase_ : Optional[int] = None , lowerCAmelCase_ : Optional[bool] = None , lowerCAmelCase_ : bool = False , lowerCAmelCase_ : bool = False , lowerCAmelCase_ : bool = False , lowerCAmelCase_ : bool = False , lowerCAmelCase_ : bool = False , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Optional[Union[str, TensorType]] = None , **lowerCAmelCase_ : Union[str, Any] , ): """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: _A: Tuple = self.tokenizer _A: Optional[int] = self.tokenizer( text=lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ , stride=lowerCAmelCase_ , pad_to_multiple_of=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , return_overflowing_tokens=lowerCAmelCase_ , return_special_tokens_mask=lowerCAmelCase_ , return_offsets_mapping=lowerCAmelCase_ , return_token_type_ids=lowerCAmelCase_ , return_length=lowerCAmelCase_ , verbose=lowerCAmelCase_ , return_tensors=lowerCAmelCase_ , **lowerCAmelCase_ , ) return text_encoding # add pixel_values _A: List[Any] = self.image_processor(lowerCAmelCase_ , return_tensors=lowerCAmelCase_ ) if text is not None: _A: Tuple = self.tokenizer( text=lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ , stride=lowerCAmelCase_ , pad_to_multiple_of=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , return_overflowing_tokens=lowerCAmelCase_ , return_special_tokens_mask=lowerCAmelCase_ , return_offsets_mapping=lowerCAmelCase_ , return_token_type_ids=lowerCAmelCase_ , return_length=lowerCAmelCase_ , verbose=lowerCAmelCase_ , return_tensors=lowerCAmelCase_ , **lowerCAmelCase_ , ) else: _A: str = None if text_encoding is not None: encoding_image_processor.update(lowerCAmelCase_ ) return encoding_image_processor def __magic_name__ ( self : Optional[Any] , *lowerCAmelCase_ : Union[str, Any] , **lowerCAmelCase_ : Tuple ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase_ , **lowerCAmelCase_ ) def __magic_name__ ( self : Union[str, Any] , *lowerCAmelCase_ : int , **lowerCAmelCase_ : Optional[int] ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase_ , **lowerCAmelCase_ ) @property def __magic_name__ ( self : Dict ): """simple docstring""" _A: Dict = self.tokenizer.model_input_names _A: List[str] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
301
1
"""simple docstring""" import os import numpy import onnx def _lowerCAmelCase ( lowercase_ , lowercase_ ): UpperCAmelCase = a.name UpperCAmelCase = b.name UpperCAmelCase = '' UpperCAmelCase = '' UpperCAmelCase = a == b UpperCAmelCase = name_a UpperCAmelCase = name_b return res def _lowerCAmelCase ( lowercase_ , lowercase_ , lowercase_ ): for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(lowercase_ , lowercase_ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , lowercase_ , lowercase_ ) _graph_replace_input_with(node_proto.attribute[1].g , lowercase_ , lowercase_ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , lowercase_ , lowercase_ ) def _lowerCAmelCase ( lowercase_ , lowercase_ , lowercase_ ): for n in graph_proto.node: _node_replace_input_with(lowercase_ , lowercase_ , lowercase_ ) def _lowerCAmelCase ( lowercase_ , lowercase_ , lowercase_ ): UpperCAmelCase = list(model.graph.initializer ) UpperCAmelCase = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i UpperCAmelCase = inits[i].name UpperCAmelCase = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , lowercase_ , lowercase_ ) def _lowerCAmelCase ( lowercase_ ): UpperCAmelCase = os.path.dirname(lowercase_ ) UpperCAmelCase = os.path.basename(lowercase_ ) UpperCAmelCase = onnx.load(os.path.join(lowercase_ , lowercase_ ) ) UpperCAmelCase = list(model.graph.initializer ) UpperCAmelCase = set() UpperCAmelCase = {} UpperCAmelCase = [] UpperCAmelCase = 0 for i in range(len(lowercase_ ) ): if i in dup_set: continue for j in range(i + 1 , len(lowercase_ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(lowercase_ ) dup_set.add(lowercase_ ) UpperCAmelCase = inits[j].data_type UpperCAmelCase = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print('unexpected data type: ' , lowercase_ ) total_reduced_size += mem_size UpperCAmelCase = inits[i].name UpperCAmelCase = inits[j].name if name_i in dup_map: dup_map[name_i].append(lowercase_ ) else: UpperCAmelCase = [name_j] ind_to_replace.append((j, i) ) print('total reduced size: ' , total_reduced_size / 1024 / 1024 / 1024 , 'GB' ) UpperCAmelCase = sorted(lowercase_ ) _remove_dup_initializers_from_model(lowercase_ , lowercase_ , lowercase_ ) UpperCAmelCase = 'optimized_' + model_file_name UpperCAmelCase = os.path.join(lowercase_ , lowercase_ ) onnx.save(lowercase_ , lowercase_ ) return new_model
78
"""simple docstring""" from collections import deque from math import floor from random import random from time import time class A_ : """simple docstring""" def __init__( self :Union[str, Any] ) -> str: UpperCAmelCase = {} def UpperCAmelCase__ ( self :Any , lowercase_ :List[Any] , lowercase_ :List[str] , lowercase_ :Dict=1 ) -> List[Any]: if self.graph.get(lowercase_ ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: UpperCAmelCase = [[w, v]] if not self.graph.get(lowercase_ ): UpperCAmelCase = [] def UpperCAmelCase__ ( self :Any ) -> Optional[int]: return list(self.graph ) def UpperCAmelCase__ ( self :List[Any] , lowercase_ :Optional[int] , lowercase_ :Optional[Any] ) -> Dict: if self.graph.get(lowercase_ ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowercase_ ) def UpperCAmelCase__ ( self :List[str] , lowercase_ :Tuple=-2 , lowercase_ :List[Any]=-1 ) -> List[Any]: if s == d: return [] UpperCAmelCase = [] UpperCAmelCase = [] if s == -2: UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(lowercase_ ) return visited else: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return visited def UpperCAmelCase__ ( self :List[str] , lowercase_ :int=-1 ) -> Tuple: if c == -1: UpperCAmelCase = floor(random() * 1_00_00 ) + 10 for i in range(lowercase_ ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): UpperCAmelCase = floor(random() * c ) + 1 if n != i: self.add_pair(lowercase_ , lowercase_ , 1 ) def UpperCAmelCase__ ( self :Tuple , lowercase_ :Optional[Any]=-2 ) -> Optional[int]: UpperCAmelCase = deque() UpperCAmelCase = [] if s == -2: UpperCAmelCase = list(self.graph )[0] d.append(lowercase_ ) visited.append(lowercase_ ) while d: UpperCAmelCase = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def UpperCAmelCase__ ( self :Any , lowercase_ :Optional[int] ) -> List[Any]: UpperCAmelCase = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def UpperCAmelCase__ ( self :Tuple , lowercase_ :List[str] ) -> List[str]: return len(self.graph[u] ) def UpperCAmelCase__ ( self :Optional[int] , lowercase_ :Any=-2 ) -> int: UpperCAmelCase = [] UpperCAmelCase = [] if s == -2: UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = s UpperCAmelCase = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return sorted_nodes def UpperCAmelCase__ ( self :str ) -> str: UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = -2 UpperCAmelCase = [] UpperCAmelCase = s UpperCAmelCase = False UpperCAmelCase = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): UpperCAmelCase = len(lowercase_ ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() UpperCAmelCase = True if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = False indirect_parents.append(lowercase_ ) UpperCAmelCase = s UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return list(lowercase_ ) def UpperCAmelCase__ ( self :Optional[int] ) -> Tuple: UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = -2 UpperCAmelCase = [] UpperCAmelCase = s UpperCAmelCase = False UpperCAmelCase = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): UpperCAmelCase = len(lowercase_ ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() UpperCAmelCase = True if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = False indirect_parents.append(lowercase_ ) UpperCAmelCase = s UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return False def UpperCAmelCase__ ( self :Optional[Any] , lowercase_ :int=-2 , lowercase_ :List[str]=-1 ) -> Any: UpperCAmelCase = time() self.dfs(lowercase_ , lowercase_ ) UpperCAmelCase = time() return end - begin def UpperCAmelCase__ ( self :Union[str, Any] , lowercase_ :List[str]=-2 ) -> str: UpperCAmelCase = time() self.bfs(lowercase_ ) UpperCAmelCase = time() return end - begin class A_ : """simple docstring""" def __init__( self :List[str] ) -> Union[str, Any]: UpperCAmelCase = {} def UpperCAmelCase__ ( self :str , lowercase_ :Dict , lowercase_ :Optional[Any] , lowercase_ :Optional[int]=1 ) -> Dict: # check if the u exists if self.graph.get(lowercase_ ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist UpperCAmelCase = [[w, v]] # add the other way if self.graph.get(lowercase_ ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist UpperCAmelCase = [[w, u]] def UpperCAmelCase__ ( self :Any , lowercase_ :Union[str, Any] , lowercase_ :Tuple ) -> Optional[Any]: if self.graph.get(lowercase_ ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowercase_ ) # the other way round if self.graph.get(lowercase_ ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(lowercase_ ) def UpperCAmelCase__ ( self :Optional[Any] , lowercase_ :Optional[int]=-2 , lowercase_ :Optional[int]=-1 ) -> List[str]: if s == d: return [] UpperCAmelCase = [] UpperCAmelCase = [] if s == -2: UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(lowercase_ ) return visited else: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return visited def UpperCAmelCase__ ( self :List[str] , lowercase_ :Optional[int]=-1 ) -> Any: if c == -1: UpperCAmelCase = floor(random() * 1_00_00 ) + 10 for i in range(lowercase_ ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): UpperCAmelCase = floor(random() * c ) + 1 if n != i: self.add_pair(lowercase_ , lowercase_ , 1 ) def UpperCAmelCase__ ( self :Dict , lowercase_ :int=-2 ) -> int: UpperCAmelCase = deque() UpperCAmelCase = [] if s == -2: UpperCAmelCase = list(self.graph )[0] d.append(lowercase_ ) visited.append(lowercase_ ) while d: UpperCAmelCase = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def UpperCAmelCase__ ( self :Union[str, Any] , lowercase_ :List[Any] ) -> str: return len(self.graph[u] ) def UpperCAmelCase__ ( self :Optional[Any] ) -> Any: UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = -2 UpperCAmelCase = [] UpperCAmelCase = s UpperCAmelCase = False UpperCAmelCase = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): UpperCAmelCase = len(lowercase_ ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() UpperCAmelCase = True if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = False indirect_parents.append(lowercase_ ) UpperCAmelCase = s UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return list(lowercase_ ) def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = list(self.graph )[0] stack.append(lowercase_ ) visited.append(lowercase_ ) UpperCAmelCase = -2 UpperCAmelCase = [] UpperCAmelCase = s UpperCAmelCase = False UpperCAmelCase = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: UpperCAmelCase = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): UpperCAmelCase = len(lowercase_ ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) UpperCAmelCase = node[1] break # check if all the children are visited if s == ss: stack.pop() UpperCAmelCase = True if len(lowercase_ ) != 0: UpperCAmelCase = stack[len(lowercase_ ) - 1] else: UpperCAmelCase = False indirect_parents.append(lowercase_ ) UpperCAmelCase = s UpperCAmelCase = ss # check if se have reached the starting point if len(lowercase_ ) == 0: return False def UpperCAmelCase__ ( self :Union[str, Any] ) -> Union[str, Any]: return list(self.graph ) def UpperCAmelCase__ ( self :List[Any] , lowercase_ :Union[str, Any]=-2 , lowercase_ :List[str]=-1 ) -> str: UpperCAmelCase = time() self.dfs(lowercase_ , lowercase_ ) UpperCAmelCase = time() return end - begin def UpperCAmelCase__ ( self :Any , lowercase_ :int=-2 ) -> str: UpperCAmelCase = time() self.bfs(lowercase_ ) UpperCAmelCase = time() return end - begin
78
1
'''simple docstring''' import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) UpperCAmelCase_ = logging.getLogger(__name__) UpperCAmelCase_ = 'Hello world! cécé herlolip' UpperCAmelCase_ = namedtuple( 'BertAbsConfig', [ 'temp_dir', 'large', 'use_bert_emb', 'finetune_bert', 'encoder', 'share_emb', 'max_pos', 'enc_layers', 'enc_hidden_size', 'enc_heads', 'enc_ff_size', 'enc_dropout', 'dec_layers', 'dec_hidden_size', 'dec_heads', 'dec_ff_size', 'dec_dropout', ], ) def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple ): '''simple docstring''' UpperCAmelCase__ = BertAbsConfig( temp_dir=""".""" , finetune_bert=SCREAMING_SNAKE_CASE__ , large=SCREAMING_SNAKE_CASE__ , share_emb=SCREAMING_SNAKE_CASE__ , use_bert_emb=SCREAMING_SNAKE_CASE__ , encoder="""bert""" , max_pos=512 , enc_layers=6 , enc_hidden_size=512 , enc_heads=8 , enc_ff_size=512 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=768 , dec_heads=8 , dec_ff_size=2048 , dec_dropout=0.2 , ) UpperCAmelCase__ = torch.load(SCREAMING_SNAKE_CASE__ , lambda SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : storage ) UpperCAmelCase__ = AbsSummarizer(SCREAMING_SNAKE_CASE__ , torch.device("""cpu""" ) , SCREAMING_SNAKE_CASE__ ) original.eval() UpperCAmelCase__ = BertAbsSummarizer(SCREAMING_SNAKE_CASE__ , torch.device("""cpu""" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("""convert the model""" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("""Make sure that the models' outputs are identical""" ) UpperCAmelCase__ = BertTokenizer.from_pretrained("""bert-base-uncased""" ) # prepare the model inputs UpperCAmelCase__ = tokenizer.encode("""This is sample éàalj'-.""" ) encoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(SCREAMING_SNAKE_CASE__ )) ) UpperCAmelCase__ = torch.tensor(SCREAMING_SNAKE_CASE__ ).unsqueeze(0 ) UpperCAmelCase__ = tokenizer.encode("""This is sample 3 éàalj'-.""" ) decoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(SCREAMING_SNAKE_CASE__ )) ) UpperCAmelCase__ = torch.tensor(SCREAMING_SNAKE_CASE__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass UpperCAmelCase__ = encoder_input_ids UpperCAmelCase__ = decoder_input_ids UpperCAmelCase__ = UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = UpperCAmelCase__ = None UpperCAmelCase__ = UpperCAmelCase__ = None UpperCAmelCase__ = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical UpperCAmelCase__ = original(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] UpperCAmelCase__ = original.generator(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = new_model( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0] UpperCAmelCase__ = new_model.generator(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("""Maximum absolute difference beween weights: {:.2f}""".format(SCREAMING_SNAKE_CASE__ ) ) UpperCAmelCase__ = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("""Maximum absolute difference beween weights: {:.2f}""".format(SCREAMING_SNAKE_CASE__ ) ) UpperCAmelCase__ = torch.allclose(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , atol=1e-3 ) if are_identical: logging.info("""all weights are equal up to 1e-3""" ) else: raise ValueError("""the weights are different. The new model is likely different from the original one.""" ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("""saving the model's state dictionary""" ) torch.save( new_model.state_dict() , """./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin""" ) if __name__ == "__main__": UpperCAmelCase_ = argparse.ArgumentParser() parser.add_argument( '--bertabs_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.', ) UpperCAmelCase_ = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
61
'''simple docstring''' import importlib.util import json import os import warnings from dataclasses import dataclass, field import torch from ..training_args import TrainingArguments from ..utils import cached_property, is_sagemaker_dp_enabled, logging UpperCAmelCase_ = logging.get_logger(__name__) def _UpperCamelCase ( ): '''simple docstring''' UpperCAmelCase__ = os.getenv("""SM_HP_MP_PARAMETERS""" , """{}""" ) try: # Parse it and check the field "partitions" is included, it is required for model parallel. UpperCAmelCase__ = json.loads(SCREAMING_SNAKE_CASE__ ) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. UpperCAmelCase__ = os.getenv("""SM_FRAMEWORK_PARAMS""" , """{}""" ) try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". UpperCAmelCase__ = json.loads(SCREAMING_SNAKE_CASE__ ) if not mpi_options.get("""sagemaker_mpi_enabled""" , SCREAMING_SNAKE_CASE__ ): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec("""smdistributed""" ) is not None if is_sagemaker_model_parallel_available(): import smdistributed.modelparallel.torch as smp smp.init() @dataclass class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : str = field( default="""""" , metadata={"""help""": """Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer"""} , ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" super().__post_init__() warnings.warn( """`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use """ """`TrainingArguments` instead.""" , _UpperCAmelCase , ) @cached_property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" logger.info("""PyTorch: setting up devices""" ) if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1: logger.warning( """torch.distributed process group is initialized, but local_rank == -1. """ """In order to use Torch DDP, launch your script with `python -m torch.distributed.launch""" ) if self.no_cuda: UpperCAmelCase__ = torch.device("""cpu""" ) UpperCAmelCase__ = 0 elif is_sagemaker_model_parallel_available(): UpperCAmelCase__ = smp.local_rank() UpperCAmelCase__ = torch.device("""cuda""" , _UpperCAmelCase ) UpperCAmelCase__ = 1 elif is_sagemaker_dp_enabled(): import smdistributed.dataparallel.torch.torch_smddp # noqa: F401 torch.distributed.init_process_group(backend="""smddp""" , timeout=self.ddp_timeout_delta ) UpperCAmelCase__ = int(os.getenv("""SMDATAPARALLEL_LOCAL_RANK""" ) ) UpperCAmelCase__ = torch.device("""cuda""" , self.local_rank ) UpperCAmelCase__ = 1 elif self.local_rank == -1: # if n_gpu is > 1 we'll use nn.DataParallel. # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0` # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will # trigger an error that a device index is missing. Index 0 takes into account the # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0` # will use the first GPU in that env, i.e. GPU#1 UpperCAmelCase__ = torch.device("""cuda:0""" if torch.cuda.is_available() else """cpu""" ) # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at # the default value. UpperCAmelCase__ = torch.cuda.device_count() else: # Here, we'll use torch.distributed. # Initializes the distributed backend which will take care of synchronizing nodes/GPUs if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend="""nccl""" , timeout=self.ddp_timeout_delta ) UpperCAmelCase__ = torch.device("""cuda""" , self.local_rank ) UpperCAmelCase__ = 1 if device.type == "cuda": torch.cuda.set_device(_UpperCAmelCase ) return device @property def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" if is_sagemaker_model_parallel_available(): return smp.dp_size() return super().world_size @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return not is_sagemaker_model_parallel_available() @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" return False
61
1
"""simple docstring""" import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def __lowercase ( _a ): snake_case_ : List[str] = VideoMAEConfig() set_architecture_configs(lowerCAmelCase__ , lowerCAmelCase__ ) if "finetuned" not in model_name: snake_case_ : Union[str, Any] = False if "finetuned" in model_name: snake_case_ : Optional[Any] = '''huggingface/label-files''' if "kinetics" in model_name: snake_case_ : Optional[int] = 400 snake_case_ : Union[str, Any] = '''kinetics400-id2label.json''' elif "ssv2" in model_name: snake_case_ : List[str] = 174 snake_case_ : int = '''something-something-v2-id2label.json''' else: raise ValueError('''Model name should either contain \'kinetics\' or \'ssv2\' in case it\'s fine-tuned.''' ) snake_case_ : Optional[Any] = json.load(open(hf_hub_download(lowerCAmelCase__ , lowerCAmelCase__ , repo_type='''dataset''' ) , '''r''' ) ) snake_case_ : Union[str, Any] = {int(lowerCAmelCase__ ): v for k, v in idalabel.items()} snake_case_ : Any = idalabel snake_case_ : Any = {v: k for k, v in idalabel.items()} return config def __lowercase ( _a , _a ): if "small" in model_name: snake_case_ : Any = 384 snake_case_ : List[Any] = 1_536 snake_case_ : List[str] = 12 snake_case_ : List[Any] = 16 snake_case_ : List[str] = 12 snake_case_ : Optional[Any] = 3 snake_case_ : Union[str, Any] = 192 snake_case_ : Optional[int] = 768 elif "large" in model_name: snake_case_ : List[Any] = 1_024 snake_case_ : str = 4_096 snake_case_ : Optional[Any] = 24 snake_case_ : List[str] = 16 snake_case_ : List[Any] = 12 snake_case_ : List[str] = 8 snake_case_ : Tuple = 512 snake_case_ : Optional[int] = 2_048 elif "huge" in model_name: snake_case_ : Union[str, Any] = 1_280 snake_case_ : str = 5_120 snake_case_ : Dict = 32 snake_case_ : List[str] = 16 snake_case_ : int = 12 snake_case_ : str = 8 snake_case_ : Optional[Any] = 640 snake_case_ : str = 2_560 elif "base" not in model_name: raise ValueError('''Model name should include either "small", "base", "large", or "huge"''' ) def __lowercase ( _a ): if "encoder." in name: snake_case_ : str = name.replace('''encoder.''' , '''''' ) if "cls_token" in name: snake_case_ : Optional[Any] = name.replace('''cls_token''' , '''videomae.embeddings.cls_token''' ) if "decoder_pos_embed" in name: snake_case_ : Union[str, Any] = name.replace('''decoder_pos_embed''' , '''decoder.decoder_pos_embed''' ) if "pos_embed" in name and "decoder" not in name: snake_case_ : List[str] = name.replace('''pos_embed''' , '''videomae.embeddings.position_embeddings''' ) if "patch_embed.proj" in name: snake_case_ : int = name.replace('''patch_embed.proj''' , '''videomae.embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: snake_case_ : Any = name.replace('''patch_embed.norm''' , '''videomae.embeddings.norm''' ) if "decoder.blocks" in name: snake_case_ : str = name.replace('''decoder.blocks''' , '''decoder.decoder_layers''' ) if "blocks" in name: snake_case_ : int = name.replace('''blocks''' , '''videomae.encoder.layer''' ) if "attn.proj" in name: snake_case_ : List[str] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name and "bias" not in name: snake_case_ : int = name.replace('''attn''' , '''attention.self''' ) if "attn" in name: snake_case_ : Union[str, Any] = name.replace('''attn''' , '''attention.attention''' ) if "norm1" in name: snake_case_ : int = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: snake_case_ : List[str] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: snake_case_ : Optional[int] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: snake_case_ : Tuple = name.replace('''mlp.fc2''' , '''output.dense''' ) if "decoder_embed" in name: snake_case_ : List[str] = name.replace('''decoder_embed''' , '''decoder.decoder_embed''' ) if "decoder_norm" in name: snake_case_ : str = name.replace('''decoder_norm''' , '''decoder.decoder_norm''' ) if "decoder_pred" in name: snake_case_ : Tuple = name.replace('''decoder_pred''' , '''decoder.decoder_pred''' ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: snake_case_ : List[str] = name.replace('''norm.weight''' , '''videomae.layernorm.weight''' ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: snake_case_ : Optional[Any] = name.replace('''norm.bias''' , '''videomae.layernorm.bias''' ) if "head" in name and "decoder" not in name: snake_case_ : str = name.replace('''head''' , '''classifier''' ) return name def __lowercase ( _a , _a ): for key in orig_state_dict.copy().keys(): snake_case_ : Any = orig_state_dict.pop(lowerCAmelCase__ ) if key.startswith('''encoder.''' ): snake_case_ : Dict = key.replace('''encoder.''' , '''''' ) if "qkv" in key: snake_case_ : Union[str, Any] = key.split('''.''' ) if key.startswith('''decoder.blocks''' ): snake_case_ : Any = config.decoder_hidden_size snake_case_ : Optional[int] = int(key_split[2] ) snake_case_ : List[str] = '''decoder.decoder_layers.''' if "weight" in key: snake_case_ : List[str] = val[:dim, :] snake_case_ : int = val[dim : dim * 2, :] snake_case_ : Union[str, Any] = val[-dim:, :] else: snake_case_ : Any = config.hidden_size snake_case_ : Tuple = int(key_split[1] ) snake_case_ : Dict = '''videomae.encoder.layer.''' if "weight" in key: snake_case_ : Optional[Any] = val[:dim, :] snake_case_ : List[Any] = val[dim : dim * 2, :] snake_case_ : List[Any] = val[-dim:, :] else: snake_case_ : Optional[Any] = val return orig_state_dict def __lowercase ( ): snake_case_ : List[Any] = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' ) snake_case_ : List[str] = np.load(lowerCAmelCase__ ) return list(lowerCAmelCase__ ) def __lowercase ( _a , _a , _a , _a ): snake_case_ : int = get_videomae_config(lowerCAmelCase__ ) if "finetuned" in model_name: snake_case_ : Any = VideoMAEForVideoClassification(lowerCAmelCase__ ) else: snake_case_ : List[Any] = VideoMAEForPreTraining(lowerCAmelCase__ ) # download original checkpoint, hosted on Google Drive snake_case_ : Union[str, Any] = '''pytorch_model.bin''' gdown.cached_download(lowerCAmelCase__ , lowerCAmelCase__ , quiet=lowerCAmelCase__ ) snake_case_ : int = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) if "model" in files: snake_case_ : Dict = files['''model'''] else: snake_case_ : Optional[int] = files['''module'''] snake_case_ : Union[str, Any] = convert_state_dict(lowerCAmelCase__ , lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ ) model.eval() # verify model on basic input snake_case_ : List[Any] = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) snake_case_ : List[str] = prepare_video() snake_case_ : Union[str, Any] = image_processor(lowerCAmelCase__ , return_tensors='''pt''' ) if "finetuned" not in model_name: snake_case_ : List[str] = hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' , filename='''bool_masked_pos.pt''' ) snake_case_ : Dict = torch.load(lowerCAmelCase__ ) snake_case_ : Any = model(**lowerCAmelCase__ ) snake_case_ : List[Any] = outputs.logits snake_case_ : Tuple = [ '''videomae-small-finetuned-kinetics''', '''videomae-small-finetuned-ssv2''', # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) '''videomae-base-short''', '''videomae-base-short-finetuned-kinetics''', '''videomae-base''', '''videomae-base-finetuned-kinetics''', '''videomae-large''', '''videomae-large-finetuned-kinetics''', '''videomae-huge-finetuned-kinetics''', # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) '''videomae-base-short-ssv2''', '''videomae-base-short-finetuned-ssv2''', '''videomae-base-ssv2''', '''videomae-base-finetuned-ssv2''', ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": snake_case_ : str = torch.Size([1, 400] ) snake_case_ : Any = torch.tensor([-0.9291, -0.4061, -0.9307] ) elif model_name == "videomae-small-finetuned-ssv2": snake_case_ : Any = torch.Size([1, 174] ) snake_case_ : Dict = torch.tensor([0.2671, -0.4689, -0.8235] ) elif model_name == "videomae-base": snake_case_ : Tuple = torch.Size([1, 1_408, 1_536] ) snake_case_ : List[str] = torch.tensor([[0.7739, 0.7968, 0.7089], [0.6701, 0.7487, 0.6209], [0.4287, 0.5158, 0.4773]] ) elif model_name == "videomae-base-short": snake_case_ : Union[str, Any] = torch.Size([1, 1_408, 1_536] ) snake_case_ : int = torch.tensor([[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] ) # we verified the loss both for normalized and unnormalized targets for this one snake_case_ : int = torch.tensor([0.5142] ) if config.norm_pix_loss else torch.tensor([0.6469] ) elif model_name == "videomae-large": snake_case_ : List[str] = torch.Size([1, 1_408, 1_536] ) snake_case_ : Tuple = torch.tensor([[0.7149, 0.7997, 0.6966], [0.6768, 0.7869, 0.6948], [0.5139, 0.6221, 0.5605]] ) elif model_name == "videomae-large-finetuned-kinetics": snake_case_ : List[Any] = torch.Size([1, 400] ) snake_case_ : Any = torch.tensor([0.0771, 0.0011, -0.3625] ) elif model_name == "videomae-huge-finetuned-kinetics": snake_case_ : Optional[int] = torch.Size([1, 400] ) snake_case_ : Tuple = torch.tensor([0.2433, 0.1632, -0.4894] ) elif model_name == "videomae-base-short-finetuned-kinetics": snake_case_ : Tuple = torch.Size([1, 400] ) snake_case_ : Union[str, Any] = torch.tensor([0.6588, 0.0990, -0.2493] ) elif model_name == "videomae-base-finetuned-kinetics": snake_case_ : Optional[Any] = torch.Size([1, 400] ) snake_case_ : Optional[Any] = torch.tensor([0.3669, -0.0688, -0.2421] ) elif model_name == "videomae-base-short-ssv2": snake_case_ : Union[str, Any] = torch.Size([1, 1_408, 1_536] ) snake_case_ : List[Any] = torch.tensor([[0.4712, 0.5296, 0.5786], [0.2278, 0.2729, 0.4026], [0.0352, 0.0730, 0.2506]] ) elif model_name == "videomae-base-short-finetuned-ssv2": snake_case_ : List[str] = torch.Size([1, 174] ) snake_case_ : int = torch.tensor([-0.0537, -0.1539, -0.3266] ) elif model_name == "videomae-base-ssv2": snake_case_ : int = torch.Size([1, 1_408, 1_536] ) snake_case_ : Any = torch.tensor([[0.8131, 0.8727, 0.8546], [0.7366, 0.9377, 0.8870], [0.5935, 0.8874, 0.8564]] ) elif model_name == "videomae-base-finetuned-ssv2": snake_case_ : Union[str, Any] = torch.Size([1, 174] ) snake_case_ : Any = torch.tensor([0.1961, -0.8337, -0.6389] ) else: raise ValueError(f"Model name not supported. Should be one of {model_names}" ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , lowerCAmelCase__ , atol=1E-4 ) else: print('''Logits:''' , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , lowerCAmelCase__ , atol=1E-4 ) print('''Logits ok!''' ) # verify loss, if applicable if model_name == "videomae-base-short": snake_case_ : Optional[Any] = outputs.loss assert torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-4 ) print('''Loss ok!''' ) if pytorch_dump_folder_path is not None: print(f"Saving model and image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(lowerCAmelCase__ ) model.save_pretrained(lowerCAmelCase__ ) if push_to_hub: print('''Pushing to the hub...''' ) model.push_to_hub(lowerCAmelCase__ , organization='''nielsr''' ) if __name__ == "__main__": lowercase__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4''', type=str, help=( '''URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct''' ''' download link.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''/Users/nielsrogge/Documents/VideoMAE/Test''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--model_name''', default='''videomae-base''', type=str, help='''Name of the model.''') parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) lowercase__ : Optional[Any] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
264
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase__ = { '''configuration_distilbert''': [ '''DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DistilBertConfig''', '''DistilBertOnnxConfig''', ], '''tokenization_distilbert''': ['''DistilBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = ['''DistilBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DistilBertForMaskedLM''', '''DistilBertForMultipleChoice''', '''DistilBertForQuestionAnswering''', '''DistilBertForSequenceClassification''', '''DistilBertForTokenClassification''', '''DistilBertModel''', '''DistilBertPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFDistilBertForMaskedLM''', '''TFDistilBertForMultipleChoice''', '''TFDistilBertForQuestionAnswering''', '''TFDistilBertForSequenceClassification''', '''TFDistilBertForTokenClassification''', '''TFDistilBertMainLayer''', '''TFDistilBertModel''', '''TFDistilBertPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''FlaxDistilBertForMaskedLM''', '''FlaxDistilBertForMultipleChoice''', '''FlaxDistilBertForQuestionAnswering''', '''FlaxDistilBertForSequenceClassification''', '''FlaxDistilBertForTokenClassification''', '''FlaxDistilBertModel''', '''FlaxDistilBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_distilbert import ( DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DistilBertConfig, DistilBertOnnxConfig, ) from .tokenization_distilbert import DistilBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_distilbert_fast import DistilBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_distilbert import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, DistilBertPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertMainLayer, TFDistilBertModel, TFDistilBertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_distilbert import ( FlaxDistilBertForMaskedLM, FlaxDistilBertForMultipleChoice, FlaxDistilBertForQuestionAnswering, FlaxDistilBertForSequenceClassification, FlaxDistilBertForTokenClassification, FlaxDistilBertModel, FlaxDistilBertPreTrainedModel, ) else: import sys UpperCamelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
181
0
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging _A : Optional[int] =logging.get_logger(__name__) _A : Any ={ '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json''' ), } class _lowercase ( _lowercase ): a = """xlm-prophetnet""" a = ["""past_key_values"""] a = { """num_attention_heads""": """num_encoder_attention_heads""", } def __init__( self: Tuple , UpperCamelCase__: Optional[float] = 0.1 , UpperCamelCase__: Optional[Union[str, Callable]] = "gelu" , UpperCamelCase__: Optional[int] = 30_522 , UpperCamelCase__: Optional[int] = 1_024 , UpperCamelCase__: Optional[int] = 4_096 , UpperCamelCase__: Optional[int] = 12 , UpperCamelCase__: Optional[int] = 16 , UpperCamelCase__: Optional[int] = 4_096 , UpperCamelCase__: Optional[int] = 12 , UpperCamelCase__: Optional[int] = 16 , UpperCamelCase__: Optional[float] = 0.1 , UpperCamelCase__: Optional[float] = 0.1 , UpperCamelCase__: Optional[int] = 512 , UpperCamelCase__: Optional[float] = 0.02 , UpperCamelCase__: Optional[bool] = True , UpperCamelCase__: Optional[bool] = True , UpperCamelCase__: Optional[int] = 0 , UpperCamelCase__: Optional[int] = 2 , UpperCamelCase__: Optional[int] = 32 , UpperCamelCase__: Optional[int] = 128 , UpperCamelCase__: Optional[bool] = False , UpperCamelCase__: Optional[float] = 0.0 , UpperCamelCase__: Optional[bool] = True , UpperCamelCase__: Optional[int] = 0 , UpperCamelCase__: Optional[int] = 1 , UpperCamelCase__: Optional[int] = 2 , **UpperCamelCase__: List[Any] , ): lowerCamelCase__ : int = vocab_size lowerCamelCase__ : int = hidden_size lowerCamelCase__ : Dict = encoder_ffn_dim lowerCamelCase__ : List[str] = num_encoder_layers lowerCamelCase__ : Optional[Any] = num_encoder_attention_heads lowerCamelCase__ : int = decoder_ffn_dim lowerCamelCase__ : Tuple = num_decoder_layers lowerCamelCase__ : str = num_decoder_attention_heads lowerCamelCase__ : List[Any] = max_position_embeddings lowerCamelCase__ : Dict = init_std # Normal(0, this parameter) lowerCamelCase__ : Tuple = activation_function # parameters for xlmprophetnet lowerCamelCase__ : Optional[int] = ngram lowerCamelCase__ : Optional[int] = num_buckets lowerCamelCase__ : Union[str, Any] = relative_max_distance lowerCamelCase__ : List[Any] = disable_ngram_loss lowerCamelCase__ : Any = eps # 3 Types of Dropout lowerCamelCase__ : Union[str, Any] = attention_dropout lowerCamelCase__ : Optional[int] = activation_dropout lowerCamelCase__ : Optional[Any] = dropout lowerCamelCase__ : int = use_cache super().__init__( pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , is_encoder_decoder=UpperCamelCase__ , add_cross_attention=UpperCamelCase__ , decoder_start_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) @property def lowerCamelCase_ ( self: int ): return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def lowerCamelCase_ ( self: List[str] , UpperCamelCase__: Optional[Any] ): raise NotImplementedError( """This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and""" """ `num_decoder_layers`.""" )
129
'''simple docstring''' import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs _A : int =imread(r'''digital_image_processing/image_data/lena_small.jpg''') _A : Optional[Any] =cvtColor(img, COLOR_BGR2GRAY) def SCREAMING_SNAKE_CASE_ () -> Any: lowerCamelCase__ : int = cn.convert_to_negative(UpperCamelCase ) # assert negative_img array for at least one True assert negative_img.any() def SCREAMING_SNAKE_CASE_ () -> Optional[int]: with Image.open("""digital_image_processing/image_data/lena_small.jpg""" ) as img: # Work around assertion for response assert str(cc.change_contrast(UpperCamelCase , 110 ) ).startswith( """<PIL.Image.Image image mode=RGB size=100x100 at""" ) def SCREAMING_SNAKE_CASE_ () -> Union[str, Any]: lowerCamelCase__ : Optional[int] = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def SCREAMING_SNAKE_CASE_ () -> str: lowerCamelCase__ : int = imread("""digital_image_processing/image_data/lena_small.jpg""" , 0 ) # assert ambiguous array for all == True assert canny_img.all() lowerCamelCase__ : Union[str, Any] = canny.canny(UpperCamelCase ) # assert canny array for at least one True assert canny_array.any() def SCREAMING_SNAKE_CASE_ () -> str: assert gg.gaussian_filter(UpperCamelCase , 5 , sigma=0.9 ).all() def SCREAMING_SNAKE_CASE_ () -> int: # laplace diagonals lowerCamelCase__ : int = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) lowerCamelCase__ : int = conv.img_convolve(UpperCamelCase , UpperCamelCase ).astype(UpperCamelCase ) assert res.any() def SCREAMING_SNAKE_CASE_ () -> Any: assert med.median_filter(UpperCamelCase , 3 ).any() def SCREAMING_SNAKE_CASE_ () -> Any: lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = sob.sobel_filter(UpperCamelCase ) assert grad.any() and theta.any() def SCREAMING_SNAKE_CASE_ () -> Tuple: lowerCamelCase__ : Union[str, Any] = sp.make_sepia(UpperCamelCase , 20 ) assert sepia.all() def SCREAMING_SNAKE_CASE_ (UpperCamelCase = "digital_image_processing/image_data/lena_small.jpg" ) -> Optional[Any]: lowerCamelCase__ : Union[str, Any] = bs.Burkes(imread(UpperCamelCase , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def SCREAMING_SNAKE_CASE_ (UpperCamelCase = "digital_image_processing/image_data/lena_small.jpg" , ) -> Optional[Any]: lowerCamelCase__ : int = rs.NearestNeighbour(imread(UpperCamelCase , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def SCREAMING_SNAKE_CASE_ () -> Optional[int]: lowerCamelCase__ : Union[str, Any] = """digital_image_processing/image_data/lena.jpg""" # Reading the image and converting it to grayscale. lowerCamelCase__ : Tuple = imread(UpperCamelCase , 0 ) # Test for get_neighbors_pixel function() return not None lowerCamelCase__ : int = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : Optional[Any] = image[x_coordinate][y_coordinate] lowerCamelCase__ : str = lbp.get_neighbors_pixel( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lowerCamelCase__ : List[str] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): lowerCamelCase__ : List[str] = lbp.local_binary_value(UpperCamelCase , UpperCamelCase , UpperCamelCase ) assert lbp_image.any()
129
1
'''simple docstring''' import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(_lowerCamelCase ) , """Tatoeba directory does not exist.""" ) class __UpperCAmelCase ( unittest.TestCase ): @cached_property def lowerCamelCase ( self ): """simple docstring""" _snake_case = tempfile.mkdtemp() return TatoebaConverter(save_dir=lowerCAmelCase_ ) @slow def lowerCamelCase ( self ): """simple docstring""" self.resolver.convert_models(['heb-eng'] ) @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = self.resolver.write_model_card('opus-mt-he-en' , dry_run=lowerCAmelCase_ ) assert mmeta["long_pair"] == "heb-eng"
42
'''simple docstring''' import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __a: Optional[int] = 4 __a: Optional[Any] = 3 class UpperCAmelCase ( a__ ): '''simple docstring''' pass def __UpperCamelCase ( UpperCAmelCase ): for shard in shards: for i in range(UpperCAmelCase ): yield {"i": i, "shard": shard} def __UpperCamelCase ( ): lowercase__ : Tuple = int(os.environ['''RANK'''] ) lowercase__ : List[str] = int(os.environ['''WORLD_SIZE'''] ) lowercase__ : Optional[Any] = ArgumentParser() parser.add_argument('''--streaming''' , type=UpperCAmelCase ) parser.add_argument('''--local_rank''' , type=UpperCAmelCase ) parser.add_argument('''--num_workers''' , type=UpperCAmelCase , default=0 ) lowercase__ : List[Any] = parser.parse_args() lowercase__ : List[str] = args.streaming lowercase__ : str = args.num_workers lowercase__ : Optional[int] = {'''shards''': [F"""shard_{shard_idx}""" for shard_idx in range(UpperCAmelCase )]} lowercase__ : Tuple = IterableDataset.from_generator(UpperCAmelCase , gen_kwargs=UpperCAmelCase ) if not streaming: lowercase__ : int = Dataset.from_list(list(UpperCAmelCase ) ) lowercase__ : str = split_dataset_by_node(UpperCAmelCase , rank=UpperCAmelCase , world_size=UpperCAmelCase ) lowercase__ : Optional[int] = torch.utils.data.DataLoader(UpperCAmelCase , num_workers=UpperCAmelCase ) lowercase__ : Dict = NUM_SHARDS * NUM_ITEMS_PER_SHARD lowercase__ : Optional[Any] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) lowercase__ : Union[str, Any] = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(F"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
198
0
A : Optional[Any] = tuple[float, float, float] A : str = tuple[float, float, float] def UpperCamelCase ( __magic_name__ : Pointad , __magic_name__ : Pointad ) -> Vectorad: """simple docstring""" lowercase__ = end_pointa[0] - end_pointa[0] lowercase__ = end_pointa[1] - end_pointa[1] lowercase__ = end_pointa[2] - end_pointa[2] return (x, y, z) def UpperCamelCase ( __magic_name__ : Vectorad , __magic_name__ : Vectorad ) -> Vectorad: """simple docstring""" lowercase__ = ab[1] * ac[2] - ab[2] * ac[1] # *i lowercase__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j lowercase__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def UpperCamelCase ( __magic_name__ : Vectorad , __magic_name__ : int ) -> bool: """simple docstring""" return tuple(round(_UpperCAmelCase , _UpperCAmelCase ) for x in vector ) == (0, 0, 0) def UpperCamelCase ( __magic_name__ : Pointad , __magic_name__ : Pointad , __magic_name__ : Pointad , __magic_name__ : int = 10 ) -> bool: """simple docstring""" lowercase__ = create_vector(_UpperCAmelCase , _UpperCAmelCase ) lowercase__ = create_vector(_UpperCAmelCase , _UpperCAmelCase ) return is_zero_vector(get_ad_vectors_cross(_UpperCAmelCase , _UpperCAmelCase ) , _UpperCAmelCase )
350
def UpperCamelCase ( __magic_name__ : str ) -> List[str]: # noqa: E741 """simple docstring""" lowercase__ = len(__magic_name__ ) lowercase__ = 0 lowercase__ = [0] * n lowercase__ = [False] * n lowercase__ = [False] * n def dfs(__magic_name__ : str , __magic_name__ : List[str] , __magic_name__ : str , __magic_name__ : Any ): if parent == root: out_edge_count += 1 lowercase__ = True lowercase__ = at for to in l[at]: if to == parent: pass elif not visited[to]: lowercase__ = dfs(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) lowercase__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: lowercase__ = True # AP found via cycle if at == low[to]: lowercase__ = True else: lowercase__ = min(low[at] , __magic_name__ ) return out_edge_count for i in range(__magic_name__ ): if not visited[i]: lowercase__ = 0 lowercase__ = dfs(__magic_name__ , __magic_name__ , -1 , __magic_name__ ) lowercase__ = out_edge_count > 1 for x in range(len(__magic_name__ ) ): if is_art[x] is True: print(__magic_name__ ) # Adjacency list of graph A : List[str] = { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], } compute_ap(data)
146
0
'''simple docstring''' from collections.abc import Sequence def _A ( A__ , A__ ): """simple docstring""" return sum(c * (x**i) for i, c in enumerate(A__ ) ) def _A ( A__ , A__ ): """simple docstring""" __lowercase = 0.0 for coeff in reversed(A__ ): __lowercase = result * x + coeff return result if __name__ == "__main__": lowerCAmelCase__ = (0.0, 0.0, 5.0, 9.3, 7.0) lowerCAmelCase__ = 10.0 print(evaluate_poly(poly, x)) print(horner(poly, x))
104
'''simple docstring''' import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL lowerCAmelCase__ = version.parse(version.parse(torch.__version__).base_version) < version.parse('''1.11''') def _A ( A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__=False , ): """simple docstring""" output_path.parent.mkdir(parents=A__ , exist_ok=A__ ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( A__ , A__ , f=output_path.as_posix() , input_names=A__ , output_names=A__ , dynamic_axes=A__ , do_constant_folding=A__ , use_external_data_format=A__ , enable_onnx_checker=A__ , opset_version=A__ , ) else: export( A__ , A__ , f=output_path.as_posix() , input_names=A__ , output_names=A__ , dynamic_axes=A__ , do_constant_folding=A__ , opset_version=A__ , ) @torch.no_grad() def _A ( A__ , A__ , A__ , A__ = False ): """simple docstring""" __lowercase = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): __lowercase = '''cuda''' elif fpaa and not torch.cuda.is_available(): raise ValueError('''`float16` model export is only supported on GPUs with CUDA''' ) else: __lowercase = '''cpu''' __lowercase = Path(A__ ) # VAE DECODER __lowercase = AutoencoderKL.from_pretrained(model_path + '''/vae''' ) __lowercase = vae_decoder.config.latent_channels # forward only through the decoder part __lowercase = vae_decoder.decode onnx_export( A__ , model_args=( torch.randn(1 , A__ , 25 , 25 ).to(device=A__ , dtype=A__ ), False, ) , output_path=output_path / '''vae_decoder''' / '''model.onnx''' , ordered_input_names=['''latent_sample''', '''return_dict'''] , output_names=['''sample'''] , dynamic_axes={ '''latent_sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=A__ , ) del vae_decoder if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument( '''--model_path''', type=str, required=True, help='''Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).''', ) parser.add_argument('''--output_path''', type=str, required=True, help='''Path to the output model.''') parser.add_argument( '''--opset''', default=14, type=int, help='''The version of the ONNX operator set to use.''', ) parser.add_argument('''--fp16''', action='''store_true''', default=False, help='''Export the models in `float16` mode''') lowerCAmelCase__ = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('''SD: Done: ONNX''')
104
1
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase_ ( __snake_case , __snake_case ): @register_to_config def __init__( self , lowercase_ , lowercase_ = None , lowercase_ = None ): super().__init__() _snake_case : Dict = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" _snake_case : int = torch.zeros(lowercase_ , lowercase_ ) else: _snake_case : Tuple = None _snake_case : List[str] = torch.nn.Parameter(lowercase_ ) class lowercase_ ( __snake_case ): _lowerCamelCase = 42 _lowerCamelCase = 42 _lowerCamelCase = 42 _lowerCamelCase = 42 _lowerCamelCase = 42 _lowerCamelCase = 42 def __init__( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , ): super().__init__() self.register_modules( vqvae=lowercase_ , transformer=lowercase_ , text_encoder=lowercase_ , tokenizer=lowercase_ , scheduler=lowercase_ , learned_classifier_free_sampling_embeddings=lowercase_ , ) def UpperCamelCase ( self , lowercase_ , lowercase_ , lowercase_ ): _snake_case : Tuple = len(lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else 1 # get prompt text embeddings _snake_case : Tuple = self.tokenizer( lowercase_ , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) _snake_case : Optional[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: _snake_case : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" f""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) _snake_case : Tuple = text_input_ids[:, : self.tokenizer.model_max_length] _snake_case : List[str] = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 _snake_case : Optional[int] = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=lowercase_ ) # duplicate text embeddings for each generation per prompt _snake_case : Optional[int] = prompt_embeds.repeat_interleave(lowercase_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: _snake_case : Optional[Any] = self.learned_classifier_free_sampling_embeddings.embeddings _snake_case : Optional[int] = negative_prompt_embeds.unsqueeze(0 ).repeat(lowercase_ , 1 , 1 ) else: _snake_case : Optional[int] = [''] * batch_size _snake_case : Optional[int] = text_input_ids.shape[-1] _snake_case : List[Any] = self.tokenizer( lowercase_ , padding="max_length" , max_length=lowercase_ , truncation=lowercase_ , return_tensors="pt" , ) _snake_case : Dict = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings _snake_case : Optional[int] = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=lowercase_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method _snake_case : List[str] = negative_prompt_embeds.shape[1] _snake_case : int = negative_prompt_embeds.repeat(1 , lowercase_ , 1 ) _snake_case : Any = negative_prompt_embeds.view(batch_size * num_images_per_prompt , lowercase_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _snake_case : str = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , lowercase_ , lowercase_ = 100 , lowercase_ = 5.0 , lowercase_ = 1.0 , lowercase_ = 1 , lowercase_ = None , lowercase_ = None , lowercase_ = "pil" , lowercase_ = True , lowercase_ = None , lowercase_ = 1 , ): if isinstance(lowercase_ , lowercase_ ): _snake_case : str = 1 elif isinstance(lowercase_ , lowercase_ ): _snake_case : List[Any] = len(lowercase_ ) else: raise ValueError(f"""`prompt` has to be of type `str` or `list` but is {type(lowercase_ )}""" ) _snake_case : str = batch_size * num_images_per_prompt _snake_case : List[Any] = guidance_scale > 1.0 _snake_case : List[str] = self._encode_prompt(lowercase_ , lowercase_ , lowercase_ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(lowercase_ , lowercase_ ) or callback_steps <= 0) ): raise ValueError( f"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" f""" {type(lowercase_ )}.""" ) # get the initial completely masked latents unless the user supplied it _snake_case : Tuple = (batch_size, self.transformer.num_latent_pixels) if latents is None: _snake_case : Tuple = self.transformer.num_vector_embeds - 1 _snake_case : List[Any] = torch.full(lowercase_ , lowercase_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( "Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0," f""" {self.transformer.num_vector_embeds - 1} (inclusive).""" ) _snake_case : List[str] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(lowercase_ , device=self.device ) _snake_case : int = self.scheduler.timesteps.to(self.device ) _snake_case : Tuple = latents for i, t in enumerate(self.progress_bar(lowercase_ ) ): # expand the sample if we are doing classifier free guidance _snake_case : Optional[Any] = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` _snake_case : str = self.transformer(lowercase_ , encoder_hidden_states=lowercase_ , timestep=lowercase_ ).sample if do_classifier_free_guidance: _snake_case : str = model_output.chunk(2 ) _snake_case : List[Any] = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(lowercase_ , dim=1 , keepdim=lowercase_ ) _snake_case : Dict = self.truncate(lowercase_ , lowercase_ ) # remove `log(0)`'s (`-inf`s) _snake_case : Optional[int] = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 _snake_case : Any = self.scheduler.step(lowercase_ , timestep=lowercase_ , sample=lowercase_ , generator=lowercase_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(lowercase_ , lowercase_ , lowercase_ ) _snake_case : Optional[int] = self.vqvae.config.vq_embed_dim _snake_case : Dict = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) _snake_case : Optional[int] = self.vqvae.quantize.get_codebook_entry(lowercase_ , shape=lowercase_ ) _snake_case : int = self.vqvae.decode(lowercase_ , force_not_quantize=lowercase_ ).sample _snake_case : Any = (image / 2 + 0.5).clamp(0 , 1 ) _snake_case : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _snake_case : Union[str, Any] = self.numpy_to_pil(lowercase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowercase_ ) def UpperCamelCase ( self , lowercase_ , lowercase_ ): _snake_case : int = torch.sort(lowercase_ , 1 , descending=lowercase_ ) _snake_case : int = torch.exp(lowercase_ ) _snake_case : int = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out _snake_case : List[str] = torch.full_like(keep_mask[:, 0:1, :] , lowercase_ ) _snake_case : Dict = torch.cat((all_true, keep_mask) , dim=1 ) _snake_case : List[Any] = keep_mask[:, :-1, :] _snake_case : str = keep_mask.gather(1 , indices.argsort(1 ) ) _snake_case : List[str] = log_p_x_0.clone() _snake_case : List[str] = -torch.inf # -inf = log(0) return rv
362
def snake_case () -> Dict: '''simple docstring''' _snake_case : List[str] = 0 for i in range(1 , 1_001 ): total += i**i return str(__lowercase )[-10:] if __name__ == "__main__": print(solution())
284
0
'''simple docstring''' from __future__ import annotations import time a_ : List[Any] = list[tuple[int, int]] a_ : List[Any] = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] a_ : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class snake_case : """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = pos_x lowerCamelCase_ = pos_y lowerCamelCase_ = (pos_y, pos_x) lowerCamelCase_ = goal_x lowerCamelCase_ = goal_y lowerCamelCase_ = parent class snake_case : """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = Node(start[1] , start[0] , goal[1] , goal[0] , UpperCamelCase ) lowerCamelCase_ = Node(goal[1] , goal[0] , goal[1] , goal[0] , UpperCamelCase ) lowerCamelCase_ = [self.start] lowerCamelCase_ = False def snake_case ( self ): """simple docstring""" while self.node_queue: lowerCamelCase_ = self.node_queue.pop(0 ) if current_node.pos == self.target.pos: lowerCamelCase_ = True return self.retrace_path(UpperCamelCase ) lowerCamelCase_ = self.get_successors(UpperCamelCase ) for node in successors: self.node_queue.append(UpperCamelCase ) if not self.reached: return [self.start.pos] return None def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = [] for action in delta: lowerCamelCase_ = parent.pos_x + action[1] lowerCamelCase_ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(UpperCamelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(UpperCamelCase , UpperCamelCase , self.target.pos_y , self.target.pos_x , UpperCamelCase ) ) return successors def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = node lowerCamelCase_ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) lowerCamelCase_ = current_node.parent path.reverse() return path class snake_case : """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = BreadthFirstSearch(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = BreadthFirstSearch(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = False def snake_case ( self ): """simple docstring""" while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: lowerCamelCase_ = self.fwd_bfs.node_queue.pop(0 ) lowerCamelCase_ = self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: lowerCamelCase_ = True return self.retrace_bidirectional_path( UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = current_bwd_node lowerCamelCase_ = current_fwd_node lowerCamelCase_ = { self.fwd_bfs: self.fwd_bfs.get_successors(UpperCamelCase ), self.bwd_bfs: self.bwd_bfs.get_successors(UpperCamelCase ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(UpperCamelCase ) if not self.reached: return [self.fwd_bfs.start.pos] return None def snake_case ( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = self.fwd_bfs.retrace_path(UpperCamelCase ) lowerCamelCase_ = self.bwd_bfs.retrace_path(UpperCamelCase ) bwd_path.pop() bwd_path.reverse() lowerCamelCase_ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() a_ : List[str] = (0, 0) a_ : List[Any] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) a_ : Tuple = time.time() a_ : int = BreadthFirstSearch(init, goal) a_ : Dict = bfs.search() a_ : Dict = time.time() - start_bfs_time print("""Unidirectional BFS computation time : """, bfs_time) a_ : int = time.time() a_ : Optional[Any] = BidirectionalBreadthFirstSearch(init, goal) a_ : Any = bd_bfs.search() a_ : Dict = time.time() - start_bd_bfs_time print("""Bidirectional BFS computation time : """, bd_bfs_time)
55
'''simple docstring''' 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, ) __lowercase : Any = logging.get_logger(__name__) __lowercase : Any = 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'''), ] ) __lowercase : List[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowercase_ ( _lowercase ) -> List[Any]: '''simple docstring''' for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: lowerCamelCase_ : Dict = model_type_to_module_name(_lowercase ) lowerCamelCase_ : Any = importlib.import_module(F""".{module_name}""" , '''transformers.models''' ) try: return getattr(_lowercase , _lowercase ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(_lowercase , '''__name__''' , _lowercase ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. lowerCamelCase_ : Optional[Any] = importlib.import_module('''transformers''' ) if hasattr(_lowercase , _lowercase ): return getattr(_lowercase , _lowercase ) return None def lowercase_ ( _lowercase , _lowercase = None , _lowercase = False , _lowercase = False , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = False , **_lowercase , ) -> List[str]: '''simple docstring''' lowerCamelCase_ : Optional[int] = get_file_from_repo( _lowercase , _lowercase , cache_dir=_lowercase , force_download=_lowercase , resume_download=_lowercase , proxies=_lowercase , use_auth_token=_lowercase , revision=_lowercase , local_files_only=_lowercase , ) 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(_lowercase , encoding='''utf-8''' ) as reader: return json.load(_lowercase ) class __lowercase : def __init__(self ): 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 UpperCAmelCase__ (cls , A , **A ): lowerCamelCase_ : Optional[Any] = kwargs.pop('''config''' , A ) lowerCamelCase_ : Union[str, Any] = kwargs.pop('''trust_remote_code''' , A ) lowerCamelCase_ : List[Any] = True lowerCamelCase_, lowerCamelCase_ : List[Any] = FeatureExtractionMixin.get_feature_extractor_dict(A , **A ) lowerCamelCase_ : Tuple = config_dict.get('''feature_extractor_type''' , A ) lowerCamelCase_ : List[Any] = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): lowerCamelCase_ : Optional[Any] = 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 ): lowerCamelCase_ : List[str] = AutoConfig.from_pretrained(A , **A ) # It could be in `config.feature_extractor_type`` lowerCamelCase_ : Union[str, Any] = getattr(A , '''feature_extractor_type''' , A ) if hasattr(A , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: lowerCamelCase_ : Optional[int] = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: lowerCamelCase_ : Any = feature_extractor_class_from_name(A ) lowerCamelCase_ : Optional[int] = feature_extractor_auto_map is not None lowerCamelCase_ : Optional[Any] = feature_extractor_class is not None or type(A ) in FEATURE_EXTRACTOR_MAPPING lowerCamelCase_ : int = resolve_trust_remote_code( A , A , A , A ) if has_remote_code and trust_remote_code: lowerCamelCase_ : Any = get_class_from_dynamic_module( A , A , **A ) lowerCamelCase_ : List[Any] = 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: lowerCamelCase_ : Optional[int] = 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 UpperCAmelCase__ (A , A ): FEATURE_EXTRACTOR_MAPPING.register(A , A )
318
0
'''simple docstring''' import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model __snake_case = '''0.12''' # assumed parallelism: 8 if is_torch_available(): import torch def a ( __a , __a , __a=None ) -> Optional[int]: '''simple docstring''' if rng is None: UpperCamelCase__ :Any = random.Random() UpperCamelCase__ :Optional[Any] = 1 for dim in shape: total_dims *= dim UpperCamelCase__ :Union[str, Any] = [] for _ in range(__a ): values.append(rng.randint(0 , vocab_size - 1 ) ) UpperCamelCase__ :str = np.array(__a , dtype=jnp.intaa ).reshape(__a ) return output def a ( __a , __a=None ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ :Optional[int] = ids_tensor(__a , vocab_size=2 , rng=__a ) # make sure that at least one token is attended to for each batch UpperCamelCase__ :Dict = 1 return attn_mask @require_flax class lowercase : """simple docstring""" _a = None _a = () def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :Optional[Any] = inputs['''input_ids'''].shape[-1] // 2 UpperCamelCase__ :Tuple = inputs['''input_ids'''][:max_batch_size, :sequence_length] UpperCamelCase__ :Dict = jnp.ones_like(UpperCamelCase_ ) UpperCamelCase__ :str = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens UpperCamelCase__ :Any = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` UpperCamelCase__ :Optional[int] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = self._get_input_ids_and_config() UpperCamelCase__ :List[Any] = False UpperCamelCase__ :Optional[Any] = max_length UpperCamelCase__ :Any = 0 for model_class in self.all_generative_model_classes: UpperCamelCase__ :str = model_class(UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCamelCase__ :Any = getattr(UpperCamelCase_ , UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = pt_model_class(UpperCamelCase_ ).eval() UpperCamelCase__ :Union[str, Any] = load_flax_weights_in_pytorch_model(UpperCamelCase_ , flax_model.params ) UpperCamelCase__ :Dict = flax_model.generate(UpperCamelCase_ ).sequences UpperCamelCase__ :str = pt_model.generate(torch.tensor(UpperCamelCase_ , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: UpperCamelCase__ :List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[int] = self._get_input_ids_and_config() UpperCamelCase__ :str = False UpperCamelCase__ :str = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ :List[str] = model_class(UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :Tuple = jit(model.generate ) UpperCamelCase__ :Dict = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self._get_input_ids_and_config() UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :Tuple = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ :Tuple = model_class(UpperCamelCase_ ) UpperCamelCase__ :str = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :Any = jit(model.generate ) UpperCamelCase__ :Union[str, Any] = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self._get_input_ids_and_config() UpperCamelCase__ :List[str] = False UpperCamelCase__ :str = max_length UpperCamelCase__ :int = 2 for model_class in self.all_generative_model_classes: UpperCamelCase__ :Any = model_class(UpperCamelCase_ ) UpperCamelCase__ :Dict = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :int = jit(model.generate ) UpperCamelCase__ :List[str] = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = self._get_input_ids_and_config() UpperCamelCase__ :Dict = False UpperCamelCase__ :Optional[int] = max_length UpperCamelCase__ :int = 2 UpperCamelCase__ :Optional[Any] = 2 for model_class in self.all_generative_model_classes: UpperCamelCase__ :str = model_class(UpperCamelCase_ ) UpperCamelCase__ :List[str] = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self._get_input_ids_and_config() UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = max_length UpperCamelCase__ :Union[str, Any] = 0.8 UpperCamelCase__ :Optional[int] = 10 UpperCamelCase__ :str = 0.3 UpperCamelCase__ :Optional[Any] = 1 UpperCamelCase__ :Union[str, Any] = 8 UpperCamelCase__ :Optional[Any] = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ :Optional[int] = model_class(UpperCamelCase_ ) UpperCamelCase__ :int = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = jit(model.generate ) UpperCamelCase__ :Tuple = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self._get_input_ids_and_config() UpperCamelCase__ :Optional[int] = max_length UpperCamelCase__ :Union[str, Any] = 1 UpperCamelCase__ :Any = 8 UpperCamelCase__ :Union[str, Any] = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ :List[Any] = model_class(UpperCamelCase_ ) UpperCamelCase__ :List[Any] = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :int = jit(model.generate ) UpperCamelCase__ :Optional[Any] = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Any = self._get_input_ids_and_config() UpperCamelCase__ :List[str] = max_length UpperCamelCase__ :Tuple = 2 UpperCamelCase__ :Dict = 1 UpperCamelCase__ :str = 8 UpperCamelCase__ :Dict = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ :List[str] = model_class(UpperCamelCase_ ) UpperCamelCase__ :List[str] = model.generate(UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :List[str] = jit(model.generate ) UpperCamelCase__ :Optional[Any] = jit_generate(UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ :Tuple = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ :Any = False UpperCamelCase__ :Union[str, Any] = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ :str = model_class(UpperCamelCase_ ) UpperCamelCase__ :List[Any] = model.generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :List[str] = jit(model.generate ) UpperCamelCase__ :List[Any] = jit_generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[int] = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ :Optional[int] = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :Union[str, Any] = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ :Tuple = model_class(UpperCamelCase_ ) UpperCamelCase__ :str = model.generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :Dict = jit(model.generate ) UpperCamelCase__ :Optional[Any] = jit_generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Any = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ :List[str] = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ :Optional[int] = model_class(UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = model.generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCamelCase_ ) UpperCamelCase__ :str = jit(model.generate ) UpperCamelCase__ :Any = jit_generate(UpperCamelCase_ , attention_mask=UpperCamelCase_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class lowercase ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert''' ) UpperCamelCase__ :Dict = FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) UpperCamelCase__ :str = '''Hello world''' UpperCamelCase__ :Dict = tokenizer(UpperCamelCase_ , return_tensors='''np''' ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(UpperCamelCase_ , '''do_samples''' ): model.generate(UpperCamelCase_ , do_samples=UpperCamelCase_ ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(UpperCamelCase_ , '''foo''' ): UpperCamelCase__ :List[str] = {'''foo''': '''bar'''} model.generate(UpperCamelCase_ , **UpperCamelCase_ )
219
'''simple docstring''' 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 = { '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''', '''distilbert-base-uncased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''', '''distilbert-base-cased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''', '''distilbert-base-multilingual-cased''': ( '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json''' ), '''distilbert-base-uncased-finetuned-sst-2-english''': ( '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json''' ), } class lowercase ( A__ ): """simple docstring""" _a = 'distilbert' _a = { 'hidden_size': 'dim', 'num_attention_heads': 'n_heads', 'num_hidden_layers': 'n_layers', } def __init__( self , UpperCamelCase_=30522 , UpperCamelCase_=512 , UpperCamelCase_=False , UpperCamelCase_=6 , UpperCamelCase_=12 , UpperCamelCase_=768 , UpperCamelCase_=4 * 768 , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_="gelu" , UpperCamelCase_=0.02 , UpperCamelCase_=0.1 , UpperCamelCase_=0.2 , UpperCamelCase_=0 , **UpperCamelCase_ , ): '''simple docstring''' UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :str = sinusoidal_pos_embds UpperCamelCase__ :Any = n_layers UpperCamelCase__ :str = n_heads UpperCamelCase__ :Tuple = dim UpperCamelCase__ :str = hidden_dim UpperCamelCase__ :Dict = dropout UpperCamelCase__ :int = attention_dropout UpperCamelCase__ :Optional[Any] = activation UpperCamelCase__ :Optional[int] = initializer_range UpperCamelCase__ :Union[str, Any] = qa_dropout UpperCamelCase__ :Dict = seq_classif_dropout super().__init__(**UpperCamelCase_ , pad_token_id=UpperCamelCase_ ) class lowercase ( A__ ): """simple docstring""" @property def lowerCAmelCase__ ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase__ :str = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCamelCase__ :str = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
219
1
"""simple docstring""" # 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 __lowerCamelCase = { "configuration_mgp_str": ["MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP", "MgpstrConfig"], "processing_mgp_str": ["MgpstrProcessor"], "tokenization_mgp_str": ["MgpstrTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ "MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST", "MgpstrModel", "MgpstrPreTrainedModel", "MgpstrForSceneTextRecognition", ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
221
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __lowerCamelCase = { "configuration_chinese_clip": [ "CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "ChineseCLIPConfig", "ChineseCLIPOnnxConfig", "ChineseCLIPTextConfig", "ChineseCLIPVisionConfig", ], "processing_chinese_clip": ["ChineseCLIPProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ["ChineseCLIPFeatureExtractor"] __lowerCamelCase = ["ChineseCLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ "CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "ChineseCLIPModel", "ChineseCLIPPreTrainedModel", "ChineseCLIPTextModel", "ChineseCLIPVisionModel", ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
221
1
"""simple docstring""" def __SCREAMING_SNAKE_CASE ( lowercase__ ): """simple docstring""" if not isinstance(lowercase__ , lowercase__ ): raise ValueError("Input must be an integer" ) if input_num <= 0: raise ValueError("Input must be positive" ) return sum( divisor for divisor in range(1 , input_num // 2 + 1 ) if input_num % divisor == 0 ) if __name__ == "__main__": import doctest doctest.testmod()
57
"""simple docstring""" import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class __UpperCamelCase ( _A , unittest.TestCase ): SCREAMING_SNAKE_CASE = WavaVecaPhonemeCTCTokenizer SCREAMING_SNAKE_CASE = False def SCREAMING_SNAKE_CASE__ (self : Tuple): super().setUp() A = ( "<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː " "ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː " "ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 " "oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ " "pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ " "yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ " "əʊ S ɡʲ onɡ2 u\" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ " "ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ " "ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ " "uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ " "ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ " "ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ " "ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4" ).split(" ") A = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE)))) A = {"pad_token": "<pad>", "unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>"} A = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE) + "\n") def SCREAMING_SNAKE_CASE__ (self : Any , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : Optional[Any]=2_0 , __SCREAMING_SNAKE_CASE : Any=5): A = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE)) for i in range(len(__SCREAMING_SNAKE_CASE))] A = list(filter(lambda __SCREAMING_SNAKE_CASE: [t[0]] == tokenizer.encode(t[1] , do_phonemize=__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE)) if max_length is not None and len(__SCREAMING_SNAKE_CASE) > max_length: A = toks[:max_length] if min_length is not None and len(__SCREAMING_SNAKE_CASE) < min_length and len(__SCREAMING_SNAKE_CASE) > 0: while len(__SCREAMING_SNAKE_CASE) < min_length: A = toks + toks # toks_str = [t[1] for t in toks] A = [t[0] for t in toks] # Ensure consistency A = tokenizer.decode(__SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE) if " " not in output_txt and len(__SCREAMING_SNAKE_CASE) > 1: A = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE) + " " + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE) ) if with_prefix_space: A = " " + output_txt A = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE) return output_txt, output_ids def SCREAMING_SNAKE_CASE__ (self : List[Any] , **__SCREAMING_SNAKE_CASE : Any): kwargs.update(self.special_tokens_map) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : Optional[Any]): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") # check adding a single token tokenizer.add_tokens("xxx") A = tokenizer("m xxx ɪ" , do_phonemize=__SCREAMING_SNAKE_CASE).input_ids self.assertEqual(__SCREAMING_SNAKE_CASE , [1_3, 3_9_2, 1_7]) # xxx should be last token tokenizer.add_tokens(["aaa", "bbb", "ccc"]) A = tokenizer("m aaa ɪ ccc" , do_phonemize=__SCREAMING_SNAKE_CASE).input_ids self.assertEqual(__SCREAMING_SNAKE_CASE , [1_3, 3_9_3, 1_7, 3_9_5]) # aaa and ccc should be after xxx and 2 after aaa A = tokenizer("maɪ c" , do_phonemize=__SCREAMING_SNAKE_CASE).input_ids self.assertEqual(__SCREAMING_SNAKE_CASE , [3, 2_0_0]) # mai should be <unk> (=3) def SCREAMING_SNAKE_CASE__ (self : Tuple): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") self.assertEqual(__SCREAMING_SNAKE_CASE , "h ə l oʊ h aʊ ɑːɹ j uː") def SCREAMING_SNAKE_CASE__ (self : List[str]): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") self.assertEqual(tokenizer(__SCREAMING_SNAKE_CASE).input_ids , tokenizer(__SCREAMING_SNAKE_CASE , do_phonemize=__SCREAMING_SNAKE_CASE).input_ids) def SCREAMING_SNAKE_CASE__ (self : Any): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") A = tokenizer.decode(tokenizer(__SCREAMING_SNAKE_CASE).input_ids) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : str): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") A = [ [1_1, 5, 1_5, tokenizer.pad_token_id, 1_5, 8, 9_8], [2_4, 2_2, 5, 2_4, 2_2, 5, 7_7], ] A = tokenizer.decode(sample_ids[0]) A = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , batch_tokens[0]) self.assertEqual(__SCREAMING_SNAKE_CASE , ["k s ɾ ɾ l ɭʲ", "j ð s j ð s oːɹ"]) def SCREAMING_SNAKE_CASE__ (self : List[str]): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token="|") tokenizer.add_tokens("|") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") self.assertEqual(__SCREAMING_SNAKE_CASE , "h ə l oʊ | h aʊ | ɑːɹ | j uː |") def SCREAMING_SNAKE_CASE__ (self : str): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token="|") tokenizer.add_tokens("|") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") self.assertEqual(tokenizer(__SCREAMING_SNAKE_CASE).input_ids , tokenizer(__SCREAMING_SNAKE_CASE , do_phonemize=__SCREAMING_SNAKE_CASE).input_ids) def SCREAMING_SNAKE_CASE__ (self : Optional[int]): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token="|") tokenizer.add_tokens("|") # fmt: off A = [ [1_1, 5, 1_5, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 1_5, 8, tokenizer.word_delimiter_token_id, 9_8], [tokenizer.word_delimiter_token_id, 2_4, 2_2, tokenizer.word_delimiter_token_id, 5, 2_4, 2_2, 5, 7_7], ] # fmt: on # decode with word_del_token filter A = tokenizer.decode(sample_ids[0]) A = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , batch_tokens[0]) self.assertEqual(__SCREAMING_SNAKE_CASE , ["k s ɾ ɾ l ɭʲ", "j ð s j ð s oːɹ"]) # decode with no word_del_token filter A = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=__SCREAMING_SNAKE_CASE) A = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , filter_word_delimiter_token=__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , batch_tokens[0]) self.assertEqual(__SCREAMING_SNAKE_CASE , ["k s ɾ | ɾ l | ɭʲ", "| j ð | s j ð s oːɹ"]) def SCREAMING_SNAKE_CASE__ (self : Dict): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token="|") tokenizer.add_tokens("|") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") A = tokenizer.decode(tokenizer(__SCREAMING_SNAKE_CASE).input_ids , filter_word_delimiter_token=__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : List[Any]): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token="|") tokenizer.add_tokens("|") A = "Hello how are you" A = tokenizer.phonemize(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us") A = tokenizer.decode(tokenizer(__SCREAMING_SNAKE_CASE).input_ids , filter_word_delimiter_token=__SCREAMING_SNAKE_CASE) self.assertEqual(" ".join([p.strip() for p in phonemes.split(" |")]).strip() , __SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : Dict): A = self.tokenizer_class.from_pretrained( "facebook/wav2vec2-lv-60-espeak-cv-ft" , word_delimiter_token=__SCREAMING_SNAKE_CASE) A = "Hello how are you" A = tokenizer(__SCREAMING_SNAKE_CASE , phonemizer_lang="en-us").input_ids A = tokenizer(__SCREAMING_SNAKE_CASE , phonemizer_lang="fr-fr").input_ids self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) A = tokenizer.decode(__SCREAMING_SNAKE_CASE) A = tokenizer.decode(__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , "h ə l oʊ h aʊ ɑːɹ j uː") self.assertEqual(__SCREAMING_SNAKE_CASE , "ɛ l o h aʊ a ʁ j u") def SCREAMING_SNAKE_CASE__ (self : Union[str, Any]): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") A = "Hello how Are you" A = "hello how are you" A = tokenizer(__SCREAMING_SNAKE_CASE).input_ids A = tokenizer(__SCREAMING_SNAKE_CASE).input_ids self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : Union[str, Any]): A = self.tokenizer_class.from_pretrained("facebook/wav2vec2-lv-60-espeak-cv-ft") tokenizer.add_tokens(["!", "?"]) tokenizer.add_special_tokens({"cls_token": "$$$"}) # fmt: off A = [ [1_1, 5, 1_5, tokenizer.pad_token_id, 1_5, 8, 9_8, 3_9_2, 3_9_2, 3_9_3, 3_9_2, 3_9_2, 3_9_3, 3_9_4, 3_9_4], [2_4, 2_2, 5, 2_4, 2_2, 5, 7_7, tokenizer.pad_token_id, 3_9_4, 3_9_4], ] # fmt: on A = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , ["k s ɾ ɾ l ɭʲ!?!? $$$", "j ð s j ð s oːɹ $$$"]) @staticmethod def SCREAMING_SNAKE_CASE__ (__SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Union[str, Any]): A = [d[key] for d in offsets] return retrieved_list def SCREAMING_SNAKE_CASE__ (self : Optional[Any]): A = self.get_tokenizer(word_delimiter_token="|") tokenizer.add_tokens("|") # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A = [1_1, 5, 5, 5, 1_5, 1_5, tokenizer.pad_token_id, 1_5, 1_5, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 1_5, 8, 8, 8, tokenizer.word_delimiter_token_id, 9_8] # fmt: on A = tokenizer.decode(__SCREAMING_SNAKE_CASE , output_char_offsets=__SCREAMING_SNAKE_CASE , filter_word_delimiter_token=__SCREAMING_SNAKE_CASE) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys()) , 2) self.assertTrue("text" in outputs) self.assertTrue("char_offsets" in outputs) self.assertTrue(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)) # check that order of chars is correct and identical for both outputs self.assertEqual(" ".join(self.get_from_offsets(outputs["char_offsets"] , "char")) , outputs.text) self.assertListEqual( self.get_from_offsets(outputs["char_offsets"] , "char") , ["k", "s", "ɾ", "ɾ", "|", "ɾ", "l", "|", "ɭʲ"]) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs["char_offsets"] , "start_offset") , [0, 1, 4, 7, 9, 1_1, 1_2, 1_5, 1_6]) self.assertListEqual( self.get_from_offsets(outputs["char_offsets"] , "end_offset") , [1, 4, 6, 9, 1_0, 1_2, 1_5, 1_6, 1_7]) def SCREAMING_SNAKE_CASE__ (self : Any): A = self.get_tokenizer(word_delimiter_token="|") def check_list_tuples_equal(__SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[Any]): self.assertTrue(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)) self.assertTrue(isinstance(outputs_list[0] , __SCREAMING_SNAKE_CASE)) # transform list to ModelOutput A = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]}) self.assertListEqual(outputs_batch["text"] , outputs_batch_a["text"]) def recursive_check(__SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any]): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): [recursive_check(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) for la, la in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) if "char_offsets" in outputs_batch: recursive_check(outputs_batch["char_offsets"] , outputs_batch_a["char_offsets"]) # fmt: off A = [ [1_1, 5, 1_5, tokenizer.pad_token_id, 1_5, 4, 8, 9_8, 3_2, 3_2, 3_2, 3_2, 4, 3_3, tokenizer.word_delimiter_token_id, 3_2, 3_2, 3_3, 3_4, 3_4], [2_4, 2_2, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 2_4, 2_2, 2_2, 2_2, 4, 5, 7_7, tokenizer.pad_token_id, 2_2, 2_2, 4, 3_4, 3_4, 3_4, 3_4], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , output_char_offsets=__SCREAMING_SNAKE_CASE) A = [tokenizer.decode(__SCREAMING_SNAKE_CASE , output_char_offsets=__SCREAMING_SNAKE_CASE) for ids in sample_ids] check_list_tuples_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) @unittest.skip("Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes") def SCREAMING_SNAKE_CASE__ (self : Optional[int]): pass @unittest.skip("Wav2Vec2PhonemeTokenizer always puts spaces between phonemes") def SCREAMING_SNAKE_CASE__ (self : Dict): pass @unittest.skip("encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency") def SCREAMING_SNAKE_CASE__ (self : str): pass @unittest.skip("Wav2Vec2PhonemeModel has no max model length => no testing") def SCREAMING_SNAKE_CASE__ (self : Optional[int]): pass def SCREAMING_SNAKE_CASE__ (self : List[str]): A = self.get_tokenizers(do_lower_case=__SCREAMING_SNAKE_CASE) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): A = tokenizer.vocab_size A = len(__SCREAMING_SNAKE_CASE) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A = ["aaaaa bbbbbb", "cccccccccdddddddd"] A = tokenizer.add_tokens(__SCREAMING_SNAKE_CASE) A = tokenizer.vocab_size A = len(__SCREAMING_SNAKE_CASE) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , len(__SCREAMING_SNAKE_CASE)) self.assertEqual(__SCREAMING_SNAKE_CASE , all_size + len(__SCREAMING_SNAKE_CASE)) A = tokenizer.encode("aaaaa bbbbbb low cccccccccdddddddd l" , add_special_tokens=__SCREAMING_SNAKE_CASE) self.assertGreaterEqual(len(__SCREAMING_SNAKE_CASE) , 4) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1) A = {"eos_token": ">>>>|||<||<<|<<", "pad_token": "<<<<<|||>|>>>>|>"} A = tokenizer.add_special_tokens(__SCREAMING_SNAKE_CASE) A = tokenizer.vocab_size A = len(__SCREAMING_SNAKE_CASE) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) self.assertEqual(__SCREAMING_SNAKE_CASE , len(__SCREAMING_SNAKE_CASE)) self.assertEqual(__SCREAMING_SNAKE_CASE , all_size_a + len(__SCREAMING_SNAKE_CASE)) A = tokenizer.encode( ">>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l" , add_special_tokens=__SCREAMING_SNAKE_CASE) self.assertGreaterEqual(len(__SCREAMING_SNAKE_CASE) , 6) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1) self.assertGreater(tokens[0] , tokens[1]) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1) self.assertGreater(tokens[-3] , tokens[-4]) self.assertEqual(tokens[0] , tokenizer.eos_token_id) self.assertEqual(tokens[-3] , tokenizer.pad_token_id) @unittest.skip("The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.") def SCREAMING_SNAKE_CASE__ (self : List[str]): pass @unittest.skip("The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.") def SCREAMING_SNAKE_CASE__ (self : List[Any]): pass def SCREAMING_SNAKE_CASE__ (self : Optional[int]): # The default common tokenizer tests assumes that the output of `convert_tokens_to_string` is a string which # is not the case for Wav2Vec2PhonemeCTCTokenizer. A = self.get_tokenizers(fast=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): A = ["ð", "ɪ", "s", "ɪ", "z", "ɐ", "t", "ɛ", "k", "s", "t"] A = tokenizer.convert_tokens_to_string(__SCREAMING_SNAKE_CASE) self.assertIsInstance(output["text"] , __SCREAMING_SNAKE_CASE)
57
1
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version lowerCAmelCase = get_logger(__name__) class _a : _lowercase : Any = '''dummy_data''' _lowercase : List[str] = '''datasets''' _lowercase : Optional[int] = False def __init__( self: Union[str, Any] , UpperCamelCase_: str , UpperCamelCase_: str , UpperCamelCase_: Union[Version, str] , UpperCamelCase_: Optional[str] = None , UpperCamelCase_: bool = False , UpperCamelCase_: bool = True , UpperCamelCase_: Optional[List[Callable]] = None , ) -> Union[str, Any]: """simple docstring""" lowercase__ = 0 lowercase__ = dataset_name lowercase__ = cache_dir lowercase__ = use_local_dummy_data lowercase__ = config # download_callbacks take a single url as input lowercase__ = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root lowercase__ = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general lowercase__ = str(UpperCamelCase_ ) # to be downloaded lowercase__ = None lowercase__ = None @property def lowerCamelCase_ ( self: int ) -> List[str]: """simple docstring""" if self._dummy_file is None: lowercase__ = self.download_dummy_data() return self._dummy_file @property def lowerCamelCase_ ( self: Any ) -> Optional[Any]: """simple docstring""" if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('''dummy''' , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join('''dummy''' , self.version_name ) @property def lowerCamelCase_ ( self: List[str] ) -> Union[str, Any]: """simple docstring""" return os.path.join(self.dummy_data_folder , '''dummy_data.zip''' ) def lowerCamelCase_ ( self: Optional[Any] ) -> Union[str, Any]: """simple docstring""" lowercase__ = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) lowercase__ = cached_path( UpperCamelCase_ , cache_dir=self.cache_dir , extract_compressed_file=UpperCamelCase_ , force_extract=UpperCamelCase_ ) return os.path.join(UpperCamelCase_ , self.dummy_file_name ) @property def lowerCamelCase_ ( self: Tuple ) -> Optional[int]: """simple docstring""" return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def lowerCamelCase_ ( self: Dict ) -> Any: """simple docstring""" if self._bucket_url is None: lowercase__ = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , '''/''' ) ) return self._bucket_url @property def lowerCamelCase_ ( self: Optional[int] ) -> Union[str, Any]: """simple docstring""" if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , '''/''' ).split('''/''' )[:-1] ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase_: Optional[Any] , *UpperCamelCase_: Any ) -> List[str]: """simple docstring""" if self.load_existing_dummy_data: # dummy data is downloaded and tested lowercase__ = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned lowercase__ = self.dummy_file_name # special case when data_url is a dict if isinstance(UpperCamelCase_ , UpperCamelCase_ ): return self.create_dummy_data_dict(UpperCamelCase_ , UpperCamelCase_ ) elif isinstance(UpperCamelCase_ , (list, tuple) ): return self.create_dummy_data_list(UpperCamelCase_ , UpperCamelCase_ ) else: return self.create_dummy_data_single(UpperCamelCase_ , UpperCamelCase_ ) def lowerCamelCase_ ( self: str , UpperCamelCase_: str , *UpperCamelCase_: Tuple ) -> Any: """simple docstring""" return self.download_and_extract(UpperCamelCase_ ) def lowerCamelCase_ ( self: Dict , UpperCamelCase_: List[str] , UpperCamelCase_: List[Any] ) -> Any: """simple docstring""" return self.download_and_extract(UpperCamelCase_ ) def lowerCamelCase_ ( self: List[Any] , UpperCamelCase_: List[Any] , *UpperCamelCase_: Optional[int] , **UpperCamelCase_: List[Any] ) -> List[Any]: """simple docstring""" return path def lowerCamelCase_ ( self: Dict ) -> List[Any]: """simple docstring""" return {} def lowerCamelCase_ ( self: str , UpperCamelCase_: int , UpperCamelCase_: Union[str, Any] ) -> List[str]: """simple docstring""" lowercase__ = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(UpperCamelCase_ , UpperCamelCase_ ): for single_url in single_urls: download_callback(UpperCamelCase_ ) else: lowercase__ = single_urls download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(UpperCamelCase_ , UpperCamelCase_ ): lowercase__ = [os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(Path(UpperCamelCase_ ).name ) ) for x in single_urls] else: lowercase__ = single_urls lowercase__ = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(Path(UpperCamelCase_ ).name ) ) lowercase__ = value # make sure that values are unique if all(isinstance(UpperCamelCase_ , UpperCamelCase_ ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique lowercase__ = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def lowerCamelCase_ ( self: Tuple , UpperCamelCase_: Optional[int] , UpperCamelCase_: List[str] ) -> Dict: """simple docstring""" lowercase__ = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one lowercase__ = all(bool(re.findall('''[0-9]{3,}-of-[0-9]{3,}''' , UpperCamelCase_ ) ) for url in data_url ) lowercase__ = all( url.startswith('''https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed''' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): lowercase__ = [data_url[0]] * len(UpperCamelCase_ ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowercase__ = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(single_url.split('''/''' )[-1] ) ) dummy_data_list.append(UpperCamelCase_ ) return dummy_data_list def lowerCamelCase_ ( self: str , UpperCamelCase_: Dict , UpperCamelCase_: List[str] ) -> Dict: """simple docstring""" for download_callback in self.download_callbacks: download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowercase__ = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(data_url.split('''/''' )[-1] ) ) if os.path.exists(UpperCamelCase_ ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def lowerCamelCase_ ( self: Tuple ) -> Optional[Any]: """simple docstring""" pass def lowerCamelCase_ ( self: str ) -> Optional[int]: """simple docstring""" pass def lowerCamelCase_ ( self: List[Any] , UpperCamelCase_: int ) -> int: """simple docstring""" def _iter_archive_members(UpperCamelCase_: Optional[int] ): # this preserves the order of the members inside the ZIP archive lowercase__ = Path(self.dummy_file ).parent lowercase__ = path.relative_to(UpperCamelCase_ ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: lowercase__ = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(UpperCamelCase_ ) lowercase__ = Path(UpperCamelCase_ ) lowercase__ = _iter_archive_members(UpperCamelCase_ ) if self.use_local_dummy_data else path.rglob('''*''' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('''.''', '''__''') ): yield file_path.relative_to(UpperCamelCase_ ).as_posix(), file_path.open('''rb''' ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase_: int ) -> int: """simple docstring""" if not isinstance(UpperCamelCase_ , UpperCamelCase_ ): lowercase__ = [paths] for path in paths: if os.path.isfile(UpperCamelCase_ ): if os.path.basename(UpperCamelCase_ ).startswith(('''.''', '''__''') ): return yield path else: for dirpath, dirnames, filenames in os.walk(UpperCamelCase_ ): if os.path.basename(UpperCamelCase_ ).startswith(('''.''', '''__''') ): continue dirnames.sort() for filename in sorted(UpperCamelCase_ ): if filename.startswith(('''.''', '''__''') ): continue yield os.path.join(UpperCamelCase_ , UpperCamelCase_ )
110
import argparse import os from . import ( ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BART_PRETRAINED_MODEL_ARCHIVE_LIST, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, BartConfig, BertConfig, CamembertConfig, CTRLConfig, DistilBertConfig, DPRConfig, ElectraConfig, FlaubertConfig, GPTaConfig, LayoutLMConfig, LxmertConfig, OpenAIGPTConfig, RobertaConfig, TaConfig, TFAlbertForPreTraining, TFBartForConditionalGeneration, TFBartForSequenceClassification, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFCamembertForMaskedLM, TFCTRLLMHeadModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, TFElectraForPreTraining, TFFlaubertWithLMHeadModel, TFGPTaLMHeadModel, TFLayoutLMForMaskedLM, TFLxmertForPreTraining, TFLxmertVisualFeatureEncoder, TFOpenAIGPTLMHeadModel, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForSequenceClassification, TFTaForConditionalGeneration, TFTransfoXLLMHeadModel, TFWavaVecaModel, TFXLMRobertaForMaskedLM, TFXLMWithLMHeadModel, TFXLNetLMHeadModel, TransfoXLConfig, WavaVecaConfig, WavaVecaModel, XLMConfig, XLMRobertaConfig, XLNetConfig, is_torch_available, load_pytorch_checkpoint_in_tfa_model, ) from .utils import CONFIG_NAME, WEIGHTS_NAME, cached_file, logging if is_torch_available(): import numpy as np import torch from . import ( AlbertForPreTraining, BartForConditionalGeneration, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, CamembertForMaskedLM, CTRLLMHeadModel, DistilBertForMaskedLM, DistilBertForQuestionAnswering, DPRContextEncoder, DPRQuestionEncoder, DPRReader, ElectraForPreTraining, FlaubertWithLMHeadModel, GPTaLMHeadModel, LayoutLMForMaskedLM, LxmertForPreTraining, LxmertVisualFeatureEncoder, OpenAIGPTLMHeadModel, RobertaForMaskedLM, RobertaForSequenceClassification, TaForConditionalGeneration, TransfoXLLMHeadModel, XLMRobertaForMaskedLM, XLMWithLMHeadModel, XLNetLMHeadModel, ) logging.set_verbosity_info() lowerCAmelCase = { 'bart': ( BartConfig, TFBartForConditionalGeneration, TFBartForSequenceClassification, BartForConditionalGeneration, BART_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'bert': ( BertConfig, TFBertForPreTraining, BertForPreTraining, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-base-cased-finetuned-mrpc': ( BertConfig, TFBertForSequenceClassification, BertForSequenceClassification, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'dpr': ( DPRConfig, TFDPRQuestionEncoder, TFDPRContextEncoder, TFDPRReader, DPRQuestionEncoder, DPRContextEncoder, DPRReader, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'gpt2': ( GPTaConfig, TFGPTaLMHeadModel, GPTaLMHeadModel, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlnet': ( XLNetConfig, TFXLNetLMHeadModel, XLNetLMHeadModel, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm': ( XLMConfig, TFXLMWithLMHeadModel, XLMWithLMHeadModel, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm-roberta': ( XLMRobertaConfig, TFXLMRobertaForMaskedLM, XLMRobertaForMaskedLM, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'transfo-xl': ( TransfoXLConfig, TFTransfoXLLMHeadModel, TransfoXLLMHeadModel, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'openai-gpt': ( OpenAIGPTConfig, TFOpenAIGPTLMHeadModel, OpenAIGPTLMHeadModel, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'roberta': ( RobertaConfig, TFRobertaForCausalLM, TFRobertaForMaskedLM, RobertaForMaskedLM, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'layoutlm': ( LayoutLMConfig, TFLayoutLMForMaskedLM, LayoutLMForMaskedLM, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'roberta-large-mnli': ( RobertaConfig, TFRobertaForSequenceClassification, RobertaForSequenceClassification, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'camembert': ( CamembertConfig, TFCamembertForMaskedLM, CamembertForMaskedLM, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'flaubert': ( FlaubertConfig, TFFlaubertWithLMHeadModel, FlaubertWithLMHeadModel, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert': ( DistilBertConfig, TFDistilBertForMaskedLM, DistilBertForMaskedLM, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert-base-distilled-squad': ( DistilBertConfig, TFDistilBertForQuestionAnswering, DistilBertForQuestionAnswering, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert': ( LxmertConfig, TFLxmertForPreTraining, LxmertForPreTraining, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert-visual-feature-encoder': ( LxmertConfig, TFLxmertVisualFeatureEncoder, LxmertVisualFeatureEncoder, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'ctrl': ( CTRLConfig, TFCTRLLMHeadModel, CTRLLMHeadModel, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'albert': ( AlbertConfig, TFAlbertForPreTraining, AlbertForPreTraining, ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 't5': ( TaConfig, TFTaForConditionalGeneration, TaForConditionalGeneration, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'electra': ( ElectraConfig, TFElectraForPreTraining, ElectraForPreTraining, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'wav2vec2': ( WavaVecaConfig, TFWavaVecaModel, WavaVecaModel, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), } def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=True ): """simple docstring""" if model_type not in MODEL_CLASSES: raise ValueError(f'Unrecognized model type, should be one of {list(MODEL_CLASSES.keys() )}.' ) lowercase__ , lowercase__ , lowercase__ , lowercase__ = MODEL_CLASSES[model_type] # Initialise TF model if config_file in aws_config_map: lowercase__ = cached_file(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , force_download=not use_cached_models ) lowercase__ = config_class.from_json_file(SCREAMING_SNAKE_CASE ) lowercase__ = True lowercase__ = True print(f'Building TensorFlow model from configuration: {config}' ) lowercase__ = model_class(SCREAMING_SNAKE_CASE ) # Load weights from tf checkpoint if pytorch_checkpoint_path in aws_config_map.keys(): lowercase__ = cached_file( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , force_download=not use_cached_models ) # Load PyTorch checkpoint in tf2 model: lowercase__ = load_pytorch_checkpoint_in_tfa_model(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if compare_with_pt_model: lowercase__ = tf_model(tf_model.dummy_inputs , training=SCREAMING_SNAKE_CASE ) # build the network lowercase__ = torch.load(SCREAMING_SNAKE_CASE , map_location='''cpu''' ) lowercase__ = pt_model_class.from_pretrained( pretrained_model_name_or_path=SCREAMING_SNAKE_CASE , config=SCREAMING_SNAKE_CASE , state_dict=SCREAMING_SNAKE_CASE ) with torch.no_grad(): lowercase__ = pt_model(**pt_model.dummy_inputs ) lowercase__ = pto[0].numpy() lowercase__ = tfo[0].numpy() lowercase__ = np.amax(np.abs(np_pt - np_tf ) ) print(f'Max absolute difference between models outputs {diff}' ) assert diff <= 2E-2, f'Error, model absolute difference is >2e-2: {diff}' # Save pytorch-model print(f'Save TensorFlow model to {tf_dump_path}' ) tf_model.save_weights(SCREAMING_SNAKE_CASE , save_format='''h5''' ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=False , ): """simple docstring""" if args_model_type is None: lowercase__ = list(MODEL_CLASSES.keys() ) else: lowercase__ = [args_model_type] for j, model_type in enumerate(SCREAMING_SNAKE_CASE , start=1 ): print('''=''' * 1_00 ) print(f' Converting model type {j}/{len(SCREAMING_SNAKE_CASE )}: {model_type}' ) print('''=''' * 1_00 ) if model_type not in MODEL_CLASSES: raise ValueError(f'Unrecognized model type {model_type}, should be one of {list(MODEL_CLASSES.keys() )}.' ) lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ = MODEL_CLASSES[model_type] if model_shortcut_names_or_path is None: lowercase__ = list(aws_model_maps.keys() ) if config_shortcut_names_or_path is None: lowercase__ = model_shortcut_names_or_path for i, (model_shortcut_name, config_shortcut_name) in enumerate( zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , start=1 ): print('''-''' * 1_00 ) if "-squad" in model_shortcut_name or "-mrpc" in model_shortcut_name or "-mnli" in model_shortcut_name: if not only_convert_finetuned_models: print(f' Skipping finetuned checkpoint {model_shortcut_name}' ) continue lowercase__ = model_shortcut_name elif only_convert_finetuned_models: print(f' Skipping not finetuned checkpoint {model_shortcut_name}' ) continue print( f' Converting checkpoint {i}/{len(SCREAMING_SNAKE_CASE )}: {model_shortcut_name} - model_type {model_type}' ) print('''-''' * 1_00 ) if config_shortcut_name in aws_config_map: lowercase__ = cached_file(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , force_download=not use_cached_models ) else: lowercase__ = config_shortcut_name if model_shortcut_name in aws_model_maps: lowercase__ = cached_file(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , force_download=not use_cached_models ) else: lowercase__ = model_shortcut_name if os.path.isfile(SCREAMING_SNAKE_CASE ): lowercase__ = '''converted_model''' convert_pt_checkpoint_to_tf( model_type=SCREAMING_SNAKE_CASE , pytorch_checkpoint_path=SCREAMING_SNAKE_CASE , config_file=SCREAMING_SNAKE_CASE , tf_dump_path=os.path.join(SCREAMING_SNAKE_CASE , model_shortcut_name + '''-tf_model.h5''' ) , compare_with_pt_model=SCREAMING_SNAKE_CASE , ) if remove_cached_files: os.remove(SCREAMING_SNAKE_CASE ) os.remove(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_dump_path', default=None, type=str, required=True, help='Path to the output Tensorflow dump file.' ) parser.add_argument( '--model_type', default=None, type=str, help=( f"""Model type selected in the list of {list(MODEL_CLASSES.keys())}. If not given, will download and """ 'convert all the models from AWS.' ), ) parser.add_argument( '--pytorch_checkpoint_path', default=None, type=str, help=( 'Path to the PyTorch checkpoint path or shortcut name to download from AWS. ' 'If not given, will download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--config_file', default=None, type=str, help=( 'The config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture. If not given and ' '--pytorch_checkpoint_path is not given or is a shortcut name ' 'use the configuration associated to the shortcut name on the AWS' ), ) parser.add_argument( '--compare_with_pt_model', action='store_true', help='Compare Tensorflow and PyTorch model predictions.' ) parser.add_argument( '--use_cached_models', action='store_true', help='Use cached models if possible instead of updating to latest checkpoint versions.', ) parser.add_argument( '--remove_cached_files', action='store_true', help='Remove pytorch models after conversion (save memory when converting in batches).', ) parser.add_argument('--only_convert_finetuned_models', action='store_true', help='Only convert finetuned models.') lowerCAmelCase = parser.parse_args() # if args.pytorch_checkpoint_path is not None: # convert_pt_checkpoint_to_tf(args.model_type.lower(), # args.pytorch_checkpoint_path, # args.config_file if args.config_file is not None else args.pytorch_checkpoint_path, # args.tf_dump_path, # compare_with_pt_model=args.compare_with_pt_model, # use_cached_models=args.use_cached_models) # else: convert_all_pt_checkpoints_to_tf( args.model_type.lower() if args.model_type is not None else None, args.tf_dump_path, model_shortcut_names_or_path=[args.pytorch_checkpoint_path] if args.pytorch_checkpoint_path is not None else None, config_shortcut_names_or_path=[args.config_file] if args.config_file is not None else None, compare_with_pt_model=args.compare_with_pt_model, use_cached_models=args.use_cached_models, remove_cached_files=args.remove_cached_files, only_convert_finetuned_models=args.only_convert_finetuned_models, )
110
1
from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class _SCREAMING_SNAKE_CASE ( A_ ): UpperCAmelCase_ :Optional[int] = "" UpperCAmelCase_ :Optional[int] = "hf-legacy" # "hf://"" is reserved for hffs def __init__( self , __A = None , __A = None , **__A , ) -> Optional[int]: super().__init__(self , **snake_case__ ) lowerCAmelCase_ :List[Any] = repo_info lowerCAmelCase_ :Any = token lowerCAmelCase_ :int = None def __lowerCAmelCase ( self ) -> Any: if self.dir_cache is None: lowerCAmelCase_ :Union[str, Any] = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes lowerCAmelCase_ :Optional[int] = { "name": hf_file.rfilename, "size": None, "type": "file", } self.dir_cache.update( { str(snake_case__ ): {"""name""": str(snake_case__ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def __lowerCAmelCase ( self , __A , __A = "rb" , **__A , ) -> Union[str, Any]: if not isinstance(self.repo_info , snake_case__ ): raise NotImplementedError(f"""Open is only implemented for dataset repositories, but got {self.repo_info}""" ) lowerCAmelCase_ :List[str] = hf_hub_url(self.repo_info.id , snake_case__ , revision=self.repo_info.sha ) return fsspec.open( snake_case__ , mode=snake_case__ , headers=get_authentication_headers_for_url(snake_case__ , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def __lowerCAmelCase ( self , __A , **__A ) -> List[str]: self._get_dirs() lowerCAmelCase_ :int = self._strip_protocol(snake_case__ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(snake_case__ ) def __lowerCAmelCase ( self , __A , __A=False , **__A ) -> Optional[int]: self._get_dirs() lowerCAmelCase_ :Dict = PurePosixPath(path.strip("""/""" ) ) lowerCAmelCase_ :Dict = {} for p, f in self.dir_cache.items(): lowerCAmelCase_ :str = PurePosixPath(p.strip("""/""" ) ) lowerCAmelCase_ :Optional[Any] = p.parent if root == path: lowerCAmelCase_ :Optional[int] = f lowerCAmelCase_ :Optional[Any] = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
359
"""simple docstring""" import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor __UpperCAmelCase = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( A__ ): def __init__( self , *__A , **__A ) -> None: warnings.warn( """The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use CLIPImageProcessor instead.""" , __A , ) super().__init__(*__A , **__A )
1
0
import math def UpperCamelCase () -> None: A__ : Any = input("""Enter message: """ ) A__ : Optional[int] = int(input(f"""Enter key [2-{len(lowercase_ ) - 1}]: """ ) ) A__ : Optional[int] = input("""Encryption/Decryption [e/d]: """ ) if mode.lower().startswith("""e""" ): A__ : List[str] = encrypt_message(lowercase_ , lowercase_ ) elif mode.lower().startswith("""d""" ): A__ : Union[str, Any] = decrypt_message(lowercase_ , lowercase_ ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(f"""Output:\n{text + '|'}""" ) def UpperCamelCase (lowercase_: int , lowercase_: str ) -> str: A__ : Union[str, Any] = [""""""] * key for col in range(lowercase_ ): A__ : Optional[int] = col while pointer < len(lowercase_ ): cipher_text[col] += message[pointer] pointer += key return "".join(lowercase_ ) def UpperCamelCase (lowercase_: int , lowercase_: str ) -> str: A__ : Any = math.ceil(len(lowercase_ ) / key ) A__ : List[str] = key A__ : Optional[Any] = (num_cols * num_rows) - len(lowercase_ ) A__ : Any = [""""""] * num_cols A__ : List[str] = 0 A__ : Tuple = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): A__ : Optional[int] = 0 row += 1 return "".join(lowercase_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
192
def UpperCamelCase (lowercase_: int = 10 ) -> str: if not isinstance(lowercase_ , lowercase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ : List[str] = 10**n A__ : Any = 28433 * (pow(2 , 7830457 , lowercase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f'''{solution(10) = }''')
192
1
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class _a ( __lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : Union[List[PIL.Image.Image], np.ndarray] SCREAMING_SNAKE_CASE_ : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
142
'''simple docstring''' import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint UpperCamelCase_ : int = { '''169M''': 12, '''430M''': 24, '''1B5''': 24, '''3B''': 32, '''7B''': 32, '''14B''': 40, } UpperCamelCase_ : str = { '''169M''': 768, '''430M''': 1024, '''1B5''': 2048, '''3B''': 2560, '''7B''': 4096, '''14B''': 5120, } def __a ( _UpperCamelCase: str ) -> Any: """simple docstring""" _snake_case = list(state_dict.keys() ) for name in state_dict_keys: _snake_case = state_dict.pop(_UpperCamelCase ) # emb -> embedding if name.startswith("emb." ): _snake_case = name.replace("emb." , "embeddings." ) # ln_0 -> pre_ln (only present at block 0) if name.startswith("blocks.0.ln0" ): _snake_case = name.replace("blocks.0.ln0" , "blocks.0.pre_ln" ) # att -> attention _snake_case = re.sub(r"blocks\.(\d+)\.att" , r"blocks.\1.attention" , _UpperCamelCase ) # ffn -> feed_forward _snake_case = re.sub(r"blocks\.(\d+)\.ffn" , r"blocks.\1.feed_forward" , _UpperCamelCase ) # time_mix_k -> time_mix_key and reshape if name.endswith(".time_mix_k" ): _snake_case = name.replace(".time_mix_k" , ".time_mix_key" ) # time_mix_v -> time_mix_value and reshape if name.endswith(".time_mix_v" ): _snake_case = name.replace(".time_mix_v" , ".time_mix_value" ) # time_mix_r -> time_mix_key and reshape if name.endswith(".time_mix_r" ): _snake_case = name.replace(".time_mix_r" , ".time_mix_receptance" ) if name != "head.weight": _snake_case = "rwkv." + name _snake_case = weight return state_dict def __a ( _UpperCamelCase: Any , _UpperCamelCase: List[Any] , _UpperCamelCase: List[Any] , _UpperCamelCase: str=None , _UpperCamelCase: Optional[Any]=None , _UpperCamelCase: List[str]=False , _UpperCamelCase: Dict=None ) -> Dict: """simple docstring""" if tokenizer_file is None: print("No `--tokenizer_file` provided, we will use the default tokenizer." ) _snake_case = 50_277 _snake_case = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b" ) else: _snake_case = PreTrainedTokenizerFast(tokenizer_file=_UpperCamelCase ) _snake_case = len(_UpperCamelCase ) tokenizer.save_pretrained(_UpperCamelCase ) # 2. Build the config _snake_case = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: _snake_case = candidate break if size is None: raise ValueError("Could not infer the size, please provide it with the `--size` argument." ) if size not in possible_sizes: raise ValueError(F"""`size` should be one of {possible_sizes}, got {size}.""" ) _snake_case = RwkvConfig( vocab_size=_UpperCamelCase , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(_UpperCamelCase ) # 3. Download model file then convert state_dict _snake_case = hf_hub_download(_UpperCamelCase , _UpperCamelCase ) _snake_case = torch.load(_UpperCamelCase , map_location="cpu" ) _snake_case = convert_state_dict(_UpperCamelCase ) # 4. Split in shards and save _snake_case , _snake_case = shard_checkpoint(_UpperCamelCase ) for shard_file, shard in shards.items(): torch.save(_UpperCamelCase , os.path.join(_UpperCamelCase , _UpperCamelCase ) ) if index is not None: _snake_case = os.path.join(_UpperCamelCase , _UpperCamelCase ) # Save the index as well with open(_UpperCamelCase , "w" , encoding="utf-8" ) as f: _snake_case = json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + "\n" f.write(_UpperCamelCase ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( "Cleaning up shards. This may error with an OOM error, it this is the case don't worry you still have converted the model." ) _snake_case = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: _snake_case = torch.load(os.path.join(_UpperCamelCase , _UpperCamelCase ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(_UpperCamelCase , _UpperCamelCase ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError("Please provide a `model_name` to push the model to the Hub." ) _snake_case = AutoModelForCausalLM.from_pretrained(_UpperCamelCase ) model.push_to_hub(_UpperCamelCase , max_shard_size="2GB" ) tokenizer.push_to_hub(_UpperCamelCase ) if __name__ == "__main__": UpperCamelCase_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--repo_id''', default=None, type=str, required=True, help='''Repo ID from which to pull the checkpoint.''' ) parser.add_argument( '''--checkpoint_file''', default=None, type=str, required=True, help='''Name of the checkpoint file in the repo.''' ) parser.add_argument( '''--output_dir''', default=None, type=str, required=True, help='''Where to save the converted model.''' ) parser.add_argument( '''--tokenizer_file''', default=None, type=str, help='''Path to the tokenizer file to use (if not provided, only the model is converted).''', ) parser.add_argument( '''--size''', default=None, type=str, help='''Size of the model. Will be inferred from the `checkpoint_file` if not passed.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Push to the Hub the converted model.''', ) parser.add_argument( '''--model_name''', default=None, type=str, help='''Name of the pushed model on the Hub, including the username / organization.''', ) UpperCamelCase_ : int = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
142
1
import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller __lowerCAmelCase : Union[str, Any] = 3 def __magic_name__ ( A : int ): '''simple docstring''' print("Generating primitive root of p" ) while True: a = random.randrange(3, A ) if pow(A, 2, A ) == 1: continue if pow(A, A, A ) == 1: continue return g def __magic_name__ ( A : int ): '''simple docstring''' print("Generating prime p..." ) a = rabin_miller.generate_large_prime(A ) # select large prime number. a = primitive_root(A ) # one primitive root on modulo p. a = random.randrange(3, A ) # private_key -> have to be greater than 2 for safety. a = cryptomath.find_mod_inverse(pow(A, A, A ), A ) a = (key_size, e_a, e_a, p) a = (key_size, d) return public_key, private_key def __magic_name__ ( A : str, A : int ): '''simple docstring''' if os.path.exists(F"""{name}_pubkey.txt""" ) or os.path.exists(F"""{name}_privkey.txt""" ): print("\nWARNING:" ) print( F"""\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n""" "Use a different name or delete these files and re-run this program." ) sys.exit() a , a = generate_key(A ) print(F"""\nWriting public key to file {name}_pubkey.txt...""" ) with open(F"""{name}_pubkey.txt""", "w" ) as fo: fo.write(F"""{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}""" ) print(F"""Writing private key to file {name}_privkey.txt...""" ) with open(F"""{name}_privkey.txt""", "w" ) as fo: fo.write(F"""{private_key[0]},{private_key[1]}""" ) def __magic_name__ ( ): '''simple docstring''' print("Making key files..." ) make_key_files("elgamal", 2048 ) print("Key files generation successful" ) if __name__ == "__main__": main()
107
import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def A ( _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Any = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg''' SCREAMING_SNAKE_CASE : Union[str, Any] = Image.open(requests.get(_lowercase , stream=_lowercase ).raw ).convert('''RGB''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.4814_5466, 0.457_8275, 0.4082_1073) , (0.2686_2954, 0.2613_0258, 0.2757_7711) ), ] ) SCREAMING_SNAKE_CASE : Tuple = transform(_lowercase ).unsqueeze(0 ).to(_lowercase ) return image def A ( _lowercase ): if "visual_encoder" in key: SCREAMING_SNAKE_CASE : Optional[int] = re.sub('''visual_encoder*''' , '''vision_model.encoder''' , _lowercase ) if "blocks" in key: SCREAMING_SNAKE_CASE : str = re.sub(R'''blocks''' , '''layers''' , _lowercase ) if "attn" in key: SCREAMING_SNAKE_CASE : List[Any] = re.sub(R'''attn''' , '''self_attn''' , _lowercase ) if "norm1" in key: SCREAMING_SNAKE_CASE : List[str] = re.sub(R'''norm1''' , '''layer_norm1''' , _lowercase ) if "norm2" in key: SCREAMING_SNAKE_CASE : str = re.sub(R'''norm2''' , '''layer_norm2''' , _lowercase ) if "encoder.norm" in key: SCREAMING_SNAKE_CASE : Optional[int] = re.sub(R'''encoder.norm''' , '''post_layernorm''' , _lowercase ) if "encoder.patch_embed.proj" in key: SCREAMING_SNAKE_CASE : Dict = re.sub(R'''encoder.patch_embed.proj''' , '''embeddings.patch_embedding''' , _lowercase ) if "encoder.pos_embed" in key: SCREAMING_SNAKE_CASE : int = re.sub(R'''encoder.pos_embed''' , '''embeddings.position_embedding''' , _lowercase ) if "encoder.cls_token" in key: SCREAMING_SNAKE_CASE : List[str] = re.sub(R'''encoder.cls_token''' , '''embeddings.class_embedding''' , _lowercase ) if "self_attn" in key: SCREAMING_SNAKE_CASE : int = re.sub(R'''self_attn.proj''' , '''self_attn.projection''' , _lowercase ) return key @torch.no_grad() def A ( _lowercase , _lowercase=None ): if config_path is not None: SCREAMING_SNAKE_CASE : Any = BlipConfig.from_pretrained(_lowercase ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = BlipConfig(projection_dim=512 , text_config={} , vision_config={} ) SCREAMING_SNAKE_CASE : str = BlipForConditionalGeneration(_lowercase ).eval() SCREAMING_SNAKE_CASE : List[Any] = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth''' SCREAMING_SNAKE_CASE : Tuple = blip_decoder(pretrained=_lowercase , image_size=384 , vit='''base''' ) SCREAMING_SNAKE_CASE : Any = pt_model.eval() SCREAMING_SNAKE_CASE : Dict = pt_model.state_dict() for key in modified_state_dict.copy(): SCREAMING_SNAKE_CASE : Tuple = modified_state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = rename_key(_lowercase ) SCREAMING_SNAKE_CASE : Any = value hf_model.load_state_dict(_lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = 384 SCREAMING_SNAKE_CASE : Tuple = load_demo_image(image_size=_lowercase , device='''cpu''' ) SCREAMING_SNAKE_CASE : List[Any] = BertTokenizer.from_pretrained('''bert-base-uncased''' ) SCREAMING_SNAKE_CASE : int = tokenizer(['''a picture of'''] ).input_ids SCREAMING_SNAKE_CASE : Optional[int] = hf_model.generate(_lowercase , _lowercase ) assert out[0].tolist() == [30_522, 1_037, 3_861, 1_997, 1_037, 2_450, 3_564, 2_006, 1_996, 3_509, 2_007, 2_014, 3_899, 102] SCREAMING_SNAKE_CASE : Union[str, Any] = hf_model.generate(_lowercase ) assert out[0].tolist() == [30_522, 1_037, 2_450, 3_564, 2_006, 1_996, 3_509, 2_007, 2_014, 3_899, 102] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(_lowercase ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' SCREAMING_SNAKE_CASE : str = ( '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth''' ) SCREAMING_SNAKE_CASE : Tuple = blip_vqa(pretrained=_lowercase , image_size=_lowercase , vit='''base''' ) vqa_model.eval() SCREAMING_SNAKE_CASE : List[str] = vqa_model.state_dict() for key in modified_state_dict.copy(): SCREAMING_SNAKE_CASE : Optional[int] = modified_state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = rename_key(_lowercase ) SCREAMING_SNAKE_CASE : str = value SCREAMING_SNAKE_CASE : Dict = BlipForQuestionAnswering(_lowercase ) hf_vqa_model.load_state_dict(_lowercase ) SCREAMING_SNAKE_CASE : Dict = ['''How many dogs are in this image?'''] SCREAMING_SNAKE_CASE : Tuple = tokenizer(_lowercase , return_tensors='''pt''' ).input_ids SCREAMING_SNAKE_CASE : Dict = hf_vqa_model.generate(_lowercase , _lowercase ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '''_vqa''' ) SCREAMING_SNAKE_CASE : Any = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth''' SCREAMING_SNAKE_CASE : int = blip_itm(pretrained=_lowercase , image_size=_lowercase , vit='''base''' ) itm_model.eval() SCREAMING_SNAKE_CASE : List[Any] = itm_model.state_dict() for key in modified_state_dict.copy(): SCREAMING_SNAKE_CASE : int = modified_state_dict.pop(_lowercase ) SCREAMING_SNAKE_CASE : Dict = rename_key(_lowercase ) SCREAMING_SNAKE_CASE : List[Any] = value SCREAMING_SNAKE_CASE : int = BlipForImageTextRetrieval(_lowercase ) SCREAMING_SNAKE_CASE : Tuple = ['''A picture of a woman with a dog sitting in a beach'''] SCREAMING_SNAKE_CASE : str = tokenizer( _lowercase , return_tensors='''pt''' , padding='''max_length''' , truncation=_lowercase , max_length=35 , ).input_ids hf_itm_model.load_state_dict(_lowercase ) hf_itm_model.eval() SCREAMING_SNAKE_CASE : int = hf_itm_model(_lowercase , _lowercase , use_itm_head=_lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = hf_itm_model(_lowercase , _lowercase , use_itm_head=_lowercase ) assert out[0].item() == 0.2110_6874_9427_7954 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.4_5698_8453_8650_5127 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + '''_itm''' ) if __name__ == "__main__": __UpperCamelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') __UpperCamelCase : int = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
182
0
'''simple docstring''' import os import sys import transformers __a = '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)
356
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) __a = { 'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'], 'processing_layoutlmv2': ['LayoutLMv2Processor'], 'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['LayoutLMv2TokenizerFast'] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['LayoutLMv2FeatureExtractor'] __a = ['LayoutLMv2ImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST', 'LayoutLMv2ForQuestionAnswering', 'LayoutLMv2ForSequenceClassification', 'LayoutLMv2ForTokenClassification', 'LayoutLMv2Layer', 'LayoutLMv2Model', 'LayoutLMv2PreTrainedModel', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
17
0
"""simple docstring""" import io import math from typing import Dict, Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, get_image_size, infer_channel_dimension_format, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_vision_available, logging from ...utils.import_utils import requires_backends if is_vision_available(): import textwrap from PIL import Image, ImageDraw, ImageFont if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: a__ : Tuple = False a__ : int = logging.get_logger(__name__) a__ : Optional[Any] = '''ybelkada/fonts''' def UpperCAmelCase__ (): '''simple docstring''' if is_torch_available() and not is_torch_greater_or_equal_than_1_11: raise ImportError( f"""You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use """ "Pix2StructImageProcessor. Please upgrade torch." ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' requires_backends(lowerCAmelCase_ , ["torch"] ) _check_torch_version() __SCREAMING_SNAKE_CASE = image_tensor.unsqueeze(0 ) __SCREAMING_SNAKE_CASE = torch.nn.functional.unfold(lowerCAmelCase_ , (patch_height, patch_width) , stride=(patch_height, patch_width) ) __SCREAMING_SNAKE_CASE = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , lowerCAmelCase_ , lowerCAmelCase_ , -1 ) __SCREAMING_SNAKE_CASE = patches.permute(0 , 4 , 2 , 3 , 1 ).reshape( image_tensor.size(2 ) // patch_height , image_tensor.size(3 ) // patch_width , image_tensor.size(1 ) * patch_height * patch_width , ) return patches.unsqueeze(0 ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = 36 , lowerCAmelCase_ = "black" , lowerCAmelCase_ = "white" , lowerCAmelCase_ = 5 , lowerCAmelCase_ = 5 , lowerCAmelCase_ = 5 , lowerCAmelCase_ = 5 , lowerCAmelCase_ = None , lowerCAmelCase_ = None , ): '''simple docstring''' requires_backends(lowerCAmelCase_ , "vision" ) # Add new lines so that each line is no more than 80 characters. __SCREAMING_SNAKE_CASE = textwrap.TextWrapper(width=80 ) __SCREAMING_SNAKE_CASE = wrapper.wrap(text=lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = "\n".join(lowerCAmelCase_ ) if font_bytes is not None and font_path is None: __SCREAMING_SNAKE_CASE = io.BytesIO(lowerCAmelCase_ ) elif font_path is not None: __SCREAMING_SNAKE_CASE = font_path else: __SCREAMING_SNAKE_CASE = hf_hub_download(lowerCAmelCase_ , "Arial.TTF" ) __SCREAMING_SNAKE_CASE = ImageFont.truetype(lowerCAmelCase_ , encoding="UTF-8" , size=lowerCAmelCase_ ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. __SCREAMING_SNAKE_CASE = ImageDraw.Draw(Image.new("RGB" , (1, 1) , lowerCAmelCase_ ) ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = temp_draw.textbbox((0, 0) , lowerCAmelCase_ , lowerCAmelCase_ ) # Create the actual image with a bit of padding around the text. __SCREAMING_SNAKE_CASE = text_width + left_padding + right_padding __SCREAMING_SNAKE_CASE = text_height + top_padding + bottom_padding __SCREAMING_SNAKE_CASE = Image.new("RGB" , (image_width, image_height) , lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = ImageDraw.Draw(lowerCAmelCase_ ) draw.text(xy=(left_padding, top_padding) , text=lowerCAmelCase_ , fill=lowerCAmelCase_ , font=lowerCAmelCase_ ) return image def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , **lowerCAmelCase_ ): '''simple docstring''' requires_backends(lowerCAmelCase_ , "vision" ) # Convert to PIL image if necessary __SCREAMING_SNAKE_CASE = to_pil_image(lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = render_text(lowerCAmelCase_ , **lowerCAmelCase_ ) __SCREAMING_SNAKE_CASE = max(header_image.width , image.width ) __SCREAMING_SNAKE_CASE = int(image.height * (new_width / image.width) ) __SCREAMING_SNAKE_CASE = int(header_image.height * (new_width / header_image.width) ) __SCREAMING_SNAKE_CASE = Image.new("RGB" , (new_width, new_height + new_header_height) , "white" ) new_image.paste(header_image.resize((new_width, new_header_height) ) , (0, 0) ) new_image.paste(image.resize((new_width, new_height) ) , (0, new_header_height) ) # Convert back to the original framework if necessary __SCREAMING_SNAKE_CASE = to_numpy_array(lowerCAmelCase_ ) if infer_channel_dimension_format(lowerCAmelCase_ ) == ChannelDimension.LAST: __SCREAMING_SNAKE_CASE = to_channel_dimension_format(lowerCAmelCase_ , ChannelDimension.LAST ) return new_image class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" snake_case__ : List[Any] = ["flattened_patches"] def __init__( self : List[str] , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : Dict[str, int] = None , UpperCAmelCase__ : int = 2_0_4_8 , UpperCAmelCase__ : bool = False , **UpperCAmelCase__ : Dict , ) -> None: super().__init__(**UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = patch_size if patch_size is not None else {"height": 1_6, "width": 1_6} __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = do_convert_rgb __SCREAMING_SNAKE_CASE = max_patches __SCREAMING_SNAKE_CASE = is_vqa def UpperCAmelCase_ ( self : List[Any] , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : int , UpperCAmelCase__ : dict , **UpperCAmelCase__ : List[str] ) -> np.ndarray: requires_backends(self.extract_flattened_patches , "torch" ) _check_torch_version() # convert to torch __SCREAMING_SNAKE_CASE = to_channel_dimension_format(UpperCAmelCase__ , ChannelDimension.FIRST ) __SCREAMING_SNAKE_CASE = torch.from_numpy(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = patch_size["height"], patch_size["width"] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = get_image_size(UpperCAmelCase__ ) # maximize scale s.t. __SCREAMING_SNAKE_CASE = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) __SCREAMING_SNAKE_CASE = max(min(math.floor(scale * image_height / patch_height ) , UpperCAmelCase__ ) , 1 ) __SCREAMING_SNAKE_CASE = max(min(math.floor(scale * image_width / patch_width ) , UpperCAmelCase__ ) , 1 ) __SCREAMING_SNAKE_CASE = max(num_feasible_rows * patch_height , 1 ) __SCREAMING_SNAKE_CASE = max(num_feasible_cols * patch_width , 1 ) __SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode="bilinear" , align_corners=UpperCAmelCase__ , antialias=UpperCAmelCase__ , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] __SCREAMING_SNAKE_CASE = torch_extract_patches(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = patches.shape __SCREAMING_SNAKE_CASE = patches_shape[1] __SCREAMING_SNAKE_CASE = patches_shape[2] __SCREAMING_SNAKE_CASE = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] __SCREAMING_SNAKE_CASE = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] __SCREAMING_SNAKE_CASE = torch.arange(UpperCAmelCase__ ).reshape([rows, 1] ).repeat(1 , UpperCAmelCase__ ).reshape([rows * columns, 1] ) __SCREAMING_SNAKE_CASE = torch.arange(UpperCAmelCase__ ).reshape([1, columns] ).repeat(UpperCAmelCase__ , 1 ).reshape([rows * columns, 1] ) # Offset by 1 so the ids do not contain zeros, which represent padding. row_ids += 1 col_ids += 1 # Prepare additional patch features. # [rows * columns, 1] __SCREAMING_SNAKE_CASE = row_ids.to(torch.floataa ) __SCREAMING_SNAKE_CASE = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] __SCREAMING_SNAKE_CASE = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] __SCREAMING_SNAKE_CASE = torch.nn.functional.pad(UpperCAmelCase__ , [0, 0, 0, max_patches - (rows * columns)] ).float() __SCREAMING_SNAKE_CASE = to_numpy_array(UpperCAmelCase__ ) return result def UpperCAmelCase_ ( self : Tuple , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase__ : Tuple ) -> np.ndarray: if image.dtype == np.uinta: __SCREAMING_SNAKE_CASE = image.astype(np.floataa ) # take mean across the whole `image` __SCREAMING_SNAKE_CASE = np.mean(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = np.std(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(UpperCAmelCase__ , mean=UpperCAmelCase__ , std=UpperCAmelCase__ , **UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Optional[Any] , UpperCAmelCase__ : ImageInput , UpperCAmelCase__ : Optional[str] = None , UpperCAmelCase__ : bool = None , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[int] = None , UpperCAmelCase__ : Optional[Dict[str, int]] = None , UpperCAmelCase__ : Optional[Union[str, TensorType]] = None , UpperCAmelCase__ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase__ : Any , ) -> ImageInput: __SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize __SCREAMING_SNAKE_CASE = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __SCREAMING_SNAKE_CASE = patch_size if patch_size is not None else self.patch_size __SCREAMING_SNAKE_CASE = max_patches if max_patches is not None else self.max_patches __SCREAMING_SNAKE_CASE = self.is_vqa if kwargs.get("data_format" , UpperCAmelCase__ ) is not None: raise ValueError("data_format is not an accepted input as the outputs are " ) __SCREAMING_SNAKE_CASE = make_list_of_images(UpperCAmelCase__ ) if not valid_images(UpperCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) # PIL RGBA images are converted to RGB if do_convert_rgb: __SCREAMING_SNAKE_CASE = [convert_to_rgb(UpperCAmelCase__ ) for image in images] # All transformations expect numpy arrays. __SCREAMING_SNAKE_CASE = [to_numpy_array(UpperCAmelCase__ ) for image in images] if is_vqa: if header_text is None: raise ValueError("A header text must be provided for VQA models." ) __SCREAMING_SNAKE_CASE = kwargs.pop("font_bytes" , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = kwargs.pop("font_path" , UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = [header_text] * len(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = [ render_header(UpperCAmelCase__ , header_text[i] , font_bytes=UpperCAmelCase__ , font_path=UpperCAmelCase__ ) for i, image in enumerate(UpperCAmelCase__ ) ] if do_normalize: __SCREAMING_SNAKE_CASE = [self.normalize(image=UpperCAmelCase__ ) for image in images] # convert to torch tensor and permute __SCREAMING_SNAKE_CASE = [ self.extract_flattened_patches(image=UpperCAmelCase__ , max_patches=UpperCAmelCase__ , patch_size=UpperCAmelCase__ ) for image in images ] # create attention mask in numpy __SCREAMING_SNAKE_CASE = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] __SCREAMING_SNAKE_CASE = BatchFeature( data={"flattened_patches": images, "attention_mask": attention_masks} , tensor_type=UpperCAmelCase__ ) return encoded_outputs
54
'''simple docstring''' from __future__ import annotations def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ ): if partitions <= 0: raise ValueError('partitions must be a positive number!' ) if partitions > number_of_bytes: raise ValueError('partitions can not > number_of_bytes!' ) UpperCAmelCase : int = number_of_bytes // partitions UpperCAmelCase : List[str] = [] for i in range(UpperCAmelCase_ ): UpperCAmelCase : List[Any] = i * bytes_per_partition + 1 UpperCAmelCase : str = ( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(F"""{start_bytes}-{end_bytes}""" ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
151
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class snake_case_ ( __A ): def __init__( self : Union[str, Any] , lowercase_ : Dict ) -> Union[str, Any]: lowercase__ : Union[str, Any] = data def __iter__( self : Optional[Any] ) -> Tuple: for element in self.data: yield element def lowercase_ ( _lowerCamelCase : Any=True): lowercase__ : Union[str, Any] = Accelerator(even_batches=_lowerCamelCase) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def lowercase_ ( _lowerCamelCase : Accelerator , _lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : bool = False): if iterable: lowercase__ : Tuple = DummyIterableDataset(torch.as_tensor(range(_lowerCamelCase))) else: lowercase__ : List[str] = TensorDataset(torch.as_tensor(range(_lowerCamelCase))) lowercase__ : List[Any] = DataLoader(_lowerCamelCase , batch_size=_lowerCamelCase) lowercase__ : str = accelerator.prepare(_lowerCamelCase) return dl def lowercase_ ( _lowerCamelCase : Accelerator , _lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : List[int] , _lowerCamelCase : List[int] , ): lowercase__ : Tuple = create_dataloader(accelerator=_lowerCamelCase , dataset_size=_lowerCamelCase , batch_size=_lowerCamelCase) lowercase__ : List[str] = [len(batch[0]) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def lowercase_ ( ): lowercase__ : Tuple = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( _lowerCamelCase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( _lowerCamelCase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def lowercase_ ( ): lowercase__ : Dict = create_accelerator(even_batches=_lowerCamelCase) verify_dataloader_batch_sizes( _lowerCamelCase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( _lowerCamelCase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def lowercase_ ( ): lowercase__ : Union[str, Any] = create_accelerator(even_batches=_lowerCamelCase) lowercase__ : Optional[int] = torch.nn.Linear(1 , 1) lowercase__ : str = accelerator.prepare(_lowerCamelCase) lowercase__ : Optional[Any] = create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1) lowercase__ : Tuple = [] with accelerator.join_uneven_inputs([ddp_model]): for batch_idx, batch in enumerate(_lowerCamelCase): lowercase__ : List[str] = ddp_model(batch[0].float()) lowercase__ : Union[str, Any] = output.sum() loss.backward() batch_idxs.append(_lowerCamelCase) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def lowercase_ ( _lowerCamelCase : str): with warnings.catch_warnings(record=_lowerCamelCase) as w: with accelerator.join_uneven_inputs([Mock()]): pass assert issubclass(w[-1].category , _lowerCamelCase) assert "only supported for multi-GPU" in str(w[-1].message) def lowercase_ ( ): lowercase__ : Union[str, Any] = True lowercase__ : str = False lowercase__ : Any = create_accelerator(even_batches=_lowerCamelCase) lowercase__ : Union[str, Any] = torch.nn.Linear(1 , 1) lowercase__ : List[Any] = accelerator.prepare(_lowerCamelCase) lowercase__ : Tuple = create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1) lowercase__ : Dict = create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1) with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowerCamelCase): lowercase__ : Dict = train_dl.batch_sampler.even_batches lowercase__ : Any = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def lowercase_ ( ): lowercase__ : Optional[Any] = True lowercase__ : List[str] = False lowercase__ : Dict = create_accelerator(even_batches=_lowerCamelCase) lowercase__ : Any = torch.nn.Linear(1 , 1) lowercase__ : List[Any] = accelerator.prepare(_lowerCamelCase) create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1 , iterable=_lowerCamelCase) lowercase__ : Optional[int] = create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1) with warnings.catch_warnings(): warnings.filterwarnings("ignore") try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowerCamelCase): lowercase__ : Union[str, Any] = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def lowercase_ ( ): lowercase__ : Optional[int] = create_accelerator() lowercase__ : Any = torch.nn.Linear(1 , 1) lowercase__ : Union[str, Any] = accelerator.prepare(_lowerCamelCase) create_dataloader(_lowerCamelCase , dataset_size=3 , batch_size=1 , iterable=_lowerCamelCase) with warnings.catch_warnings(record=_lowerCamelCase) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=_lowerCamelCase): pass assert issubclass(w[-1].category , _lowerCamelCase) assert "only supported for map-style datasets" in str(w[-1].message) def lowercase_ ( ): lowercase__ : str = create_accelerator() accelerator.print("Test that even_batches variable ensures uniform batches across processes") test_default_ensures_even_batch_sizes() accelerator.print("Run tests with even_batches disabled") test_can_disable_even_batches() accelerator.print("Test joining uneven inputs") test_can_join_uneven_inputs() accelerator.print("Test overriding even_batches when joining uneven inputs") test_join_can_override_even_batches() accelerator.print("Test overriding even_batches for mixed dataloader types") test_join_can_override_for_mixed_type_dataloaders() accelerator.print("Test overriding even_batches raises a warning for iterable dataloaders") test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print("Test join with non DDP distributed raises warning") lowercase__ : Optional[Any] = accelerator.state.distributed_type lowercase__ : Union[str, Any] = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(_lowerCamelCase) lowercase__ : Dict = original_state if __name__ == "__main__": main()
333
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class snake_case_ ( __A ): __A : Optional[int] = "rwkv" __A : List[str] = {"max_position_embeddings": "context_length"} def __init__( self : Dict , lowercase_ : List[Any]=5_02_77 , lowercase_ : Union[str, Any]=10_24 , lowercase_ : Any=40_96 , lowercase_ : int=32 , lowercase_ : Dict=None , lowercase_ : str=None , lowercase_ : Any=1E-5 , lowercase_ : Optional[Any]=0 , lowercase_ : Any=0 , lowercase_ : List[str]=6 , lowercase_ : List[Any]=False , lowercase_ : int=True , **lowercase_ : List[str] , ) -> int: lowercase__ : List[str] = vocab_size lowercase__ : str = context_length lowercase__ : List[Any] = hidden_size lowercase__ : Optional[Any] = num_hidden_layers lowercase__ : Optional[Any] = attention_hidden_size if attention_hidden_size is not None else hidden_size lowercase__ : str = intermediate_size if intermediate_size is not None else 4 * hidden_size lowercase__ : List[Any] = layer_norm_epsilon lowercase__ : str = rescale_every lowercase__ : Optional[int] = use_cache lowercase__ : int = bos_token_id lowercase__ : Optional[Any] = eos_token_id super().__init__( tie_word_embeddings=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ )
333
1
"""simple docstring""" lowerCamelCase_ : int = range(2, 2_0 + 1) lowerCamelCase_ : Tuple = [1_0**k for k in range(ks[-1] + 1)] lowerCamelCase_ : dict[int, dict[int, list[list[int]]]] = {} def _A ( lowercase , lowercase , lowercase , lowercase ): """simple docstring""" a =sum(a_i[j] for j in range(lowercase , len(lowercase ) ) ) a =sum(a_i[j] * base[j] for j in range(min(len(lowercase ) , lowercase ) ) ) a , a =0, 0 a =n - i a =memo.get(lowercase ) if sub_memo is not None: a =sub_memo.get(lowercase ) if jumps is not None and len(lowercase ) > 0: # find and make the largest jump without going over a =-1 for _k in range(len(lowercase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: a =_k break if max_jump >= 0: a , a , a =jumps[max_jump] # since the difference between jumps is cached, add c a =diff + c for j in range(min(lowercase , len(lowercase ) ) ): a , a =divmod(lowercase , 10 ) if new_c > 0: add(lowercase , lowercase , lowercase ) else: a =[] else: a ={c: []} a =sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps a , a =next_term(lowercase , k - 1 , i + dn , lowercase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead a , a =compute(lowercase , lowercase , i + dn , lowercase ) diff += _diff dn += terms_jumped a =sub_memo[c] # keep jumps sorted by # of terms skipped a =0 while j < len(lowercase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(lowercase , (diff, dn, k) ) return (diff, dn) def _A ( lowercase , lowercase , lowercase , lowercase ): """simple docstring""" if i >= n: return 0, i if k > len(lowercase ): a_i.extend([0 for _ in range(k - len(lowercase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) a =i a , a , a =0, 0, 0 for j in range(len(lowercase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 a =ds_c + ds_b diff += addend a =0 for j in range(lowercase ): a =a_i[j] + addend a , a =divmod(lowercase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(lowercase , lowercase , lowercase ) return diff, i - start_i def _A ( lowercase , lowercase , lowercase ): """simple docstring""" for j in range(lowercase , len(lowercase ) ): a =digits[j] + addend if s >= 10: a , a =divmod(lowercase , 10 ) a =addend // 10 + quotient else: a =s a =addend // 10 if addend == 0: break while addend > 0: a , a =divmod(lowercase , 10 ) digits.append(lowercase ) def _A ( lowercase = 10**15 ): """simple docstring""" a =[1] a =1 a =0 while True: a , a =next_term(lowercase , 20 , i + dn , lowercase ) dn += terms_jumped if dn == n - i: break a =0 for j in range(len(lowercase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(F'{solution() = }')
81
import math class snake_case__ : def __init__( self , lowerCAmelCase__=0 ) -> Optional[int]: # a graph with Node 0,1,...,N-1 __magic_name__ : Tuple = n __magic_name__ : Union[str, Any] = [ [math.inf for j in range(0 , lowerCAmelCase__ )] for i in range(0 , lowerCAmelCase__ ) ] # adjacency matrix for weight __magic_name__ : List[Any] = [ [math.inf for j in range(0 , lowerCAmelCase__ )] for i in range(0 , lowerCAmelCase__ ) ] # dp[i][j] stores minimum distance from i to j def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Tuple: __magic_name__ : Dict = w def __magic_name__ ( self ) -> Optional[int]: for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): __magic_name__ : Optional[Any] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> str: return self.dp[u][v] if __name__ == "__main__": __magic_name__: Dict = 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)
342
0
"""simple docstring""" def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> Any: '''simple docstring''' def get_matched_characters(__lowerCAmelCase , __lowerCAmelCase ) -> str: lowercase_ = [] lowercase_ = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): lowercase_ = int(max(0 , i - limit ) ) lowercase_ = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(a_ ) lowercase_ = F'''{_stra[0:_stra.index(a_ )]} {_stra[_stra.index(a_ ) + 1:]}''' return "".join(a_ ) # matching characters lowercase_ = get_matched_characters(a_ , a_ ) lowercase_ = get_matched_characters(a_ , a_ ) lowercase_ = len(a_ ) # transposition lowercase_ = ( len([(ca, ca) for ca, ca in zip(a_ , a_ ) if ca != ca] ) // 2 ) if not match_count: lowercase_ = 0.0 else: lowercase_ = ( 1 / 3 * ( match_count / len(a_ ) + match_count / len(a_ ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters lowercase_ = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("hello", "world"))
367
"""simple docstring""" import unittest from .lib import ( Matrix, Vector, axpy, square_zero_matrix, unit_basis_vector, zero_vector, ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def _UpperCAmelCase ( self : str): """simple docstring""" lowercase_ = Vector([1, 2, 3]) self.assertEqual(x.component(0) , 1) self.assertEqual(x.component(2) , 3) lowercase_ = Vector() def _UpperCAmelCase ( self : Tuple): """simple docstring""" lowercase_ = Vector([0, 0, 0, 0, 0, 1]) self.assertEqual(str(lowerCAmelCase_) , """(0,0,0,0,0,1)""") def _UpperCAmelCase ( self : int): """simple docstring""" lowercase_ = Vector([1, 2, 3, 4]) self.assertEqual(len(lowerCAmelCase_) , 4) def _UpperCAmelCase ( self : Any): """simple docstring""" lowercase_ = Vector([1, 2]) lowercase_ = Vector([1, 2, 3, 4, 5]) lowercase_ = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) lowercase_ = Vector([1, -1, 1, -1, 2, -3, 4, -5]) self.assertAlmostEqual(x.euclidean_length() , 2.236 , 3) self.assertAlmostEqual(y.euclidean_length() , 7.416 , 3) self.assertEqual(z.euclidean_length() , 0) self.assertAlmostEqual(w.euclidean_length() , 7.616 , 3) def _UpperCAmelCase ( self : Dict): """simple docstring""" lowercase_ = Vector([1, 2, 3]) lowercase_ = Vector([1, 1, 1]) self.assertEqual((x + y).component(0) , 2) self.assertEqual((x + y).component(1) , 3) self.assertEqual((x + y).component(2) , 4) def _UpperCAmelCase ( self : List[Any]): """simple docstring""" lowercase_ = Vector([1, 2, 3]) lowercase_ = Vector([1, 1, 1]) self.assertEqual((x - y).component(0) , 0) self.assertEqual((x - y).component(1) , 1) self.assertEqual((x - y).component(2) , 2) def _UpperCAmelCase ( self : Optional[int]): """simple docstring""" lowercase_ = Vector([1, 2, 3]) lowercase_ = Vector([2, -1, 4]) # for test of dot product lowercase_ = Vector([1, -2, -1]) self.assertEqual(str(x * 3.0) , """(3.0,6.0,9.0)""") self.assertEqual((a * b) , 0) def _UpperCAmelCase ( self : int): """simple docstring""" self.assertEqual(str(zero_vector(1_0)).count("""0""") , 1_0) def _UpperCAmelCase ( self : Dict): """simple docstring""" self.assertEqual(str(unit_basis_vector(3 , 1)) , """(0,1,0)""") def _UpperCAmelCase ( self : Optional[Any]): """simple docstring""" lowercase_ = Vector([1, 2, 3]) lowercase_ = Vector([1, 0, 1]) self.assertEqual(str(axpy(2 , lowerCAmelCase_ , lowerCAmelCase_)) , """(3,4,7)""") def _UpperCAmelCase ( self : List[Any]): """simple docstring""" lowercase_ = Vector([1, 0, 0, 0, 0, 0]) lowercase_ = x.copy() self.assertEqual(str(lowerCAmelCase_) , str(lowerCAmelCase_)) def _UpperCAmelCase ( self : Dict): """simple docstring""" lowercase_ = Vector([1, 0, 0]) x.change_component(0 , 0) x.change_component(1 , 1) self.assertEqual(str(lowerCAmelCase_) , """(0,1,0)""") def _UpperCAmelCase ( self : Dict): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n""" , str(lowerCAmelCase_)) def _UpperCAmelCase ( self : str): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) lowercase_ = [[-3, -1_4, -1_0], [-5, -1_0, -5], [-2, -1, 0]] for x in range(a.height()): for y in range(a.width()): self.assertEqual(minors[x][y] , a.minor(lowerCAmelCase_ , lowerCAmelCase_)) def _UpperCAmelCase ( self : List[str]): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) lowercase_ = [[-3, 1_4, -1_0], [5, -1_0, 5], [-2, 1, 0]] for x in range(a.height()): for y in range(a.width()): self.assertEqual(cofactors[x][y] , a.cofactor(lowerCAmelCase_ , lowerCAmelCase_)) def _UpperCAmelCase ( self : List[str]): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) self.assertEqual(-5 , a.determinant()) def _UpperCAmelCase ( self : int): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]] , 3 , 3) lowercase_ = Vector([1, 2, 3]) self.assertEqual("""(14,32,50)""" , str(a * x)) self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n""" , str(a * 2)) def _UpperCAmelCase ( self : List[str]): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) a.change_component(0 , 2 , 5) self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n""" , str(lowerCAmelCase_)) def _UpperCAmelCase ( self : str): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) self.assertEqual(7 , a.component(2 , 1) , 0.01) def _UpperCAmelCase ( self : Dict): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) lowercase_ = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]] , 3 , 3) self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n""" , str(a + b)) def _UpperCAmelCase ( self : Optional[Any]): """simple docstring""" lowercase_ = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3) lowercase_ = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]] , 3 , 3) self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n""" , str(a - b)) def _UpperCAmelCase ( self : Optional[Any]): """simple docstring""" self.assertEqual( """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n""" , str(square_zero_matrix(5)) , ) if __name__ == "__main__": unittest.main()
313
0
def lowerCAmelCase__ ( a__: int = 2_0_0_0_0_0_0 ) -> List[str]: '''simple docstring''' _UpperCAmelCase = [0 for i in range(n + 1 )] _UpperCAmelCase = 1 _UpperCAmelCase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , lowercase__ ): _UpperCAmelCase = 1 _UpperCAmelCase = 0 for i in range(lowercase__ ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'''{solution() = }''')
329
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
148
0
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Any , _UpperCamelCase : Any ) -> Tuple: '''simple docstring''' UpperCamelCase__ = [] for part_id in partition_order: UpperCamelCase__ = df.where(F'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(_UpperCamelCase ): expected_row_ids_and_row_dicts.append((F'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(1_00 ).repartition(1 ) UpperCamelCase__ = Spark(_UpperCamelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(10 ).repartition(2 ) UpperCamelCase__ = [1, 0] UpperCamelCase__ = _generate_iterable_examples(_UpperCamelCase , _UpperCamelCase ) # Reverse the partitions. UpperCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_UpperCamelCase , _UpperCamelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): UpperCamelCase__ , UpperCamelCase__ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> str: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(10 ).repartition(1 ) UpperCamelCase__ = SparkExamplesIterable(_UpperCamelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_UpperCamelCase ): assert row_id == F'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> Dict: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch("numpy.random.Generator" ) as generator_mock: UpperCamelCase__ = lambda _UpperCamelCase : x.reverse() UpperCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_UpperCamelCase , [2, 1, 0] ) UpperCamelCase__ = SparkExamplesIterable(_UpperCamelCase ).shuffle_data_sources(_UpperCamelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_UpperCamelCase ): UpperCamelCase__ , UpperCamelCase__ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> Tuple: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 UpperCamelCase__ = SparkExamplesIterable(_UpperCamelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 UpperCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_UpperCamelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_UpperCamelCase ): UpperCamelCase__ , UpperCamelCase__ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 UpperCamelCase__ = SparkExamplesIterable(_UpperCamelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 UpperCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_UpperCamelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_UpperCamelCase ): UpperCamelCase__ , UpperCamelCase__ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def SCREAMING_SNAKE_CASE__( ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() UpperCamelCase__ = spark.range(1_00 ).repartition(1 ) UpperCamelCase__ = Spark(_UpperCamelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
31
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Any, a_ : VQModel, a_ : UNetaDModel, a_ : DDIMScheduler ): """simple docstring""" super().__init__() self.register_modules(vqvae=a_, unet=a_, scheduler=a_ ) @torch.no_grad() def __call__( self : Union[str, Any], a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : float = 0.0, a_ : int = 50, a_ : Optional[str] = "pil", a_ : bool = True, **a_ : Tuple, ): """simple docstring""" UpperCamelCase__ = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), generator=a_, ) UpperCamelCase__ = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(a_ ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature UpperCamelCase__ = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ = {} if accepts_eta: UpperCamelCase__ = eta for t in self.progress_bar(self.scheduler.timesteps ): UpperCamelCase__ = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual UpperCamelCase__ = self.unet(a_, a_ ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # decode the image latents with the VAE UpperCamelCase__ = self.vqvae.decode(a_ ).sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
1
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class SCREAMING_SNAKE_CASE__ ( datasets.BeamBasedBuilder ): """simple docstring""" def A__ ( self )-> Tuple: '''simple docstring''' return datasets.DatasetInfo( features=datasets.Features({'''content''': datasets.Value('''string''' )} ) , supervised_keys=SCREAMING_SNAKE_CASE_ , ) def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )-> Optional[Any]: '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_dummy_examples()} )] def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )-> Any: '''simple docstring''' import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE__ ( datasets.BeamBasedBuilder ): """simple docstring""" def A__ ( self )-> List[Any]: '''simple docstring''' return datasets.DatasetInfo( features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) , supervised_keys=SCREAMING_SNAKE_CASE_ , ) def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )-> Any: '''simple docstring''' return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_nested_examples()} ) ] def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )-> List[str]: '''simple docstring''' import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(SCREAMING_SNAKE_CASE_ ) def A_ ( ) -> Optional[int]: '''simple docstring''' return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] def A_ ( ) -> int: '''simple docstring''' return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" @require_beam def A__ ( self )-> Any: '''simple docstring''' __UpperCamelCase = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: __UpperCamelCase = DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE_ , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , F"{builder.name}-train.arrow" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) __UpperCamelCase = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , SCREAMING_SNAKE_CASE_ ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , SCREAMING_SNAKE_CASE_ ) self.assertDictEqual(dset['''train'''][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def A__ ( self )-> List[str]: '''simple docstring''' import apache_beam as beam __UpperCamelCase = beam.io.parquetio.WriteToParquet __UpperCamelCase = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: __UpperCamelCase = DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE_ , beam_runner='''DirectRunner''' ) with patch('''apache_beam.io.parquetio.WriteToParquet''' ) as write_parquet_mock: __UpperCamelCase = partial(SCREAMING_SNAKE_CASE_ , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , F"{builder.name}-train-00000-of-00002.arrow" ) ) ) self.assertTrue( os.path.exists( os.path.join( SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , F"{builder.name}-train-00000-of-00002.arrow" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) __UpperCamelCase = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , SCREAMING_SNAKE_CASE_ ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , SCREAMING_SNAKE_CASE_ ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['''train''']['''content'''] ) , sorted(['''foo''', '''bar''', '''foobar'''] ) ) self.assertTrue( os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def A__ ( self )-> str: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_cache_dir: __UpperCamelCase = DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE_ ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def A__ ( self )-> Any: '''simple docstring''' __UpperCamelCase = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: __UpperCamelCase = NestedBeamDataset(cache_dir=SCREAMING_SNAKE_CASE_ , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , F"{builder.name}-train.arrow" ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) ) __UpperCamelCase = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , SCREAMING_SNAKE_CASE_ ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , SCREAMING_SNAKE_CASE_ ) self.assertDictEqual(dset['''train'''][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset
328
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase__ : Any = logging.get_logger(__name__) lowercase__ : Tuple = { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json", } class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" _snake_case = 'xlnet' _snake_case = ['mems'] _snake_case = { 'n_token': 'vocab_size', # Backward compatibility 'hidden_size': 'd_model', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , SCREAMING_SNAKE_CASE_=32000 , SCREAMING_SNAKE_CASE_=1024 , SCREAMING_SNAKE_CASE_=24 , SCREAMING_SNAKE_CASE_=16 , SCREAMING_SNAKE_CASE_=4096 , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_="bi" , SCREAMING_SNAKE_CASE_=0.0_2 , SCREAMING_SNAKE_CASE_=1E-12 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=512 , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=-1 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_="last" , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_="tanh" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=1 , SCREAMING_SNAKE_CASE_=2 , **SCREAMING_SNAKE_CASE_ , )-> List[str]: '''simple docstring''' __UpperCamelCase = vocab_size __UpperCamelCase = d_model __UpperCamelCase = n_layer __UpperCamelCase = n_head if d_model % n_head != 0: raise ValueError(F"'d_model % n_head' ({d_model % n_head}) should be equal to 0" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})" ) __UpperCamelCase = d_model // n_head __UpperCamelCase = ff_activation __UpperCamelCase = d_inner __UpperCamelCase = untie_r __UpperCamelCase = attn_type __UpperCamelCase = initializer_range __UpperCamelCase = layer_norm_eps __UpperCamelCase = dropout __UpperCamelCase = mem_len __UpperCamelCase = reuse_len __UpperCamelCase = bi_data __UpperCamelCase = clamp_len __UpperCamelCase = same_length __UpperCamelCase = summary_type __UpperCamelCase = summary_use_proj __UpperCamelCase = summary_activation __UpperCamelCase = summary_last_dropout __UpperCamelCase = start_n_top __UpperCamelCase = end_n_top __UpperCamelCase = bos_token_id __UpperCamelCase = pad_token_id __UpperCamelCase = eos_token_id if "use_cache" in kwargs: warnings.warn( '''The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`''' ''' instead.''' , SCREAMING_SNAKE_CASE_ , ) __UpperCamelCase = kwargs['''use_cache'''] __UpperCamelCase = use_mems_eval __UpperCamelCase = use_mems_train super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @property def A__ ( self )-> Optional[Any]: '''simple docstring''' logger.info(F"The model {self.model_type} is one of the few models that has no sequence length limit." ) return -1 @max_position_embeddings.setter def A__ ( self , SCREAMING_SNAKE_CASE_ )-> List[str]: '''simple docstring''' raise NotImplementedError( F"The model {self.model_type} is one of the few models that has no sequence length limit." )
328
1
"""simple docstring""" # Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess from packaging.version import Version, parse from accelerate.commands.config.config_args import default_config_file, load_config_from_file _lowercase : Optional[int] = "Run commands across TPU VMs for initial setup before running `accelerate launch`." def snake_case__ ( __lowerCamelCase : str=None ): """simple docstring""" if subparsers is not None: lowerCamelCase__ : Optional[int] =subparsers.add_parser('''tpu-config''' , description=_description ) else: lowerCamelCase__ : Dict =argparse.ArgumentParser('''Accelerate tpu-config command''' , description=_description ) # Core arguments lowerCamelCase__ : Union[str, Any] =parser.add_argument_group( '''Config Arguments''' , '''Arguments that can be configured through `accelerate config`.''' ) config_args.add_argument( '''--config_file''' , type=__lowerCamelCase , default=__lowerCamelCase , help='''Path to the config file to use for accelerate.''' , ) config_args.add_argument( '''--tpu_name''' , default=__lowerCamelCase , help='''The name of the TPU to use. If not specified, will use the TPU specified in the config file.''' , ) config_args.add_argument( '''--tpu_zone''' , default=__lowerCamelCase , help='''The zone of the TPU to use. If not specified, will use the zone specified in the config file.''' , ) lowerCamelCase__ : str =parser.add_argument_group('''TPU Arguments''' , '''Arguments for options ran inside the TPU.''' ) pod_args.add_argument( '''--use_alpha''' , action='''store_true''' , help='''Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.''' , ) pod_args.add_argument( '''--command_file''' , default=__lowerCamelCase , help='''The path to the file containing the commands to run on the pod on startup.''' , ) pod_args.add_argument( '''--command''' , action='''append''' , nargs='''+''' , help='''A command to run on the pod. Can be passed multiple times.''' , ) pod_args.add_argument( '''--install_accelerate''' , action='''store_true''' , help='''Whether to install accelerate on the pod. Defaults to False.''' , ) pod_args.add_argument( '''--accelerate_version''' , default='''latest''' , help='''The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.''' , ) pod_args.add_argument( '''--debug''' , action='''store_true''' , help='''If set, will print the command that would be run instead of running it.''' ) if subparsers is not None: parser.set_defaults(func=__lowerCamelCase ) return parser def snake_case__ ( __lowerCamelCase : Optional[Any] ): """simple docstring""" lowerCamelCase__ : Any =None # Get the default from the config file if it exists. if args.config_file is not None or os.path.isfile(__lowerCamelCase ): lowerCamelCase__ : List[Any] =load_config_from_file(args.config_file ) if not args.command_file and defaults.command_file is not None and not args.command: lowerCamelCase__ : Dict =defaults.command_file if not args.command and defaults.commands is not None: lowerCamelCase__ : Any =defaults.commands if not args.tpu_name: lowerCamelCase__ : Optional[Any] =defaults.tpu_name if not args.tpu_zone: lowerCamelCase__ : List[str] =defaults.tpu_zone if args.accelerate_version == "dev": lowerCamelCase__ : int ='''git+https://github.com/huggingface/accelerate.git''' elif args.accelerate_version == "latest": lowerCamelCase__ : Union[str, Any] ='''accelerate -U''' elif isinstance(parse(args.accelerate_version ) , __lowerCamelCase ): lowerCamelCase__ : Dict =f'''accelerate=={args.accelerate_version}''' if not args.command_file and not args.command: raise ValueError('''You must specify either a command file or a command to run on the pod.''' ) if args.command_file: with open(args.command_file , '''r''' ) as f: lowerCamelCase__ : Optional[Any] =[f.read().splitlines()] # To turn list of lists into list of strings if isinstance(args.command[0] , __lowerCamelCase ): lowerCamelCase__ : List[Any] =[line for cmd in args.command for line in cmd] # Default to the shared folder and install accelerate lowerCamelCase__ : List[str] =['''cd /usr/share'''] if args.install_accelerate: new_cmd += [f'''pip install {args.accelerate_version}'''] new_cmd += args.command lowerCamelCase__ : int ='''; '''.join(__lowerCamelCase ) # Then send it to gcloud # Eventually try to use google-api-core to do this instead of subprocess lowerCamelCase__ : int =['''gcloud'''] if args.use_alpha: cmd += ["alpha"] cmd += [ "compute", "tpus", "tpu-vm", "ssh", args.tpu_name, "--zone", args.tpu_zone, "--command", args.command, "--worker", "all", ] if args.debug: print(f'''Running {" ".join(__lowerCamelCase )}''' ) return subprocess.run(__lowerCamelCase ) print('''Successfully setup pod.''' ) def snake_case__ ( ): """simple docstring""" lowerCamelCase__ : List[Any] =tpu_command_parser() lowerCamelCase__ : Optional[Any] =parser.parse_args() tpu_command_launcher(__lowerCamelCase )
272
"""simple docstring""" import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def snake_case__ ( __lowerCamelCase : str ): """simple docstring""" if "model" in orig_key: lowerCamelCase__ : Optional[int] =orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: lowerCamelCase__ : Union[str, Any] =orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: lowerCamelCase__ : List[Any] =orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: lowerCamelCase__ : List[str] =orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: lowerCamelCase__ : str =orig_key.split('''.''' )[0].split('''_''' )[-1] lowerCamelCase__ : Dict =orig_key.replace(f'''transformer_{layer_num}''' , f'''encoder.layer.{layer_num}''' ) if "mha.attn" in orig_key: lowerCamelCase__ : Union[str, Any] =orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: lowerCamelCase__ : str =orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: lowerCamelCase__ : Union[str, Any] =orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: lowerCamelCase__ : Optional[int] =orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: lowerCamelCase__ : List[str] =orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: lowerCamelCase__ : Dict =orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: lowerCamelCase__ : Union[str, Any] =orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: lowerCamelCase__ : str =orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: lowerCamelCase__ : Tuple =orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: lowerCamelCase__ : Optional[int] =orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: lowerCamelCase__ : Optional[int] ='''yoso.''' + orig_key return orig_key def snake_case__ ( __lowerCamelCase : List[str] , __lowerCamelCase : Any ): """simple docstring""" for key in orig_state_dict.copy().keys(): lowerCamelCase__ : Optional[Any] =orig_state_dict.pop(__lowerCamelCase ) if ("pooler" in key) or ("sen_class" in key): continue else: lowerCamelCase__ : List[str] =val lowerCamelCase__ : Optional[int] =orig_state_dict['''cls.predictions.decoder.bias'''] lowerCamelCase__ : str =torch.arange(__lowerCamelCase ).expand((1, -1) ) + 2 return orig_state_dict def snake_case__ ( __lowerCamelCase : str , __lowerCamelCase : Tuple , __lowerCamelCase : Tuple ): """simple docstring""" lowerCamelCase__ : Union[str, Any] =torch.load(__lowerCamelCase , map_location='''cpu''' )['''model_state_dict'''] lowerCamelCase__ : List[Any] =YosoConfig.from_json_file(__lowerCamelCase ) lowerCamelCase__ : List[str] =YosoForMaskedLM(__lowerCamelCase ) lowerCamelCase__ : Tuple =convert_checkpoint_helper(config.max_position_embeddings , __lowerCamelCase ) print(model.load_state_dict(__lowerCamelCase ) ) model.eval() model.save_pretrained(__lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) if __name__ == "__main__": _lowercase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) _lowercase : Optional[Any] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
272
1
'''simple docstring''' import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class UpperCamelCase__ ( snake_case_ ): """simple docstring""" SCREAMING_SNAKE_CASE__ = (IPNDMScheduler,) SCREAMING_SNAKE_CASE__ = (('''num_inference_steps''', 50),) def lowerCamelCase_ ( self : int , **lowerCamelCase_ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = {"""num_train_timesteps""": 10_00} config.update(**__UpperCAmelCase ) return config def lowerCamelCase_ ( self : Optional[Any] , lowerCamelCase_ : List[str]=0 , **lowerCamelCase_ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = dict(self.forward_default_kwargs ) SCREAMING_SNAKE_CASE : str = kwargs.pop("""num_inference_steps""" , __UpperCAmelCase ) SCREAMING_SNAKE_CASE : Any = self.dummy_sample SCREAMING_SNAKE_CASE : str = 0.1 * sample SCREAMING_SNAKE_CASE : List[str] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE : Optional[int] = self.get_scheduler_config(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE : Union[str, Any] = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(__UpperCAmelCase ) # copy over dummy past residuals SCREAMING_SNAKE_CASE : Union[str, Any] = dummy_past_residuals[:] if time_step is None: SCREAMING_SNAKE_CASE : str = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__UpperCAmelCase ) SCREAMING_SNAKE_CASE : str = scheduler_class.from_pretrained(__UpperCAmelCase ) new_scheduler.set_timesteps(__UpperCAmelCase ) # copy over dummy past residuals SCREAMING_SNAKE_CASE : Optional[Any] = dummy_past_residuals[:] SCREAMING_SNAKE_CASE : Optional[Any] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : Union[str, 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" SCREAMING_SNAKE_CASE : Optional[int] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : 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 : Dict ): '''simple docstring''' pass def lowerCamelCase_ ( self : Optional[Any] , lowerCamelCase_ : Optional[Any]=0 , **lowerCamelCase_ : List[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = dict(self.forward_default_kwargs ) SCREAMING_SNAKE_CASE : List[str] = kwargs.pop("""num_inference_steps""" , __UpperCAmelCase ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_sample SCREAMING_SNAKE_CASE : str = 0.1 * sample SCREAMING_SNAKE_CASE : Tuple = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE : str = self.get_scheduler_config() SCREAMING_SNAKE_CASE : Dict = scheduler_class(**__UpperCAmelCase ) scheduler.set_timesteps(__UpperCAmelCase ) # copy over dummy past residuals (must be after setting timesteps) SCREAMING_SNAKE_CASE : Any = dummy_past_residuals[:] if time_step is None: SCREAMING_SNAKE_CASE : Optional[Any] = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__UpperCAmelCase ) SCREAMING_SNAKE_CASE : Union[str, Any] = 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) SCREAMING_SNAKE_CASE : int = dummy_past_residuals[:] SCREAMING_SNAKE_CASE : Dict = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : 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" SCREAMING_SNAKE_CASE : Tuple = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : 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 : str , **lowerCamelCase_ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : List[Any] = self.get_scheduler_config(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE : Any = scheduler_class(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE : Optional[Any] = 10 SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_model() SCREAMING_SNAKE_CASE : Dict = self.dummy_sample_deter scheduler.set_timesteps(__UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE : Union[str, Any] = model(__UpperCAmelCase , __UpperCAmelCase ) SCREAMING_SNAKE_CASE : int = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ).prev_sample for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE : Dict = model(__UpperCAmelCase , __UpperCAmelCase ) SCREAMING_SNAKE_CASE : Optional[int] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ).prev_sample return sample def lowerCamelCase_ ( self : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = dict(self.forward_default_kwargs ) SCREAMING_SNAKE_CASE : str = kwargs.pop("""num_inference_steps""" , __UpperCAmelCase ) for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE : Optional[int] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : Union[str, Any] = scheduler_class(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE : List[Any] = self.dummy_sample SCREAMING_SNAKE_CASE : str = 0.1 * sample if num_inference_steps is not None and hasattr(__UpperCAmelCase , """set_timesteps""" ): scheduler.set_timesteps(__UpperCAmelCase ) elif num_inference_steps is not None and not hasattr(__UpperCAmelCase , """set_timesteps""" ): SCREAMING_SNAKE_CASE : str = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) SCREAMING_SNAKE_CASE : str = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] SCREAMING_SNAKE_CASE : Union[str, Any] = dummy_past_residuals[:] SCREAMING_SNAKE_CASE : Tuple = scheduler.timesteps[5] SCREAMING_SNAKE_CASE : int = scheduler.timesteps[6] SCREAMING_SNAKE_CASE : List[str] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : List[Any] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) SCREAMING_SNAKE_CASE : str = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample SCREAMING_SNAKE_CASE : List[Any] = scheduler.step(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowerCamelCase_ ( self : List[Any] ): '''simple docstring''' for timesteps in [1_00, 10_00]: self.check_over_configs(num_train_timesteps=__UpperCAmelCase , time_step=__UpperCAmelCase ) def lowerCamelCase_ ( self : List[Any] ): '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 1_00] ): self.check_over_forward(num_inference_steps=__UpperCAmelCase , time_step=__UpperCAmelCase ) def lowerCamelCase_ ( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.full_loop() SCREAMING_SNAKE_CASE : Tuple = torch.mean(torch.abs(__UpperCAmelCase ) ) assert abs(result_mean.item() - 2_54_05_29 ) < 10
323
'''simple docstring''' from __future__ import annotations import unittest from transformers import RoFormerConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class _UpperCAmelCase : """simple docstring""" def __init__( self : Tuple , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Tuple=13 , __UpperCAmelCase : Optional[int]=7 , __UpperCAmelCase : int=True , __UpperCAmelCase : str=True , __UpperCAmelCase : Union[str, Any]=True , __UpperCAmelCase : str=True , __UpperCAmelCase : List[str]=99 , __UpperCAmelCase : List[str]=32 , __UpperCAmelCase : Union[str, Any]=2 , __UpperCAmelCase : List[str]=4 , __UpperCAmelCase : Optional[Any]=37 , __UpperCAmelCase : Any="gelu" , __UpperCAmelCase : Optional[Any]=0.1 , __UpperCAmelCase : int=0.1 , __UpperCAmelCase : Dict=512 , __UpperCAmelCase : List[Any]=16 , __UpperCAmelCase : List[str]=2 , __UpperCAmelCase : Optional[Any]=0.02 , __UpperCAmelCase : int=3 , __UpperCAmelCase : Dict=4 , __UpperCAmelCase : str=None , ): '''simple docstring''' _A = parent _A = 13 _A = 7 _A = True _A = True _A = True _A = True _A = 99 _A = 32 _A = 2 _A = 4 _A = 37 _A = "gelu" _A = 0.1 _A = 0.1 _A = 512 _A = 16 _A = 2 _A = 0.02 _A = 3 _A = 4 _A = None def lowerCAmelCase ( self : Dict ): '''simple docstring''' _A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A = None if self.use_input_mask: _A = random_attention_mask([self.batch_size, self.seq_length] ) _A = None if self.use_token_type_ids: _A = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _A = None _A = None _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _A = ids_tensor([self.batch_size] , self.num_choices ) _A = RoFormerConfig( 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 , return_dict=__UpperCAmelCase , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : Dict , __UpperCAmelCase : Any , __UpperCAmelCase : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Union[str, Any] ): '''simple docstring''' _A = TFRoFormerModel(config=__UpperCAmelCase ) _A = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _A = [input_ids, input_mask] _A = model(__UpperCAmelCase ) _A = model(__UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase ( self : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Any , __UpperCAmelCase : List[Any] ): '''simple docstring''' _A = True _A = TFRoFormerForCausalLM(config=__UpperCAmelCase ) _A = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } _A = model(__UpperCAmelCase )["logits"] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def lowerCAmelCase ( self : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : str , __UpperCAmelCase : Tuple , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : str ): '''simple docstring''' _A = TFRoFormerForMaskedLM(config=__UpperCAmelCase ) _A = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } _A = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Union[str, Any] ): '''simple docstring''' _A = self.num_labels _A = TFRoFormerForSequenceClassification(config=__UpperCAmelCase ) _A = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } _A = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any] ): '''simple docstring''' _A = self.num_choices _A = TFRoFormerForMultipleChoice(config=__UpperCAmelCase ) _A = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) _A = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) _A = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) _A = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } _A = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase ( self : Dict , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : int , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ): '''simple docstring''' _A = self.num_labels _A = TFRoFormerForTokenClassification(config=__UpperCAmelCase ) _A = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } _A = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Dict , __UpperCAmelCase : int , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Any , __UpperCAmelCase : int , __UpperCAmelCase : int ): '''simple docstring''' _A = TFRoFormerForQuestionAnswering(config=__UpperCAmelCase ) _A = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } _A = model(__UpperCAmelCase ) 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] ): '''simple docstring''' _A = self.prepare_config_and_inputs() ( ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ) = config_and_inputs _A = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class _UpperCAmelCase ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" snake_case = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) snake_case = ( { '''feature-extraction''': TFRoFormerModel, '''fill-mask''': TFRoFormerForMaskedLM, '''question-answering''': TFRoFormerForQuestionAnswering, '''text-classification''': TFRoFormerForSequenceClassification, '''text-generation''': TFRoFormerForCausalLM, '''token-classification''': TFRoFormerForTokenClassification, '''zero-shot''': TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) snake_case = False snake_case = False def lowerCAmelCase ( self : int , __UpperCAmelCase : Dict , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Optional[int] ): '''simple docstring''' if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' _A = TFRoFormerModelTester(self ) _A = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=37 ) def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase ) def lowerCAmelCase ( self : Any ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase ) def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*__UpperCAmelCase ) def lowerCAmelCase ( self : Optional[Any] ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase ) def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase ) def lowerCAmelCase ( self : str ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase ) def lowerCAmelCase ( self : Any ): '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase ) @slow def lowerCAmelCase ( self : Dict ): '''simple docstring''' _A = TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base" ) self.assertIsNotNone(__UpperCAmelCase ) @require_tf class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' _A = TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) _A = tf.constant([[0, 1, 2, 3, 4, 5]] ) _A = model(__UpperCAmelCase )[0] # TODO Replace vocab size _A = 50000 _A = [1, 6, vocab_size] self.assertEqual(output.shape , __UpperCAmelCase ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _A = tf.constant( [ [ [-0.12053341, -1.0264901, 0.29221946], [-1.5133783, 0.197433, 0.15190607], [-5.0135403, -3.900256, -0.84038764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __UpperCAmelCase , atol=1E-4 ) @require_tf class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" snake_case = 1E-4 def lowerCAmelCase ( self : List[str] ): '''simple docstring''' _A = tf.constant([[4, 10]] ) _A = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _A = emba(input_ids.shape ) _A = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) def lowerCAmelCase ( self : Tuple ): '''simple docstring''' _A = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) _A = TFRoFormerSinusoidalPositionalEmbedding(num_positions=512 , embedding_dim=512 ) emba([2, 16, 512] ) _A = emba.weight[:3, :5] tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) @require_tf class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" snake_case = 1E-4 def lowerCAmelCase ( self : str ): '''simple docstring''' _A = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 100 _A = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 100 _A = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _A = embed_positions([2, 16, 768] )[None, None, :, :] _A , _A = TFRoFormerSelfAttention.apply_rotary_position_embeddings( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) _A = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) _A = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance )
79
0
'''simple docstring''' import unittest from dataclasses import dataclass import pytest from accelerate.commands.config.config_args import SageMakerConfig from accelerate.utils import ComputeEnvironment from accelerate.utils.launch import _convert_nargs_to_dict @dataclass class A__ ( A__ ): A__ = ComputeEnvironment.AMAZON_SAGEMAKER A__ = True A__ = 'ml.p3.2xlarge' A__ = 'accelerate_sagemaker_execution_role' A__ = 'hf-sm' A__ = 'us-east-1' A__ = 1 A__ = 'accelerate-sagemaker-1' A__ = '1.6' A__ = '4.4' A__ = 'train.py' A__ = [ '--model_name_or_path', 'bert', '--do_train', 'False', '--epochs', '3', '--learning_rate', '5e-5', '--max_steps', '50.5', ] A__ = [ '--model_name_or_path', 'bert', '--do_train', '--do_test', 'False', '--do_predict', '--epochs', '3', '--learning_rate', '5e-5', '--max_steps', '50.5', ] class A__ ( unittest.TestCase ): def A ( self : int ) -> Optional[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE =_convert_nargs_to_dict(MockLaunchConfig.success_training_script_args ) assert isinstance(converted_args['model_name_or_path'] , _a ) assert isinstance(converted_args['do_train'] , _a ) assert isinstance(converted_args['epochs'] , _a ) assert isinstance(converted_args['learning_rate'] , _a ) assert isinstance(converted_args['max_steps'] , _a ) with pytest.raises(_a ): _convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args )
114
'''simple docstring''' def _lowerCAmelCase ( _UpperCamelCase : str ) -> bool: """simple docstring""" _SCREAMING_SNAKE_CASE =0 for ch in input_str: _SCREAMING_SNAKE_CASE =ord(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =pow(2 , _UpperCamelCase ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
114
1
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , __a , __a=13 , __a=7 , __a=True , __a=True , __a=True , __a=True , __a=99 , __a=32 , __a=5 , __a=4 , __a=37 , __a="gelu" , __a=0.1 , __a=0.1 , __a=5_12 , __a=16 , __a=2 , __a=0.0_2 , __a=4 , ): __lowerCAmelCase = parent __lowerCAmelCase = batch_size __lowerCAmelCase = seq_length __lowerCAmelCase = is_training __lowerCAmelCase = use_attention_mask __lowerCAmelCase = use_token_type_ids __lowerCAmelCase = use_labels __lowerCAmelCase = vocab_size __lowerCAmelCase = hidden_size __lowerCAmelCase = num_hidden_layers __lowerCAmelCase = num_attention_heads __lowerCAmelCase = intermediate_size __lowerCAmelCase = hidden_act __lowerCAmelCase = hidden_dropout_prob __lowerCAmelCase = attention_probs_dropout_prob __lowerCAmelCase = max_position_embeddings __lowerCAmelCase = type_vocab_size __lowerCAmelCase = type_sequence_label_size __lowerCAmelCase = initializer_range __lowerCAmelCase = num_choices def snake_case ( self ): __lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase = None if self.use_attention_mask: __lowerCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase = None if self.use_token_type_ids: __lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def snake_case ( self ): __lowerCAmelCase = self.prepare_config_and_inputs() __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = config_and_inputs __lowerCAmelCase = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict @require_flax class _UpperCamelCase ( lowerCAmelCase__ ,unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] =True __UpperCAmelCase : List[Any] =( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def snake_case ( self ): __lowerCAmelCase = FlaxRoFormerModelTester(self ) @slow def snake_case ( self ): for model_class_name in self.all_model_classes: __lowerCAmelCase = model_class_name.from_pretrained("junnyu/roformer_chinese_small" , from_pt=__a ) __lowerCAmelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(__a ) @require_flax class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' @slow def snake_case ( self ): __lowerCAmelCase = FlaxRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) __lowerCAmelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) __lowerCAmelCase = model(__a )[0] __lowerCAmelCase = 5_00_00 __lowerCAmelCase = (1, 6, vocab_size) self.assertEqual(output.shape , __a ) __lowerCAmelCase = jnp.array( [[[-0.1_2_0_5, -1.0_2_6_5, 0.2_9_2_2], [-1.5_1_3_4, 0.1_9_7_4, 0.1_5_1_9], [-5.0_1_3_5, -3.9_0_0_3, -0.8_4_0_4]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __a , atol=1e-4 ) )
57
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _UpperCamelCase ( lowerCAmelCase__ ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] =["""image_processor""", """tokenizer"""] __UpperCAmelCase : Optional[Any] ="""CLIPImageProcessor""" __UpperCAmelCase : Union[str, Any] =("""XLMRobertaTokenizer""", """XLMRobertaTokenizerFast""") def __init__( self , __a=None , __a=None , **__a ): __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." , __a , ) __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__(__a , __a ) def __call__( self , __a=None , __a=None , __a=None , **__a ): 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(__a , return_tensors=__a , **__a ) if images is not None: __lowerCAmelCase = self.image_processor(__a , return_tensors=__a , **__a ) 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(**__a ) , tensor_type=__a ) def snake_case ( self , *__a , **__a ): return self.tokenizer.batch_decode(*__a , **__a ) def snake_case ( self , *__a , **__a ): return self.tokenizer.decode(*__a , **__a ) @property def snake_case ( self ): __lowerCAmelCase = self.tokenizer.model_input_names __lowerCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
57
1
"""simple docstring""" from graphs.minimum_spanning_tree_kruskal import kruskal def __SCREAMING_SNAKE_CASE ( ): lowerCAmelCase__ : int = 9 lowerCAmelCase__ : int = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] lowerCAmelCase__ : Tuple = kruskal(A_ , A_ ) lowerCAmelCase__ : List[Any] = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] assert sorted(A_ ) == sorted(A_ )
368
"""simple docstring""" from __future__ import annotations import math __UpperCamelCase : Dict = '''2020.9.26''' __UpperCamelCase : Tuple = '''xcodz-dot, cclaus, dhruvmanila''' def __SCREAMING_SNAKE_CASE ( A_ , A_ , A_ , A_ , A_ ): if not all(isinstance(A_ , (float, int) ) for val in locals().values() ): lowerCAmelCase__ : Optional[Any] = f'Input values must either be float or int: {list(locals().values() )}' raise TypeError(A_ ) lowerCAmelCase__ : Optional[Any] = ((x * distance) / (z + distance)) * scale lowerCAmelCase__ : Optional[int] = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def __SCREAMING_SNAKE_CASE ( A_ , A_ , A_ , A_ , A_ ): if not isinstance(A_ , A_ ): raise TypeError('''Axis must be a str''' ) lowerCAmelCase__ : str = locals() del input_variables["axis"] if not all(isinstance(A_ , (float, int) ) for val in input_variables.values() ): lowerCAmelCase__ : int = ( '''Input values except axis must either be float or int: ''' f'{list(input_variables.values() )}' ) raise TypeError(A_ ) lowerCAmelCase__ : Any = (angle % 3_60) / 4_50 * 1_80 / math.pi if axis == "z": lowerCAmelCase__ : Tuple = x * math.cos(A_ ) - y * math.sin(A_ ) lowerCAmelCase__ : List[str] = y * math.cos(A_ ) + x * math.sin(A_ ) lowerCAmelCase__ : Optional[Any] = z elif axis == "x": lowerCAmelCase__ : List[str] = y * math.cos(A_ ) - z * math.sin(A_ ) lowerCAmelCase__ : str = z * math.cos(A_ ) + y * math.sin(A_ ) lowerCAmelCase__ : Union[str, Any] = x elif axis == "y": lowerCAmelCase__ : Optional[int] = x * math.cos(A_ ) - z * math.sin(A_ ) lowerCAmelCase__ : Tuple = z * math.cos(A_ ) + x * math.sin(A_ ) lowerCAmelCase__ : Optional[int] = y else: raise ValueError('''not a valid axis, choose one of \'x\', \'y\', \'z\'''' ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(F'''{convert_to_ad(1.0, 2.0, 3.0, 1_0.0, 1_0.0) = }''') print(F'''{rotate(1.0, 2.0, 3.0, 'y', 9_0.0) = }''')
74
0
from PIL import Image def lowerCAmelCase_ ( __a ) -> Image: """simple docstring""" lowerCamelCase__ , lowerCamelCase__: Tuple =image.size lowerCamelCase__: Optional[Any] =0 lowerCamelCase__: List[str] =image.load() for i in range(__a ): for j in range(__a ): lowerCamelCase__: Optional[Any] =pixels[j, i] mean += pixel mean //= width * height for j in range(__a ): for i in range(__a ): lowerCamelCase__: int =255 if pixels[i, j] > mean else 0 return image if __name__ == "__main__": __A = mean_threshold(Image.open("path_to_image").convert("L")) image.save("output_image_path")
10
'''simple docstring''' def lowerCAmelCase_ ( snake_case_ : int , snake_case_ : int ) -> str: '''simple docstring''' if a < 0 or b < 0: raise ValueError("the value of both inputs must be positive" ) UpperCAmelCase_ = str(bin(snake_case_ ) )[2:] # remove the leading "0b" UpperCAmelCase_ = str(bin(snake_case_ ) )[2:] UpperCAmelCase_ = max(len(snake_case_ ) , len(snake_case_ ) ) return "0b" + "".join( str(int("1" in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(snake_case_ ) , b_binary.zfill(snake_case_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
1
0
"""simple docstring""" from __future__ import annotations import requests lowerCamelCase__ = set( "approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports".split() ) def lowercase__ ( lowercase_ ,lowercase_ = 1 ,lowercase_ = "new" ,lowercase_ = None ) -> dict: """simple docstring""" _UpperCamelCase : str = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(lowercase_ ) - valid_terms ) ): _UpperCamelCase : List[Any] = F'''Invalid search term: {invalid_search_terms}''' raise ValueError(lowercase_ ) _UpperCamelCase : List[Any] = requests.get( F'''https://reddit.com/r/{subreddit}/{age}.json?limit={limit}''' ,headers={"User-agent": "A random string"} ,) if response.status_code == 429: raise requests.HTTPError _UpperCamelCase : Optional[Any] = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(lowercase_ )} _UpperCamelCase : List[str] = {} for id_ in range(lowercase_ ): _UpperCamelCase : Dict = { item: data["data"]["children"][id_]["data"][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data("learnpython", wanted_data=["title", "url", "selftext"]))
356
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(">=", "4.25.0")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
310
0
'''simple docstring''' import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def __lowerCamelCase ( __snake_case : List[Any], __snake_case : Optional[int], __snake_case : str ) -> Any: """simple docstring""" A__ : List[Any] =OmegaConf.load(__snake_case ) A__ : Tuple =torch.load(__snake_case, map_location="""cpu""" )["""model"""] A__ : Tuple =list(state_dict.keys() ) # extract state_dict for VQVAE A__ : str ={} A__ : Optional[Any] ="""first_stage_model.""" for key in keys: if key.startswith(__snake_case ): A__ : Tuple =state_dict[key] # extract state_dict for UNetLDM A__ : int ={} A__ : Optional[int] ="""model.diffusion_model.""" for key in keys: if key.startswith(__snake_case ): A__ : Dict =state_dict[key] A__ : Optional[Any] =config.model.params.first_stage_config.params A__ : Tuple =config.model.params.unet_config.params A__ : Optional[int] =VQModel(**__snake_case ).eval() vqvae.load_state_dict(__snake_case ) A__ : Dict =UNetLDMModel(**__snake_case ).eval() unet.load_state_dict(__snake_case ) A__ : Tuple =DDIMScheduler( timesteps=config.model.params.timesteps, beta_schedule="""scaled_linear""", beta_start=config.model.params.linear_start, beta_end=config.model.params.linear_end, clip_sample=__snake_case, ) A__ : Optional[int] =LDMPipeline(__snake_case, __snake_case, __snake_case ) pipeline.save_pretrained(__snake_case ) if __name__ == "__main__": __snake_case : Any = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', type=str, required=True) parser.add_argument('--config_path', type=str, required=True) parser.add_argument('--output_path', type=str, required=True) __snake_case : List[str] = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
134
'''simple docstring''' from ..utils import DummyObject, requires_backends class lowerCamelCase ( metaclass=lowercase_ ): '''simple docstring''' __snake_case = ['speech'] def __init__( self : Tuple , *lowerCAmelCase_ : List[str] , **lowerCAmelCase_ : Dict ) -> int: '''simple docstring''' requires_backends(self , ["""speech"""] ) class lowerCamelCase ( metaclass=lowercase_ ): '''simple docstring''' __snake_case = ['speech'] def __init__( self : Union[str, Any] , *lowerCAmelCase_ : Tuple , **lowerCAmelCase_ : List[str] ) -> str: '''simple docstring''' requires_backends(self , ["""speech"""] )
134
1
"""simple docstring""" 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: UpperCAmelCase = None UpperCAmelCase = logging.get_logger(__name__) UpperCAmelCase = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} UpperCAmelCase = { """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""", }, } UpperCAmelCase = { """google/fnet-base""": 512, """google/fnet-large""": 512, } UpperCAmelCase = """▁""" class UpperCAmelCase_ ( _lowercase): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ['''input_ids''', '''token_type_ids'''] snake_case__ = FNetTokenizer def __init__( self : int , __UpperCamelCase : str=None , __UpperCamelCase : Any=None , __UpperCamelCase : Dict=False , __UpperCamelCase : Dict=True , __UpperCamelCase : List[Any]=True , __UpperCamelCase : Union[str, Any]="<unk>" , __UpperCamelCase : str="[SEP]" , __UpperCamelCase : List[Any]="<pad>" , __UpperCamelCase : Optional[int]="[CLS]" , __UpperCamelCase : str="[MASK]" , **__UpperCamelCase : List[Any] , ) -> List[str]: # 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. _UpperCamelCase = ( AddedToken(__UpperCamelCase , lstrip=__UpperCamelCase , rstrip=__UpperCamelCase , normalized=__UpperCamelCase ) if isinstance(__UpperCamelCase , __UpperCamelCase ) else mask_token ) super().__init__( __UpperCamelCase , tokenizer_file=__UpperCamelCase , do_lower_case=__UpperCamelCase , remove_space=__UpperCamelCase , keep_accents=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , pad_token=__UpperCamelCase , cls_token=__UpperCamelCase , mask_token=__UpperCamelCase , **__UpperCamelCase , ) _UpperCamelCase = do_lower_case _UpperCamelCase = remove_space _UpperCamelCase = keep_accents _UpperCamelCase = vocab_file _UpperCamelCase = False if not self.vocab_file else True def _UpperCamelCase ( self : Optional[int] , __UpperCamelCase : List[int] , __UpperCamelCase : Optional[List[int]] = None ) -> List[int]: _UpperCamelCase = [self.sep_token_id] _UpperCamelCase = [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 _UpperCamelCase ( self : List[Any] , __UpperCamelCase : List[int] , __UpperCamelCase : Optional[List[int]] = None ) -> List[int]: _UpperCamelCase = [self.sep_token_id] _UpperCamelCase = [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 _UpperCamelCase ( self : Dict , __UpperCamelCase : str , __UpperCamelCase : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__UpperCamelCase ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _UpperCamelCase = os.path.join( __UpperCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCamelCase ): copyfile(self.vocab_file , __UpperCamelCase ) return (out_vocab_file,)
351
"""simple docstring""" import copy import json import os import tempfile from transformers import is_torch_available from .test_configuration_utils import config_common_kwargs class UpperCAmelCase_ ( _lowercase): def __init__( self : Any , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Dict=None , __UpperCamelCase : Optional[Any]=True , __UpperCamelCase : Optional[Any]=None , **__UpperCamelCase : Any ) -> Dict: _UpperCamelCase = parent _UpperCamelCase = config_class _UpperCamelCase = has_text_modality _UpperCamelCase = kwargs _UpperCamelCase = common_properties def _UpperCamelCase ( self : Optional[Any] ) -> List[str]: _UpperCamelCase = self.config_class(**self.inputs_dict ) _UpperCamelCase = ( ['''hidden_size''', '''num_attention_heads''', '''num_hidden_layers'''] if self.common_properties is None else self.common_properties ) # Add common fields for text models if self.has_text_modality: common_properties.extend(['''vocab_size'''] ) # Test that config has the common properties as getters for prop in common_properties: self.parent.assertTrue(hasattr(__UpperCamelCase , __UpperCamelCase ) , msg=F'''`{prop}` does not exist''' ) # Test that config has the common properties as setter for idx, name in enumerate(__UpperCamelCase ): try: setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) self.parent.assertEqual( getattr(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase , msg=F'''`{name} value {idx} expected, but was {getattr(__UpperCamelCase , __UpperCamelCase )}''' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass # Test if config class can be called with Config(prop_name=..) for idx, name in enumerate(__UpperCamelCase ): try: _UpperCamelCase = self.config_class(**{name: idx} ) self.parent.assertEqual( getattr(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase , msg=F'''`{name} value {idx} expected, but was {getattr(__UpperCamelCase , __UpperCamelCase )}''' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass def _UpperCamelCase ( self : Any ) -> List[str]: _UpperCamelCase = self.config_class(**self.inputs_dict ) _UpperCamelCase = json.loads(config.to_json_string() ) for key, value in self.inputs_dict.items(): self.parent.assertEqual(obj[key] , __UpperCamelCase ) def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: _UpperCamelCase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _UpperCamelCase = os.path.join(__UpperCamelCase , '''config.json''' ) config_first.to_json_file(__UpperCamelCase ) _UpperCamelCase = self.config_class.from_json_file(__UpperCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _UpperCamelCase ( self : int ) -> List[str]: _UpperCamelCase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: config_first.save_pretrained(__UpperCamelCase ) _UpperCamelCase = self.config_class.from_pretrained(__UpperCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _UpperCamelCase ( self : Dict ) -> Any: _UpperCamelCase = self.config_class(**self.inputs_dict ) _UpperCamelCase = '''test''' with tempfile.TemporaryDirectory() as tmpdirname: _UpperCamelCase = os.path.join(__UpperCamelCase , __UpperCamelCase ) config_first.save_pretrained(__UpperCamelCase ) _UpperCamelCase = self.config_class.from_pretrained(__UpperCamelCase , subfolder=__UpperCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _UpperCamelCase ( self : Dict ) -> int: _UpperCamelCase = self.config_class(**self.inputs_dict , num_labels=5 ) self.parent.assertEqual(len(config.idalabel ) , 5 ) self.parent.assertEqual(len(config.labelaid ) , 5 ) _UpperCamelCase = 3 self.parent.assertEqual(len(config.idalabel ) , 3 ) self.parent.assertEqual(len(config.labelaid ) , 3 ) def _UpperCamelCase ( self : Any ) -> str: if self.config_class.is_composition: return _UpperCamelCase = self.config_class() self.parent.assertIsNotNone(__UpperCamelCase ) def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: _UpperCamelCase = copy.deepcopy(__UpperCamelCase ) _UpperCamelCase = self.config_class(**__UpperCamelCase ) _UpperCamelCase = [] for key, value in config_common_kwargs.items(): if key == "torch_dtype": if not is_torch_available(): continue else: import torch if config.torch_dtype != torch.floataa: wrong_values.append(('''torch_dtype''', config.torch_dtype, torch.floataa) ) elif getattr(__UpperCamelCase , __UpperCamelCase ) != value: wrong_values.append((key, getattr(__UpperCamelCase , __UpperCamelCase ), value) ) if len(__UpperCamelCase ) > 0: _UpperCamelCase = '''\n'''.join([F'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values] ) raise ValueError(F'''The following keys were not properly set in the config:\n{errors}''' ) def _UpperCamelCase ( self : Tuple ) -> int: self.create_and_test_config_common_properties() self.create_and_test_config_to_json_string() self.create_and_test_config_to_json_file() self.create_and_test_config_from_and_save_pretrained() self.create_and_test_config_from_and_save_pretrained_subfolder() self.create_and_test_config_with_num_labels() self.check_config_can_be_init_without_params() self.check_config_arguments_init()
54
0
import math lowerCAmelCase : Union[str, Any] = 10 lowerCAmelCase : List[str] = 7 lowerCAmelCase : Dict = BALLS_PER_COLOUR * NUM_COLOURS def A_ ( _UpperCAmelCase = 20 ): SCREAMING_SNAKE_CASE_: Any = math.comb(UpperCamelCase_ , UpperCamelCase_ ) SCREAMING_SNAKE_CASE_: Union[str, Any] = math.comb(NUM_BALLS - BALLS_PER_COLOUR , UpperCamelCase_ ) SCREAMING_SNAKE_CASE_: Union[str, Any] = NUM_COLOURS * (1 - missing_colour / total) return f"{result:.9f}" if __name__ == "__main__": print(solution(20))
13
"""simple docstring""" def _A ( UpperCamelCase_ : list[int]) -> float: '''simple docstring''' if not nums: # Makes sure that the list is not empty raise ValueError("List is empty") __lowercase = sum(UpperCamelCase_) / len(UpperCamelCase_) # Calculate the average return sum(abs(x - average) for x in nums) / len(UpperCamelCase_) if __name__ == "__main__": import doctest doctest.testmod()
17
0
import math import os import unittest from transformers import MegatronBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, ) class _A : def __init__( self : Dict , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int]=13 , __SCREAMING_SNAKE_CASE : Optional[int]=7 , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : int=99 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : str=32 , __SCREAMING_SNAKE_CASE : str=5 , __SCREAMING_SNAKE_CASE : int=4 , __SCREAMING_SNAKE_CASE : str=37 , __SCREAMING_SNAKE_CASE : str="gelu" , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : str=0.1 , __SCREAMING_SNAKE_CASE : Any=512 , __SCREAMING_SNAKE_CASE : Optional[int]=16 , __SCREAMING_SNAKE_CASE : List[Any]=2 , __SCREAMING_SNAKE_CASE : Optional[int]=0.02 , __SCREAMING_SNAKE_CASE : Tuple=3 , __SCREAMING_SNAKE_CASE : int=4 , __SCREAMING_SNAKE_CASE : Dict=None , ): '''simple docstring''' __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_input_mask __a = use_token_type_ids __a = use_labels __a = vocab_size __a = hidden_size __a = embedding_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 = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope def _lowerCamelCase ( self : Any): '''simple docstring''' __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length]) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) __a = ids_tensor([self.batch_size] , self.num_choices) __a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : List[Any]): '''simple docstring''' return MegatronBertConfig( 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 , embedding_size=self.embedding_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' __a = MegatronBertModel(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE) __a = model(__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE) __a = model(__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size)) def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' __a = MegatronBertForMaskedLM(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def _lowerCamelCase ( self : List[str] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int]): '''simple docstring''' __a = MegatronBertForCausalLM(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Any): '''simple docstring''' __a = MegatronBertForNextSentencePrediction(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2)) def _lowerCamelCase ( self : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str): '''simple docstring''' __a = MegatronBertForPreTraining(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , next_sentence_label=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2)) def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[Any]): '''simple docstring''' __a = MegatronBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , start_positions=__SCREAMING_SNAKE_CASE , end_positions=__SCREAMING_SNAKE_CASE , ) 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 : int , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Any): '''simple docstring''' __a = self.num_labels __a = MegatronBertForSequenceClassification(__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any]): '''simple docstring''' __a = self.num_labels __a = MegatronBertForTokenClassification(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int): '''simple docstring''' __a = self.num_choices __a = MegatronBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() __a = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() __a = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() __a = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class _A ( __UpperCAmelCase ,__UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : List[str] = ( ( MegatronBertModel, MegatronBertForMaskedLM, MegatronBertForCausalLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, ) if is_torch_available() else () ) UpperCamelCase__ : str = ( { '''feature-extraction''': MegatronBertModel, '''fill-mask''': MegatronBertForMaskedLM, '''question-answering''': MegatronBertForQuestionAnswering, '''text-classification''': MegatronBertForSequenceClassification, '''text-generation''': MegatronBertForCausalLM, '''token-classification''': MegatronBertForTokenClassification, '''zero-shot''': MegatronBertForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase__ : Dict = True # test_resize_embeddings = False UpperCamelCase__ : Tuple = False def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : int=False): '''simple docstring''' __a = super()._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE) if return_labels: if model_class in get_values(__SCREAMING_SNAKE_CASE): __a = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=__SCREAMING_SNAKE_CASE) __a = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__SCREAMING_SNAKE_CASE) return inputs_dict def _lowerCamelCase ( self : str): '''simple docstring''' __a = MegatronBertModelTester(self) __a = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37) def _lowerCamelCase ( self : Dict): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : Dict): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_model(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_pretraining(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_question_answering(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_token_classification(*__SCREAMING_SNAKE_CASE) def __snake_case ( _UpperCAmelCase ): return torch.tensor( _UpperCAmelCase , dtype=torch.long , device=_UpperCAmelCase , ) __snake_case :Any = 1E-4 @require_torch @require_sentencepiece @require_tokenizers class _A ( unittest.TestCase ): @slow @unittest.skip('''Model is not available.''') def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = '''nvidia/megatron-bert-uncased-345m''' if "MYDIR" in os.environ: __a = os.path.join(os.environ['''MYDIR'''] , __SCREAMING_SNAKE_CASE) __a = MegatronBertModel.from_pretrained(__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.half() __a = _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]) with torch.no_grad(): __a = model(__SCREAMING_SNAKE_CASE)[0] __a = torch.Size((1, 9, 1_024)) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE) __a = [-0.60_40, -0.25_17, -0.10_25, 0.34_20, -0.67_58, -0.00_17, -0.10_89, -0.19_90, 0.57_28] for ii in range(3): for jj in range(3): __a = output[0, ii, jj] __a = expected[3 * ii + jj] __a = '''ii={} jj={} a={} b={}'''.format(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) self.assertTrue(math.isclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , rel_tol=__SCREAMING_SNAKE_CASE , abs_tol=__SCREAMING_SNAKE_CASE) , msg=__SCREAMING_SNAKE_CASE)
131
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def __snake_case ( _UpperCAmelCase ): if isinstance(_UpperCAmelCase , collections.abc.Iterable ): return x return (x, x) @require_tf class _A : def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Any): '''simple docstring''' pass def _lowerCamelCase ( self : Any): '''simple docstring''' pass def _lowerCamelCase ( self : Dict): '''simple docstring''' pass def _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any]=None , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a = VisionTextDualEncoderConfig.from_vision_text_configs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel(__SCREAMING_SNAKE_CASE) __a = model(input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) 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 _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : List[str]=None , **__SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' __a , __a = self.get_vision_text_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel(vision_model=__SCREAMING_SNAKE_CASE , text_model=__SCREAMING_SNAKE_CASE) __a = model(input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) 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 _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int]=None , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a , __a = self.get_vision_text_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = {'''vision_model''': vision_model, '''text_model''': text_model} __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__SCREAMING_SNAKE_CASE) __a = model(input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) 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 _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Dict=None , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a , __a = self.get_vision_text_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel(vision_model=__SCREAMING_SNAKE_CASE , text_model=__SCREAMING_SNAKE_CASE) __a = model(input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) __a = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel.from_pretrained(__SCREAMING_SNAKE_CASE) __a = model(input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) __a = after_output[0].numpy() __a = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1E-5) def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : int=None , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a , __a = self.get_vision_text_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel(vision_model=__SCREAMING_SNAKE_CASE , text_model=__SCREAMING_SNAKE_CASE) __a = model( input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE) __a = output.vision_model_output.attentions self.assertEqual(len(__SCREAMING_SNAKE_CASE) , vision_config.num_hidden_layers) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) __a = to_atuple(vision_model.config.image_size) __a = to_atuple(vision_model.config.patch_size) __a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) __a = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) __a = output.text_model_output.attentions self.assertEqual(len(__SCREAMING_SNAKE_CASE) , 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 _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : float): '''simple docstring''' __a = np.abs((a - b)).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , F'Difference between torch and flax is {diff} (>= {tol}).') def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' __a = self.prepare_config_and_inputs() self.check_save_load(**__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**__SCREAMING_SNAKE_CASE) @slow def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a , __a = self.get_pretrained_model_and_inputs() __a = model_a(**__SCREAMING_SNAKE_CASE) __a = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(__SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel.from_pretrained(__SCREAMING_SNAKE_CASE) __a = model_a(**__SCREAMING_SNAKE_CASE) __a = after_outputs[0].numpy() __a = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1E-5) @require_tf class _A ( __UpperCAmelCase ,unittest.TestCase ): def _lowerCamelCase ( self : Dict): '''simple docstring''' __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''hf-internal-testing/tiny-random-vit''' , '''hf-internal-testing/tiny-random-bert''') __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) __a = random_attention_mask([batch_size, 4]) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : str): '''simple docstring''' __a = TFViTModel(__SCREAMING_SNAKE_CASE , name='''vision_model''') __a = TFBertModel(__SCREAMING_SNAKE_CASE , name='''text_model''') return vision_model, text_model def _lowerCamelCase ( self : Dict): '''simple docstring''' __a = TFViTModelTester(self) __a = TFBertModelTester(self) __a = vit_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class _A ( __UpperCAmelCase ,unittest.TestCase ): def _lowerCamelCase ( self : Any): '''simple docstring''' __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-deit-tf''' , '''hf-internal-testing/tiny-random-roberta''') __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) __a = random_attention_mask([batch_size, 4]) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any]=None , **__SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' __a , __a = self.get_vision_text_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = TFVisionTextDualEncoderModel(vision_model=__SCREAMING_SNAKE_CASE , text_model=__SCREAMING_SNAKE_CASE) __a = model( input_ids=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE) __a = output.vision_model_output.attentions self.assertEqual(len(__SCREAMING_SNAKE_CASE) , vision_config.num_hidden_layers) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) __a = to_atuple(vision_model.config.image_size) __a = to_atuple(vision_model.config.patch_size) __a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) __a = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) __a = output.text_model_output.attentions self.assertEqual(len(__SCREAMING_SNAKE_CASE) , 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 _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' __a = TFDeiTModel(__SCREAMING_SNAKE_CASE , name='''vision_model''') __a = TFRobertaModel(__SCREAMING_SNAKE_CASE , name='''text_model''') return vision_model, text_model def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = TFDeiTModelTester(self) __a = TFRobertaModelTester(self) __a = vit_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class _A ( __UpperCAmelCase ,unittest.TestCase ): def _lowerCamelCase ( self : Any): '''simple docstring''' __a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-clip-tf''' , '''hf-internal-testing/tiny-random-bert''') __a = 13 __a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) __a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) __a = random_attention_mask([batch_size, 4]) __a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' __a = TFCLIPVisionModel(__SCREAMING_SNAKE_CASE , name='''vision_model''') __a = TFBertModel(__SCREAMING_SNAKE_CASE , name='''text_model''') return vision_model, text_model def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = TFCLIPVisionModelTester(self) __a = TFBertModelTester(self) __a = clip_model_tester.prepare_config_and_inputs() __a = bert_model_tester.prepare_config_and_inputs() __a , __a = vision_config_and_inputs ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class _A ( unittest.TestCase ): @slow def _lowerCamelCase ( self : str): '''simple docstring''' __a = TFVisionTextDualEncoderModel.from_pretrained( '''clip-italian/clip-italian''' , logit_scale_init_value=1.0 , from_pt=__SCREAMING_SNAKE_CASE) __a = VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian''') __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') __a = processor( text=['''una foto di un gatto''', '''una foto di un cane'''] , images=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors='''np''') __a = model(**__SCREAMING_SNAKE_CASE) # 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]) , ) __a = np.array([[1.2_28_47_27, 0.3_10_41_22]]) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , __SCREAMING_SNAKE_CASE , atol=1E-3))
131
1
"""simple docstring""" from pathlib import Path import numpy as np from PIL import Image def A_ ( _lowerCAmelCase : np.ndarray ): """simple docstring""" _a , _a , _a = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b def A_ ( _lowerCAmelCase : np.ndarray ): """simple docstring""" return (gray > 1_27) & (gray <= 2_55) def A_ ( _lowerCAmelCase : np.ndarray, _lowerCAmelCase : np.ndarray ): """simple docstring""" _a = np.zeros_like(_lowerCAmelCase ) _a = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image _a = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): _a = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() _a = int(summation > 0 ) return output if __name__ == "__main__": # read original image __snake_case = Path(__file__).resolve().parent / '''image_data''' / '''lena.jpg''' __snake_case = np.array(Image.open(lena_path)) # kernel to be applied __snake_case = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) __snake_case = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image __snake_case = Image.fromarray(output).convert('''RGB''') pil_img.save('''result_dilation.png''')
320
"""simple docstring""" import re import string from collections import Counter import sacrebleu import sacremoses from packaging import version import datasets __snake_case = ''' @inproceedings{xu-etal-2016-optimizing, title = {Optimizing Statistical Machine Translation for Text Simplification}, authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris}, journal = {Transactions of the Association for Computational Linguistics}, volume = {4}, year={2016}, url = {https://www.aclweb.org/anthology/Q16-1029}, pages = {401--415 }, @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' __snake_case = '''\ WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU It can be used to evaluate the quality of machine-generated texts. ''' __snake_case = ''' Calculates sari score (between 0 and 100) given a list of source and predicted sentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score. Args: sources: list of source sentences where each sentence should be a string. predictions: list of predicted sentences where each sentence should be a string. references: list of lists of reference sentences where each sentence should be a string. Returns: sari: sari score sacrebleu: sacrebleu score exact: exact score Examples: >>> sources=["About 95 species are currently accepted ."] >>> predictions=["About 95 you now get in ."] >>> references=[["About 95 species are currently known ."]] >>> wiki_split = datasets.load_metric("wiki_split") >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references) >>> print(results) {\'sari\': 21.805555555555557, \'sacrebleu\': 14.535768424205482, \'exact\': 0.0} ''' def A_ ( _lowerCAmelCase : List[str] ): """simple docstring""" def remove_articles(_lowerCAmelCase : Optional[int] ): _a = re.compile(R'''\b(a|an|the)\b''', re.UNICODE ) return re.sub(_lowerCAmelCase, ''' ''', _lowerCAmelCase ) def white_space_fix(_lowerCAmelCase : Tuple ): return " ".join(text.split() ) def remove_punc(_lowerCAmelCase : Tuple ): _a = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(_lowerCAmelCase : List[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(_lowerCAmelCase ) ) ) ) def A_ ( _lowerCAmelCase : List[Any], _lowerCAmelCase : Optional[Any] ): """simple docstring""" return int(normalize_answer(_lowerCAmelCase ) == normalize_answer(_lowerCAmelCase ) ) def A_ ( _lowerCAmelCase : Tuple, _lowerCAmelCase : Any ): """simple docstring""" _a = [any(compute_exact(_lowerCAmelCase, _lowerCAmelCase ) for ref in refs ) for pred, refs in zip(_lowerCAmelCase, _lowerCAmelCase )] return (sum(_lowerCAmelCase ) / len(_lowerCAmelCase )) * 1_00 def A_ ( _lowerCAmelCase : List[str], _lowerCAmelCase : List[Any], _lowerCAmelCase : str, _lowerCAmelCase : str ): """simple docstring""" _a = [rgram for rgrams in rgramslist for rgram in rgrams] _a = Counter(_lowerCAmelCase ) _a = Counter(_lowerCAmelCase ) _a = Counter() for sgram, scount in sgramcounter.items(): _a = scount * numref _a = Counter(_lowerCAmelCase ) _a = Counter() for cgram, ccount in cgramcounter.items(): _a = ccount * numref # KEEP _a = sgramcounter_rep & cgramcounter_rep _a = keepgramcounter_rep & rgramcounter _a = sgramcounter_rep & rgramcounter _a = 0 _a = 0 for keepgram in keepgramcountergood_rep: keeptmpscorea += keepgramcountergood_rep[keepgram] / keepgramcounter_rep[keepgram] # Fix an alleged bug [2] in the keep score computation. # keeptmpscore2 += keepgramcountergood_rep[keepgram] / keepgramcounterall_rep[keepgram] keeptmpscorea += keepgramcountergood_rep[keepgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a = 1 _a = 1 if len(_lowerCAmelCase ) > 0: _a = keeptmpscorea / len(_lowerCAmelCase ) if len(_lowerCAmelCase ) > 0: # Fix an alleged bug [2] in the keep score computation. # keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep) _a = keeptmpscorea / sum(keepgramcounterall_rep.values() ) _a = 0 if keepscore_precision > 0 or keepscore_recall > 0: _a = 2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall) # DELETION _a = sgramcounter_rep - cgramcounter_rep _a = delgramcounter_rep - rgramcounter _a = sgramcounter_rep - rgramcounter _a = 0 _a = 0 for delgram in delgramcountergood_rep: deltmpscorea += delgramcountergood_rep[delgram] / delgramcounter_rep[delgram] deltmpscorea += delgramcountergood_rep[delgram] / delgramcounterall_rep[delgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a = 1 if len(_lowerCAmelCase ) > 0: _a = deltmpscorea / len(_lowerCAmelCase ) # ADDITION _a = set(_lowerCAmelCase ) - set(_lowerCAmelCase ) _a = set(_lowerCAmelCase ) & set(_lowerCAmelCase ) _a = set(_lowerCAmelCase ) - set(_lowerCAmelCase ) _a = 0 for addgram in addgramcountergood: addtmpscore += 1 # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a = 1 _a = 1 if len(_lowerCAmelCase ) > 0: _a = addtmpscore / len(_lowerCAmelCase ) if len(_lowerCAmelCase ) > 0: _a = addtmpscore / len(_lowerCAmelCase ) _a = 0 if addscore_precision > 0 or addscore_recall > 0: _a = 2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall) return (keepscore, delscore_precision, addscore) def A_ ( _lowerCAmelCase : Tuple, _lowerCAmelCase : Dict, _lowerCAmelCase : Any ): """simple docstring""" _a = len(_lowerCAmelCase ) _a = ssent.split(''' ''' ) _a = csent.split(''' ''' ) _a = [] _a = [] _a = [] _a = [] _a = [] _a = [] _a = [] _a = [] _a = [] _a = [] for rsent in rsents: _a = rsent.split(''' ''' ) _a = [] _a = [] _a = [] ragramslist.append(_lowerCAmelCase ) for i in range(0, len(_lowerCAmelCase ) - 1 ): if i < len(_lowerCAmelCase ) - 1: _a = ragrams[i] + ''' ''' + ragrams[i + 1] ragrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 2: _a = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] ragrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 3: _a = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] + ''' ''' + ragrams[i + 3] ragrams.append(_lowerCAmelCase ) ragramslist.append(_lowerCAmelCase ) ragramslist.append(_lowerCAmelCase ) ragramslist.append(_lowerCAmelCase ) for i in range(0, len(_lowerCAmelCase ) - 1 ): if i < len(_lowerCAmelCase ) - 1: _a = sagrams[i] + ''' ''' + sagrams[i + 1] sagrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 2: _a = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] sagrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 3: _a = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] + ''' ''' + sagrams[i + 3] sagrams.append(_lowerCAmelCase ) for i in range(0, len(_lowerCAmelCase ) - 1 ): if i < len(_lowerCAmelCase ) - 1: _a = cagrams[i] + ''' ''' + cagrams[i + 1] cagrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 2: _a = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] cagrams.append(_lowerCAmelCase ) if i < len(_lowerCAmelCase ) - 3: _a = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3] cagrams.append(_lowerCAmelCase ) ((_a) , (_a) , (_a)) = SARIngram(_lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase ) ((_a) , (_a) , (_a)) = SARIngram(_lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase ) ((_a) , (_a) , (_a)) = SARIngram(_lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase ) ((_a) , (_a) , (_a)) = SARIngram(_lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase ) _a = sum([keepascore, keepascore, keepascore, keepascore] ) / 4 _a = sum([delascore, delascore, delascore, delascore] ) / 4 _a = sum([addascore, addascore, addascore, addascore] ) / 4 _a = (avgkeepscore + avgdelscore + avgaddscore) / 3 return finalscore def A_ ( _lowerCAmelCase : str, _lowerCAmelCase : bool = True, _lowerCAmelCase : str = "13a", _lowerCAmelCase : bool = True ): """simple docstring""" if lowercase: _a = sentence.lower() if tokenizer in ["13a", "intl"]: if version.parse(sacrebleu.__version__ ).major >= 2: _a = sacrebleu.metrics.bleu._get_tokenizer(_lowerCAmelCase )()(_lowerCAmelCase ) else: _a = sacrebleu.TOKENIZERS[tokenizer]()(_lowerCAmelCase ) elif tokenizer == "moses": _a = sacremoses.MosesTokenizer().tokenize(_lowerCAmelCase, return_str=_lowerCAmelCase, escape=_lowerCAmelCase ) elif tokenizer == "penn": _a = sacremoses.MosesTokenizer().penn_tokenize(_lowerCAmelCase, return_str=_lowerCAmelCase ) else: _a = sentence if not return_str: _a = normalized_sent.split() return normalized_sent def A_ ( _lowerCAmelCase : List[Any], _lowerCAmelCase : Dict, _lowerCAmelCase : Optional[Any] ): """simple docstring""" if not (len(_lowerCAmelCase ) == len(_lowerCAmelCase ) == len(_lowerCAmelCase )): raise ValueError('''Sources length must match predictions and references lengths.''' ) _a = 0 for src, pred, refs in zip(_lowerCAmelCase, _lowerCAmelCase, _lowerCAmelCase ): sari_score += SARIsent(normalize(_lowerCAmelCase ), normalize(_lowerCAmelCase ), [normalize(_lowerCAmelCase ) for sent in refs] ) _a = sari_score / len(_lowerCAmelCase ) return 1_00 * sari_score def A_ ( _lowerCAmelCase : Tuple, _lowerCAmelCase : Tuple, _lowerCAmelCase : Any="exp", _lowerCAmelCase : Tuple=None, _lowerCAmelCase : Union[str, Any]=False, _lowerCAmelCase : Optional[Any]=False, _lowerCAmelCase : List[str]=False, ): """simple docstring""" _a = len(references[0] ) if any(len(_lowerCAmelCase ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) _a = [[refs[i] for refs in references] for i in range(_lowerCAmelCase )] _a = sacrebleu.corpus_bleu( _lowerCAmelCase, _lowerCAmelCase, smooth_method=_lowerCAmelCase, smooth_value=_lowerCAmelCase, force=_lowerCAmelCase, lowercase=_lowerCAmelCase, use_effective_order=_lowerCAmelCase, ) return output.score @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCamelCase ( datasets.Metric ): '''simple docstring''' def _UpperCAmelCase ( self ) -> List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Sequence(datasets.Value('''string''' , id='''sequence''' ) , id='''references''' ), } ) , codebase_urls=[ '''https://github.com/huggingface/transformers/blob/master/src/transformers/data/metrics/squad_metrics.py''', '''https://github.com/cocoxu/simplification/blob/master/SARI.py''', '''https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/sari_hook.py''', '''https://github.com/mjpost/sacreBLEU''', ] , reference_urls=[ '''https://www.aclweb.org/anthology/Q16-1029.pdf''', '''https://github.com/mjpost/sacreBLEU''', '''https://en.wikipedia.org/wiki/BLEU''', '''https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213''', ] , ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> str: _a = {} result.update({'''sari''': compute_sari(sources=__UpperCAmelCase , predictions=__UpperCAmelCase , references=__UpperCAmelCase )} ) result.update({'''sacrebleu''': compute_sacrebleu(predictions=__UpperCAmelCase , references=__UpperCAmelCase )} ) result.update({'''exact''': compute_em(predictions=__UpperCAmelCase , references=__UpperCAmelCase )} ) return result
320
1
from __future__ import annotations def lowercase__ ( __snake_case : list[int] ): '''simple docstring''' if len(__snake_case ) == 0: return array UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = min(__snake_case ), max(__snake_case ) # Compute the variables UpperCAmelCase_ : str = _max - _min + 1 UpperCAmelCase_ , UpperCAmelCase_ : Dict = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCAmelCase_ : Optional[Any] = i - _min UpperCAmelCase_ : List[Any] = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCAmelCase_ : Optional[int] = 0 for i in range(__snake_case ): while holes_repeat[i] > 0: UpperCAmelCase_ : List[str] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase = input('Enter numbers separated by comma:\n') __UpperCAmelCase = [int(x) for x in user_input.split(',')] print(pigeon_sort(unsorted))
145
def lowercase__ ( __snake_case : str , __snake_case : int , __snake_case : List[str] ): '''simple docstring''' if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__snake_case , n - 1 , __snake_case ) * a) % mod else: UpperCAmelCase_ : Optional[int] = binary_exponentiation(__snake_case , n / 2 , __snake_case ) return (b * b) % mod # a prime number __UpperCAmelCase = 701 __UpperCAmelCase = 1000000000 __UpperCAmelCase = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
145
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _a = logging.get_logger(__name__) _a = { '''kssteven/ibert-roberta-base''': '''https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json''', '''kssteven/ibert-roberta-large''': '''https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json''', '''kssteven/ibert-roberta-large-mnli''': ( '''https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json''' ), } class A_ ( snake_case__ ): _lowercase : Any = 'ibert' def __init__( self : Tuple , UpperCAmelCase : Any=3_0_5_2_2 , UpperCAmelCase : str=7_6_8 , UpperCAmelCase : Optional[Any]=1_2 , UpperCAmelCase : Optional[Any]=1_2 , UpperCAmelCase : int=3_0_7_2 , UpperCAmelCase : Optional[int]="gelu" , UpperCAmelCase : int=0.1 , UpperCAmelCase : List[Any]=0.1 , UpperCAmelCase : Tuple=5_1_2 , UpperCAmelCase : List[str]=2 , UpperCAmelCase : List[Any]=0.02 , UpperCAmelCase : Optional[Any]=1E-12 , UpperCAmelCase : Tuple=1 , UpperCAmelCase : List[str]=0 , UpperCAmelCase : Any=2 , UpperCAmelCase : Union[str, Any]="absolute" , UpperCAmelCase : str=False , UpperCAmelCase : int="none" , **UpperCAmelCase : int , ) -> List[Any]: super().__init__(pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase ) __lowerCAmelCase: Union[str, Any] = vocab_size __lowerCAmelCase: str = hidden_size __lowerCAmelCase: Tuple = num_hidden_layers __lowerCAmelCase: Union[str, Any] = num_attention_heads __lowerCAmelCase: Dict = hidden_act __lowerCAmelCase: Tuple = intermediate_size __lowerCAmelCase: Optional[Any] = hidden_dropout_prob __lowerCAmelCase: str = attention_probs_dropout_prob __lowerCAmelCase: Optional[int] = max_position_embeddings __lowerCAmelCase: Dict = type_vocab_size __lowerCAmelCase: List[str] = initializer_range __lowerCAmelCase: Optional[Any] = layer_norm_eps __lowerCAmelCase: Dict = position_embedding_type __lowerCAmelCase: List[str] = quant_mode __lowerCAmelCase: int = force_dequant class A_ ( snake_case__ ): @property def UpperCAmelCase ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": __lowerCAmelCase: Tuple = {0: 'batch', 1: 'choice', 2: 'sequence'} else: __lowerCAmelCase: Union[str, Any] = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
322
def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square(SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __lowerCAmelCase: Union[str, Any] = update_area_of_max_square(SCREAMING_SNAKE_CASE , col + 1 ) __lowerCAmelCase: Tuple = update_area_of_max_square(row + 1 , col + 1 ) __lowerCAmelCase: int = update_area_of_max_square(row + 1 , SCREAMING_SNAKE_CASE ) if mat[row][col]: __lowerCAmelCase: List[str] = 1 + min([right, diagonal, down] ) __lowerCAmelCase: List[str] = max(largest_square_area[0] , SCREAMING_SNAKE_CASE ) return sub_problem_sol else: return 0 __lowerCAmelCase: List[str] = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square_using_dp_array( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __lowerCAmelCase: List[Any] = update_area_of_max_square_using_dp_array(SCREAMING_SNAKE_CASE , col + 1 , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Union[str, Any] = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Any = update_area_of_max_square_using_dp_array(row + 1 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if mat[row][col]: __lowerCAmelCase: int = 1 + min([right, diagonal, down] ) __lowerCAmelCase: Union[str, Any] = max(largest_square_area[0] , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Optional[int] = sub_problem_sol return sub_problem_sol else: return 0 __lowerCAmelCase: int = [0] __lowerCAmelCase: int = [[-1] * cols for _ in range(SCREAMING_SNAKE_CASE )] update_area_of_max_square_using_dp_array(0 , 0 , SCREAMING_SNAKE_CASE ) return largest_square_area[0] def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" __lowerCAmelCase: int = [[0] * (cols + 1) for _ in range(rows + 1 )] __lowerCAmelCase: Optional[Any] = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCAmelCase: Union[str, Any] = dp_array[row][col + 1] __lowerCAmelCase: str = dp_array[row + 1][col + 1] __lowerCAmelCase: Optional[int] = dp_array[row + 1][col] if mat[row][col] == 1: __lowerCAmelCase: Optional[Any] = 1 + min(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: str = max(dp_array[row][col] , SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase: Dict = 0 return largest_square_area def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" __lowerCAmelCase: Tuple = [0] * (cols + 1) __lowerCAmelCase: Optional[int] = [0] * (cols + 1) __lowerCAmelCase: str = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCAmelCase: int = current_row[col + 1] __lowerCAmelCase: Union[str, Any] = next_row[col + 1] __lowerCAmelCase: Any = next_row[col] if mat[row][col] == 1: __lowerCAmelCase: str = 1 + min(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: str = max(current_row[col] , SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase: Optional[Any] = 0 __lowerCAmelCase: int = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
322
1
import numpy as np from PIL import Image def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : Union[str, Any] = np.array(lowercase__ ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) A_ : Union[str, Any] = 0 A_ : Optional[int] = 0 A_ : Optional[int] = 0 A_ : int = 0 # compute the shape of the output matrix A_ : str = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape A_ : Union[str, Any] = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix A_ : Optional[Any] = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 A_ : List[Any] = 0 A_ : List[Any] = 0 return updated_arr def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : Union[str, Any] = np.array(lowercase__ ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) A_ : int = 0 A_ : List[str] = 0 A_ : Dict = 0 A_ : int = 0 # compute the shape of the output matrix A_ : str = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape A_ : int = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix A_ : Dict = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 A_ : List[Any] = 0 A_ : Optional[int] = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name="""avgpooling""", verbose=True) # Loading the image UpperCamelCase = Image.open("""path_to_image""") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
363
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor UpperCamelCase = logging.get_logger(__name__) class _lowerCamelCase ( UpperCamelCase ): """simple docstring""" def __init__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )->None: '''simple docstring''' warnings.warn( '''The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use YolosImageProcessor instead.''' , _SCREAMING_SNAKE_CASE , ) super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )
65
0
'''simple docstring''' import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin __SCREAMING_SNAKE_CASE : int = logging.get_logger(__name__) enable_full_determinism() class lowerCamelCase_ (snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[int] = UNetaDModel __UpperCamelCase: str = "sample" @property def _A ( self : str ): _UpperCAmelCase : int = 4 _UpperCAmelCase : Any = 3 _UpperCAmelCase : List[str] = (32, 32) _UpperCAmelCase : Optional[int] = floats_tensor((batch_size, num_channels) + sizes ).to(A ) _UpperCAmelCase : Optional[Any] = torch.tensor([10] ).to(A ) return {"sample": noise, "timestep": time_step} @property def _A ( self : Optional[int] ): return (3, 32, 32) @property def _A ( self : Optional[Any] ): return (3, 32, 32) def _A ( self : Any ): _UpperCAmelCase : Optional[Any] = { "block_out_channels": (32, 64), "down_block_types": ("DownBlock2D", "AttnDownBlock2D"), "up_block_types": ("AttnUpBlock2D", "UpBlock2D"), "attention_head_dim": 3, "out_channels": 3, "in_channels": 3, "layers_per_block": 2, "sample_size": 32, } _UpperCAmelCase : Union[str, Any] = self.dummy_input return init_dict, inputs_dict class lowerCamelCase_ (snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = UNetaDModel __UpperCamelCase: List[Any] = "sample" @property def _A ( self : Optional[Any] ): _UpperCAmelCase : List[Any] = 4 _UpperCAmelCase : Union[str, Any] = 4 _UpperCAmelCase : Optional[Any] = (32, 32) _UpperCAmelCase : str = floats_tensor((batch_size, num_channels) + sizes ).to(A ) _UpperCAmelCase : Tuple = torch.tensor([10] ).to(A ) return {"sample": noise, "timestep": time_step} @property def _A ( self : Dict ): return (4, 32, 32) @property def _A ( self : Dict ): return (4, 32, 32) def _A ( self : str ): _UpperCAmelCase : Tuple = { "sample_size": 32, "in_channels": 4, "out_channels": 4, "layers_per_block": 2, "block_out_channels": (32, 64), "attention_head_dim": 32, "down_block_types": ("DownBlock2D", "DownBlock2D"), "up_block_types": ("UpBlock2D", "UpBlock2D"), } _UpperCAmelCase : str = self.dummy_input return init_dict, inputs_dict def _A ( self : Optional[int] ): _UpperCAmelCase , _UpperCAmelCase : List[str] = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" , output_loading_info=A ) self.assertIsNotNone(A ) self.assertEqual(len(loading_info["missing_keys"] ) , 0 ) model.to(A ) _UpperCAmelCase : Optional[int] = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != "cuda" , "This test is supposed to run on GPU" ) def _A ( self : List[Any] ): _UpperCAmelCase , _UpperCAmelCase : List[str] = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" , output_loading_info=A ) model.to(A ) _UpperCAmelCase : Optional[Any] = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != "cuda" , "This test is supposed to run on GPU" ) def _A ( self : Dict ): # by defautl model loading will use accelerate as `low_cpu_mem_usage=True` _UpperCAmelCase , _UpperCAmelCase : List[Any] = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" , output_loading_info=A ) model_accelerate.to(A ) model_accelerate.eval() _UpperCAmelCase : Optional[Any] = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) _UpperCAmelCase : str = noise.to(A ) _UpperCAmelCase : Optional[Any] = torch.tensor([10] * noise.shape[0] ).to(A ) _UpperCAmelCase : Dict = model_accelerate(A , A )["sample"] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() _UpperCAmelCase , _UpperCAmelCase : Optional[int] = UNetaDModel.from_pretrained( "fusing/unet-ldm-dummy-update" , output_loading_info=A , low_cpu_mem_usage=A ) model_normal_load.to(A ) model_normal_load.eval() _UpperCAmelCase : Any = model_normal_load(A , A )["sample"] assert torch_all_close(A , A , rtol=1E-3 ) def _A ( self : Optional[Any] ): _UpperCAmelCase : List[str] = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ) model.eval() model.to(A ) _UpperCAmelCase : Optional[Any] = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) _UpperCAmelCase : Union[str, Any] = noise.to(A ) _UpperCAmelCase : Optional[Any] = torch.tensor([10] * noise.shape[0] ).to(A ) with torch.no_grad(): _UpperCAmelCase : Dict = model(A , A ).sample _UpperCAmelCase : Dict = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off _UpperCAmelCase : int = torch.tensor([-13.3_258, -20.1_100, -15.9_873, -17.6_617, -23.0_596, -17.9_419, -13.3_675, -16.1_889, -12.3_800] ) # fmt: on self.assertTrue(torch_all_close(A , A , rtol=1E-3 ) ) class lowerCamelCase_ (snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: List[Any] = UNetaDModel __UpperCamelCase: Dict = "sample" @property def _A ( self : Optional[Any] , A : Optional[Any]=(32, 32) ): _UpperCAmelCase : Optional[Any] = 4 _UpperCAmelCase : int = 3 _UpperCAmelCase : Union[str, Any] = floats_tensor((batch_size, num_channels) + sizes ).to(A ) _UpperCAmelCase : Optional[Any] = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=A ) return {"sample": noise, "timestep": time_step} @property def _A ( self : str ): return (3, 32, 32) @property def _A ( self : str ): return (3, 32, 32) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = { "block_out_channels": [32, 64, 64, 64], "in_channels": 3, "layers_per_block": 1, "out_channels": 3, "time_embedding_type": "fourier", "norm_eps": 1E-6, "mid_block_scale_factor": math.sqrt(2.0 ), "norm_num_groups": None, "down_block_types": [ "SkipDownBlock2D", "AttnSkipDownBlock2D", "SkipDownBlock2D", "SkipDownBlock2D", ], "up_block_types": [ "SkipUpBlock2D", "SkipUpBlock2D", "AttnSkipUpBlock2D", "SkipUpBlock2D", ], } _UpperCAmelCase : str = self.dummy_input return init_dict, inputs_dict @slow def _A ( self : Union[str, Any] ): _UpperCAmelCase , _UpperCAmelCase : Tuple = UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" , output_loading_info=A ) self.assertIsNotNone(A ) self.assertEqual(len(loading_info["missing_keys"] ) , 0 ) model.to(A ) _UpperCAmelCase : Optional[int] = self.dummy_input _UpperCAmelCase : Optional[int] = floats_tensor((4, 3) + (256, 256) ).to(A ) _UpperCAmelCase : Union[str, Any] = noise _UpperCAmelCase : Optional[Any] = model(**A ) assert image is not None, "Make sure output is not None" @slow def _A ( self : Tuple ): _UpperCAmelCase : List[str] = UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" ) model.to(A ) _UpperCAmelCase : List[Any] = 4 _UpperCAmelCase : List[str] = 3 _UpperCAmelCase : str = (256, 256) _UpperCAmelCase : List[str] = torch.ones((batch_size, num_channels) + sizes ).to(A ) _UpperCAmelCase : Tuple = torch.tensor(batch_size * [1E-4] ).to(A ) with torch.no_grad(): _UpperCAmelCase : Any = model(A , A ).sample _UpperCAmelCase : Tuple = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off _UpperCAmelCase : Union[str, Any] = torch.tensor([-4_842.8_691, -6_499.6_631, -3_800.1_953, -7_978.2_686, -10_980.7_129, -20_028.8_535, 8_148.2_822, 2_342.2_905, 567.7_608] ) # fmt: on self.assertTrue(torch_all_close(A , A , rtol=1E-2 ) ) def _A ( self : List[str] ): _UpperCAmelCase : Optional[int] = UNetaDModel.from_pretrained("fusing/ncsnpp-ffhq-ve-dummy-update" ) model.to(A ) _UpperCAmelCase : Optional[int] = 4 _UpperCAmelCase : Dict = 3 _UpperCAmelCase : Dict = (32, 32) _UpperCAmelCase : List[Any] = torch.ones((batch_size, num_channels) + sizes ).to(A ) _UpperCAmelCase : Optional[int] = torch.tensor(batch_size * [1E-4] ).to(A ) with torch.no_grad(): _UpperCAmelCase : Optional[Any] = model(A , A ).sample _UpperCAmelCase : List[str] = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off _UpperCAmelCase : List[str] = torch.tensor([-0.0_325, -0.0_900, -0.0_869, -0.0_332, -0.0_725, -0.0_270, -0.0_101, 0.0_227, 0.0_256] ) # fmt: on self.assertTrue(torch_all_close(A , A , rtol=1E-2 ) ) def _A ( self : List[Any] ): # not required for this model pass
31
'''simple docstring''' import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger __SCREAMING_SNAKE_CASE : Dict = get_logger(__name__) class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] , A : Optional[str] = None ): _UpperCAmelCase : Dict = ( os.path.join(A , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) _UpperCAmelCase : Union[str, Any] = Extractor def _A ( self : Tuple , A : str ): from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" _UpperCAmelCase : Dict = os.path.abspath(A ) return os.path.join(self.extract_dir , hash_url_to_filename(A ) ) def _A ( self : int , A : str , A : bool ): return force_extract or ( not os.path.isfile(A ) and not (os.path.isdir(A ) and os.listdir(A )) ) def _A ( self : Optional[int] , A : str , A : bool = False ): _UpperCAmelCase : Union[str, Any] = self.extractor.infer_extractor_format(A ) if not extractor_format: return input_path _UpperCAmelCase : Optional[Any] = self._get_output_path(A ) if self._do_extract(A , A ): self.extractor.extract(A , A , A ) return output_path class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod @abstractmethod def _A ( cls : str , A : Union[Path, str] , **A : Dict ): ... @staticmethod @abstractmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): ... class lowerCamelCase_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: List[bytes] = [] @staticmethod def _A ( A : Union[Path, str] , A : int ): with open(A , "rb" ) as f: return f.read(A ) @classmethod def _A ( cls : Any , A : Union[Path, str] , A : bytes = b"" ): if not magic_number: _UpperCAmelCase : Any = max(len(A ) for cls_magic_number in cls.magic_numbers ) try: _UpperCAmelCase : int = cls.read_magic_number(A , A ) except OSError: return False return any(magic_number.startswith(A ) for cls_magic_number in cls.magic_numbers ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod def _A ( cls : str , A : Union[Path, str] , **A : List[Any] ): return tarfile.is_tarfile(A ) @staticmethod def _A ( A : Union[str, Any] , A : str ): def resolved(A : str ) -> str: return os.path.realpath(os.path.abspath(A ) ) def badpath(A : str , A : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(A , A ) ).startswith(A ) def badlink(A : str , A : str ) -> bool: # Links are interpreted relative to the directory containing the link _UpperCAmelCase : List[str] = resolved(os.path.join(A , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=A ) _UpperCAmelCase : Optional[int] = resolved(A ) for finfo in members: if badpath(finfo.name , A ): logger.error(F"""Extraction of {finfo.name} is blocked (illegal path)""" ) elif finfo.issym() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}""" ) elif finfo.islnk() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}""" ) else: yield finfo @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) _UpperCAmelCase : int = tarfile.open(A ) tar_file.extractall(A , members=TarExtractor.safemembers(A , A ) ) tar_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = [b"\x1F\x8B"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with gzip.open(A , "rb" ) as gzip_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def _A ( cls : Dict , A : Union[Path, str] , A : bytes = b"" ): if super().is_extractable(A , magic_number=A ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(A , "rb" ) as fp: _UpperCAmelCase : Tuple = _EndRecData(A ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: _UpperCAmelCase : Dict = fp.read(A ) # CD is where we expect it to be if len(A ) == sizeCentralDir: _UpperCAmelCase : Any = struct.unpack(A , A ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) with zipfile.ZipFile(A , "r" ) as zip_file: zip_file.extractall(A ) zip_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with lzma.open(A ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[str] = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(A , exist_ok=A ) _UpperCAmelCase : List[str] = rarfile.RarFile(A ) rf.extractall(A ) rf.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x28\xb5\x2F\xFD"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd _UpperCAmelCase : Optional[Any] = zstd.ZstdDecompressor() with open(A , "rb" ) as ifh, open(A , "wb" ) as ofh: dctx.copy_stream(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x42\x5A\x68"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with bza.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[Any] = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(A , exist_ok=A ) with pyazr.SevenZipFile(A , "r" ) as archive: archive.extractall(A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = [b"\x04\x22\x4D\x18"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def _A ( cls : List[Any] ): return max( len(A ) for extractor in cls.extractors.values() if issubclass(A , A ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def _A ( A : Union[Path, str] , A : int ): try: return MagicNumberBaseExtractor.read_magic_number(A , magic_number_length=A ) except OSError: return b"" @classmethod def _A ( cls : Optional[Any] , A : Union[Path, str] , A : bool = False ): warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=A , ) _UpperCAmelCase : Union[str, Any] = cls.infer_extractor_format(A ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def _A ( cls : Dict , A : Union[Path, str] ): # <Added version="2.4.0"/> _UpperCAmelCase : Optional[int] = cls._get_magic_number_max_length() _UpperCAmelCase : str = cls._read_magic_number(A , A ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(A , magic_number=A ): return extractor_format @classmethod def _A ( cls : List[str] , A : Union[Path, str] , A : Union[Path, str] , A : Optional[str] = None , A : Optional[BaseExtractor] = "deprecated" , ): os.makedirs(os.path.dirname(A ) , exist_ok=A ) # Prevent parallel extractions _UpperCAmelCase : Tuple = str(Path(A ).with_suffix(".lock" ) ) with FileLock(A ): shutil.rmtree(A , ignore_errors=A ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(A , A ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=A , ) _UpperCAmelCase : Tuple = extractor if extractor != "deprecated" else extractor_format else: _UpperCAmelCase : Tuple = cls.extractors[extractor_format] return extractor.extract(A , A ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=A , ) for extractor in cls.extractors.values(): if extractor.is_extractable(A ): return extractor.extract(A , A )
31
1
import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast 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 __magic_name__ = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( A__ , unittest.TestCase ): '''simple docstring''' __SCREAMING_SNAKE_CASE = PegasusTokenizer __SCREAMING_SNAKE_CASE = PegasusTokenizerFast __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = True def snake_case_ ( self ) -> Optional[Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase = PegasusTokenizer(_snake_case ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def snake_case_ ( self ) -> Dict: """simple docstring""" return PegasusTokenizer.from_pretrained('''google/pegasus-large''' ) def snake_case_ ( self , **_snake_case ) -> PegasusTokenizer: """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname , **_snake_case ) def snake_case_ ( self , _snake_case ) -> Any: """simple docstring""" return ("This is a test", "This is a test") def snake_case_ ( self ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase = '''</s>''' UpperCAmelCase = 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 snake_case_ ( self ) -> str: """simple docstring""" UpperCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<pad>''' ) self.assertEqual(vocab_keys[1] , '''</s>''' ) self.assertEqual(vocab_keys[-1] , '''v''' ) self.assertEqual(len(_snake_case ) , 1103 ) def snake_case_ ( self ) -> Union[str, Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def snake_case_ ( self ) -> List[str]: """simple docstring""" UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) UpperCAmelCase = self.tokenizer_class.from_pretrained(self.tmpdirname ) UpperCAmelCase = ( '''Let\'s see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important''' ''' </s> <pad> <pad> <pad>''' ) UpperCAmelCase = rust_tokenizer([raw_input_str] , return_tensors=_snake_case , add_special_tokens=_snake_case ).input_ids[0] UpperCAmelCase = py_tokenizer([raw_input_str] , return_tensors=_snake_case , add_special_tokens=_snake_case ).input_ids[0] self.assertListEqual(_snake_case , _snake_case ) def snake_case_ ( self ) -> Tuple: """simple docstring""" UpperCAmelCase = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word UpperCAmelCase = '''<mask_1> To ensure a <mask_2> flow of bank resolutions.''' UpperCAmelCase = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] UpperCAmelCase = tokenizer([raw_input_str] , return_tensors=_snake_case ).input_ids[0] self.assertListEqual(_snake_case , _snake_case ) def snake_case_ ( self ) -> List[str]: """simple docstring""" UpperCAmelCase = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1024 UpperCAmelCase = '''To ensure a smooth flow of bank resolutions.''' UpperCAmelCase = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] UpperCAmelCase = tokenizer([raw_input_str] , return_tensors=_snake_case ).input_ids[0] self.assertListEqual(_snake_case , _snake_case ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def snake_case_ ( self ) -> List[str]: """simple docstring""" UpperCAmelCase = ['''This is going to be way too long.''' * 150, '''short example'''] UpperCAmelCase = ['''not super long but more than 5 tokens''', '''tiny'''] UpperCAmelCase = self._large_tokenizer(_snake_case , padding=_snake_case , truncation=_snake_case , return_tensors='''pt''' ) UpperCAmelCase = self._large_tokenizer( text_target=_snake_case , max_length=5 , padding=_snake_case , truncation=_snake_case , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 1024) assert batch.attention_mask.shape == (2, 1024) assert targets["input_ids"].shape == (2, 5) assert len(_snake_case ) == 2 # input_ids, attention_mask. @slow def snake_case_ ( self ) -> List[str]: """simple docstring""" UpperCAmelCase = {'''input_ids''': [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 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], [139, 1235, 2799, 1_8289, 1_7780, 204, 109, 9474, 1296, 107, 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]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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-pegasus-large-arxiv''' , revision='''ba85d0851d708441f91440d509690f1ab6353415''' , ) @require_sentencepiece @require_tokenizers class lowercase ( A__ , unittest.TestCase ): '''simple docstring''' __SCREAMING_SNAKE_CASE = PegasusTokenizer __SCREAMING_SNAKE_CASE = PegasusTokenizerFast __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = True def snake_case_ ( self ) -> int: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing UpperCAmelCase = PegasusTokenizer(_snake_case , offset=0 , mask_token_sent=_snake_case , mask_token='''[MASK]''' ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def snake_case_ ( self ) -> Dict: """simple docstring""" return PegasusTokenizer.from_pretrained('''google/bigbird-pegasus-large-arxiv''' ) def snake_case_ ( self , **_snake_case ) -> PegasusTokenizer: """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname , **_snake_case ) def snake_case_ ( self , _snake_case ) -> List[str]: """simple docstring""" return ("This is a test", "This is a test") def snake_case_ ( self ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) UpperCAmelCase = self.tokenizer_class.from_pretrained(self.tmpdirname ) UpperCAmelCase = ( '''Let\'s see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>''' ''' <pad> <pad> <pad>''' ) UpperCAmelCase = rust_tokenizer([raw_input_str] , return_tensors=_snake_case , add_special_tokens=_snake_case ).input_ids[0] UpperCAmelCase = py_tokenizer([raw_input_str] , return_tensors=_snake_case , add_special_tokens=_snake_case ).input_ids[0] self.assertListEqual(_snake_case , _snake_case ) @require_torch def snake_case_ ( self ) -> Optional[Any]: """simple docstring""" UpperCAmelCase = ['''This is going to be way too long.''' * 1000, '''short example'''] UpperCAmelCase = ['''not super long but more than 5 tokens''', '''tiny'''] UpperCAmelCase = self._large_tokenizer(_snake_case , padding=_snake_case , truncation=_snake_case , return_tensors='''pt''' ) UpperCAmelCase = self._large_tokenizer( text_target=_snake_case , max_length=5 , padding=_snake_case , truncation=_snake_case , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 4096) assert batch.attention_mask.shape == (2, 4096) assert targets["input_ids"].shape == (2, 5) assert len(_snake_case ) == 2 # input_ids, attention_mask. def snake_case_ ( self ) -> Any: """simple docstring""" UpperCAmelCase = ( '''This is an example string that is used to test the original TF implementation against the HF''' ''' implementation''' ) UpperCAmelCase = self._large_tokenizer(_snake_case ).input_ids self.assertListEqual( _snake_case , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
356
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys __magic_name__ = subprocess.check_output("git merge-base main HEAD".split()).decode("utf-8") __magic_name__ = ( subprocess.check_output(f'''git diff --diff-filter=d --name-only {fork_point_sha}'''.split()).decode("utf-8").split() ) __magic_name__ = "|".join(sys.argv[1:]) __magic_name__ = re.compile(rf'''^({joined_dirs}).*?\.py$''') __magic_name__ = [x for x in modified_files if regex.match(x)] print(" ".join(relevant_modified_files), end="")
152
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __UpperCAmelCase = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys __UpperCAmelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
299
# limitations under the License. # 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 .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( "pipelines_utils", "0.22.0", "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.", standard_warn=False, stacklevel=3, )
299
1
'''simple docstring''' import math import qiskit def _a ( _lowercase : int = 1 , _lowercase : int = 1 , _lowercase : int = 1 ): '''simple docstring''' if ( isinstance(_lowercase , _lowercase ) or isinstance(_lowercase , _lowercase ) or isinstance(_lowercase , _lowercase ) ): raise TypeError('''inputs must be integers.''' ) if (input_a < 0) or (input_a < 0) or (carry_in < 0): raise ValueError('''inputs must be positive.''' ) if ( (math.floor(_lowercase ) != input_a) or (math.floor(_lowercase ) != input_a) or (math.floor(_lowercase ) != carry_in) ): raise ValueError('''inputs must be exact integers.''' ) if (input_a > 2) or (input_a > 2) or (carry_in > 2): raise ValueError('''inputs must be less or equal to 2.''' ) # build registers __UpperCAmelCase : str = qiskit.QuantumRegister(4 , '''qr''' ) __UpperCAmelCase : str = qiskit.ClassicalRegister(2 , '''cr''' ) # list the entries __UpperCAmelCase : Union[str, Any] = [input_a, input_a, carry_in] __UpperCAmelCase : Dict = qiskit.QuantumCircuit(_lowercase , _lowercase ) for i in range(0 , 3 ): if entry[i] == 2: quantum_circuit.h(_lowercase ) # for hadamard entries elif entry[i] == 1: quantum_circuit.x(_lowercase ) # for 1 entries elif entry[i] == 0: quantum_circuit.i(_lowercase ) # for 0 entries # build the circuit quantum_circuit.ccx(0 , 1 , 3 ) # ccx = toffoli gate quantum_circuit.cx(0 , 1 ) quantum_circuit.ccx(1 , 2 , 3 ) quantum_circuit.cx(1 , 2 ) quantum_circuit.cx(0 , 1 ) quantum_circuit.measure([2, 3] , _lowercase ) # measure the last two qbits __UpperCAmelCase : Tuple = qiskit.Aer.get_backend('''aer_simulator''' ) __UpperCAmelCase : Optional[Any] = qiskit.execute(_lowercase , _lowercase , shots=1000 ) return job.result().get_counts(_lowercase ) if __name__ == "__main__": print(f"""Total sum count for state is: {quantum_full_adder(1, 1, 1)}""")
240
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCAmelCase :List[Any] = { "configuration_lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig"], "tokenization_lxmert": ["LxmertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase :List[Any] = ["LxmertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase :List[Any] = [ "LxmertEncoder", "LxmertForPreTraining", "LxmertForQuestionAnswering", "LxmertModel", "LxmertPreTrainedModel", "LxmertVisualFeatureEncoder", "LxmertXLayer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase :List[str] = [ "TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFLxmertForPreTraining", "TFLxmertMainLayer", "TFLxmertModel", "TFLxmertPreTrainedModel", "TFLxmertVisualFeatureEncoder", ] if TYPE_CHECKING: from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig from .tokenization_lxmert import LxmertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_lxmert_fast import LxmertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lxmert import ( LxmertEncoder, LxmertForPreTraining, LxmertForQuestionAnswering, LxmertModel, LxmertPreTrainedModel, LxmertVisualFeatureEncoder, LxmertXLayer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_lxmert import ( TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFLxmertForPreTraining, TFLxmertMainLayer, TFLxmertModel, TFLxmertPreTrainedModel, TFLxmertVisualFeatureEncoder, ) else: import sys __UpperCAmelCase :int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
240
1
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=0.0 , __lowerCamelCase = None , __lowerCamelCase = "geglu" , __lowerCamelCase = None , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = True , __lowerCamelCase = "layer_norm" , __lowerCamelCase = False , ) -> Optional[int]: super().__init__() _A : Optional[Any] = only_cross_attention _A : int = (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero" _A : List[Any] = (num_embeds_ada_norm is not None) and norm_type == "ada_norm" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to" F" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.") # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: _A : Any = AdaLayerNorm(__lowerCamelCase , __lowerCamelCase) elif self.use_ada_layer_norm_zero: _A : Optional[int] = AdaLayerNormZero(__lowerCamelCase , __lowerCamelCase) else: _A : str = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) _A : int = Attention( query_dim=__lowerCamelCase , heads=__lowerCamelCase , dim_head=__lowerCamelCase , dropout=__lowerCamelCase , bias=__lowerCamelCase , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=__lowerCamelCase , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. _A : int = ( AdaLayerNorm(__lowerCamelCase , __lowerCamelCase) if self.use_ada_layer_norm else nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) ) _A : Optional[int] = Attention( query_dim=__lowerCamelCase , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=__lowerCamelCase , dim_head=__lowerCamelCase , dropout=__lowerCamelCase , bias=__lowerCamelCase , upcast_attention=__lowerCamelCase , ) # is self-attn if encoder_hidden_states is none else: _A : Tuple = None _A : List[Any] = None # 3. Feed-forward _A : Any = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) _A : Any = FeedForward(__lowerCamelCase , dropout=__lowerCamelCase , activation_fn=__lowerCamelCase , final_dropout=__lowerCamelCase) # let chunk size default to None _A : Optional[int] = None _A : List[Any] = 0 def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: # Sets chunk feed-forward _A : Optional[int] = chunk_size _A : str = dim def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , ) -> int: # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: _A : Union[str, Any] = self.norma(__lowerCamelCase , __lowerCamelCase) elif self.use_ada_layer_norm_zero: _A , _A , _A , _A , _A : Tuple = self.norma( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , hidden_dtype=hidden_states.dtype) else: _A : Tuple = self.norma(__lowerCamelCase) _A : Dict = cross_attention_kwargs if cross_attention_kwargs is not None else {} _A : Optional[Any] = self.attna( __lowerCamelCase , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=__lowerCamelCase , **__lowerCamelCase , ) if self.use_ada_layer_norm_zero: _A : int = gate_msa.unsqueeze(1) * attn_output _A : str = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: _A : Any = ( self.norma(__lowerCamelCase , __lowerCamelCase) if self.use_ada_layer_norm else self.norma(__lowerCamelCase) ) _A : Union[str, Any] = self.attna( __lowerCamelCase , encoder_hidden_states=__lowerCamelCase , attention_mask=__lowerCamelCase , **__lowerCamelCase , ) _A : Any = attn_output + hidden_states # 3. Feed-forward _A : str = self.norma(__lowerCamelCase) if self.use_ada_layer_norm_zero: _A : Optional[Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.") _A : Optional[Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size _A : int = torch.cat( [self.ff(__lowerCamelCase) for hid_slice in norm_hidden_states.chunk(__lowerCamelCase , dim=self._chunk_dim)] , dim=self._chunk_dim , ) else: _A : List[str] = self.ff(__lowerCamelCase) if self.use_ada_layer_norm_zero: _A : Optional[int] = gate_mlp.unsqueeze(1) * ff_output _A : Optional[int] = ff_output + hidden_states return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = 4 , __lowerCamelCase = 0.0 , __lowerCamelCase = "geglu" , __lowerCamelCase = False , ) -> Tuple: super().__init__() _A : List[Any] = int(dim * mult) _A : Any = dim_out if dim_out is not None else dim if activation_fn == "gelu": _A : Tuple = GELU(__lowerCamelCase , __lowerCamelCase) if activation_fn == "gelu-approximate": _A : List[Any] = GELU(__lowerCamelCase , __lowerCamelCase , approximate="tanh") elif activation_fn == "geglu": _A : Optional[Any] = GEGLU(__lowerCamelCase , __lowerCamelCase) elif activation_fn == "geglu-approximate": _A : Any = ApproximateGELU(__lowerCamelCase , __lowerCamelCase) _A : List[str] = nn.ModuleList([]) # project in self.net.append(__lowerCamelCase) # project dropout self.net.append(nn.Dropout(__lowerCamelCase)) # project out self.net.append(nn.Linear(__lowerCamelCase , __lowerCamelCase)) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(__lowerCamelCase)) def _lowerCamelCase ( self , __lowerCamelCase) -> List[str]: for module in self.net: _A : List[Any] = module(__lowerCamelCase) return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = "none") -> Any: super().__init__() _A : str = nn.Linear(__lowerCamelCase , __lowerCamelCase) _A : int = approximate def _lowerCamelCase ( self , __lowerCamelCase) -> Optional[int]: if gate.device.type != "mps": return F.gelu(__lowerCamelCase , approximate=self.approximate) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa) , approximate=self.approximate).to(dtype=gate.dtype) def _lowerCamelCase ( self , __lowerCamelCase) -> Union[str, Any]: _A : Dict = self.proj(__lowerCamelCase) _A : int = self.gelu(__lowerCamelCase) return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: super().__init__() _A : int = nn.Linear(__lowerCamelCase , dim_out * 2) def _lowerCamelCase ( self , __lowerCamelCase) -> Optional[int]: if gate.device.type != "mps": return F.gelu(__lowerCamelCase) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa)).to(dtype=gate.dtype) def _lowerCamelCase ( self , __lowerCamelCase) -> Dict: _A , _A : List[Any] = self.proj(__lowerCamelCase).chunk(2 , dim=-1) return hidden_states * self.gelu(__lowerCamelCase) class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: super().__init__() _A : Union[str, Any] = nn.Linear(__lowerCamelCase , __lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase) -> Any: _A : Any = self.proj(__lowerCamelCase) return x * torch.sigmoid(1.7_0_2 * x) class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> List[str]: super().__init__() _A : Optional[int] = nn.Embedding(__lowerCamelCase , __lowerCamelCase) _A : Tuple = nn.SiLU() _A : Union[str, Any] = nn.Linear(__lowerCamelCase , embedding_dim * 2) _A : int = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> List[Any]: _A : Dict = self.linear(self.silu(self.emb(__lowerCamelCase))) _A , _A : Optional[Any] = torch.chunk(__lowerCamelCase , 2) _A : Any = self.norm(__lowerCamelCase) * (1 + scale) + shift return x class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Optional[Any]: super().__init__() _A : str = CombinedTimestepLabelEmbeddings(__lowerCamelCase , __lowerCamelCase) _A : Optional[int] = nn.SiLU() _A : Dict = nn.Linear(__lowerCamelCase , 6 * embedding_dim , bias=__lowerCamelCase) _A : int = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase , eps=1e-6) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=None) -> Union[str, Any]: _A : List[Any] = self.linear(self.silu(self.emb(__lowerCamelCase , __lowerCamelCase , hidden_dtype=__lowerCamelCase))) _A , _A , _A , _A , _A , _A : Optional[int] = emb.chunk(6 , dim=1) _A : int = self.norm(__lowerCamelCase) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = 1e-5) -> int: super().__init__() _A : Any = num_groups _A : Any = eps if act_fn is None: _A : Tuple = None else: _A : str = get_activation(__lowerCamelCase) _A : List[Any] = nn.Linear(__lowerCamelCase , out_dim * 2) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> Optional[Any]: if self.act: _A : Tuple = self.act(__lowerCamelCase) _A : Optional[Any] = self.linear(__lowerCamelCase) _A : Tuple = emb[:, :, None, None] _A , _A : Union[str, Any] = emb.chunk(2 , dim=1) _A : List[str] = F.group_norm(__lowerCamelCase , self.num_groups , eps=self.eps) _A : int = x * (1 + scale) + shift return x
11
from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class a ( lowercase__ ): """simple docstring""" def __init__( self : Any , __lowercase : Optional[NestedDataStructureLike[PathLike]] = None , __lowercase : Optional[NamedSplit] = None , __lowercase : Optional[Features] = None , __lowercase : str = None , __lowercase : bool = False , __lowercase : bool = False , __lowercase : Optional[int] = None , **__lowercase : List[str] , ) -> Tuple: __UpperCAmelCase : Any = path_or_paths __UpperCAmelCase : Dict = split if split or isinstance(__lowercase , __lowercase ) else """train""" __UpperCAmelCase : Optional[int] = features __UpperCAmelCase : str = cache_dir __UpperCAmelCase : str = keep_in_memory __UpperCAmelCase : Optional[int] = streaming __UpperCAmelCase : Dict = num_proc __UpperCAmelCase : Tuple = kwargs @abstractmethod def UpperCAmelCase ( self : List[Any] ) -> Union[Dataset, DatasetDict, IterableDataset, IterableDatasetDict]: pass class a ( lowercase__ ): """simple docstring""" def __init__( self : List[str] , __lowercase : Optional[Features] = None , __lowercase : str = None , __lowercase : bool = False , __lowercase : bool = False , __lowercase : Optional[int] = None , **__lowercase : Optional[Any] , ) -> Optional[int]: __UpperCAmelCase : Optional[Any] = features __UpperCAmelCase : str = cache_dir __UpperCAmelCase : Optional[int] = keep_in_memory __UpperCAmelCase : Dict = streaming __UpperCAmelCase : Optional[Any] = num_proc __UpperCAmelCase : Union[str, Any] = kwargs @abstractmethod def UpperCAmelCase ( self : List[str] ) -> Union[Dataset, IterableDataset]: pass
114
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = { 'microsoft/unispeech-sat-base-100h-libri-ft': ( 'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json' ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class snake_case_ ( __lowercase ): A_ = 'unispeech-sat' def __init__( self : str , _snake_case : List[Any]=32 , _snake_case : Union[str, Any]=768 , _snake_case : Tuple=12 , _snake_case : Optional[int]=12 , _snake_case : Optional[Any]=3072 , _snake_case : Tuple="gelu" , _snake_case : int=0.1 , _snake_case : List[Any]=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : str=0.0 , _snake_case : List[str]=0.0 , _snake_case : int=0.1 , _snake_case : Optional[Any]=0.1 , _snake_case : Optional[Any]=0.02 , _snake_case : int=1E-5 , _snake_case : Dict="group" , _snake_case : Optional[Any]="gelu" , _snake_case : Optional[Any]=(512, 512, 512, 512, 512, 512, 512) , _snake_case : int=(5, 2, 2, 2, 2, 2, 2) , _snake_case : int=(10, 3, 3, 3, 3, 2, 2) , _snake_case : Any=False , _snake_case : Optional[Any]=128 , _snake_case : Tuple=16 , _snake_case : str=False , _snake_case : Dict=True , _snake_case : Tuple=0.05 , _snake_case : str=10 , _snake_case : Tuple=2 , _snake_case : List[Any]=0.0 , _snake_case : str=10 , _snake_case : Any=0 , _snake_case : List[Any]=320 , _snake_case : Union[str, Any]=2 , _snake_case : Dict=0.1 , _snake_case : Dict=100 , _snake_case : Union[str, Any]=256 , _snake_case : int=256 , _snake_case : Union[str, Any]=0.1 , _snake_case : Optional[Any]="mean" , _snake_case : int=False , _snake_case : str=False , _snake_case : str=256 , _snake_case : List[Any]=(512, 512, 512, 512, 1500) , _snake_case : Optional[int]=(5, 3, 3, 1, 1) , _snake_case : Tuple=(1, 2, 3, 1, 1) , _snake_case : Dict=512 , _snake_case : Union[str, Any]=0 , _snake_case : List[str]=1 , _snake_case : Optional[Any]=2 , _snake_case : Optional[int]=504 , **_snake_case : Optional[int] , )->Union[str, Any]: '''simple docstring''' super().__init__(**_snake_case , pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case ) __lowerCAmelCase : Dict = hidden_size __lowerCAmelCase : List[Any] = feat_extract_norm __lowerCAmelCase : int = feat_extract_activation __lowerCAmelCase : Union[str, Any] = list(_snake_case ) __lowerCAmelCase : str = list(_snake_case ) __lowerCAmelCase : Optional[Any] = list(_snake_case ) __lowerCAmelCase : Optional[int] = conv_bias __lowerCAmelCase : Dict = num_conv_pos_embeddings __lowerCAmelCase : List[Any] = num_conv_pos_embedding_groups __lowerCAmelCase : Tuple = len(self.conv_dim ) __lowerCAmelCase : int = num_hidden_layers __lowerCAmelCase : str = intermediate_size __lowerCAmelCase : str = hidden_act __lowerCAmelCase : Any = num_attention_heads __lowerCAmelCase : Optional[int] = hidden_dropout __lowerCAmelCase : str = attention_dropout __lowerCAmelCase : int = activation_dropout __lowerCAmelCase : Union[str, Any] = feat_proj_dropout __lowerCAmelCase : List[str] = final_dropout __lowerCAmelCase : Dict = layerdrop __lowerCAmelCase : Tuple = layer_norm_eps __lowerCAmelCase : Optional[Any] = initializer_range __lowerCAmelCase : str = vocab_size __lowerCAmelCase : Optional[int] = num_clusters __lowerCAmelCase : List[Any] = do_stable_layer_norm __lowerCAmelCase : Tuple = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==""" """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =""" F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __lowerCAmelCase : Dict = apply_spec_augment __lowerCAmelCase : List[Any] = mask_time_prob __lowerCAmelCase : List[str] = mask_time_length __lowerCAmelCase : Dict = mask_time_min_masks __lowerCAmelCase : Tuple = mask_feature_prob __lowerCAmelCase : List[str] = mask_feature_length __lowerCAmelCase : str = mask_feature_min_masks # parameters for pretraining with codevector quantized representations __lowerCAmelCase : Optional[int] = num_codevectors_per_group __lowerCAmelCase : List[Any] = num_codevector_groups __lowerCAmelCase : int = contrastive_logits_temperature __lowerCAmelCase : str = feat_quantizer_dropout __lowerCAmelCase : int = num_negatives __lowerCAmelCase : str = codevector_dim __lowerCAmelCase : Any = proj_codevector_dim __lowerCAmelCase : Any = diversity_loss_weight # ctc loss __lowerCAmelCase : Tuple = ctc_loss_reduction __lowerCAmelCase : Any = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. __lowerCAmelCase : Any = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __lowerCAmelCase : List[str] = list(_snake_case ) __lowerCAmelCase : List[str] = list(_snake_case ) __lowerCAmelCase : Optional[int] = list(_snake_case ) __lowerCAmelCase : Optional[int] = xvector_output_dim @property def UpperCAmelCase__ ( self : Optional[Any] )->Any: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
232
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = {'vocab_file': 'vocab.txt', 'emoji_file': 'emoji.json'} _UpperCAmelCase = { 'vocab_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt', }, 'emoji_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json', }, } _UpperCAmelCase = { 'abeja/gpt-neox-japanese-2.7b': 2048, } def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :int , SCREAMING_SNAKE_CASE :Optional[int] ) -> Optional[Any]: with open(SCREAMING_SNAKE_CASE , """r""" , encoding="""utf-8""" ) as f: __lowerCAmelCase : int = json.loads(f.read() ) __lowerCAmelCase : Dict = collections.OrderedDict() __lowerCAmelCase : str = collections.OrderedDict() __lowerCAmelCase : Union[str, Any] = collections.OrderedDict() with open(SCREAMING_SNAKE_CASE , """r""" , encoding="""utf-8""" ) as f: __lowerCAmelCase : Tuple = f.readlines() __lowerCAmelCase : Tuple = [[t.rstrip("""\n""" )] if (t == """,""" or """,""" not in t) else t.rstrip("""\n""" ).split(""",""" ) for t in token] for idx, b in enumerate(SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = b __lowerCAmelCase : Dict = idx for wd in b: __lowerCAmelCase : List[str] = idx return vocab, raw_vocab, ids_to_tokens, emoji class snake_case_ ( __lowercase ): A_ = VOCAB_FILES_NAMES A_ = PRETRAINED_VOCAB_FILES_MAP A_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A_ = ['input_ids', 'attention_mask'] def __init__( self : str , _snake_case : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : Any="<|endoftext|>" , _snake_case : str="<|endoftext|>" , _snake_case : str="<|startoftext|>" , _snake_case : List[Any]="<|endoftext|>" , _snake_case : str=False , **_snake_case : List[Any] , )->Union[str, Any]: '''simple docstring''' super().__init__( unk_token=_snake_case , pad_token=_snake_case , bos_token=_snake_case , eos_token=_snake_case , do_clean_text=_snake_case , **_snake_case , ) if not os.path.isfile(_snake_case ): raise ValueError( F'''Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained''' """ model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`""" ) if not os.path.isfile(_snake_case ): raise ValueError( F'''Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google''' """ pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`""" ) __lowerCAmelCase : Any = do_clean_text __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : Union[str, Any] = load_vocab_and_emoji(_snake_case , _snake_case ) __lowerCAmelCase : int = SubWordJapaneseTokenizer( vocab=self.vocab , ids_to_tokens=self.ids_to_tokens , emoji=self.emoji ) @property def UpperCAmelCase__ ( self : int )->str: '''simple docstring''' return len(self.raw_vocab ) def UpperCAmelCase__ ( self : Tuple )->Any: '''simple docstring''' return dict(self.raw_vocab , **self.added_tokens_encoder ) def UpperCAmelCase__ ( self : Any , _snake_case : str )->Optional[int]: '''simple docstring''' return self.subword_tokenizer.tokenize(_snake_case , clean=self.do_clean_text ) def UpperCAmelCase__ ( self : Optional[Any] , _snake_case : Optional[Any] )->Any: '''simple docstring''' return self.vocab.get(_snake_case , self.vocab.get(self.unk_token ) ) def UpperCAmelCase__ ( self : int , _snake_case : Any )->int: '''simple docstring''' return self.subword_tokenizer.convert_id_to_token(_snake_case ) def UpperCAmelCase__ ( self : Optional[int] , _snake_case : int )->List[Any]: '''simple docstring''' __lowerCAmelCase : str = """""".join(_snake_case ).strip() return out_string def UpperCAmelCase__ ( self : List[str] , _snake_case : "Conversation" )->List[int]: '''simple docstring''' __lowerCAmelCase : Union[str, Any] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_snake_case , add_special_tokens=_snake_case ) + [self.eos_token_id] ) if len(_snake_case ) > self.model_max_length: __lowerCAmelCase : List[str] = input_ids[-self.model_max_length :] return input_ids def UpperCAmelCase__ ( self : Optional[Any] , _snake_case : str , _snake_case : Optional[str] = None )->Tuple[str]: '''simple docstring''' __lowerCAmelCase : Optional[Any] = 0 if os.path.isdir(_snake_case ): __lowerCAmelCase : Dict = os.path.join( _snake_case , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) __lowerCAmelCase : List[Any] = os.path.join( _snake_case , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""emoji_file"""] ) else: __lowerCAmelCase : Union[str, Any] = ( (filename_prefix + """-""" if filename_prefix else """""") + save_directory + VOCAB_FILES_NAMES["""vocab_file"""] ) __lowerCAmelCase : Dict = ( (filename_prefix + """-""" if filename_prefix else """""") + save_directory + VOCAB_FILES_NAMES["""emoji_file"""] ) with open(_snake_case , """w""" , encoding="""utf-8""" ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' """ Please check that the vocabulary is not corrupted!""" ) __lowerCAmelCase : List[str] = token_index writer.write(""",""".join(_snake_case ) + """\n""" ) index += 1 with open(_snake_case , """w""" , encoding="""utf-8""" ) as writer: json.dump(self.emoji , _snake_case ) return vocab_file, emoji_file class snake_case_ ( __lowercase ): def __init__( self : Optional[Any] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Optional[int] )->List[Any]: '''simple docstring''' __lowerCAmelCase : Optional[Any] = vocab # same as swe __lowerCAmelCase : str = ids_to_tokens # same as bpe __lowerCAmelCase : Dict = emoji __lowerCAmelCase : int = np.max([len(_snake_case ) for w in self.vocab.keys()] ) __lowerCAmelCase : str = re.compile(R"""(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)""" ) __lowerCAmelCase : Optional[Any] = re.compile(R"""[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*""" ) __lowerCAmelCase : Tuple = re.compile(R"""[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}""" ) __lowerCAmelCase : Optional[Any] = re.compile( R"""([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*""" ) __lowerCAmelCase : Union[str, Any] = re.compile( R"""(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*""" ) __lowerCAmelCase : str = re.compile( R"""((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*""" ) __lowerCAmelCase : List[Any] = """─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿""" __lowerCAmelCase : Union[str, Any] = """▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟""" __lowerCAmelCase : str = str.maketrans({k: """<BLOCK>""" for k in keisen + blocks} ) def __len__( self : int )->int: '''simple docstring''' return len(self.ids_to_tokens ) def UpperCAmelCase__ ( self : List[str] , _snake_case : Any )->str: '''simple docstring''' __lowerCAmelCase : List[str] = self.content_repattera.sub("""<URL>""" , _snake_case ) __lowerCAmelCase : Tuple = self.content_repattera.sub("""<EMAIL>""" , _snake_case ) __lowerCAmelCase : Optional[Any] = self.content_repattera.sub("""<TEL>""" , _snake_case ) __lowerCAmelCase : str = self.content_repattera.sub("""<DATE>""" , _snake_case ) __lowerCAmelCase : Tuple = self.content_repattera.sub("""<DATE>""" , _snake_case ) __lowerCAmelCase : Tuple = self.content_repattera.sub("""<PRICE>""" , _snake_case ) __lowerCAmelCase : List[Any] = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: __lowerCAmelCase : str = content.replace("""<BLOCK><BLOCK>""" , """<BLOCK>""" ) return content def UpperCAmelCase__ ( self : str , _snake_case : List[Any] , _snake_case : Optional[int]=False )->int: '''simple docstring''' __lowerCAmelCase : Optional[int] = text.replace(""" """ , """<SP>""" ) __lowerCAmelCase : Optional[int] = text.replace(""" """ , """<SP>""" ) __lowerCAmelCase : Union[str, Any] = text.replace("""\r\n""" , """<BR>""" ) __lowerCAmelCase : Tuple = text.replace("""\n""" , """<BR>""" ) __lowerCAmelCase : List[str] = text.replace("""\r""" , """<BR>""" ) __lowerCAmelCase : Dict = text.replace("""\t""" , """<TAB>""" ) __lowerCAmelCase : Dict = text.replace("""—""" , """ー""" ) __lowerCAmelCase : Tuple = text.replace("""−""" , """ー""" ) for k, v in self.emoji["emoji"].items(): if k in text: __lowerCAmelCase : Optional[Any] = text.replace(_snake_case , _snake_case ) if clean: __lowerCAmelCase : List[Any] = self.clean_text(_snake_case ) def check_simbol(_snake_case : List[str] ): __lowerCAmelCase : Optional[int] = x.encode() if len(_snake_case ) == 1 and len(_snake_case ) == 2: __lowerCAmelCase : Optional[Any] = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0xc2a1 and c <= 0xc2bf) or (c >= 0xc780 and c <= 0xc783) or (c >= 0xcab9 and c <= 0xcbbf) or (c >= 0xcc80 and c <= 0xcda2) ): return True return False def checkuae(_snake_case : Union[str, Any] ): __lowerCAmelCase : Dict = x.encode() if len(_snake_case ) == 1 and len(_snake_case ) == 3: __lowerCAmelCase : List[str] = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0xe2_8080 and c <= 0xe2_b07f: return True return False __lowerCAmelCase : Dict = 0 __lowerCAmelCase : Dict = [] while pos < len(_snake_case ): __lowerCAmelCase : str = min(len(_snake_case ) , pos + self.maxlen + 1 ) if text[pos] == """<""" else pos + 3 __lowerCAmelCase : Tuple = [] # (token_id, token, pos) for e in range(_snake_case , _snake_case , -1 ): __lowerCAmelCase : Optional[int] = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(_snake_case ) > 2: __lowerCAmelCase : Tuple = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(_snake_case ) > 0: # the smallest token_id is adopted __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : int = sorted(_snake_case , key=lambda _snake_case : x[0] )[0] result.append(_snake_case ) __lowerCAmelCase : int = e else: __lowerCAmelCase : Dict = pos + 1 __lowerCAmelCase : Dict = text[pos:end] if check_simbol(_snake_case ): result.append("""<KIGOU>""" ) elif checkuae(_snake_case ): result.append("""<U2000U2BFF>""" ) else: for i in wd.encode("""utf-8""" ): result.append("""<|byte%d|>""" % i ) __lowerCAmelCase : int = end return result def UpperCAmelCase__ ( self : List[str] , _snake_case : Optional[int] , _snake_case : List[Any]="\n" )->List[Any]: '''simple docstring''' __lowerCAmelCase : List[str] = [] __lowerCAmelCase : Union[str, Any] = [] __lowerCAmelCase : Optional[Any] = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(_snake_case ) > 0: words.append(bytearray(_snake_case ).decode("""utf-8""" , errors="""replace""" ) ) __lowerCAmelCase : Optional[Any] = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji["""emoji_inv"""][word] ) elif word == "<SP>": words.append(""" """ ) elif word == "<BR>": words.append(_snake_case ) elif word == "<TAB>": words.append("""\t""" ) elif word == "<BLOCK>": words.append("""▀""" ) elif word == "<KIGOU>": words.append("""ǀ""" ) elif word == "<U2000U2BFF>": words.append("""‖""" ) else: words.append(_snake_case ) if len(_snake_case ) > 0: words.append(bytearray(_snake_case ).decode("""utf-8""" , errors="""replace""" ) ) __lowerCAmelCase : Dict = """""".join(_snake_case ) return text
232
1
import argparse 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 ######################################################################## # This is a fully working simple example to use Accelerate # and perform gradient accumulation # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## UpperCamelCase__ = 16 UpperCamelCase__ = 32 def _a ( SCREAMING_SNAKE_CASE_ : Accelerator , SCREAMING_SNAKE_CASE_ : int = 16 ): __lowerCAmelCase = AutoTokenizer.from_pretrained("bert-base-cased" ) __lowerCAmelCase = load_dataset("glue" , "mrpc" ) def tokenize_function(SCREAMING_SNAKE_CASE_ : List[str] ): # max_length=None => use the model max length (it's actually the default) __lowerCAmelCase = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) 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(): __lowerCAmelCase = datasets.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , 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 __lowerCAmelCase = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(SCREAMING_SNAKE_CASE_ : Union[str, Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. __lowerCAmelCase = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __lowerCAmelCase = 16 elif accelerator.mixed_precision != "no": __lowerCAmelCase = 8 else: __lowerCAmelCase = None return tokenizer.pad( SCREAMING_SNAKE_CASE_ , padding="longest" , max_length=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_tensors="pt" , ) # Instantiate dataloaders. __lowerCAmelCase = DataLoader( tokenized_datasets["train"] , shuffle=SCREAMING_SNAKE_CASE_ , collate_fn=SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = DataLoader( tokenized_datasets["validation"] , shuffle=SCREAMING_SNAKE_CASE_ , collate_fn=SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders UpperCamelCase__ = mocked_dataloaders # noqa: F811 def _a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Tuple ): # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS" , SCREAMING_SNAKE_CASE_ ) == "1": __lowerCAmelCase = 2 # New Code # __lowerCAmelCase = int(args.gradient_accumulation_steps ) # Initialize accelerator __lowerCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=SCREAMING_SNAKE_CASE_ ) if accelerator.distributed_type == DistributedType.TPU and gradient_accumulation_steps > 1: raise NotImplementedError( "Gradient accumulation on TPUs is currently not supported. Pass `gradient_accumulation_steps=1`" ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __lowerCAmelCase = config["lr"] __lowerCAmelCase = int(config["num_epochs"] ) __lowerCAmelCase = int(config["seed"] ) __lowerCAmelCase = int(config["batch_size"] ) __lowerCAmelCase = evaluate.load("glue" , "mrpc" ) set_seed(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase , __lowerCAmelCase = get_dataloaders(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __lowerCAmelCase = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=SCREAMING_SNAKE_CASE_ ) # 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). __lowerCAmelCase = model.to(accelerator.device ) # Instantiate optimizer __lowerCAmelCase = AdamW(params=model.parameters() , lr=SCREAMING_SNAKE_CASE_ ) # Instantiate scheduler __lowerCAmelCase = get_linear_schedule_with_warmup( optimizer=SCREAMING_SNAKE_CASE_ , num_warmup_steps=1_00 , num_training_steps=(len(SCREAMING_SNAKE_CASE_ ) * num_epochs) , ) # 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. __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = accelerator.prepare( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Now we train the model for epoch in range(SCREAMING_SNAKE_CASE_ ): model.train() for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(SCREAMING_SNAKE_CASE_ ): __lowerCAmelCase = model(**SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = output.loss accelerator.backward(SCREAMING_SNAKE_CASE_ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __lowerCAmelCase = model(**SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = outputs.logits.argmax(dim=-1 ) __lowerCAmelCase , __lowerCAmelCase = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=SCREAMING_SNAKE_CASE_ , references=SCREAMING_SNAKE_CASE_ , ) __lowerCAmelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , SCREAMING_SNAKE_CASE_ ) def _a ( ): __lowerCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , 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." , ) # New Code # parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE_ , default=1 , help="The number of minibatches to be ran before gradients are accumulated." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": main()
92
import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class a__ ( snake_case__ , snake_case__ , snake_case__ , unittest.TestCase ): _a : str = StableUnCLIPPipeline _a : Union[str, Any] = TEXT_TO_IMAGE_PARAMS _a : Dict = TEXT_TO_IMAGE_BATCH_PARAMS _a : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS _a : Dict = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false _a : Optional[Any] = False def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = 3_2 __lowerCAmelCase = embedder_hidden_size # prior components torch.manual_seed(0 ) __lowerCAmelCase = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) torch.manual_seed(0 ) __lowerCAmelCase = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=_A , intermediate_size=3_7 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) ) torch.manual_seed(0 ) __lowerCAmelCase = PriorTransformer( num_attention_heads=2 , attention_head_dim=1_2 , embedding_dim=_A , num_layers=1 , ) torch.manual_seed(0 ) __lowerCAmelCase = DDPMScheduler( variance_type="fixed_small_log" , prediction_type="sample" , num_train_timesteps=1_0_0_0 , clip_sample=_A , clip_sample_range=5.0 , beta_schedule="squaredcos_cap_v2" , ) # regular denoising components torch.manual_seed(0 ) __lowerCAmelCase = StableUnCLIPImageNormalizer(embedding_dim=_A ) __lowerCAmelCase = DDPMScheduler(beta_schedule="squaredcos_cap_v2" ) torch.manual_seed(0 ) __lowerCAmelCase = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) torch.manual_seed(0 ) __lowerCAmelCase = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) ) torch.manual_seed(0 ) __lowerCAmelCase = UNetaDConditionModel( sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=("CrossAttnDownBlock2D", "DownBlock2D") , up_block_types=("UpBlock2D", "CrossAttnUpBlock2D") , block_out_channels=(3_2, 6_4) , attention_head_dim=(2, 4) , class_embed_type="projection" , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) __lowerCAmelCase = DDIMScheduler( beta_schedule="scaled_linear" , beta_start=0.0_00_85 , beta_end=0.0_12 , prediction_type="v_prediction" , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) __lowerCAmelCase = AutoencoderKL() __lowerCAmelCase = { # prior components "prior_tokenizer": prior_tokenizer, "prior_text_encoder": prior_text_encoder, "prior": prior, "prior_scheduler": prior_scheduler, # image noising components "image_normalizer": image_normalizer, "image_noising_scheduler": image_noising_scheduler, # regular denoising components "tokenizer": tokenizer, "text_encoder": text_encoder, "unet": unet, "scheduler": scheduler, "vae": vae, } return components def __SCREAMING_SNAKE_CASE( self , _A , _A=0 ): """simple docstring""" if str(_A ).startswith("mps" ): __lowerCAmelCase = torch.manual_seed(_A ) else: __lowerCAmelCase = torch.Generator(device=_A ).manual_seed(_A ) __lowerCAmelCase = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "prior_num_inference_steps": 2, "output_type": "numpy", } return inputs def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = torch_device == "cpu" self._test_attention_slicing_forward_pass(test_max_difference=_A ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = torch_device in ["cpu", "mps"] self._test_inference_batch_single_identical(test_max_difference=_A ) @slow @require_torch_gpu class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy" ) __lowerCAmelCase = StableUnCLIPPipeline.from_pretrained("fusing/stable-unclip-2-1-l" , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __lowerCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) __lowerCAmelCase = pipe("anime turle" , generator=_A , output_type="np" ) __lowerCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_A , _A ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __lowerCAmelCase = StableUnCLIPPipeline.from_pretrained("fusing/stable-unclip-2-1-l" , torch_dtype=torch.floataa ) __lowerCAmelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __lowerCAmelCase = pipe( "anime turtle" , prior_num_inference_steps=2 , num_inference_steps=2 , output_type="np" , ) __lowerCAmelCase = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 1_0**9
92
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __magic_name__ = logging.get_logger(__name__) __magic_name__ = { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json" ), "distilbert-base-uncased-finetuned-sst-2-english": ( "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json" ), } class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : str = '''distilbert''' __lowercase : Optional[int] = { '''hidden_size''': '''dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', } def __init__( self , lowerCAmelCase__=3_0_5_2_2 , lowerCAmelCase__=5_1_2 , lowerCAmelCase__=False , lowerCAmelCase__=6 , lowerCAmelCase__=1_2 , lowerCAmelCase__=7_6_8 , lowerCAmelCase__=4 * 7_6_8 , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.02 , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.2 , lowerCAmelCase__=0 , **lowerCAmelCase__ , ): __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = sinusoidal_pos_embds __SCREAMING_SNAKE_CASE = n_layers __SCREAMING_SNAKE_CASE = n_heads __SCREAMING_SNAKE_CASE = dim __SCREAMING_SNAKE_CASE = hidden_dim __SCREAMING_SNAKE_CASE = dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = qa_dropout __SCREAMING_SNAKE_CASE = seq_classif_dropout super().__init__(**lowerCAmelCase__ , pad_token_id=lowerCAmelCase__) class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" @property def snake_case_ ( self): if self.task == "multiple-choice": __SCREAMING_SNAKE_CASE = {0: """batch""", 1: """choice""", 2: """sequence"""} else: __SCREAMING_SNAKE_CASE = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ])
255
"""simple docstring""" import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow __magic_name__ = False class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): """simple docstring""" def snake_case_ ( self , lowerCAmelCase__=3_2): set_seed(0) __SCREAMING_SNAKE_CASE = UNetaDModel(sample_size=lowerCAmelCase__ , in_channels=3 , out_channels=3) __SCREAMING_SNAKE_CASE = torch.optim.SGD(model.parameters() , lr=0.00_01) return model, optimizer @slow def snake_case_ ( self): __SCREAMING_SNAKE_CASE = """cpu""" # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable __SCREAMING_SNAKE_CASE = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.00_01 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.00_01 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=lowerCAmelCase__ , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0) __SCREAMING_SNAKE_CASE = [torch.randn((4, 3, 3_2, 3_2)).clip(-1 , 1).to(lowerCAmelCase__) for _ in range(4)] __SCREAMING_SNAKE_CASE = [torch.randn((4, 3, 3_2, 3_2)).to(lowerCAmelCase__) for _ in range(4)] __SCREAMING_SNAKE_CASE = [torch.randint(0 , 1_0_0_0 , (4,)).long().to(lowerCAmelCase__) for _ in range(4)] # train with a DDPM scheduler __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = self.get_model_optimizer(resolution=3_2) model.train().to(lowerCAmelCase__) for i in range(4): optimizer.zero_grad() __SCREAMING_SNAKE_CASE = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i]) __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , timesteps[i]).sample __SCREAMING_SNAKE_CASE = torch.nn.functional.mse_loss(lowerCAmelCase__ , noise[i]) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = self.get_model_optimizer(resolution=3_2) model.train().to(lowerCAmelCase__) for i in range(4): optimizer.zero_grad() __SCREAMING_SNAKE_CASE = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i]) __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , timesteps[i]).sample __SCREAMING_SNAKE_CASE = torch.nn.functional.mse_loss(lowerCAmelCase__ , noise[i]) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-5)) self.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-5))
255
1
"""simple docstring""" from sklearn.metrics import matthews_corrcoef import datasets lowerCamelCase_ = ''' Compute the Matthews correlation coefficient (MCC) The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia] ''' lowerCamelCase_ = ''' Args: predictions (list of int): Predicted labels, as returned by a model. references (list of int): Ground truth labels. sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`. Returns: matthews_correlation (dict containing float): Matthews correlation. Examples: Example 1, a basic example with only predictions and references as inputs: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.54 Example 2, the same example as above, but also including sample weights: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 3, 1, 1, 1, 2]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.1 Example 3, the same example as above, but with sample weights that cause a negative correlation: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 1, 0, 0, 0, 1]) >>> print(round(results[\'matthews_correlation\'], 2)) -0.25 ''' lowerCamelCase_ = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase_ (datasets.Metric ): def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int32" ), "references": datasets.Value("int32" ), } ) , reference_urls=[ "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html" ] , ) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase_ : str , lowerCAmelCase_ : int , lowerCAmelCase_ : Dict=None ) -> Tuple: return { "matthews_correlation": float(matthews_corrcoef(lowerCAmelCase_ , lowerCAmelCase_ , sample_weight=lowerCAmelCase_ ) ), }
268
"""simple docstring""" lowerCamelCase_ = [ (1000, '''M'''), (900, '''CM'''), (500, '''D'''), (400, '''CD'''), (100, '''C'''), (90, '''XC'''), (50, '''L'''), (40, '''XL'''), (10, '''X'''), (9, '''IX'''), (5, '''V'''), (4, '''IV'''), (1, '''I'''), ] def snake_case ( A__ ): UpperCAmelCase_ : List[str] = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 1_00, "D": 5_00, "M": 10_00} UpperCAmelCase_ : Optional[Any] = 0 UpperCAmelCase_ : Tuple = 0 while place < len(A__ ): if (place + 1 < len(A__ )) and (vals[roman[place]] < vals[roman[place + 1]]): total += vals[roman[place + 1]] - vals[roman[place]] place += 2 else: total += vals[roman[place]] place += 1 return total def snake_case ( A__ ): UpperCAmelCase_ : Union[str, Any] = [] for arabic, roman in ROMAN: ((UpperCAmelCase_) , (UpperCAmelCase_)) : str = divmod(A__ ,A__ ) result.append(roman * factor ) if number == 0: break return "".join(A__ ) if __name__ == "__main__": import doctest doctest.testmod()
268
1
"""simple docstring""" import inspect from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch import torch.utils.checkpoint from ...models import UNetaDModel, VQModel from ...schedulers import ( DDIMScheduler, DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, ) from ...utils import PIL_INTERPOLATION, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase = image.size __lowerCAmelCase , __lowerCAmelCase = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 __lowerCAmelCase = image.resize((w, h) , resample=PIL_INTERPOLATION["lanczos"] ) __lowerCAmelCase = np.array(_UpperCamelCase ).astype(np.floataa ) / 2_55.0 __lowerCAmelCase = image[None].transpose(0 , 3 , 1 , 2 ) __lowerCAmelCase = torch.from_numpy(_UpperCamelCase ) return 2.0 * image - 1.0 class _UpperCamelCase ( lowerCAmelCase__ ): '''simple docstring''' def __init__( self , __a , __a , __a , ): super().__init__() self.register_modules(vqvae=__a , unet=__a , scheduler=__a ) @torch.no_grad() def __call__( self , __a = None , __a = 1 , __a = 1_00 , __a = 0.0 , __a = None , __a = "pil" , __a = True , ): if isinstance(__a , PIL.Image.Image ): __lowerCAmelCase = 1 elif isinstance(__a , torch.Tensor ): __lowerCAmelCase = image.shape[0] else: raise ValueError(f"`image` has to be of type `PIL.Image.Image` or `torch.Tensor` but is {type(__a )}" ) if isinstance(__a , PIL.Image.Image ): __lowerCAmelCase = preprocess(__a ) __lowerCAmelCase , __lowerCAmelCase = image.shape[-2:] # in_channels should be 6: 3 for latents, 3 for low resolution image __lowerCAmelCase = (batch_size, self.unet.config.in_channels // 2, height, width) __lowerCAmelCase = next(self.unet.parameters() ).dtype __lowerCAmelCase = randn_tensor(__a , generator=__a , device=self.device , dtype=__a ) __lowerCAmelCase = image.to(device=self.device , dtype=__a ) # set timesteps and move to the correct device self.scheduler.set_timesteps(__a , device=self.device ) __lowerCAmelCase = self.scheduler.timesteps # scale the initial noise by the standard deviation required by the scheduler __lowerCAmelCase = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature. # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __lowerCAmelCase = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __lowerCAmelCase = {} if accepts_eta: __lowerCAmelCase = eta for t in self.progress_bar(__a ): # concat latents and low resolution image in the channel dimension. __lowerCAmelCase = torch.cat([latents, image] , dim=1 ) __lowerCAmelCase = self.scheduler.scale_model_input(__a , __a ) # predict the noise residual __lowerCAmelCase = self.unet(__a , __a ).sample # compute the previous noisy sample x_t -> x_t-1 __lowerCAmelCase = self.scheduler.step(__a , __a , __a , **__a ).prev_sample # decode the image latents with the VQVAE __lowerCAmelCase = self.vqvae.decode(__a ).sample __lowerCAmelCase = torch.clamp(__a , -1.0 , 1.0 ) __lowerCAmelCase = image / 2 + 0.5 __lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __lowerCAmelCase = self.numpy_to_pil(__a ) if not return_dict: return (image,) return ImagePipelineOutput(images=__a )
259
"""simple docstring""" from __future__ import annotations from collections.abc import Generator def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = {} __lowerCAmelCase = 2 while True: __lowerCAmelCase = factor_map.pop(_UpperCamelCase , _UpperCamelCase ) if factor: __lowerCAmelCase = factor + prime while x in factor_map: x += factor __lowerCAmelCase = factor else: __lowerCAmelCase = prime yield prime prime += 1 def _lowerCamelCase ( _UpperCamelCase = 1e10 ): '''simple docstring''' __lowerCAmelCase = sieve() __lowerCAmelCase = 1 while True: __lowerCAmelCase = next(_UpperCamelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(_UpperCamelCase ) n += 2 if __name__ == "__main__": print(solution())
259
1
import shutil import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_tf_cross_test, require_tf, require_torch, require_torchvision, require_vision, ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, SamImageProcessor, SamProcessor if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf @require_vision @require_torchvision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def UpperCamelCase ( self ): A__ = tempfile.mkdtemp() A__ = SamImageProcessor() A__ = SamProcessor(__lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self,**__lowerCamelCase ): return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase ).image_processor def UpperCamelCase ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self ): A__ = [np.random.randint(255,size=(3, 30, 400),dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase ( self ): A__ = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_image_processor(do_normalize=__lowerCamelCase,padding_value=1.0 ) A__ = SamProcessor.from_pretrained(self.tmpdirname,do_normalize=__lowerCamelCase,padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string(),image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor,__lowerCamelCase ) def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCamelCase,return_tensors='''np''' ) A__ = processor(images=__lowerCamelCase,return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop original_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum(),input_processor[key].sum(),delta=1E-2 ) @require_torch def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = [torch.ones((1, 3, 5, 5) )] A__ = [[1764, 2646]] A__ = [[683, 1024]] A__ = processor.post_process_masks(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) A__ = processor.post_process_masks( __lowerCamelCase,torch.tensor(__lowerCamelCase ),torch.tensor(__lowerCamelCase ) ) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) # should also work with np A__ = [np.ones((1, 3, 5, 5) )] A__ = processor.post_process_masks(__lowerCamelCase,np.array(__lowerCamelCase ),np.array(__lowerCamelCase ) ) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) A__ = [[1, 0], [0, 1]] with self.assertRaises(__lowerCamelCase ): A__ = processor.post_process_masks(__lowerCamelCase,np.array(__lowerCamelCase ),np.array(__lowerCamelCase ) ) @require_vision @require_tf class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def UpperCamelCase ( self ): A__ = tempfile.mkdtemp() A__ = SamImageProcessor() A__ = SamProcessor(__lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self,**__lowerCamelCase ): return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase ).image_processor def UpperCamelCase ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self ): A__ = [np.random.randint(255,size=(3, 30, 400),dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase ( self ): A__ = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_image_processor(do_normalize=__lowerCamelCase,padding_value=1.0 ) A__ = SamProcessor.from_pretrained(self.tmpdirname,do_normalize=__lowerCamelCase,padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string(),image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor,__lowerCamelCase ) def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCamelCase,return_tensors='''np''' ) A__ = processor(images=__lowerCamelCase,return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop reshaped_input_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum(),input_processor[key].sum(),delta=1E-2 ) @require_tf def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = [tf.ones((1, 3, 5, 5) )] A__ = [[1764, 2646]] A__ = [[683, 1024]] A__ = processor.post_process_masks(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''tf''' ) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) A__ = processor.post_process_masks( __lowerCamelCase,tf.convert_to_tensor(__lowerCamelCase ),tf.convert_to_tensor(__lowerCamelCase ),return_tensors='''tf''',) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) # should also work with np A__ = [np.ones((1, 3, 5, 5) )] A__ = processor.post_process_masks( __lowerCamelCase,np.array(__lowerCamelCase ),np.array(__lowerCamelCase ),return_tensors='''tf''' ) self.assertEqual(masks[0].shape,(1, 3, 1764, 2646) ) A__ = [[1, 0], [0, 1]] with self.assertRaises(tf.errors.InvalidArgumentError ): A__ = processor.post_process_masks( __lowerCamelCase,np.array(__lowerCamelCase ),np.array(__lowerCamelCase ),return_tensors='''tf''' ) @require_vision @require_torchvision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def UpperCamelCase ( self ): A__ = tempfile.mkdtemp() A__ = SamImageProcessor() A__ = SamProcessor(__lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self,**__lowerCamelCase ): return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase ).image_processor def UpperCamelCase ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self ): A__ = [np.random.randint(255,size=(3, 30, 400),dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1 ) ) for x in image_inputs] return image_inputs @is_pt_tf_cross_test def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = np.random.randint(0,2,size=(1, 3, 5, 5) ).astype(np.floataa ) A__ = [tf.convert_to_tensor(__lowerCamelCase )] A__ = [torch.tensor(__lowerCamelCase )] A__ = [[1764, 2646]] A__ = [[683, 1024]] A__ = processor.post_process_masks( __lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''tf''' ) A__ = processor.post_process_masks( __lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''pt''' ) self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy() ) ) @is_pt_tf_cross_test def UpperCamelCase ( self ): A__ = self.get_image_processor() A__ = SamProcessor(image_processor=__lowerCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCamelCase,return_tensors='''pt''' )['''pixel_values'''].numpy() A__ = processor(images=__lowerCamelCase,return_tensors='''pt''' )['''pixel_values'''].numpy() A__ = image_processor(__lowerCamelCase,return_tensors='''tf''' )['''pixel_values'''].numpy() A__ = processor(images=__lowerCamelCase,return_tensors='''tf''' )['''pixel_values'''].numpy() self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase ) ) self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase ) ) self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase ) )
193
import inspect import unittest from transformers import RegNetConfig, is_flax_available from transformers.testing_utils import require_flax, slow from transformers.utils import cached_property, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def __init__( self,__lowerCamelCase,__lowerCamelCase=3,__lowerCamelCase=32,__lowerCamelCase=3,__lowerCamelCase=10,__lowerCamelCase=[10, 20, 30, 40],__lowerCamelCase=[1, 1, 2, 1],__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase="relu",__lowerCamelCase=3,__lowerCamelCase=None,): A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = embeddings_size A__ = hidden_sizes A__ = depths A__ = is_training A__ = use_labels A__ = hidden_act A__ = num_labels A__ = scope A__ = len(__lowerCamelCase ) def UpperCamelCase ( self ): A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = self.get_config() return config, pixel_values def UpperCamelCase ( self ): return RegNetConfig( num_channels=self.num_channels,embeddings_size=self.embeddings_size,hidden_sizes=self.hidden_sizes,depths=self.depths,hidden_act=self.hidden_act,num_labels=self.num_labels,image_size=self.image_size,) def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase ): A__ = FlaxRegNetModel(config=__lowerCamelCase ) A__ = model(__lowerCamelCase ) # Output shape (b, c, h, w) self.parent.assertEqual( result.last_hidden_state.shape,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32),) def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase ): A__ = self.num_labels A__ = FlaxRegNetForImageClassification(config=__lowerCamelCase ) A__ = model(__lowerCamelCase ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels) ) def UpperCamelCase ( self ): A__ = self.prepare_config_and_inputs() A__ , A__ = config_and_inputs A__ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_flax class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ , unittest.TestCase ): __SCREAMING_SNAKE_CASE = (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else () __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ): A__ = FlaxRegNetModelTester(self ) A__ = ConfigTester(self,config_class=__lowerCamelCase,has_text_modality=__lowerCamelCase ) def UpperCamelCase ( self ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase ( self ): return def UpperCamelCase ( self ): A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def UpperCamelCase ( self ): A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase ) @unittest.skip(reason='''RegNet does not use inputs_embeds''' ) def UpperCamelCase ( self ): pass @unittest.skip(reason='''RegNet does not support input and output embeddings''' ) def UpperCamelCase ( self ): pass def UpperCamelCase ( self ): A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCamelCase ) A__ = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['''pixel_values'''] self.assertListEqual(arg_names[:1],__lowerCamelCase ) def UpperCamelCase ( self ): def check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ): A__ = model_class(__lowerCamelCase ) A__ = model(**self._prepare_for_class(__lowerCamelCase,__lowerCamelCase ) ) A__ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A__ = self.model_tester.num_stages self.assertEqual(len(__lowerCamelCase ),expected_num_stages + 1 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ) def UpperCamelCase ( self ): A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A__ = self._prepare_for_class(__lowerCamelCase,__lowerCamelCase ) A__ = model_class(__lowerCamelCase ) @jax.jit def model_jitted(__lowerCamelCase,**__lowerCamelCase ): return model(pixel_values=__lowerCamelCase,**__lowerCamelCase ) with self.subTest('''JIT Enabled''' ): A__ = model_jitted(**__lowerCamelCase ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): A__ = model_jitted(**__lowerCamelCase ).to_tuple() self.assertEqual(len(__lowerCamelCase ),len(__lowerCamelCase ) ) for jitted_output, output in zip(__lowerCamelCase,__lowerCamelCase ): self.assertEqual(jitted_output.shape,output.shape ) def UpperCamelCase__( )->Optional[int]: A__ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_flax class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): @cached_property def UpperCamelCase ( self ): return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040''' ) if is_vision_available() else None @slow def UpperCamelCase ( self ): A__ = FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040''' ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=__lowerCamelCase,return_tensors='''np''' ) A__ = model(**__lowerCamelCase ) # verify the logits A__ = (1, 1000) self.assertEqual(outputs.logits.shape,__lowerCamelCase ) A__ = jnp.array([-0.4180, -1.5051, -3.4836] ) self.assertTrue(jnp.allclose(outputs.logits[0, :3],__lowerCamelCase,atol=1E-4 ) )
193
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available _A = {"""configuration_speech_encoder_decoder""": ["""SpeechEncoderDecoderConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = ["""SpeechEncoderDecoderModel"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = ["""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 _A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
364
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoImageProcessor, ViTImageProcessor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_image_processing import CustomImageProcessor # noqa E402 _A = get_tests_dir('''fixtures''') class A ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = mock.Mock() lowerCAmelCase_ = 500 lowerCAmelCase_ = {} lowerCAmelCase_ = HTTPError lowerCAmelCase_ = {} # Download this model to make sure it's in the cache. lowerCAmelCase_ = ViTImageProcessor.from_pretrained('''hf-internal-testing/tiny-random-vit''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''', return_value=UpperCamelCase__ ) as mock_head: lowerCAmelCase_ = ViTImageProcessor.from_pretrained('''hf-internal-testing/tiny-random-vit''' ) # This check we did call the fake head request mock_head.assert_called() def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = ViTImageProcessor.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json''' ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" with self.assertRaises(UpperCamelCase__ ): # config is in subfolder, the following should not work without specifying the subfolder lowerCAmelCase_ = AutoImageProcessor.from_pretrained('''hf-internal-testing/stable-diffusion-all-variants''' ) lowerCAmelCase_ = AutoImageProcessor.from_pretrained( '''hf-internal-testing/stable-diffusion-all-variants''', subfolder='''feature_extractor''' ) self.assertIsNotNone(UpperCamelCase__ ) @is_staging_test class A ( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE__ ( cls ): """simple docstring""" lowerCAmelCase_ = TOKEN HfFolder.save_token(UpperCamelCase__ ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls ): """simple docstring""" try: delete_repo(token=cls._token, repo_id='''test-image-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='''valid_org/test-image-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='''test-dynamic-image-processor''' ) except HTTPError: pass def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = ViTImageProcessor.from_pretrained(UpperCamelCase__ ) image_processor.push_to_hub('''test-image-processor''', use_auth_token=self._token ) lowerCAmelCase_ = ViTImageProcessor.from_pretrained(f"{USER}/test-image-processor" ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCamelCase__, getattr(UpperCamelCase__, UpperCamelCase__ ) ) # Reset repo delete_repo(token=self._token, repo_id='''test-image-processor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( UpperCamelCase__, repo_id='''test-image-processor''', push_to_hub=UpperCamelCase__, use_auth_token=self._token ) lowerCAmelCase_ = ViTImageProcessor.from_pretrained(f"{USER}/test-image-processor" ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCamelCase__, getattr(UpperCamelCase__, UpperCamelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = ViTImageProcessor.from_pretrained(UpperCamelCase__ ) image_processor.push_to_hub('''valid_org/test-image-processor''', use_auth_token=self._token ) lowerCAmelCase_ = ViTImageProcessor.from_pretrained('''valid_org/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCamelCase__, getattr(UpperCamelCase__, UpperCamelCase__ ) ) # Reset repo delete_repo(token=self._token, repo_id='''valid_org/test-image-processor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( UpperCamelCase__, repo_id='''valid_org/test-image-processor-org''', push_to_hub=UpperCamelCase__, use_auth_token=self._token ) lowerCAmelCase_ = ViTImageProcessor.from_pretrained('''valid_org/test-image-processor-org''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(UpperCamelCase__, getattr(UpperCamelCase__, UpperCamelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" CustomImageProcessor.register_for_auto_class() lowerCAmelCase_ = CustomImageProcessor.from_pretrained(UpperCamelCase__ ) image_processor.push_to_hub('''test-dynamic-image-processor''', use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( image_processor.auto_map, {'''AutoImageProcessor''': '''custom_image_processing.CustomImageProcessor'''}, ) lowerCAmelCase_ = AutoImageProcessor.from_pretrained( f"{USER}/test-dynamic-image-processor", trust_remote_code=UpperCamelCase__ ) # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module self.assertEqual(new_image_processor.__class__.__name__, '''CustomImageProcessor''' )
167
0
"""simple docstring""" from __future__ import annotations import math def _lowerCamelCase( a ): 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(a ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _lowerCamelCase( a ): __a = str(a ) __a = [n] for i in range(1 , len(a ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def _lowerCamelCase( a ): if len(str(a ) ) > 3: if not is_prime(int(str(a )[-3:] ) ) or not is_prime(int(str(a )[:3] ) ): return False return True def _lowerCamelCase( a = 1_1 ): __a = [] __a = 1_3 while len(a ) != count: if validate(a ): __a = list_truncated_nums(a ) if all(is_prime(a ) for i in list_nums ): list_truncated_primes.append(a ) num += 2 return list_truncated_primes def _lowerCamelCase( ): return sum(compute_truncated_primes(1_1 ) ) if __name__ == "__main__": print(F'''{sum(compute_truncated_primes(11)) = }''')
261
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class snake_case__ ( snake_case_, snake_case_ ): @register_to_config def __init__( self , lowerCamelCase = 768 , ): super().__init__() __a = nn.Parameter(torch.zeros(1 , lowerCamelCase ) ) __a = nn.Parameter(torch.ones(1 , lowerCamelCase ) ) def a__ ( self , lowerCamelCase = None , lowerCamelCase = None , ): __a = nn.Parameter(self.mean.to(lowerCamelCase ).to(lowerCamelCase ) ) __a = nn.Parameter(self.std.to(lowerCamelCase ).to(lowerCamelCase ) ) return self def a__ ( self , lowerCamelCase ): __a = (embeds - self.mean) * 1.0 / self.std return embeds def a__ ( self , lowerCamelCase ): __a = (embeds * self.std) + self.mean return embeds
261
1
import comet # From: unbabel-comet import torch import datasets __UpperCamelCase : Tuple = datasets.logging.get_logger(__name__) __UpperCamelCase : str = "\\n@inproceedings{rei-EtAl:2020:WMT,\n author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon},\n title = {Unbabel's Participation in the WMT20 Metrics Shared Task},\n booktitle = {Proceedings of the Fifth Conference on Machine Translation},\n month = {November},\n year = {2020},\n address = {Online},\n publisher = {Association for Computational Linguistics},\n pages = {909--918},\n}\n@inproceedings{rei-etal-2020-comet,\n title = \"{COMET}: A Neural Framework for {MT} Evaluation\",\n author = \"Rei, Ricardo and\n Stewart, Craig and\n Farinha, Ana C and\n Lavie, Alon\",\n booktitle = \"Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)\",\n month = nov,\n year = \"2020\",\n address = \"Online\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/2020.emnlp-main.213\",\n pages = \"2685--2702\",\n}\n" __UpperCamelCase : Tuple = "\\nCrosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA's or MQM).\nWith the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition.\n\nSee the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information.\n" __UpperCamelCase : Optional[int] = "\nCOMET score.\n\nArgs:\n\n`sources` (list of str): Source sentences\n`predictions` (list of str): candidate translations\n`references` (list of str): reference translations\n`cuda` (bool): If set to True, runs COMET using GPU\n`show_progress` (bool): Shows progress\n`model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None.\n\nReturns:\n `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`.\n `scores`: List of scores.\n\nExamples:\n\n >>> comet_metric = datasets.load_metric('comet')\n >>> # comet_metric = load_metric('comet', 'wmt20-comet-da') # you can also choose which model to use\n >>> source = [\"Dem Feuer konnte Einhalt geboten werden\", \"Schulen und Kindergärten wurden eröffnet.\"]\n >>> hypothesis = [\"The fire could be stopped\", \"Schools and kindergartens were open\"]\n >>> reference = [\"They were able to control the fire.\", \"Schools and kindergartens opened\"]\n >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source)\n >>> print([round(v, 2) for v in results[\"scores\"]])\n [0.19, 0.92]\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowercase__ ( datasets.Metric): def __A ( self : Any ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A ( self : List[Any] , UpperCamelCase__ : Optional[int] ): '''simple docstring''' if self.config_name == "default": SCREAMING_SNAKE_CASE : Any = comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: SCREAMING_SNAKE_CASE : str = comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A ( self : List[Any] , UpperCamelCase__ : str , UpperCamelCase__ : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : int=None , UpperCamelCase__ : Optional[Any]=False ): '''simple docstring''' if gpus is None: SCREAMING_SNAKE_CASE : Optional[Any] = 1 if torch.cuda.is_available() else 0 SCREAMING_SNAKE_CASE : Any = {'''src''': sources, '''mt''': predictions, '''ref''': references} SCREAMING_SNAKE_CASE : Union[str, Any] = [dict(zip(_snake_case , _snake_case ) ) for t in zip(*data.values() )] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = self.scorer.predict(_snake_case , gpus=_snake_case , progress_bar=_snake_case ) return {"mean_score": mean_score, "scores": scores}
357
import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int __UpperCamelCase : Union[str, Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class lowercase__ ( datasets.BuilderConfig): UpperCamelCase_ = None def A ( _lowercase , _lowercase , ): import pyspark def generate_fn(): SCREAMING_SNAKE_CASE : str = df.select('''*''' , pyspark.sql.functions.spark_partition_id().alias('''part_id''' ) ) for partition_id in partition_order: SCREAMING_SNAKE_CASE : Any = df_with_partition_id.select('''*''' ).where(f"""part_id = {partition_id}""" ).drop('''part_id''' ) SCREAMING_SNAKE_CASE : Tuple = partition_df.collect() SCREAMING_SNAKE_CASE : str = 0 for row in rows: yield f"""{partition_id}_{row_id}""", row.asDict() row_id += 1 return generate_fn class lowercase__ ( _BaseExamplesIterable): def __init__( self : Optional[Any] , UpperCamelCase__ : "pyspark.sql.DataFrame" , UpperCamelCase__ : Union[str, Any]=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = df SCREAMING_SNAKE_CASE : List[str] = partition_order or range(self.df.rdd.getNumPartitions() ) SCREAMING_SNAKE_CASE : Dict = _generate_iterable_examples(self.df , self.partition_order ) def __iter__( self : List[Any] ): '''simple docstring''' yield from self.generate_examples_fn() def __A ( self : Tuple , UpperCamelCase__ : np.random.Generator ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(UpperCamelCase__ ) return SparkExamplesIterable(self.df , partition_order=UpperCamelCase__ ) def __A ( self : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.split_shard_indices_by_worker(UpperCamelCase__ , UpperCamelCase__ ) return SparkExamplesIterable(self.df , partition_order=UpperCamelCase__ ) @property def __A ( self : Tuple ): '''simple docstring''' return len(self.partition_order ) class lowercase__ ( datasets.DatasetBuilder): UpperCamelCase_ = SparkConfig def __init__( self : Union[str, Any] , UpperCamelCase__ : "pyspark.sql.DataFrame" , UpperCamelCase__ : str = None , UpperCamelCase__ : str = None , **UpperCamelCase__ : Union[str, Any] , ): '''simple docstring''' import pyspark SCREAMING_SNAKE_CASE : str = pyspark.sql.SparkSession.builder.getOrCreate() SCREAMING_SNAKE_CASE : List[Any] = df SCREAMING_SNAKE_CASE : Tuple = working_dir super().__init__( cache_dir=UpperCamelCase__ , config_name=str(self.df.semanticHash() ) , **UpperCamelCase__ , ) def __A ( self : Tuple ): '''simple docstring''' def create_cache_and_write_probe(UpperCamelCase__ : str ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir , exist_ok=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Any = os.path.join(self._cache_dir , '''fs_test''' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(UpperCamelCase__ , '''a''' ) return [probe_file] if self._spark.conf.get('''spark.master''' , '''''' ).startswith('''local''' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: SCREAMING_SNAKE_CASE : Dict = ( self._spark.sparkContext.parallelize(range(1 ) , 1 ).mapPartitions(UpperCamelCase__ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( '''When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir''' ) def __A ( self : Any ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def __A ( self : str , UpperCamelCase__ : datasets.download.download_manager.DownloadManager ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def __A ( self : int , UpperCamelCase__ : List[Any] ): '''simple docstring''' import pyspark def get_arrow_batch_size(UpperCamelCase__ : Tuple ): for batch in it: yield pa.RecordBatch.from_pydict({'''batch_bytes''': [batch.nbytes]} ) SCREAMING_SNAKE_CASE : int = self.df.count() SCREAMING_SNAKE_CASE : Tuple = df_num_rows if df_num_rows <= 100 else 100 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. SCREAMING_SNAKE_CASE : Optional[Any] = ( self.df.limit(UpperCamelCase__ ) .repartition(1 ) .mapInArrow(UpperCamelCase__ , '''batch_bytes: long''' ) .agg(pyspark.sql.functions.sum('''batch_bytes''' ).alias('''sample_bytes''' ) ) .collect()[0] .sample_bytes / sample_num_rows ) SCREAMING_SNAKE_CASE : Optional[Any] = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. SCREAMING_SNAKE_CASE : List[str] = min(UpperCamelCase__ , int(approx_total_size / max_shard_size ) ) SCREAMING_SNAKE_CASE : Optional[int] = self.df.repartition(UpperCamelCase__ ) def __A ( self : str , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int , ): '''simple docstring''' import pyspark SCREAMING_SNAKE_CASE : int = ParquetWriter if file_format == '''parquet''' else ArrowWriter SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(self._working_dir , os.path.basename(UpperCamelCase__ ) ) if self._working_dir else fpath SCREAMING_SNAKE_CASE : Optional[int] = file_format == '''parquet''' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. SCREAMING_SNAKE_CASE : str = self.config.features SCREAMING_SNAKE_CASE : Optional[int] = self._writer_batch_size SCREAMING_SNAKE_CASE : Optional[int] = self._fs.storage_options def write_arrow(UpperCamelCase__ : Optional[Any] ): # Within the same SparkContext, no two task attempts will share the same attempt ID. SCREAMING_SNAKE_CASE : int = pyspark.TaskContext().taskAttemptId() SCREAMING_SNAKE_CASE : str = next(UpperCamelCase__ , UpperCamelCase__ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] , names=['''task_id''', '''num_examples''', '''num_bytes'''] , ) SCREAMING_SNAKE_CASE : List[Any] = 0 SCREAMING_SNAKE_CASE : Optional[int] = writer_class( features=UpperCamelCase__ , path=working_fpath.replace('''SSSSS''' , f"""{shard_id:05d}""" ).replace('''TTTTT''' , f"""{task_id:05d}""" ) , writer_batch_size=UpperCamelCase__ , storage_options=UpperCamelCase__ , embed_local_files=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : Tuple = pa.Table.from_batches([first_batch] ) writer.write_table(UpperCamelCase__ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['''task_id''', '''num_examples''', '''num_bytes'''] , ) shard_id += 1 SCREAMING_SNAKE_CASE : Optional[int] = writer_class( features=writer._features , path=working_fpath.replace('''SSSSS''' , f"""{shard_id:05d}""" ).replace('''TTTTT''' , f"""{task_id:05d}""" ) , writer_batch_size=UpperCamelCase__ , storage_options=UpperCamelCase__ , embed_local_files=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE : List[str] = pa.Table.from_batches([batch] ) writer.write_table(UpperCamelCase__ ) if writer._num_bytes > 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['''task_id''', '''num_examples''', '''num_bytes'''] , ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE : int = os.path.join(os.path.dirname(UpperCamelCase__ ) , os.path.basename(UpperCamelCase__ ) ) shutil.move(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = ( self.df.mapInArrow(UpperCamelCase__ , '''task_id: long, num_examples: long, num_bytes: long''' ) .groupBy('''task_id''' ) .agg( pyspark.sql.functions.sum('''num_examples''' ).alias('''total_num_examples''' ) , pyspark.sql.functions.sum('''num_bytes''' ).alias('''total_num_bytes''' ) , pyspark.sql.functions.count('''num_bytes''' ).alias('''num_shards''' ) , pyspark.sql.functions.collect_list('''num_examples''' ).alias('''shard_lengths''' ) , ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def __A ( self : Dict , UpperCamelCase__ : "datasets.SplitGenerator" , UpperCamelCase__ : str = "arrow" , UpperCamelCase__ : Optional[Union[str, int]] = None , UpperCamelCase__ : Optional[int] = None , **UpperCamelCase__ : str , ): '''simple docstring''' self._validate_cache_dir() SCREAMING_SNAKE_CASE : List[str] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = not is_remote_filesystem(self._fs ) SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join if is_local else posixpath.join SCREAMING_SNAKE_CASE : List[Any] = '''-TTTTT-SSSSS-of-NNNNN''' SCREAMING_SNAKE_CASE : List[str] = f"""{self.name}-{split_generator.name}{SUFFIX}.{file_format}""" SCREAMING_SNAKE_CASE : Dict = path_join(self._output_dir , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = 0 SCREAMING_SNAKE_CASE : Dict = 0 SCREAMING_SNAKE_CASE : Tuple = 0 SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Dict = [] for task_id, content in self._prepare_split_single(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = total_num_examples SCREAMING_SNAKE_CASE : Dict = total_num_bytes # should rename everything at the end logger.debug(f"""Renaming {total_shards} shards.""" ) if total_shards > 1: SCREAMING_SNAKE_CASE : Tuple = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. SCREAMING_SNAKE_CASE : str = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int , ): rename( UpperCamelCase__ , fpath.replace('''SSSSS''' , f"""{shard_id:05d}""" ).replace('''TTTTT''' , f"""{task_id:05d}""" ) , fpath.replace('''TTTTT-SSSSS''' , f"""{global_shard_id:05d}""" ).replace('''NNNNN''' , f"""{total_shards:05d}""" ) , ) SCREAMING_SNAKE_CASE : Optional[Any] = [] SCREAMING_SNAKE_CASE : Optional[Any] = 0 for i in range(len(UpperCamelCase__ ) ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = task_id_and_num_shards[i] for shard_id in range(UpperCamelCase__ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(UpperCamelCase__ , len(UpperCamelCase__ ) ).map(lambda UpperCamelCase__ : _rename_shard(*UpperCamelCase__ ) ).collect() else: # don't use any pattern SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = task_id_and_num_shards[0][0] self._rename( fpath.replace('''SSSSS''' , f"""{shard_id:05d}""" ).replace('''TTTTT''' , f"""{task_id:05d}""" ) , fpath.replace(UpperCamelCase__ , '''''' ) , ) def __A ( self : int , UpperCamelCase__ : "datasets.SplitGenerator" , ): '''simple docstring''' return SparkExamplesIterable(self.df )
258
0
"""simple docstring""" def _A ( lowercase , lowercase ): """simple docstring""" return "\n".join( f'''{number} * {i} = {number * i}''' for i in range(1 , number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=1_0))
81
"""simple docstring""" from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = DistilBertTokenizer __lowerCAmelCase = DistilBertTokenizerFast __lowerCAmelCase = True @slow def SCREAMING_SNAKE_CASE ( self ) -> int: a =DistilBertTokenizer.from_pretrained('''distilbert-base-uncased''' ) a =tokenizer.encode('''sequence builders''' , add_special_tokens=__A ) a =tokenizer.encode('''multi-sequence build''' , add_special_tokens=__A ) a =tokenizer.build_inputs_with_special_tokens(__A ) a =tokenizer.build_inputs_with_special_tokens(__A , __A ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
81
1
from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
109
from __future__ import annotations def __lowercase ( __lowerCAmelCase : list[int] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int ): if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): a__ , a__ = array[indexa], array[indexa] def __lowercase ( __lowerCAmelCase : list[int] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int ): if length > 1: a__ = int(length / 2 ) for i in range(__lowerCAmelCase , low + middle ): comp_and_swap(__lowerCAmelCase , __lowerCAmelCase , i + middle , __lowerCAmelCase ) bitonic_merge(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) bitonic_merge(__lowerCAmelCase , low + middle , __lowerCAmelCase , __lowerCAmelCase ) def __lowercase ( __lowerCAmelCase : list[int] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int ): if length > 1: a__ = int(length / 2 ) bitonic_sort(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , 1 ) bitonic_sort(__lowerCAmelCase , low + middle , __lowerCAmelCase , 0 ) bitonic_merge(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if __name__ == "__main__": snake_case : int = input('''Enter numbers separated by a comma:\n''').strip() snake_case : Optional[int] = [int(item.strip()) for item in user_input.split(''',''')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('''\nSorted array in ascending order is: ''', end='''''') print(*unsorted, sep=''', ''') bitonic_merge(unsorted, 0, len(unsorted), 0) print('''Sorted array in descending order is: ''', end='''''') print(*unsorted, sep=''', ''')
109
1
"""simple docstring""" import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class A_ : """simple docstring""" def __init__( self :Optional[Any] , lowercase_ :List[Any] , lowercase_ :Tuple=13 , lowercase_ :Union[str, Any]=7 , lowercase_ :Optional[Any]=True , lowercase_ :Optional[int]=True , lowercase_ :str=False , lowercase_ :Dict=True , lowercase_ :Optional[int]=99 , lowercase_ :List[str]=32 , lowercase_ :List[Any]=5 , lowercase_ :int=4 , lowercase_ :str=37 , lowercase_ :Optional[int]="gelu" , lowercase_ :Union[str, Any]=0.1 , lowercase_ :Optional[int]=0.1 , lowercase_ :Tuple=5_12 , lowercase_ :Optional[int]=16 , lowercase_ :Optional[int]=2 , lowercase_ :int=0.02 , lowercase_ :Union[str, Any]=3 , lowercase_ :Tuple=4 , lowercase_ :Any=None , ) -> str: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_input_mask UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = num_labels UpperCAmelCase = num_choices UpperCAmelCase = scope def UpperCAmelCase__ ( self :Dict ) -> Union[str, Any]: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = None if self.use_input_mask: UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase = None if self.use_token_type_ids: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase__ ( self :int ) -> Optional[Any]: return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase_ , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self :List[Any] , lowercase_ :int , lowercase_ :Union[str, Any] , lowercase_ :List[str] , lowercase_ :List[Any] , lowercase_ :Tuple , lowercase_ :Dict , lowercase_ :Union[str, Any] ) -> int: UpperCAmelCase = LlamaModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ ) UpperCAmelCase = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self :Dict , lowercase_ :Dict , lowercase_ :Union[str, Any] , lowercase_ :Optional[int] , lowercase_ :List[Any] , lowercase_ :List[str] , lowercase_ :Optional[int] , lowercase_ :int , lowercase_ :Dict , lowercase_ :str , ) -> Dict: UpperCAmelCase = True UpperCAmelCase = LlamaModel(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model( lowercase_ , attention_mask=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , ) UpperCAmelCase = model( lowercase_ , attention_mask=lowercase_ , encoder_hidden_states=lowercase_ , ) UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self :Dict , lowercase_ :Optional[int] , lowercase_ :Union[str, Any] , lowercase_ :Any , lowercase_ :List[Any] , lowercase_ :List[str] , lowercase_ :Dict , lowercase_ :Optional[Any] , lowercase_ :Tuple , lowercase_ :Union[str, Any] , ) -> List[str]: UpperCAmelCase = LlamaForCausalLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase__ ( self :str , lowercase_ :List[Any] , lowercase_ :str , lowercase_ :Optional[int] , lowercase_ :Optional[int] , lowercase_ :Optional[int] , lowercase_ :Dict , lowercase_ :Tuple , lowercase_ :Optional[int] , lowercase_ :Any , ) -> List[str]: UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = LlamaForCausalLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() # first forward pass UpperCAmelCase = model( lowercase_ , attention_mask=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , use_cache=lowercase_ , ) UpperCAmelCase = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCAmelCase = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCAmelCase = model( lowercase_ , attention_mask=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , output_hidden_states=lowercase_ , )['hidden_states'][0] UpperCAmelCase = model( lowercase_ , attention_mask=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , past_key_values=lowercase_ , output_hidden_states=lowercase_ , )['hidden_states'][0] # select random slice UpperCAmelCase = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowercase_ , lowercase_ , atol=1E-3 ) ) def UpperCAmelCase__ ( self :Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) = config_and_inputs UpperCAmelCase = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __UpperCamelCase = (LlamaForCausalLM,) if is_torch_available() else () __UpperCamelCase = ( { """feature-extraction""": LlamaModel, """text-classification""": LlamaForSequenceClassification, """text-generation""": LlamaForCausalLM, """zero-shot""": LlamaForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = LlamaModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowercase_ , hidden_size=37 ) def UpperCAmelCase__ ( self :str ) -> Optional[int]: self.config_tester.run_common_tests() def UpperCAmelCase__ ( self :List[Any] ) -> Dict: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def UpperCAmelCase__ ( self :Tuple ) -> List[str]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase = type self.model_tester.create_and_check_model(*lowercase_ ) def UpperCAmelCase__ ( self :str ) -> List[str]: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = input_dict['input_ids'] UpperCAmelCase = input_ids.ne(1 ).to(lowercase_ ) UpperCAmelCase = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) UpperCAmelCase = LlamaForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCAmelCase__ ( self :Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = 'single_label_classification' UpperCAmelCase = input_dict['input_ids'] UpperCAmelCase = input_ids.ne(1 ).to(lowercase_ ) UpperCAmelCase = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) UpperCAmelCase = LlamaForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCAmelCase__ ( self :List[str] ) -> Tuple: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = 'multi_label_classification' UpperCAmelCase = input_dict['input_ids'] UpperCAmelCase = input_ids.ne(1 ).to(lowercase_ ) UpperCAmelCase = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) UpperCAmelCase = LlamaForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def UpperCAmelCase__ ( self :Dict ) -> List[str]: pass @parameterized.expand([('linear',), ('dynamic',)] ) def UpperCAmelCase__ ( self :Optional[int] , lowercase_ :Optional[int] ) -> int: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = ids_tensor([1, 10] , config.vocab_size ) UpperCAmelCase = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights UpperCAmelCase = LlamaModel(lowercase_ ) original_model.to(lowercase_ ) original_model.eval() UpperCAmelCase = original_model(lowercase_ ).last_hidden_state UpperCAmelCase = original_model(lowercase_ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights UpperCAmelCase = {'type': scaling_type, 'factor': 10.0} UpperCAmelCase = LlamaModel(lowercase_ ) scaled_model.to(lowercase_ ) scaled_model.eval() UpperCAmelCase = scaled_model(lowercase_ ).last_hidden_state UpperCAmelCase = scaled_model(lowercase_ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(lowercase_ , lowercase_ , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(lowercase_ , lowercase_ , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(lowercase_ , lowercase_ , atol=1E-5 ) ) @require_torch class A_ ( unittest.TestCase ): """simple docstring""" @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def UpperCAmelCase__ ( self :Any ) -> Union[str, Any]: UpperCAmelCase = [1, 3_06, 46_58, 2_78, 65_93, 3_10, 28_34, 3_38] UpperCAmelCase = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto' ) UpperCAmelCase = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 UpperCAmelCase = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) , lowercase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off UpperCAmelCase = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowercase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def UpperCAmelCase__ ( self :Optional[Any] ) -> Any: UpperCAmelCase = [1, 3_06, 46_58, 2_78, 65_93, 3_10, 28_34, 3_38] UpperCAmelCase = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto' ) UpperCAmelCase = model(torch.tensor(lowercase_ ) ) # Expected mean on dim = -1 UpperCAmelCase = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) , lowercase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off UpperCAmelCase = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowercase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def UpperCAmelCase__ ( self :List[str] ) -> List[str]: UpperCAmelCase = [1, 3_06, 46_58, 2_78, 65_93, 3_10, 28_34, 3_38] UpperCAmelCase = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto' ) UpperCAmelCase = model(torch.tensor(lowercase_ ) ) # Expected mean on dim = -1 UpperCAmelCase = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) , lowercase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off UpperCAmelCase = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , lowercase_ , atol=1E-2 , rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def UpperCAmelCase__ ( self :int ) -> Optional[int]: UpperCAmelCase = [1, 3_06, 46_58, 2_78, 65_93, 3_10, 28_34, 3_38] UpperCAmelCase = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto' ) UpperCAmelCase = model(torch.tensor(lowercase_ ) ) UpperCAmelCase = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , lowercase_ , atol=1E-2 , rtol=1E-2 ) # fmt: off UpperCAmelCase = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowercase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def UpperCAmelCase__ ( self :Any ) -> int: UpperCAmelCase = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' UpperCAmelCase = 'Simply put, the theory of relativity states that ' UpperCAmelCase = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) UpperCAmelCase = tokenizer.encode(lowercase_ , return_tensors='pt' ) UpperCAmelCase = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=lowercase_ ) # greedy generation outputs UpperCAmelCase = model.generate(lowercase_ , max_new_tokens=64 , top_p=lowercase_ , temperature=1 , do_sample=lowercase_ ) UpperCAmelCase = tokenizer.decode(generated_ids[0] , skip_special_tokens=lowercase_ ) self.assertEqual(lowercase_ , lowercase_ )
78
def lowerCAmelCase_ ( __A, __A ) -> float: '''simple docstring''' def get_matched_characters(__A, __A ) -> str: UpperCAmelCase__ = [] UpperCAmelCase__ = min(len(_stra ), len(_stra ) ) // 2 for i, l in enumerate(_stra ): UpperCAmelCase__ = int(max(0, i - limit ) ) UpperCAmelCase__ = int(min(i + limit + 1, len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) UpperCAmelCase__ = f"""{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}""" return "".join(__A ) # matching characters UpperCAmelCase__ = get_matched_characters(__A, __A ) UpperCAmelCase__ = get_matched_characters(__A, __A ) UpperCAmelCase__ = len(__A ) # transposition UpperCAmelCase__ = ( len([(ca, ca) for ca, ca in zip(__A, __A ) if ca != ca] ) // 2 ) if not match_count: UpperCAmelCase__ = 0.0 else: UpperCAmelCase__ = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters UpperCAmelCase__ = 0 for ca, ca in zip(stra[:4], stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
65
0
import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL UpperCamelCase__ = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def lowerCAmelCase_ ( __A, __A, __A, __A, __A, __A, __A, __A=False, ) -> Optional[int]: '''simple docstring''' output_path.parent.mkdir(parents=__A, exist_ok=__A ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( __A, __A, f=output_path.as_posix(), input_names=__A, output_names=__A, dynamic_axes=__A, do_constant_folding=__A, use_external_data_format=__A, enable_onnx_checker=__A, opset_version=__A, ) else: export( __A, __A, f=output_path.as_posix(), input_names=__A, output_names=__A, dynamic_axes=__A, do_constant_folding=__A, opset_version=__A, ) @torch.no_grad() def lowerCAmelCase_ ( __A, __A, __A, __A = False ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase__ = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): UpperCAmelCase__ = "cuda" elif fpaa and not torch.cuda.is_available(): raise ValueError("`float16` model export is only supported on GPUs with CUDA" ) else: UpperCAmelCase__ = "cpu" UpperCAmelCase__ = Path(__A ) # VAE DECODER UpperCAmelCase__ = AutoencoderKL.from_pretrained(model_path + "/vae" ) UpperCAmelCase__ = vae_decoder.config.latent_channels # forward only through the decoder part UpperCAmelCase__ = vae_decoder.decode onnx_export( __A, model_args=( torch.randn(1, __A, 25, 25 ).to(device=__A, dtype=__A ), False, ), output_path=output_path / "vae_decoder" / "model.onnx", ordered_input_names=["latent_sample", "return_dict"], output_names=["sample"], dynamic_axes={ "latent_sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, }, opset=__A, ) del vae_decoder if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=1_4, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') UpperCamelCase__ = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('SD: Done: ONNX')
362
from __future__ import annotations def lowerCAmelCase_ ( __A ) -> list[int]: '''simple docstring''' if len(__A ) == 0: return array UpperCAmelCase__ , UpperCAmelCase__ = min(__A ), max(__A ) # Compute the variables UpperCAmelCase__ = _max - _min + 1 UpperCAmelCase__ , UpperCAmelCase__ = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCAmelCase__ = i - _min UpperCAmelCase__ = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCAmelCase__ = 0 for i in range(__A ): while holes_repeat[i] > 0: UpperCAmelCase__ = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ = input('Enter numbers separated by comma:\n') UpperCamelCase__ = [int(x) for x in user_input.split(',')] print(pigeon_sort(unsorted))
143
0
import re def A__ ( SCREAMING_SNAKE_CASE__) -> bool: __snake_case: Union[str, Any] = re.compile( r"""^(?:0|94|\+94|0{2}94)""" r"""7(0|1|2|4|5|6|7|8)""" r"""(-| |)""" r"""\d{7}$""") return bool(re.search(_lowerCamelCase , _lowerCamelCase)) if __name__ == "__main__": __UpperCAmelCase : List[Any] = "0094702343221" print(is_sri_lankan_phone_number(phone))
111
"""simple docstring""" import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _snake_case ( a__ , unittest.TestCase ): snake_case__ = CTRLTokenizer snake_case__ = False snake_case__ = False def lowerCamelCase__ ( self : Union[str, Any] ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __lowerCamelCase : Optional[int] = ["adapt", "re@@", "a@@", "apt", "c@@", "t", "<unk>"] __lowerCamelCase : str = dict(zip(UpperCAmelCase , range(len(UpperCAmelCase ) ) ) ) __lowerCamelCase : Any = ["#version: 0.2", "a p", "ap t</w>", "r e", "a d", "ad apt</w>", ""] __lowerCamelCase : Dict = {"unk_token": "<unk>"} __lowerCamelCase : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) __lowerCamelCase : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(UpperCAmelCase ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(UpperCAmelCase ) ) def lowerCamelCase__ ( self : Tuple , **UpperCAmelCase : List[str] ): kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase ) def lowerCamelCase__ ( self : Dict , UpperCAmelCase : Dict ): __lowerCamelCase : Any = "adapt react readapt apt" __lowerCamelCase : Dict = "adapt react readapt apt" return input_text, output_text def lowerCamelCase__ ( self : List[Any] ): __lowerCamelCase : List[str] = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) __lowerCamelCase : Dict = "adapt react readapt apt" __lowerCamelCase : Dict = "adapt re@@ a@@ c@@ t re@@ adapt apt".split() __lowerCamelCase : List[str] = tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) __lowerCamelCase : Any = tokens + [tokenizer.unk_token] __lowerCamelCase : Any = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , UpperCAmelCase )
135
0
import sys UpperCamelCase__ = ( """73167176531330624919225119674426574742355349194934""" """96983520312774506326239578318016984801869478851843""" """85861560789112949495459501737958331952853208805511""" """12540698747158523863050715693290963295227443043557""" """66896648950445244523161731856403098711121722383113""" """62229893423380308135336276614282806444486645238749""" """30358907296290491560440772390713810515859307960866""" """70172427121883998797908792274921901699720888093776""" """65727333001053367881220235421809751254540594752243""" """52584907711670556013604839586446706324415722155397""" """53697817977846174064955149290862569321978468622482""" """83972241375657056057490261407972968652414535100474""" """82166370484403199890008895243450658541227588666881""" """16427171479924442928230863465674813919123162824586""" """17866458359124566529476545682848912883142607690042""" """24219022671055626321111109370544217506941658960408""" """07198403850962455444362981230987879927244284909188""" """84580156166097919133875499200524063689912560717606""" """05886116467109405077541002256983155200055935729725""" """71636269561882670428252483600823257530420752963450""" ) def _a ( SCREAMING_SNAKE_CASE_ : str ): __lowerCAmelCase = 1 for digit in s: product *= int(SCREAMING_SNAKE_CASE_ ) return product def _a ( SCREAMING_SNAKE_CASE_ : str = N ): __lowerCAmelCase = -sys.maxsize - 1 __lowerCAmelCase = n[:13] __lowerCAmelCase = 13 while cur_index < len(SCREAMING_SNAKE_CASE_ ) - 13: if int(n[cur_index] ) >= int(substr[0] ): __lowerCAmelCase = substr[1:] + n[cur_index] cur_index += 1 else: __lowerCAmelCase = max(SCREAMING_SNAKE_CASE_ , str_eval(SCREAMING_SNAKE_CASE_ ) ) __lowerCAmelCase = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(f'''{solution() = }''')
102
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets UpperCamelCase__ = datasets.logging.get_logger(__name__) UpperCamelCase__ = """\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = \"Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric\", author = \"Moosavi, Nafise Sadat and Strube, Michael\", booktitle = \"Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)\", month = aug, year = \"2016\", address = \"Berlin, Germany\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/P16-1060\", doi = \"10.18653/v1/P16-1060\", pages = \"632--642\", } """ UpperCamelCase__ = """\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the \"([pos] [word])\" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a \"-\" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section \"*_conll File Format\"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. """ UpperCamelCase__ = """ Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting 'keep_singletons=False', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the 'NP_only' option, the scorer will only evaluate the resolution of NPs. min_span: By setting 'min_span', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: 'mentions': mentions 'muc': MUC metric [Vilain et al, 1995] 'bcub': B-cubed [Bagga and Baldwin, 1998] 'ceafe': CEAFe [Luo et al., 2005] 'lea': LEA [Moosavi and Strube, 2016] 'conll_score': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric('coval') >>> words = ['bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -', ... 'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)', ... 'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)', ... 'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -', ... 'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -', ... 'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {'mentions/recall': 1.0,[...] 'conll_score': 100.0} """ def _a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict=False , SCREAMING_SNAKE_CASE_ : Any=False , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : int=False , SCREAMING_SNAKE_CASE_ : Optional[int]="dummy_doc" ): __lowerCAmelCase = {doc: key_lines} __lowerCAmelCase = {doc: sys_lines} __lowerCAmelCase = {} __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase , __lowerCAmelCase = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) key_singletons_num += singletons_num if NP_only or min_span: __lowerCAmelCase = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase , __lowerCAmelCase = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , sys_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) sys_singletons_num += singletons_num if NP_only or min_span: __lowerCAmelCase = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if remove_nested: __lowerCAmelCase , __lowerCAmelCase = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters __lowerCAmelCase , __lowerCAmelCase = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters __lowerCAmelCase = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( "Number of removed nested coreferring mentions in the key " F"""annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}""" ) logger.info( "Number of resulting singleton clusters in the key " F"""annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}""" ) if not keep_singletons: logger.info( F"""{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system """ "files, respectively" ) return doc_coref_infos def _a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): __lowerCAmelCase = get_coref_infos(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = {} __lowerCAmelCase = 0 __lowerCAmelCase = 0 for name, metric in metrics: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = evaluator.evaluate_documents(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F"""{name}/recall""": recall, F"""{name}/precision""": precision, F"""{name}/f1""": fa} ) logger.info( name.ljust(10 ) , F"""Recall: {recall * 1_00:.2f}""" , F""" Precision: {precision * 1_00:.2f}""" , F""" F1: {fa * 1_00:.2f}""" , ) if conll_subparts_num == 3: __lowerCAmelCase = (conll / 3) * 1_00 logger.info(F"""CoNLL score: {conll:.2f}""" ) output_scores.update({"conll_score": conll} ) return output_scores def _a ( SCREAMING_SNAKE_CASE_ : List[str] ): __lowerCAmelCase = False for line in key_lines: if not line.startswith("#" ): if len(line.split() ) > 6: __lowerCAmelCase = line.split()[5] if not parse_col == "-": __lowerCAmelCase = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Sequence(datasets.Value("string" ) ), } ) , codebase_urls=["https://github.com/ns-moosavi/coval"] , reference_urls=[ "https://github.com/ns-moosavi/coval", "https://www.aclweb.org/anthology/P16-1060", "http://www.conll.cemantix.org/2012/data.html", ] , ) def __SCREAMING_SNAKE_CASE( self , _A , _A , _A=True , _A=False , _A=False , _A=False ): """simple docstring""" __lowerCAmelCase = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: __lowerCAmelCase = util.check_gold_parse_annotation(_A ) if not has_gold_parse: raise NotImplementedError("References should have gold parse annotation to use 'min_span'." ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" __lowerCAmelCase = evaluate( key_lines=_A , sys_lines=_A , metrics=_A , NP_only=_A , remove_nested=_A , keep_singletons=_A , min_span=_A , ) return score
102
1
import json import os from functools import lru_cache from typing import TYPE_CHECKING, List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } UpperCAmelCase__ = { "vocab_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"}, "merges_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"}, "tokenizer_config_file": { "facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json" }, } UpperCAmelCase__ = {"facebook/blenderbot-3B": 128} @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def _a ( ) -> Dict: a = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) a = bs[:] a = 0 for b in range(2**8 ): if b not in bs: bs.append(UpperCamelCase__ ) cs.append(2**8 + n ) n += 1 a = [chr(UpperCamelCase__ ) for n in cs] return dict(zip(UpperCamelCase__ , UpperCamelCase__ ) ) def _a ( a :List[Any] ) -> int: a = set() a = word[0] for char in word[1:]: pairs.add((prev_char, char) ) a = char return pairs class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = ['''input_ids''', '''attention_mask'''] def __init__( self : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : Any , __UpperCAmelCase : Dict="replace" , __UpperCAmelCase : Optional[int]="<s>" , __UpperCAmelCase : Any="</s>" , __UpperCAmelCase : Tuple="</s>" , __UpperCAmelCase : Optional[Any]="<s>" , __UpperCAmelCase : str="<unk>" , __UpperCAmelCase : Dict="<pad>" , __UpperCAmelCase : List[Any]="<mask>" , __UpperCAmelCase : List[str]=False , **__UpperCAmelCase : Tuple , ) ->Optional[Any]: """simple docstring""" a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else bos_token a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else eos_token a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else sep_token a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else cls_token a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else unk_token a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it a = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else mask_token super().__init__( errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , **lowerCamelCase__ , ) with open(lowerCamelCase__ , encoding='''utf-8''' ) as vocab_handle: a = json.load(lowerCamelCase__ ) a = {v: k for k, v in self.encoder.items()} a = errors # how to handle errors in decoding a = bytes_to_unicode() a = {v: k for k, v in self.byte_encoder.items()} with open(lowerCamelCase__ , encoding='''utf-8''' ) as merges_handle: a = merges_handle.read().split('''\n''' )[1:-1] a = [tuple(merge.split() ) for merge in bpe_merges] a = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a = {} a = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions a = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" return len(self.encoder ) def __lowerCAmelCase ( self : int ) ->str: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Optional[Any] ) ->Dict: """simple docstring""" if token in self.cache: return self.cache[token] a = tuple(lowerCamelCase__ ) a = get_pairs(lowerCamelCase__ ) if not pairs: return token while True: a = min(lowerCamelCase__ , key=lambda __UpperCAmelCase : self.bpe_ranks.get(lowerCamelCase__ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break a , a = bigram a = [] a = 0 while i < len(lowerCamelCase__ ): try: a = word.index(lowerCamelCase__ , lowerCamelCase__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) a = j if word[i] == first and i < len(lowerCamelCase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 a = tuple(lowerCamelCase__ ) a = new_word if len(lowerCamelCase__ ) == 1: break else: a = get_pairs(lowerCamelCase__ ) a = ''' '''.join(lowerCamelCase__ ) a = word return word def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Dict ) ->Dict: """simple docstring""" a = [] for token in re.findall(self.pat , lowerCamelCase__ ): a = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCamelCase__ ).split(''' ''' ) ) return bpe_tokens def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.encoder.get(lowerCamelCase__ , self.encoder.get(self.unk_token ) ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : int ) ->Union[str, Any]: """simple docstring""" return self.decoder.get(lowerCamelCase__ ) def __lowerCAmelCase ( self : str , __UpperCAmelCase : Union[str, Any] ) ->List[Any]: """simple docstring""" a = ''''''.join(lowerCamelCase__ ) a = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[Any] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(lowerCamelCase__ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a = os.path.join( lowerCamelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) a = os.path.join( lowerCamelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(lowerCamelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCamelCase__ , ensure_ascii=lowerCamelCase__ ) + '''\n''' ) a = 0 with open(lowerCamelCase__ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __UpperCAmelCase : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) a = token_index writer.write(''' '''.join(lowerCamelCase__ ) + '''\n''' ) index += 1 return vocab_file, merge_file def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : int = None , __UpperCAmelCase : Union[str, Any] = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase__ , token_ids_a=lowerCamelCase__ , already_has_special_tokens=lowerCamelCase__ ) if token_ids_a is None: return [1] + ([0] * len(lowerCamelCase__ )) + [1] return [1] + ([0] * len(lowerCamelCase__ )) + [1, 1] + ([0] * len(lowerCamelCase__ )) + [1] def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Tuple , __UpperCAmelCase : int = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple=False , **__UpperCAmelCase : Optional[Any] ) ->Dict: """simple docstring""" a = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(lowerCamelCase__ ) > 0 and not text[0].isspace()): a = ''' ''' + text return (text, kwargs) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Dict = None ) ->Any: """simple docstring""" return token_ids_a + [self.eos_token_id] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) ->List[int]: """simple docstring""" a = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(lowerCamelCase__ ) a = ''' '''.join(lowerCamelCase__ ) a = self.encode(lowerCamelCase__ ) if len(lowerCamelCase__ ) > self.model_max_length: a = input_ids[-self.model_max_length :] logger.warning(F"""Trimmed input from conversation as it was longer than {self.model_max_length} tokens.""" ) return input_ids
0
import unittest from transformers import DebertaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, ) from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase ( __magic_name__ ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=13 , lowerCamelCase__=7 , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=99 , lowerCamelCase__=32 , lowerCamelCase__=5 , lowerCamelCase__=4 , lowerCamelCase__=37 , lowerCamelCase__="gelu" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=512 , lowerCamelCase__=16 , lowerCamelCase__=2 , lowerCamelCase__=0.02 , lowerCamelCase__=False , lowerCamelCase__=True , lowerCamelCase__="None" , lowerCamelCase__=3 , lowerCamelCase__=4 , lowerCamelCase__=None , ) -> int: '''simple docstring''' __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_input_mask __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = relative_attention __lowerCamelCase = position_biased_input __lowerCamelCase = pos_att_type __lowerCamelCase = scope def lowercase_ ( self ) -> str: '''simple docstring''' __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_input_mask: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' return DebertaConfig( 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 , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = self.get_config() __lowerCamelCase = 300 return config def lowercase_ ( self , lowerCamelCase__ ) -> Optional[int]: '''simple docstring''' self.parent.assertListEqual(list(result.loss.size() ) , [] ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Any: '''simple docstring''' __lowerCamelCase = DebertaModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ )[0] __lowerCamelCase = model(lowerCamelCase__ , token_type_ids=lowerCamelCase__ )[0] __lowerCamelCase = model(lowerCamelCase__ )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Tuple: '''simple docstring''' __lowerCamelCase = DebertaForMaskedLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = self.num_labels __lowerCamelCase = DebertaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(lowerCamelCase__ ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = self.num_labels __lowerCamelCase = DebertaForTokenClassification(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Dict: '''simple docstring''' __lowerCamelCase = DebertaForQuestionAnswering(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , ) 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 lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( __magic_name__ , __magic_name__ , unittest.TestCase ): """simple docstring""" snake_case_ = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) snake_case_ = ( { '''feature-extraction''': DebertaModel, '''fill-mask''': DebertaForMaskedLM, '''question-answering''': DebertaForQuestionAnswering, '''text-classification''': DebertaForSequenceClassification, '''token-classification''': DebertaForTokenClassification, '''zero-shot''': DebertaForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = True snake_case_ = False snake_case_ = False snake_case_ = False snake_case_ = False def lowercase_ ( self ) -> List[Any]: '''simple docstring''' __lowerCamelCase = DebertaModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*lowerCamelCase__ ) def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCamelCase__ ) def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCamelCase__ ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*lowerCamelCase__ ) def lowercase_ ( self ) -> str: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*lowerCamelCase__ ) @slow def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = DebertaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_torch @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @unittest.skip(reason='Model not available yet' ) def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' pass @slow def lowercase_ ( self ) -> Tuple: '''simple docstring''' __lowerCamelCase = DebertaModel.from_pretrained('microsoft/deberta-base' ) __lowerCamelCase = torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) __lowerCamelCase = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __lowerCamelCase = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ )[0] # compare the actual values for a slice. __lowerCamelCase = torch.tensor( [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , lowerCamelCase__ , atol=1e-4 ) , f"""{output[:, 1:4, 1:4]}""" )
90
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) _lowerCamelCase : Optional[Any] = {'configuration_vit': ['VIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTConfig', 'ViTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = ['ViTFeatureExtractor'] _lowerCamelCase : Dict = ['ViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Any = [ 'VIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTForImageClassification', 'ViTForMaskedImageModeling', 'ViTModel', 'ViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = [ 'TFViTForImageClassification', 'TFViTModel', 'TFViTPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Any = [ 'FlaxViTForImageClassification', 'FlaxViTModel', 'FlaxViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys _lowerCamelCase : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
361
'''simple docstring''' from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES _lowerCamelCase : List[str] = logging.get_logger(__name__) _lowerCamelCase : Any = OrderedDict( [ # Base model mapping ('albert', 'FlaxAlbertModel'), ('bart', 'FlaxBartModel'), ('beit', 'FlaxBeitModel'), ('bert', 'FlaxBertModel'), ('big_bird', 'FlaxBigBirdModel'), ('blenderbot', 'FlaxBlenderbotModel'), ('blenderbot-small', 'FlaxBlenderbotSmallModel'), ('clip', 'FlaxCLIPModel'), ('distilbert', 'FlaxDistilBertModel'), ('electra', 'FlaxElectraModel'), ('gpt-sw3', 'FlaxGPT2Model'), ('gpt2', 'FlaxGPT2Model'), ('gpt_neo', 'FlaxGPTNeoModel'), ('gptj', 'FlaxGPTJModel'), ('longt5', 'FlaxLongT5Model'), ('marian', 'FlaxMarianModel'), ('mbart', 'FlaxMBartModel'), ('mt5', 'FlaxMT5Model'), ('opt', 'FlaxOPTModel'), ('pegasus', 'FlaxPegasusModel'), ('regnet', 'FlaxRegNetModel'), ('resnet', 'FlaxResNetModel'), ('roberta', 'FlaxRobertaModel'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormModel'), ('roformer', 'FlaxRoFormerModel'), ('t5', 'FlaxT5Model'), ('vision-text-dual-encoder', 'FlaxVisionTextDualEncoderModel'), ('vit', 'FlaxViTModel'), ('wav2vec2', 'FlaxWav2Vec2Model'), ('whisper', 'FlaxWhisperModel'), ('xglm', 'FlaxXGLMModel'), ('xlm-roberta', 'FlaxXLMRobertaModel'), ] ) _lowerCamelCase : Optional[Any] = OrderedDict( [ # Model for pre-training mapping ('albert', 'FlaxAlbertForPreTraining'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForPreTraining'), ('big_bird', 'FlaxBigBirdForPreTraining'), ('electra', 'FlaxElectraForPreTraining'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('t5', 'FlaxT5ForConditionalGeneration'), ('wav2vec2', 'FlaxWav2Vec2ForPreTraining'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) _lowerCamelCase : int = OrderedDict( [ # Model for Masked LM mapping ('albert', 'FlaxAlbertForMaskedLM'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForMaskedLM'), ('big_bird', 'FlaxBigBirdForMaskedLM'), ('distilbert', 'FlaxDistilBertForMaskedLM'), ('electra', 'FlaxElectraForMaskedLM'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) _lowerCamelCase : List[str] = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ('bart', 'FlaxBartForConditionalGeneration'), ('blenderbot', 'FlaxBlenderbotForConditionalGeneration'), ('blenderbot-small', 'FlaxBlenderbotSmallForConditionalGeneration'), ('encoder-decoder', 'FlaxEncoderDecoderModel'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('marian', 'FlaxMarianMTModel'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('pegasus', 'FlaxPegasusForConditionalGeneration'), ('t5', 'FlaxT5ForConditionalGeneration'), ] ) _lowerCamelCase : int = OrderedDict( [ # Model for Image-classsification ('beit', 'FlaxBeitForImageClassification'), ('regnet', 'FlaxRegNetForImageClassification'), ('resnet', 'FlaxResNetForImageClassification'), ('vit', 'FlaxViTForImageClassification'), ] ) _lowerCamelCase : int = OrderedDict( [ ('vision-encoder-decoder', 'FlaxVisionEncoderDecoderModel'), ] ) _lowerCamelCase : Optional[int] = OrderedDict( [ # Model for Causal LM mapping ('bart', 'FlaxBartForCausalLM'), ('bert', 'FlaxBertForCausalLM'), ('big_bird', 'FlaxBigBirdForCausalLM'), ('electra', 'FlaxElectraForCausalLM'), ('gpt-sw3', 'FlaxGPT2LMHeadModel'), ('gpt2', 'FlaxGPT2LMHeadModel'), ('gpt_neo', 'FlaxGPTNeoForCausalLM'), ('gptj', 'FlaxGPTJForCausalLM'), ('opt', 'FlaxOPTForCausalLM'), ('roberta', 'FlaxRobertaForCausalLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForCausalLM'), ('xglm', 'FlaxXGLMForCausalLM'), ('xlm-roberta', 'FlaxXLMRobertaForCausalLM'), ] ) _lowerCamelCase : Optional[Any] = OrderedDict( [ # Model for Sequence Classification mapping ('albert', 'FlaxAlbertForSequenceClassification'), ('bart', 'FlaxBartForSequenceClassification'), ('bert', 'FlaxBertForSequenceClassification'), ('big_bird', 'FlaxBigBirdForSequenceClassification'), ('distilbert', 'FlaxDistilBertForSequenceClassification'), ('electra', 'FlaxElectraForSequenceClassification'), ('mbart', 'FlaxMBartForSequenceClassification'), ('roberta', 'FlaxRobertaForSequenceClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForSequenceClassification'), ('roformer', 'FlaxRoFormerForSequenceClassification'), ('xlm-roberta', 'FlaxXLMRobertaForSequenceClassification'), ] ) _lowerCamelCase : Any = OrderedDict( [ # Model for Question Answering mapping ('albert', 'FlaxAlbertForQuestionAnswering'), ('bart', 'FlaxBartForQuestionAnswering'), ('bert', 'FlaxBertForQuestionAnswering'), ('big_bird', 'FlaxBigBirdForQuestionAnswering'), ('distilbert', 'FlaxDistilBertForQuestionAnswering'), ('electra', 'FlaxElectraForQuestionAnswering'), ('mbart', 'FlaxMBartForQuestionAnswering'), ('roberta', 'FlaxRobertaForQuestionAnswering'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForQuestionAnswering'), ('roformer', 'FlaxRoFormerForQuestionAnswering'), ('xlm-roberta', 'FlaxXLMRobertaForQuestionAnswering'), ] ) _lowerCamelCase : Union[str, Any] = OrderedDict( [ # Model for Token Classification mapping ('albert', 'FlaxAlbertForTokenClassification'), ('bert', 'FlaxBertForTokenClassification'), ('big_bird', 'FlaxBigBirdForTokenClassification'), ('distilbert', 'FlaxDistilBertForTokenClassification'), ('electra', 'FlaxElectraForTokenClassification'), ('roberta', 'FlaxRobertaForTokenClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForTokenClassification'), ('roformer', 'FlaxRoFormerForTokenClassification'), ('xlm-roberta', 'FlaxXLMRobertaForTokenClassification'), ] ) _lowerCamelCase : Dict = OrderedDict( [ # Model for Multiple Choice mapping ('albert', 'FlaxAlbertForMultipleChoice'), ('bert', 'FlaxBertForMultipleChoice'), ('big_bird', 'FlaxBigBirdForMultipleChoice'), ('distilbert', 'FlaxDistilBertForMultipleChoice'), ('electra', 'FlaxElectraForMultipleChoice'), ('roberta', 'FlaxRobertaForMultipleChoice'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMultipleChoice'), ('roformer', 'FlaxRoFormerForMultipleChoice'), ('xlm-roberta', 'FlaxXLMRobertaForMultipleChoice'), ] ) _lowerCamelCase : int = OrderedDict( [ ('bert', 'FlaxBertForNextSentencePrediction'), ] ) _lowerCamelCase : Union[str, Any] = OrderedDict( [ ('speech-encoder-decoder', 'FlaxSpeechEncoderDecoderModel'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ] ) _lowerCamelCase : Any = OrderedDict( [ ('whisper', 'FlaxWhisperForAudioClassification'), ] ) _lowerCamelCase : List[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) _lowerCamelCase : Dict = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) _lowerCamelCase : Tuple = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) _lowerCamelCase : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) _lowerCamelCase : Union[str, Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) _lowerCamelCase : Optional[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) _lowerCamelCase : Optional[int] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) _lowerCamelCase : int = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) _lowerCamelCase : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) _lowerCamelCase : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) _lowerCamelCase : List[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) _lowerCamelCase : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) _lowerCamelCase : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) _lowerCamelCase : Optional[int] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_MAPPING _lowerCamelCase : Optional[Any] = auto_class_update(FlaxAutoModel) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_PRETRAINING_MAPPING _lowerCamelCase : List[str] = auto_class_update(FlaxAutoModelForPreTraining, head_doc='pretraining') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING _lowerCamelCase : List[Any] = auto_class_update(FlaxAutoModelForCausalLM, head_doc='causal language modeling') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_MASKED_LM_MAPPING _lowerCamelCase : List[str] = auto_class_update(FlaxAutoModelForMaskedLM, head_doc='masked language modeling') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _lowerCamelCase : Tuple = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc='sequence-to-sequence language modeling', checkpoint_for_example='t5-base' ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING _lowerCamelCase : Tuple = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc='sequence classification' ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING _lowerCamelCase : Any = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc='question answering') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING _lowerCamelCase : str = auto_class_update( FlaxAutoModelForTokenClassification, head_doc='token classification' ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING _lowerCamelCase : Tuple = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc='multiple choice') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING _lowerCamelCase : List[Any] = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc='next sentence prediction' ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING _lowerCamelCase : Union[str, Any] = auto_class_update( FlaxAutoModelForImageClassification, head_doc='image classification' ) class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING _lowerCamelCase : Optional[int] = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc='vision-to-text modeling') class __UpperCAmelCase ( _BaseAutoModelClass ): '''simple docstring''' __lowerCAmelCase = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING _lowerCamelCase : Optional[int] = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc='sequence-to-sequence speech-to-text modeling' )
337
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer UpperCAmelCase = logging.get_logger(__name__) UpperCAmelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase = { '''vocab_file''': { '''squeezebert/squeezebert-uncased''': ( '''https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt''' ), '''squeezebert/squeezebert-mnli''': '''https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt''', '''squeezebert/squeezebert-mnli-headless''': ( '''https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''squeezebert/squeezebert-uncased''': ( '''https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json''' ), '''squeezebert/squeezebert-mnli''': ( '''https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json''' ), '''squeezebert/squeezebert-mnli-headless''': ( '''https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json''' ), }, } UpperCAmelCase = { '''squeezebert/squeezebert-uncased''': 512, '''squeezebert/squeezebert-mnli''': 512, '''squeezebert/squeezebert-mnli-headless''': 512, } UpperCAmelCase = { '''squeezebert/squeezebert-uncased''': {'''do_lower_case''': True}, '''squeezebert/squeezebert-mnli''': {'''do_lower_case''': True}, '''squeezebert/squeezebert-mnli-headless''': {'''do_lower_case''': True}, } class lowerCAmelCase ( A ): lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = PRETRAINED_INIT_CONFIGURATION lowerCAmelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase_ = SqueezeBertTokenizer def __init__( self : List[Any] , __lowercase : str=None , __lowercase : Union[str, Any]=None , __lowercase : Optional[int]=True , __lowercase : Dict="[UNK]" , __lowercase : str="[SEP]" , __lowercase : Optional[int]="[PAD]" , __lowercase : int="[CLS]" , __lowercase : Any="[MASK]" , __lowercase : int=True , __lowercase : str=None , **__lowercase : List[str] , ): """simple docstring""" super().__init__( __lowercase , tokenizer_file=__lowercase , do_lower_case=__lowercase , unk_token=__lowercase , sep_token=__lowercase , pad_token=__lowercase , cls_token=__lowercase , mask_token=__lowercase , tokenize_chinese_chars=__lowercase , strip_accents=__lowercase , **__lowercase , ) __lowercase =json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , __lowercase ) != do_lower_case or normalizer_state.get('strip_accents' , __lowercase ) != strip_accents or normalizer_state.get('handle_chinese_chars' , __lowercase ) != tokenize_chinese_chars ): __lowercase =getattr(__lowercase , normalizer_state.pop('type' ) ) __lowercase =do_lower_case __lowercase =strip_accents __lowercase =tokenize_chinese_chars __lowercase =normalizer_class(**__lowercase ) __lowercase =do_lower_case def snake_case ( self : List[str] , __lowercase : str , __lowercase : Optional[Any]=None ): """simple docstring""" __lowercase =[self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def snake_case ( self : int , __lowercase : List[int] , __lowercase : Optional[List[int]] = None ): """simple docstring""" __lowercase =[self.sep_token_id] __lowercase =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def snake_case ( self : List[str] , __lowercase : str , __lowercase : Optional[str] = None ): """simple docstring""" __lowercase =self._tokenizer.model.save(__lowercase , name=__lowercase ) return tuple(__lowercase )
141
'''simple docstring''' import math class lowerCAmelCase : def snake_case ( self : Optional[int] , __lowercase : list[list[float]] , __lowercase : list[int] ): """simple docstring""" __lowercase =0.0 __lowercase =0.0 for i in range(len(__lowercase ) ): da += math.pow((sample[i] - weights[0][i]) , 2 ) da += math.pow((sample[i] - weights[1][i]) , 2 ) return 0 if da > da else 1 return 0 def snake_case ( self : Union[str, Any] , __lowercase : list[list[int | float]] , __lowercase : list[int] , __lowercase : int , __lowercase : float ): """simple docstring""" for i in range(len(__lowercase ) ): weights[j][i] += alpha * (sample[i] - weights[j][i]) return weights def __UpperCamelCase ( ): '''simple docstring''' __lowercase =[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] # weight initialization ( n, C ) __lowercase =[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] # training __lowercase =SelfOrganizingMap() __lowercase =3 __lowercase =0.5 for _ in range(lowercase__ ): for j in range(len(lowercase__ ) ): # training sample __lowercase =training_samples[j] # Compute the winning vector __lowercase =self_organizing_map.get_winner(lowercase__, lowercase__ ) # Update the winning vector __lowercase =self_organizing_map.update(lowercase__, lowercase__, lowercase__, lowercase__ ) # classify test sample __lowercase =[0, 0, 0, 1] __lowercase =self_organizing_map.get_winner(lowercase__, lowercase__ ) # results print(F'''Clusters that the test sample belongs to : {winner}''' ) print(F'''Weights that have been trained : {weights}''' ) # running the main() function if __name__ == "__main__": main()
141
1
'''simple docstring''' from __future__ import annotations from typing import Any def _lowerCAmelCase ( lowercase ) -> Union[str, Any]: create_state_space_tree(__a , [] , 0 ) def _lowerCAmelCase ( lowercase , lowercase , lowercase ) -> List[str]: if index == len(__a ): print(__a ) return create_state_space_tree(__a , __a , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(__a , __a , index + 1 ) current_subsequence.pop() if __name__ == "__main__": _a : Any = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(["""A""", """B""", """C"""]) generate_all_subsequences(seq)
371
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _UpperCAmelCase ( lowerCAmelCase_ , unittest.TestCase ): a : Optional[int] =KandinskyVaaImgaImgPipeline a : List[Any] =["""image_embeds""", """negative_image_embeds""", """image"""] a : Union[str, Any] =[ """image_embeds""", """negative_image_embeds""", """image""", ] a : Optional[int] =[ """generator""", """height""", """width""", """strength""", """guidance_scale""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] a : Optional[Any] =False @property def lowerCamelCase__ ( self ): '''simple docstring''' return 32 @property def lowerCamelCase__ ( self ): '''simple docstring''' return 32 @property def lowerCamelCase__ ( self ): '''simple docstring''' return self.time_input_dim @property def lowerCamelCase__ ( self ): '''simple docstring''' return self.time_input_dim * 4 @property def lowerCamelCase__ ( self ): '''simple docstring''' return 1_00 @property def lowerCamelCase__ ( self ): '''simple docstring''' torch.manual_seed(0 ) __lowerCAmelCase = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } __lowerCAmelCase = UNetaDConditionModel(**__SCREAMING_SNAKE_CASE ) return model @property def lowerCamelCase__ ( self ): '''simple docstring''' return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def lowerCamelCase__ ( self ): '''simple docstring''' torch.manual_seed(0 ) __lowerCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def lowerCamelCase__ ( self ): '''simple docstring''' __lowerCAmelCase = self.dummy_unet __lowerCAmelCase = self.dummy_movq __lowerCAmelCase = { """num_train_timesteps""": 10_00, """beta_schedule""": """linear""", """beta_start""": 0.0_0085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } __lowerCAmelCase = DDIMScheduler(**__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE=0 ): '''simple docstring''' __lowerCAmelCase = floats_tensor((1, self.text_embedder_hidden_size),rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = floats_tensor((1, self.text_embedder_hidden_size),rng=random.Random(seed + 1 ) ).to( __SCREAMING_SNAKE_CASE ) # create init_image __lowerCAmelCase = floats_tensor((1, 3, 64, 64),rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = image.cpu().permute(0,2,3,1 )[0] __lowerCAmelCase = Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE ) ).convert("""RGB""" ).resize((2_56, 2_56) ) if str(__SCREAMING_SNAKE_CASE ).startswith("""mps""" ): __lowerCAmelCase = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def lowerCamelCase__ ( self ): '''simple docstring''' __lowerCAmelCase = """cpu""" __lowerCAmelCase = self.get_dummy_components() __lowerCAmelCase = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = pipe(**self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase = output.images __lowerCAmelCase = pipe( **self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ),return_dict=__SCREAMING_SNAKE_CASE,)[0] __lowerCAmelCase = image[0, -3:, -3:, -1] __lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCAmelCase = np.array( [0.619_9778, 0.6398_4406, 0.4614_5785, 0.6294_4984, 0.562_2215, 0.4730_6132, 0.4744_1456, 0.460_7606, 0.4871_9263] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): def lowerCamelCase__ ( self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase__ ( self ): '''simple docstring''' __lowerCAmelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_img2img_frog.npy""" ) __lowerCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) __lowerCAmelCase = """A red cartoon frog, 4k""" __lowerCAmelCase = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""",torch_dtype=torch.floataa ) pipe_prior.to(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = KandinskyVaaImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""",torch_dtype=torch.floataa ) __lowerCAmelCase = pipeline.to(__SCREAMING_SNAKE_CASE ) pipeline.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = torch.Generator(device="""cpu""" ).manual_seed(0 ) __lowerCAmelCase , __lowerCAmelCase = pipe_prior( __SCREAMING_SNAKE_CASE,generator=__SCREAMING_SNAKE_CASE,num_inference_steps=5,negative_prompt="""""",).to_tuple() __lowerCAmelCase = pipeline( image=__SCREAMING_SNAKE_CASE,image_embeds=__SCREAMING_SNAKE_CASE,negative_image_embeds=__SCREAMING_SNAKE_CASE,generator=__SCREAMING_SNAKE_CASE,num_inference_steps=1_00,height=7_68,width=7_68,strength=0.2,output_type="""np""",) __lowerCAmelCase = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE )
46
0
import inspect import unittest from transformers import ViTMSNConfig 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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _lowerCamelCase : """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.0_2 , _SCREAMING_SNAKE_CASE=None , )->List[str]: '''simple docstring''' A_ : str = parent A_ : Optional[int] = batch_size A_ : List[str] = image_size A_ : str = patch_size A_ : Union[str, Any] = num_channels A_ : Dict = is_training A_ : str = use_labels A_ : Dict = hidden_size A_ : List[Any] = num_hidden_layers A_ : int = num_attention_heads A_ : Tuple = intermediate_size A_ : Union[str, Any] = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Optional[Any] = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : Optional[Any] = initializer_range A_ : str = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Union[str, Any] = (image_size // patch_size) ** 2 A_ : List[Any] = num_patches + 1 def _snake_case ( self )->List[str]: '''simple docstring''' A_ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Optional[Any] = None if self.use_labels: A_ : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = self.get_config() return config, pixel_values, labels def _snake_case ( self )->List[str]: '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )->Union[str, Any]: '''simple docstring''' A_ : int = ViTMSNModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ : Union[str, Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )->Dict: '''simple docstring''' A_ : List[Any] = self.type_sequence_label_size A_ : Any = ViTMSNForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ : Union[str, Any] = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) print('''Pixel and labels shape: {pixel_values.shape}, {labels.shape}''' ) print('''Labels: {labels}''' ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : List[str] = 1 A_ : int = ViTMSNForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ : Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : int = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _snake_case ( self )->Any: '''simple docstring''' A_ : Optional[int] = self.prepare_config_and_inputs() A_ , A_ , A_ : Any = config_and_inputs A_ : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class _lowerCamelCase ( UpperCamelCase , UpperCamelCase , unittest.TestCase ): """simple docstring""" snake_case = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () snake_case = ( {"feature-extraction": ViTMSNModel, "image-classification": ViTMSNForImageClassification} if is_torch_available() else {} ) snake_case = False snake_case = False snake_case = False snake_case = False def _snake_case ( self )->int: '''simple docstring''' A_ : Optional[int] = ViTMSNModelTester(self ) A_ : Any = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def _snake_case ( self )->Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMSN does not use inputs_embeds''' ) def _snake_case ( self )->List[Any]: '''simple docstring''' pass def _snake_case ( self )->Tuple: '''simple docstring''' A_ , A_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Tuple = model_class(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE , nn.Linear ) ) def _snake_case ( self )->Union[str, Any]: '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(_SCREAMING_SNAKE_CASE ) A_ : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : Dict = [*signature.parameters.keys()] A_ : List[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )->int: '''simple docstring''' A_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )->str: '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) @slow def _snake_case ( self )->Dict: '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _SCREAMING_SNAKE_CASE ( ): A_ : Union[str, Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _snake_case ( self )->Tuple: '''simple docstring''' return ViTImageProcessor.from_pretrained('''facebook/vit-msn-small''' ) if is_vision_available() else None @slow def _snake_case ( self )->Union[str, Any]: '''simple docstring''' torch.manual_seed(2 ) A_ : List[str] = ViTMSNForImageClassification.from_pretrained('''facebook/vit-msn-small''' ).to(_SCREAMING_SNAKE_CASE ) A_ : Tuple = self.default_image_processor A_ : List[Any] = prepare_img() A_ : Optional[int] = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): A_ : Tuple = model(**_SCREAMING_SNAKE_CASE ) # verify the logits A_ : int = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ : Optional[int] = torch.tensor([-0.0_8_0_3, -0.4_4_5_4, -0.2_3_7_5] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ) )
186
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase = { """configuration_roberta""": ["""ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RobertaConfig""", """RobertaOnnxConfig"""], """tokenization_roberta""": ["""RobertaTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ["""RobertaTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""", """RobertaForCausalLM""", """RobertaForMaskedLM""", """RobertaForMultipleChoice""", """RobertaForQuestionAnswering""", """RobertaForSequenceClassification""", """RobertaForTokenClassification""", """RobertaModel""", """RobertaPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFRobertaForCausalLM""", """TFRobertaForMaskedLM""", """TFRobertaForMultipleChoice""", """TFRobertaForQuestionAnswering""", """TFRobertaForSequenceClassification""", """TFRobertaForTokenClassification""", """TFRobertaMainLayer""", """TFRobertaModel""", """TFRobertaPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """FlaxRobertaForCausalLM""", """FlaxRobertaForMaskedLM""", """FlaxRobertaForMultipleChoice""", """FlaxRobertaForQuestionAnswering""", """FlaxRobertaForSequenceClassification""", """FlaxRobertaForTokenClassification""", """FlaxRobertaModel""", """FlaxRobertaPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
186
1
import json import sys def __lowerCamelCase ( lowerCamelCase__ : List[Any] , lowerCamelCase__ : Union[str, Any] ): '''simple docstring''' with open(snake_case_ , encoding="""utf-8""" ) as f: lowerCamelCase = json.load(snake_case_ ) lowerCamelCase = ["""<details>""", """<summary>Show updated benchmarks!</summary>""", """ """] for benchmark_name in sorted(snake_case_ ): lowerCamelCase = results[benchmark_name] lowerCamelCase = benchmark_name.split("""/""" )[-1] output_md.append(f'### Benchmark: {benchmark_file_name}' ) lowerCamelCase = """| metric |""" lowerCamelCase = """|--------|""" lowerCamelCase = """| new / old (diff) |""" for metric_name in sorted(snake_case_ ): lowerCamelCase = benchmark_res[metric_name] lowerCamelCase = metric_vals["""new"""] lowerCamelCase = metric_vals.get("""old""" , snake_case_ ) lowerCamelCase = metric_vals.get("""diff""" , snake_case_ ) lowerCamelCase = f' {new_val:f}' if isinstance(snake_case_ , (int, float) ) else """None""" if old_val is not None: val_str += f' / {old_val:f}' if isinstance(snake_case_ , (int, float) ) else "None" if dif_val is not None: val_str += f' ({dif_val:f})' if isinstance(snake_case_ , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("""</details>""" ) with open(snake_case_ , """w""" , encoding="""utf-8""" ) as f: f.writelines("""\n""".join(snake_case_ ) ) if __name__ == "__main__": UpperCAmelCase : Optional[Any] = sys.argv[1] UpperCAmelCase : List[str] = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
355
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("""socket.socket""" ) @patch("""builtins.open""" ) def __lowerCamelCase ( lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : Dict ): '''simple docstring''' lowerCamelCase = Mock() lowerCamelCase = conn, Mock() lowerCamelCase = iter([1, None] ) lowerCamelCase = lambda lowerCamelCase__ : next(lowerCamelCase__ ) # ===== invoke ===== send_file(filename="""mytext.txt""" , testing=lowerCamelCase__ ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
66
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class lowercase ( _UpperCAmelCase , unittest.TestCase ): _SCREAMING_SNAKE_CASE = ShapEPipeline _SCREAMING_SNAKE_CASE = ['prompt'] _SCREAMING_SNAKE_CASE = ['prompt'] _SCREAMING_SNAKE_CASE = [ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] _SCREAMING_SNAKE_CASE = False @property def _snake_case ( self ) -> Tuple: return 32 @property def _snake_case ( self ) -> Optional[Any]: return 32 @property def _snake_case ( self ) -> Tuple: return self.time_input_dim * 4 @property def _snake_case ( self ) -> str: return 8 @property def _snake_case ( self ) -> Optional[int]: lowerCAmelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def _snake_case ( self ) -> int: torch.manual_seed(0 ) lowerCAmelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(lowercase ) @property def _snake_case ( self ) -> int: torch.manual_seed(0 ) lowerCAmelCase = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } lowerCAmelCase = PriorTransformer(**lowercase ) return model @property def _snake_case ( self ) -> Union[str, Any]: torch.manual_seed(0 ) lowerCAmelCase = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } lowerCAmelCase = ShapERenderer(**lowercase ) return model def _snake_case ( self ) -> Tuple: lowerCAmelCase = self.dummy_prior lowerCAmelCase = self.dummy_text_encoder lowerCAmelCase = self.dummy_tokenizer lowerCAmelCase = self.dummy_renderer lowerCAmelCase = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1_024 , prediction_type="""sample""" , use_karras_sigmas=lowercase , clip_sample=lowercase , clip_sample_range=1.0 , ) lowerCAmelCase = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def _snake_case ( self , lowercase , lowercase=0 ) -> Optional[Any]: if str(lowercase ).startswith("""mps""" ): lowerCAmelCase = torch.manual_seed(lowercase ) else: lowerCAmelCase = torch.Generator(device=lowercase ).manual_seed(lowercase ) lowerCAmelCase = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def _snake_case ( self ) -> Union[str, Any]: lowerCAmelCase = """cpu""" lowerCAmelCase = self.get_dummy_components() lowerCAmelCase = self.pipeline_class(**lowercase ) lowerCAmelCase = pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) lowerCAmelCase = pipe(**self.get_dummy_inputs(lowercase ) ) lowerCAmelCase = output.images[0] lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCAmelCase = np.array( [ 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ) -> List[Any]: # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def _snake_case ( self ) -> Union[str, Any]: lowerCAmelCase = torch_device == """cpu""" lowerCAmelCase = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowercase , relax_max_difference=lowercase , ) def _snake_case ( self ) -> Optional[Any]: lowerCAmelCase = self.get_dummy_components() lowerCAmelCase = self.pipeline_class(**lowercase ) lowerCAmelCase = pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) lowerCAmelCase = 1 lowerCAmelCase = 2 lowerCAmelCase = self.get_dummy_inputs(lowercase ) for key in inputs.keys(): if key in self.batch_params: lowerCAmelCase = batch_size * [inputs[key]] lowerCAmelCase = pipe(**lowercase , num_images_per_prompt=lowercase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def _snake_case ( self ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self ) -> Any: lowerCAmelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) lowerCAmelCase = ShapEPipeline.from_pretrained("""openai/shap-e""" ) lowerCAmelCase = pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) lowerCAmelCase = torch.Generator(device=lowercase ).manual_seed(0 ) lowerCAmelCase = pipe( """a shark""" , generator=lowercase , guidance_scale=15.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(lowercase , lowercase )
46
"""simple docstring""" from __future__ import annotations def _snake_case ( _snake_case : tuple[int, int] , _snake_case : int ) -> list[tuple[int, int]]: '''simple docstring''' _A , _A = position _A = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] _A = [] for position in positions: _A , _A = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(_snake_case ) return permissible_positions def _snake_case ( _snake_case : list[list[int]] ) -> bool: '''simple docstring''' return not any(elem == 0 for row in board for elem in row ) def _snake_case ( _snake_case : list[list[int]] , _snake_case : tuple[int, int] , _snake_case : int ) -> bool: '''simple docstring''' if is_complete(_snake_case ): return True for position in get_valid_pos(_snake_case , len(_snake_case ) ): _A , _A = position if board[y][x] == 0: _A = curr + 1 if open_knight_tour_helper(_snake_case , _snake_case , curr + 1 ): return True _A = 0 return False def _snake_case ( _snake_case : int ) -> list[list[int]]: '''simple docstring''' _A = [[0 for i in range(_snake_case )] for j in range(_snake_case )] for i in range(_snake_case ): for j in range(_snake_case ): _A = 1 if open_knight_tour_helper(_snake_case , (i, j) , 1 ): return board _A = 0 _A = F'''Open Kight Tour cannot be performed on a board of size {n}''' raise ValueError(_snake_case ) if __name__ == "__main__": import doctest doctest.testmod()
315
0
"""simple docstring""" from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" @slow @require_torch def UpperCAmelCase_ ( self : Dict ) -> Any: __SCREAMING_SNAKE_CASE = EncoderDecoderModel.from_encoder_decoder_pretrained("prajjwal1/bert-tiny" , "prajjwal1/bert-tiny" ) __SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) __SCREAMING_SNAKE_CASE = bertabert.config.encoder.vocab_size __SCREAMING_SNAKE_CASE = tokenizer.sep_token_id __SCREAMING_SNAKE_CASE = tokenizer.cls_token_id __SCREAMING_SNAKE_CASE = 1_2_8 __SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="train[:1%]" ) __SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="validation[:1%]" ) __SCREAMING_SNAKE_CASE = train_dataset.select(range(3_2 ) ) __SCREAMING_SNAKE_CASE = val_dataset.select(range(1_6 ) ) __SCREAMING_SNAKE_CASE = 4 def _map_to_encoder_decoder_inputs(UpperCAmelCase__ : Tuple ): # Tokenizer will automatically set [BOS] <text> [EOS] __SCREAMING_SNAKE_CASE = tokenizer(batch["article"] , padding="max_length" , truncation=UpperCAmelCase__ , max_length=5_1_2 ) __SCREAMING_SNAKE_CASE = tokenizer(batch["highlights"] , padding="max_length" , truncation=UpperCAmelCase__ , max_length=1_2_8 ) __SCREAMING_SNAKE_CASE = inputs.input_ids __SCREAMING_SNAKE_CASE = inputs.attention_mask __SCREAMING_SNAKE_CASE = outputs.input_ids __SCREAMING_SNAKE_CASE = outputs.input_ids.copy() __SCREAMING_SNAKE_CASE = [ [-1_0_0 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch["labels"] ] __SCREAMING_SNAKE_CASE = outputs.attention_mask assert all(len(UpperCAmelCase__ ) == 5_1_2 for x in inputs.input_ids ) assert all(len(UpperCAmelCase__ ) == 1_2_8 for x in outputs.input_ids ) return batch def _compute_metrics(UpperCAmelCase__ : Optional[int] ): __SCREAMING_SNAKE_CASE = pred.label_ids __SCREAMING_SNAKE_CASE = pred.predictions # all unnecessary tokens are removed __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = sum([int(pred_str[i] == label_str[i] ) for i in range(len(UpperCAmelCase__ ) )] ) / len(UpperCAmelCase__ ) return {"accuracy": accuracy} # map train dataset __SCREAMING_SNAKE_CASE = train_dataset.map( _map_to_encoder_decoder_inputs , batched=UpperCAmelCase__ , batch_size=UpperCAmelCase__ , remove_columns=["article", "highlights"] , ) train_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) # same for validation dataset __SCREAMING_SNAKE_CASE = val_dataset.map( _map_to_encoder_decoder_inputs , batched=UpperCAmelCase__ , batch_size=UpperCAmelCase__ , remove_columns=["article", "highlights"] , ) val_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) __SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() __SCREAMING_SNAKE_CASE = SeqaSeqTrainingArguments( output_dir=UpperCAmelCase__ , per_device_train_batch_size=UpperCAmelCase__ , per_device_eval_batch_size=UpperCAmelCase__ , predict_with_generate=UpperCAmelCase__ , evaluation_strategy="steps" , do_train=UpperCAmelCase__ , do_eval=UpperCAmelCase__ , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer __SCREAMING_SNAKE_CASE = SeqaSeqTrainer( model=UpperCAmelCase__ , args=UpperCAmelCase__ , compute_metrics=_compute_metrics , train_dataset=UpperCAmelCase__ , eval_dataset=UpperCAmelCase__ , tokenizer=UpperCAmelCase__ , ) # start training trainer.train()
195
"""simple docstring""" def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [0 for i in range(r + 1 )] # nc0 = 1 __SCREAMING_SNAKE_CASE = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. __SCREAMING_SNAKE_CASE = min(lowerCAmelCase_ , lowerCAmelCase_ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=1_0, r=5))
195
1
import unittest from .lib import ( Matrix, Vector, axpy, square_zero_matrix, unit_basis_vector, zero_vector, ) class __magic_name__ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Dict = Vector([1, 2, 3] ) self.assertEqual(x.component(0 ) , 1 ) self.assertEqual(x.component(2 ) , 3 ) A_ : Tuple = Vector() def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = Vector([0, 0, 0, 0, 0, 1] ) self.assertEqual(str(snake_case ) , "(0,0,0,0,0,1)" ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : List[str] = Vector([1, 2, 3, 4] ) self.assertEqual(len(snake_case ) , 4 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = Vector([1, 2] ) A_ : Dict = Vector([1, 2, 3, 4, 5] ) A_ : Optional[Any] = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) A_ : Any = Vector([1, -1, 1, -1, 2, -3, 4, -5] ) self.assertAlmostEqual(x.euclidean_length() , 2.236 , 3 ) self.assertAlmostEqual(y.euclidean_length() , 7.416 , 3 ) self.assertEqual(z.euclidean_length() , 0 ) self.assertAlmostEqual(w.euclidean_length() , 7.616 , 3 ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = Vector([1, 2, 3] ) A_ : int = Vector([1, 1, 1] ) self.assertEqual((x + y).component(0 ) , 2 ) self.assertEqual((x + y).component(1 ) , 3 ) self.assertEqual((x + y).component(2 ) , 4 ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : List[Any] = Vector([1, 2, 3] ) A_ : Any = Vector([1, 1, 1] ) self.assertEqual((x - y).component(0 ) , 0 ) self.assertEqual((x - y).component(1 ) , 1 ) self.assertEqual((x - y).component(2 ) , 2 ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Union[str, Any] = Vector([1, 2, 3] ) A_ : Dict = Vector([2, -1, 4] ) # for test of dot product A_ : Tuple = Vector([1, -2, -1] ) self.assertEqual(str(x * 3.0 ) , "(3.0,6.0,9.0)" ) self.assertEqual((a * b) , 0 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.assertEqual(str(zero_vector(10 ) ).count("0" ) , 10 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' self.assertEqual(str(unit_basis_vector(3 , 1 ) ) , "(0,1,0)" ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Dict = Vector([1, 2, 3] ) A_ : Union[str, Any] = Vector([1, 0, 1] ) self.assertEqual(str(axpy(2 , snake_case , snake_case ) ) , "(3,4,7)" ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = Vector([1, 0, 0, 0, 0, 0] ) A_ : Optional[Any] = x.copy() self.assertEqual(str(snake_case ) , str(snake_case ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Dict = Vector([1, 0, 0] ) x.change_component(0 , 0 ) x.change_component(1 , 1 ) self.assertEqual(str(snake_case ) , "(0,1,0)" ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Tuple = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n" , str(snake_case ) ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : List[Any] = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) A_ : Tuple = [[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(minors[x][y] , a.minor(snake_case , snake_case ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) A_ : List[str] = [[-3, 14, -10], [5, -10, 5], [-2, 1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(cofactors[x][y] , a.cofactor(snake_case , snake_case ) ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Tuple = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(-5 , a.determinant() ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : List[str] = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]] , 3 , 3 ) A_ : List[str] = Vector([1, 2, 3] ) self.assertEqual("(14,32,50)" , str(a * x ) ) self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n" , str(a * 2 ) ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : str = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) a.change_component(0 , 2 , 5 ) self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n" , str(snake_case ) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Any = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(7 , a.component(2 , 1 ) , 0.01 ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) A_ : List[str] = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n" , str(a + b ) ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : List[Any] = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) A_ : Tuple = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n" , str(a - b ) ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' self.assertEqual( "|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n" , str(square_zero_matrix(5 ) ) , ) if __name__ == "__main__": unittest.main()
300
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 __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' 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 SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' 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 __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __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 SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , 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 SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , 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 SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , 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 SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): 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(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = 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" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[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" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == 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"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : 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" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == 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"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
1
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowercase = logging.get_logger(__name__) __lowercase = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} __lowercase = { """tokenizer_file""": { """EleutherAI/gpt-neox-20b""": """https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json""", }, } __lowercase = { """gpt-neox-20b""": 2048, } class _A ( _a ): """simple docstring""" UpperCAmelCase : Tuple = VOCAB_FILES_NAMES UpperCAmelCase : int = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase : str = ["""input_ids""", """attention_mask"""] def __init__( self : int , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : List[str]=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : List[Any]="<|endoftext|>" , __UpperCAmelCase : Optional[int]="<|endoftext|>" , __UpperCAmelCase : Dict="<|endoftext|>" , __UpperCAmelCase : str=False , **__UpperCAmelCase : List[Any] , ): super().__init__( __UpperCAmelCase , __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , unk_token=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , add_prefix_space=__UpperCAmelCase , **__UpperCAmelCase , ) a : Union[str, Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get("add_prefix_space" , __UpperCAmelCase) != add_prefix_space: a : List[Any] = getattr(__UpperCAmelCase , pre_tok_state.pop("type")) a : Tuple = add_prefix_space a : Union[str, Any] = pre_tok_class(**__UpperCAmelCase) a : Any = add_prefix_space def __snake_case ( self : str , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None): a : Dict = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase) return tuple(__UpperCAmelCase) def __snake_case ( self : List[Any] , __UpperCAmelCase : "Conversation"): a : Tuple = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase) + [self.eos_token_id]) if len(__UpperCAmelCase) > self.model_max_length: a : Tuple = input_ids[-self.model_max_length :] return input_ids
226
"""simple docstring""" import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __lowercase = True except ImportError: __lowercase = False try: from torch.hub import _get_torch_home __lowercase = _get_torch_home() except ImportError: __lowercase = os.path.expanduser( os.getenv("""TORCH_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """torch""")) ) __lowercase = os.path.join(torch_cache_home, """transformers""") __lowercase = """https://cdn.huggingface.co""" __lowercase = """https://s3.amazonaws.com/models.huggingface.co/bert""" __lowercase = """/""".join(str(Path(__file__).resolve()).split("""/""")[:-1]) __lowercase = os.path.join(PATH, """config.yaml""") __lowercase = os.path.join(PATH, """attributes.txt""") __lowercase = os.path.join(PATH, """objects.txt""") __lowercase = os.getenv("""PYTORCH_PRETRAINED_BERT_CACHE""", default_cache_path) __lowercase = os.getenv("""PYTORCH_TRANSFORMERS_CACHE""", PYTORCH_PRETRAINED_BERT_CACHE) __lowercase = os.getenv("""TRANSFORMERS_CACHE""", PYTORCH_TRANSFORMERS_CACHE) __lowercase = """pytorch_model.bin""" __lowercase = """config.yaml""" def lowercase ( A_=OBJECTS , A_=ATTRIBUTES )-> Union[str, Any]: '''simple docstring''' a : Optional[Any] = [] with open(A_ ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) a : Union[str, Any] = [] with open(A_ ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def lowercase ( A_ )-> Optional[Any]: '''simple docstring''' a : Dict = OrderedDict() with open(A_ , "rb" ) as f: a : Optional[Any] = pkl.load(A_ )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): a : Dict = ckp.pop(A_ ) if isinstance(A_ , np.ndarray ): a : Optional[Any] = torch.tensor(A_ ) else: assert isinstance(A_ , torch.tensor ), type(A_ ) a : int = v return r class _A : """simple docstring""" UpperCAmelCase : int = {} def __init__( self : Any , __UpperCAmelCase : dict , __UpperCAmelCase : str = "root" , __UpperCAmelCase : Optional[int]=0): a : List[str] = name a : Tuple = level a : int = {} for k, v in dictionary.items(): if v is None: raise ValueError() a : List[Any] = copy.deepcopy(__UpperCAmelCase) a : int = copy.deepcopy(__UpperCAmelCase) if isinstance(__UpperCAmelCase , __UpperCAmelCase): a : Union[str, Any] = Config(__UpperCAmelCase , name=__UpperCAmelCase , level=level + 1) a : Dict = v setattr(self , __UpperCAmelCase , __UpperCAmelCase) a : Tuple = d def __repr__( self : List[str]): return str(list((self._pointer.keys()))) def __setattr__( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Tuple): a : Optional[Any] = val a : Tuple = val a : Dict = key.split(".") a : Union[str, Any] = len(__UpperCAmelCase) - 1 a : Optional[int] = self._pointer if len(__UpperCAmelCase) > 1: for i, l in enumerate(__UpperCAmelCase): if hasattr(self , __UpperCAmelCase) and isinstance(getattr(self , __UpperCAmelCase) , __UpperCAmelCase): setattr(getattr(self , __UpperCAmelCase) , ".".join(levels[i:]) , __UpperCAmelCase) if l == last_level: a : int = val else: a : str = pointer[l] def __snake_case ( self : str): return self._pointer def __snake_case ( self : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[Any]): with open(f'''{file_name}''' , "w") as stream: dump(__UpperCAmelCase , __UpperCAmelCase) def __snake_case ( self : int , __UpperCAmelCase : Dict , __UpperCAmelCase : int): with open(f'''{file_name}''' , "w") as stream: json.dump(__UpperCAmelCase , __UpperCAmelCase) @staticmethod def __snake_case ( __UpperCAmelCase : Dict): with open(__UpperCAmelCase) as stream: a : List[str] = load(__UpperCAmelCase , Loader=__UpperCAmelCase) return data def __str__( self : Tuple): a : str = " " if self._name != "root": a : List[str] = f'''{t * (self._level-1)}{self._name}:\n''' else: a : Optional[Any] = "" a : List[Any] = self._level for i, (k, v) in enumerate(self._pointer.items()): if isinstance(__UpperCAmelCase , __UpperCAmelCase): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(__UpperCAmelCase).__name__})\n''' a : Tuple = level return r[:-1] @classmethod def __snake_case ( cls : str , __UpperCAmelCase : str , **__UpperCAmelCase : List[Any]): a , a : Tuple = cls.get_config_dict(__UpperCAmelCase , **__UpperCAmelCase) return cls(__UpperCAmelCase) @classmethod def __snake_case ( cls : Union[str, Any] , __UpperCAmelCase : str , **__UpperCAmelCase : List[str]): a : int = kwargs.pop("cache_dir" , __UpperCAmelCase) a : List[Any] = kwargs.pop("force_download" , __UpperCAmelCase) a : Optional[int] = kwargs.pop("resume_download" , __UpperCAmelCase) a : Tuple = kwargs.pop("proxies" , __UpperCAmelCase) a : int = kwargs.pop("local_files_only" , __UpperCAmelCase) if os.path.isdir(__UpperCAmelCase): a : Union[str, Any] = os.path.join(__UpperCAmelCase , __UpperCAmelCase) elif os.path.isfile(__UpperCAmelCase) or is_remote_url(__UpperCAmelCase): a : List[Any] = pretrained_model_name_or_path else: a : int = hf_bucket_url(__UpperCAmelCase , filename=__UpperCAmelCase , use_cdn=__UpperCAmelCase) try: # Load from URL or cache if already cached a : Optional[Any] = cached_path( __UpperCAmelCase , cache_dir=__UpperCAmelCase , force_download=__UpperCAmelCase , proxies=__UpperCAmelCase , resume_download=__UpperCAmelCase , local_files_only=__UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError a : Union[str, Any] = Config.load_yaml(__UpperCAmelCase) except EnvironmentError: a : str = "Can't load config for" raise EnvironmentError(__UpperCAmelCase) if resolved_config_file == config_file: print("loading configuration file from path") else: print("loading configuration file cache") return Config.load_yaml(__UpperCAmelCase), kwargs def lowercase ( A_ )-> str: '''simple docstring''' a : Tuple = torch.load("dump.pt" , map_location=in_tensor.device ) a : Any = in_tensor.numpy() a : Optional[int] = out_tensor.numpy()[0] print(na.shape , na[0, 0, :5] ) print(na.shape , na[0, 0, :5] ) assert np.allclose(A_ , A_ , rtol=0.0_1 , atol=0.1 ), ( F'''{sum([1 for x in np.isclose(A_ , A_ , rtol=0.0_1 , atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def lowercase ( A_ )-> Optional[Any]: '''simple docstring''' a : Optional[Any] = urlparse(A_ ) return parsed.scheme in ("http", "https") def lowercase ( A_ , A_ , A_=True )-> str: '''simple docstring''' a : List[Any] = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX a : str = "/" not in model_id if legacy_format: return F'''{endpoint}/{model_id}-{filename}''' else: return F'''{endpoint}/{model_id}/{filename}''' def lowercase ( A_ , A_ , A_=None , A_=0 , A_=None , )-> List[str]: '''simple docstring''' a : Optional[int] = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(A_ , A_ ): ua += "; " + "; ".join("{}/{}".format(A_ , A_ ) for k, v in user_agent.items() ) elif isinstance(A_ , A_ ): ua += "; " + user_agent a : str = {"user-agent": ua} if resume_size > 0: a : List[Any] = "bytes=%d-" % (resume_size,) a : str = requests.get(A_ , stream=A_ , proxies=A_ , headers=A_ ) if response.status_code == 416: # Range not satisfiable return a : Optional[int] = response.headers.get("Content-Length" ) a : List[Any] = resume_size + int(A_ ) if content_length is not None else None a : List[Any] = tqdm( unit="B" , unit_scale=A_ , total=A_ , initial=A_ , desc="Downloading" , ) for chunk in response.iter_content(chunk_size=1_024 ): if chunk: # filter out keep-alive new chunks progress.update(len(A_ ) ) temp_file.write(A_ ) progress.close() def lowercase ( A_ , A_=None , A_=False , A_=None , A_=10 , A_=False , A_=None , A_=False , )-> str: '''simple docstring''' if cache_dir is None: a : List[Any] = TRANSFORMERS_CACHE if isinstance(A_ , A_ ): a : Tuple = str(A_ ) os.makedirs(A_ , exist_ok=A_ ) a : Optional[Any] = None if not local_files_only: try: a : Dict = requests.head(A_ , allow_redirects=A_ , proxies=A_ , timeout=A_ ) if response.status_code == 200: a : int = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass a : List[str] = url_to_filename(A_ , A_ ) # get cache path to put the file a : List[str] = os.path.join(A_ , A_ ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(A_ ): return cache_path else: a : Any = [ file for file in fnmatch.filter(os.listdir(A_ ) , filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(A_ ) > 0: return os.path.join(A_ , matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(A_ ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. a : Dict = cache_path + ".lock" with FileLock(A_ ): # If the download just completed while the lock was activated. if os.path.exists(A_ ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: a : Optional[Any] = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(A_ , "a+b" ) as f: yield f a : Tuple = _resumable_file_manager if os.path.exists(A_ ): a : Optional[Any] = os.stat(A_ ).st_size else: a : Optional[int] = 0 else: a : Union[str, Any] = partial(tempfile.NamedTemporaryFile , dir=A_ , delete=A_ ) a : Dict = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" , A_ , temp_file.name , ) http_get( A_ , A_ , proxies=A_ , resume_size=A_ , user_agent=A_ , ) os.replace(temp_file.name , A_ ) a : List[str] = {"url": url, "etag": etag} a : Tuple = cache_path + ".json" with open(A_ , "w" ) as meta_file: json.dump(A_ , A_ ) return cache_path def lowercase ( A_ , A_=None )-> Any: '''simple docstring''' a : Dict = url.encode("utf-8" ) a : Optional[Any] = shaaaa(A_ ) a : Any = url_hash.hexdigest() if etag: a : Union[str, Any] = etag.encode("utf-8" ) a : Tuple = shaaaa(A_ ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def lowercase ( A_ , A_=None , A_=False , A_=None , A_=False , A_=None , A_=False , A_=False , A_=False , )-> Tuple: '''simple docstring''' if cache_dir is None: a : Union[str, Any] = TRANSFORMERS_CACHE if isinstance(A_ , A_ ): a : List[Any] = str(A_ ) if isinstance(A_ , A_ ): a : int = str(A_ ) if is_remote_url(A_ ): # URL, so get it from the cache (downloading if necessary) a : Optional[Any] = get_from_cache( A_ , cache_dir=A_ , force_download=A_ , proxies=A_ , resume_download=A_ , user_agent=A_ , local_files_only=A_ , ) elif os.path.exists(A_ ): # File, and it exists. a : Union[str, Any] = url_or_filename elif urlparse(A_ ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(A_ ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(A_ ) ) if extract_compressed_file: if not is_zipfile(A_ ) and not tarfile.is_tarfile(A_ ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" a , a : Dict = os.path.split(A_ ) a : List[str] = output_file.replace("." , "-" ) + "-extracted" a : Optional[Any] = os.path.join(A_ , A_ ) if os.path.isdir(A_ ) and os.listdir(A_ ) and not force_extract: return output_path_extracted # Prevent parallel extractions a : Tuple = output_path + ".lock" with FileLock(A_ ): shutil.rmtree(A_ , ignore_errors=A_ ) os.makedirs(A_ ) if is_zipfile(A_ ): with ZipFile(A_ , "r" ) as zip_file: zip_file.extractall(A_ ) zip_file.close() elif tarfile.is_tarfile(A_ ): a : List[str] = tarfile.open(A_ ) tar_file.extractall(A_ ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(A_ ) ) return output_path_extracted return output_path def lowercase ( A_ , A_="," )-> Union[str, Any]: '''simple docstring''' assert isinstance(A_ , A_ ) if os.path.isfile(A_ ): with open(A_ ) as f: a : str = eval(f.read() ) else: a : List[Any] = requests.get(A_ ) try: a : Any = requests.json() except Exception: a : Any = req.content.decode() assert data is not None, "could not connect" try: a : Optional[Any] = eval(A_ ) except Exception: a : Any = data.split("\n" ) req.close() return data def lowercase ( A_ )-> str: '''simple docstring''' a : Optional[int] = requests.get(A_ ) a : List[str] = np.array(Image.open(BytesIO(response.content ) ) ) return img def lowercase ( A_ )-> Any: '''simple docstring''' a : List[Any] = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(A_ ) with open(A_ , "rb" ) as stream: a : Any = pkl.load(A_ ) a : List[str] = weights.pop("model" ) a : Dict = {} for k, v in model.items(): a : List[str] = torch.from_numpy(A_ ) if "running_var" in k: a : Dict = torch.tensor([0] ) a : Any = k.replace("running_var" , "num_batches_tracked" ) a : List[Any] = zero return new def lowercase ( )-> Optional[int]: '''simple docstring''' print(F'''{os.path.abspath(os.path.join(A_ , os.pardir ) )}/demo.ipynb''' ) def lowercase ( A_ , A_="RGB" )-> Any: '''simple docstring''' assert isinstance(A_ , A_ ) if os.path.isfile(A_ ): a : Dict = cva.imread(A_ ) else: a : Union[str, Any] = get_image_from_url(A_ ) assert img is not None, F'''could not connect to: {im}''' a : int = cva.cvtColor(A_ , cva.COLOR_BGR2RGB ) if input_format == "RGB": a : List[str] = img[:, :, ::-1] return img def lowercase ( A_ , A_=1 )-> int: '''simple docstring''' return (images[i : i + batch] for i in range(0 , len(A_ ) , A_ ))
226
1
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase__ :Union[str, Any] = logging.get_logger(__name__) # TODO Update this lowercase__ :Union[str, Any] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase ( lowerCamelCase__ ): lowercase_ : Any ='''esm''' def __init__( self ,A__=None ,A__=None ,A__=None ,A__=7_6_8 ,A__=1_2 ,A__=1_2 ,A__=3_0_7_2 ,A__=0.1 ,A__=0.1 ,A__=1_0_2_6 ,A__=0.02 ,A__=1E-12 ,A__="absolute" ,A__=True ,A__=None ,A__=False ,A__=False ,A__=None ,A__=None ,**A__ ,): super().__init__(pad_token_id=__snake_case ,mask_token_id=__snake_case ,**__snake_case) lowercase = vocab_size lowercase = hidden_size lowercase = num_hidden_layers lowercase = num_attention_heads lowercase = intermediate_size lowercase = hidden_dropout_prob lowercase = attention_probs_dropout_prob lowercase = max_position_embeddings lowercase = initializer_range lowercase = layer_norm_eps lowercase = position_embedding_type lowercase = use_cache lowercase = emb_layer_norm_before lowercase = token_dropout lowercase = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''') lowercase = EsmFoldConfig() elif isinstance(__snake_case ,__snake_case): lowercase = EsmFoldConfig(**__snake_case) lowercase = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''') lowercase = get_default_vocab_list() else: lowercase = vocab_list else: lowercase = None lowercase = None if self.esmfold_config is not None and getattr(self.esmfold_config ,'''use_esm_attn_map''' ,__snake_case): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''') def A__ ( self): lowercase = super().to_dict() if isinstance(self.esmfold_config ,__snake_case): lowercase = self.esmfold_config.to_dict() return output @dataclass class lowercase : lowercase_ : List[str] =None lowercase_ : List[str] =True lowercase_ : Tuple =False lowercase_ : Optional[Any] =False lowercase_ : int =False lowercase_ : Any =0 lowercase_ : List[Any] =True lowercase_ : Optional[int] =False lowercase_ : Any =128 lowercase_ : Union[str, Any] =None def A__ ( self): if self.trunk is None: lowercase = TrunkConfig() elif isinstance(self.trunk ,__snake_case): lowercase = TrunkConfig(**self.trunk) def A__ ( self): lowercase = asdict(self) lowercase = self.trunk.to_dict() return output @dataclass class lowercase : lowercase_ : Tuple =48 lowercase_ : Optional[Any] =1024 lowercase_ : Any =128 lowercase_ : int =32 lowercase_ : Tuple =32 lowercase_ : Optional[int] =32 lowercase_ : str =0 lowercase_ : List[str] =0 lowercase_ : Any =False lowercase_ : Dict =4 lowercase_ : List[str] =128 lowercase_ : Dict =None def A__ ( self): if self.structure_module is None: lowercase = StructureModuleConfig() elif isinstance(self.structure_module ,__snake_case): lowercase = StructureModuleConfig(**self.structure_module) if self.max_recycles <= 0: raise ValueError(f'`max_recycles` should be positive, got {self.max_recycles}.') if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f' {self.sequence_state_dim} and {self.sequence_state_dim}.') if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f' {self.pairwise_state_dim} and {self.pairwise_state_dim}.') lowercase = self.sequence_state_dim // self.sequence_head_width lowercase = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.') if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.') if self.pairwise_state_dim % 2 != 0: raise ValueError(f'`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.') if self.dropout >= 0.4: raise ValueError(f'`dropout` should not be greater than 0.4, got {self.dropout}.') def A__ ( self): lowercase = asdict(self) lowercase = self.structure_module.to_dict() return output @dataclass class lowercase : lowercase_ : Any =384 lowercase_ : Any =128 lowercase_ : Union[str, Any] =16 lowercase_ : Union[str, Any] =128 lowercase_ : Tuple =12 lowercase_ : List[str] =4 lowercase_ : Tuple =8 lowercase_ : Optional[Any] =0.1 lowercase_ : Tuple =8 lowercase_ : Optional[int] =1 lowercase_ : Optional[int] =2 lowercase_ : str =7 lowercase_ : List[Any] =10 lowercase_ : List[str] =1e-8 lowercase_ : Any =1e5 def A__ ( self): return asdict(self) def UpperCamelCase ( ): '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
101
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class a__( unittest.TestCase ): @slow def lowercase_ ( self : List[Any] ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: a : Optional[int] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Dict = TFAutoModel.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Tuple = AutoModel.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : str ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: a : List[str] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Union[str, Any] = TFAutoModelForPreTraining.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : List[Any] = AutoModelForPreTraining.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : int ): for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Union[str, Any] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : List[Any] = TFAutoModelForCausalLM.from_pretrained(__snake_case , from_pt=__snake_case ) a , a : Any = TFAutoModelForCausalLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Dict = AutoModelForCausalLM.from_pretrained(__snake_case , from_tf=__snake_case ) a , a : Tuple = AutoModelForCausalLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : Any ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Tuple = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : List[str] = TFAutoModelWithLMHead.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Dict = AutoModelWithLMHead.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : Optional[int] ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : List[str] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Optional[Any] = TFAutoModelForMaskedLM.from_pretrained(__snake_case , from_pt=__snake_case ) a , a : Optional[int] = TFAutoModelForMaskedLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : str = AutoModelForMaskedLM.from_pretrained(__snake_case , from_tf=__snake_case ) a , a : Tuple = AutoModelForMaskedLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : int ): for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Optional[Any] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : str = TFAutoModelForSeqaSeqLM.from_pretrained(__snake_case , from_pt=__snake_case ) a , a : Tuple = TFAutoModelForSeqaSeqLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Dict = AutoModelForSeqaSeqLM.from_pretrained(__snake_case , from_tf=__snake_case ) a , a : str = AutoModelForSeqaSeqLM.from_pretrained( __snake_case , output_loading_info=__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : Optional[Any] ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: a : Tuple = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : List[Any] = TFAutoModelForSequenceClassification.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Dict = AutoModelForSequenceClassification.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) @slow def lowercase_ ( self : str ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: a : Optional[Any] = AutoConfig.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : int = TFAutoModelForQuestionAnswering.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) a : Tuple = AutoModelForQuestionAnswering.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsNotNone(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) def lowercase_ ( self : Tuple ): a : List[Any] = TFAutoModelWithLMHead.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=__snake_case ) , 1_44_10 ) a : Optional[int] = AutoModelWithLMHead.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=__snake_case ) , 1_44_10 ) def lowercase_ ( self : Any ): a : int = TFAutoModelWithLMHead.from_pretrained(__snake_case , from_pt=__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=__snake_case ) , 1_44_10 ) a : Optional[Any] = AutoModelWithLMHead.from_pretrained(__snake_case , from_tf=__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=__snake_case ) , 1_44_10 )
297
0
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem snake_case : List[str] = importlib.util.find_spec('''s3fs''') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 snake_case : List[compression.BaseCompressedFileFileSystem] = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f"""A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.""") fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def __lowercase ( __lowerCAmelCase : str ): if "://" in dataset_path: a__ = dataset_path.split('://' )[1] return dataset_path def __lowercase ( __lowerCAmelCase : fsspec.AbstractFileSystem ): if fs is not None and fs.protocol != "file": return True else: return False def __lowercase ( __lowerCAmelCase : fsspec.AbstractFileSystem , __lowerCAmelCase : str , __lowerCAmelCase : str ): a__ = not is_remote_filesystem(__lowerCAmelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(__lowerCAmelCase ) , fs._strip_protocol(__lowerCAmelCase ) ) else: fs.mv(__lowerCAmelCase , __lowerCAmelCase , recursive=__lowerCAmelCase ) def __lowercase ( ): if hasattr(fsspec.asyn , 'reset_lock' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: a__ = None a__ = None a__ = threading.Lock()
350
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL snake_case : str = logging.get_logger(__name__) def __lowercase ( __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] ): a__ = b.T a__ = np.sum(np.square(__lowerCAmelCase ) , axis=1 ) a__ = np.sum(np.square(__lowerCAmelCase ) , axis=0 ) a__ = np.matmul(__lowerCAmelCase , __lowerCAmelCase ) a__ = aa[:, None] - 2 * ab + ba[None, :] return d def __lowercase ( __lowerCAmelCase : List[Any] , __lowerCAmelCase : str ): a__ = x.reshape(-1 , 3 ) a__ = squared_euclidean_distance(__lowerCAmelCase , __lowerCAmelCase ) return np.argmin(__lowerCAmelCase , axis=1 ) class snake_case_ (lowerCamelCase_ ): UpperCAmelCase__ : Optional[Any] = ['''pixel_values'''] def __init__( self :Dict ,__snake_case :Optional[Union[List[List[int]], np.ndarray]] = None ,__snake_case :bool = True ,__snake_case :Dict[str, int] = None ,__snake_case :PILImageResampling = PILImageResampling.BILINEAR ,__snake_case :bool = True ,__snake_case :bool = True ,**__snake_case :Optional[int] ,) -> None: super().__init__(**__snake_case ) a__ = size if size is not None else {'height': 2_56, 'width': 2_56} a__ = get_size_dict(__snake_case ) a__ = np.array(__snake_case ) if clusters is not None else None a__ = do_resize a__ = size a__ = resample a__ = do_normalize a__ = do_color_quantize def lowerCamelCase__( self :Union[str, Any] ,__snake_case :np.ndarray ,__snake_case :Dict[str, int] ,__snake_case :PILImageResampling = PILImageResampling.BILINEAR ,__snake_case :Optional[Union[str, ChannelDimension]] = None ,**__snake_case :Any ,) -> np.ndarray: a__ = get_size_dict(__snake_case ) if "height" not in size or "width" not in size: raise ValueError(F'Size dictionary must contain both height and width keys. Got {size.keys()}' ) return resize( __snake_case ,size=(size['height'], size['width']) ,resample=__snake_case ,data_format=__snake_case ,**__snake_case ) def lowerCamelCase__( self :List[Any] ,__snake_case :np.ndarray ,__snake_case :Optional[Union[str, ChannelDimension]] = None ,) -> np.ndarray: a__ = rescale(image=__snake_case ,scale=1 / 1_27.5 ,data_format=__snake_case ) a__ = image - 1 return image def lowerCamelCase__( self :Optional[Any] ,__snake_case :ImageInput ,__snake_case :bool = None ,__snake_case :Dict[str, int] = None ,__snake_case :PILImageResampling = None ,__snake_case :bool = None ,__snake_case :Optional[bool] = None ,__snake_case :Optional[Union[List[List[int]], np.ndarray]] = None ,__snake_case :Optional[Union[str, TensorType]] = None ,__snake_case :Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST ,**__snake_case :List[str] ,) -> PIL.Image.Image: a__ = do_resize if do_resize is not None else self.do_resize a__ = size if size is not None else self.size a__ = get_size_dict(__snake_case ) a__ = resample if resample is not None else self.resample a__ = do_normalize if do_normalize is not None else self.do_normalize a__ = do_color_quantize if do_color_quantize is not None else self.do_color_quantize a__ = clusters if clusters is not None else self.clusters a__ = np.array(__snake_case ) a__ = make_list_of_images(__snake_case ) if not valid_images(__snake_case ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_color_quantize and clusters is None: raise ValueError('Clusters must be specified if do_color_quantize is True.' ) # All transformations expect numpy arrays. a__ = [to_numpy_array(__snake_case ) for image in images] if do_resize: a__ = [self.resize(image=__snake_case ,size=__snake_case ,resample=__snake_case ) for image in images] if do_normalize: a__ = [self.normalize(image=__snake_case ) for image in images] if do_color_quantize: a__ = [to_channel_dimension_format(__snake_case ,ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) a__ = np.array(__snake_case ) a__ = color_quantize(__snake_case ,__snake_case ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) a__ = images.shape[0] a__ = images.reshape(__snake_case ,-1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. a__ = list(__snake_case ) else: a__ = [to_channel_dimension_format(__snake_case ,__snake_case ) for image in images] a__ = {'input_ids': images} return BatchFeature(data=__snake_case ,tensor_type=__snake_case )
109
0
"""simple docstring""" import qiskit def a__ ( lowerCAmelCase , lowerCAmelCase ) -> qiskit.result.counts.Counts: UpperCAmelCase__ : Optional[Any] = qiskit.Aer.get_backend("""aer_simulator""" ) # Create a Quantum Circuit acting on the q register UpperCAmelCase__ : Any = qiskit.QuantumCircuit(lowerCAmelCase , lowerCAmelCase ) # Map the quantum measurement to the classical bits circuit.measure([0] , [0] ) # Execute the circuit on the simulator UpperCAmelCase__ : Any = qiskit.execute(lowerCAmelCase , lowerCAmelCase , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(lowerCAmelCase ) if __name__ == "__main__": print(f'''Total count for various states are: {single_qubit_measure(1, 1)}''')
171
"""simple docstring""" def a__ ( lowerCAmelCase , lowerCAmelCase ) -> bool: UpperCAmelCase__ : Any = len(lowerCAmelCase ) UpperCAmelCase__ : List[Any] = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): UpperCAmelCase__ : int = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): UpperCAmelCase__ : Dict = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: UpperCAmelCase__ : str = subset[i - 1][j] if arr[i - 1] <= j: UpperCAmelCase__ : Optional[int] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
171
1
from math import isqrt def UpperCamelCase ( __lowercase : int ): '''simple docstring''' return all(number % divisor != 0 for divisor in range(2 ,isqrt(__lowercase ) + 1 ) ) def UpperCamelCase ( __lowercase : int = 10**6 ): '''simple docstring''' A_ : Optional[Any] = 0 A_ : List[str] = 1 A_ : Dict = 7 while prime_candidate < max_prime: primes_count += is_prime(__lowercase ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(F"""{solution() = }""")
192
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
192
1
from math import ceil def A_ ( _lowerCAmelCase = 1001 ) -> int: UpperCamelCase : str = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): UpperCamelCase : List[str] = 2 * i + 1 UpperCamelCase : Tuple = 2 * i UpperCamelCase : List[Any] = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: __lowerCamelCase : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print("""Invalid entry - please enter a number""")
52
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCamelCase : Optional[int] = {"""configuration_mmbt""": ["""MMBTConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase : List[Any] = ["""MMBTForClassification""", """MMBTModel""", """ModalEmbeddings"""] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys __lowerCamelCase : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
52
1
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): # Initialise PyTorch model _lowerCamelCase : Optional[Any] = RemBertConfig.from_json_file(lowercase__ ) print('Building PyTorch model from configuration: {}'.format(str(lowercase__ ) ) ) _lowerCamelCase : List[str] = RemBertModel(lowercase__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowercase__ , lowercase__ , lowercase__ ) # Save pytorch-model print('Save PyTorch model to {}'.format(lowercase__ ) ) torch.save(model.state_dict() , lowercase__ ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--rembert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained RemBERT model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) lowercase__ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
354
"""simple docstring""" def _snake_case ( lowercase__ ): # if the collection is empty, returns empty if collection == []: return [] # get some information about the collection _lowerCamelCase : List[str] = len(lowercase__ ) _lowerCamelCase : List[str] = max(lowercase__ ) _lowerCamelCase : List[str] = min(lowercase__ ) # create the counting array _lowerCamelCase : List[Any] = coll_max + 1 - coll_min _lowerCamelCase : List[Any] = [0] * counting_arr_length # count how much a number appears in the collection for number in collection: counting_arr[number - coll_min] += 1 # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1 , lowercase__ ): _lowerCamelCase : Optional[int] = counting_arr[i] + counting_arr[i - 1] # create the output collection _lowerCamelCase : Dict = [0] * coll_len # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0 , lowercase__ ) ): _lowerCamelCase : Any = collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered def _snake_case ( lowercase__ ): return "".join([chr(lowercase__ ) for i in counting_sort([ord(lowercase__ ) for c in string] )] ) if __name__ == "__main__": # Test string sort assert counting_sort_string("""thisisthestring""") == "eghhiiinrsssttt" lowercase__ = input("""Enter numbers separated by a comma:\n""").strip() lowercase__ = [int(item) for item in user_input.split(""",""")] print(counting_sort(unsorted))
12
0
import os import re import shutil import sys import tempfile import unittest import black _snake_case : Any = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case : str = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class a (unittest.TestCase ): """simple docstring""" def __snake_case ( self : Tuple ) -> Optional[Any]: __snake_case : List[Any] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , "models/bert/" ) ) __snake_case : Dict = self.transformer_dir shutil.copy( os.path.join(lowerCamelCase , "src/transformers/models/bert/modeling_bert.py" ) , os.path.join(self.transformer_dir , "models/bert/modeling_bert.py" ) , ) def __snake_case ( self : Optional[int] ) -> Optional[Any]: __snake_case : Tuple = "src/transformers" shutil.rmtree(self.transformer_dir ) def __snake_case ( self : Optional[int] , lowerCamelCase : Tuple , lowerCamelCase : str , lowerCamelCase : Union[str, Any] , lowerCamelCase : Tuple=None ) -> Optional[Any]: __snake_case : Dict = comment + F'\nclass {class_name}(nn.Module):\n' + class_code if overwrite_result is not None: __snake_case : Dict = comment + F'\nclass {class_name}(nn.Module):\n' + overwrite_result __snake_case : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) __snake_case : Dict = black.format_str(lowerCamelCase , mode=lowerCamelCase ) __snake_case : Optional[Any] = os.path.join(self.transformer_dir , "new_code.py" ) with open(lowerCamelCase , "w" , newline="\n" ) as f: f.write(lowerCamelCase ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(lowerCamelCase ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=lowerCamelCase ) with open(lowerCamelCase , "r" ) as f: self.assertTrue(f.read() , lowerCamelCase ) def __snake_case ( self : Optional[int] ) -> Optional[int]: __snake_case : Any = check_copies.find_code_in_transformers("models.bert.modeling_bert.BertLMPredictionHead" ) self.assertEqual(lowerCamelCase , lowerCamelCase ) def __snake_case ( self : Optional[Any] ) -> Tuple: # Base copy consistency self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead" , "BertLMPredictionHead" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead" , "BertLMPredictionHead" , lowerCamelCase , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , re.sub("Bert" , "TestModel" , lowerCamelCase ) , ) # Copy consistency with a really long name __snake_case : Tuple = "TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( F'# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}' , F'{long_class_name}LMPredictionHead' , re.sub("Bert" , lowerCamelCase , lowerCamelCase ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , lowerCamelCase , overwrite_result=re.sub("Bert" , "TestModel" , lowerCamelCase ) , ) def __snake_case ( self : str ) -> str: __snake_case : Optional[Any] = check_copies.LOCALIZED_READMES["README_zh-hans.md"] __snake_case : Tuple = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the" " Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for" " Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong" " Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1." " **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace)," " released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and" " lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same" " method has been applied to compress GPT2 into" " [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into" " [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation)," " Multilingual BERT into" " [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German" " version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**" " (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders" " as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang" " Luong, Quoc V. Le, Christopher D. Manning." ) __snake_case : Dict = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) __snake_case : Any = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1." " **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文" " [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and" " lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same" " method has been applied to compress GPT2 into" " [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into" " [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation)," " Multilingual BERT into" " [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German" " version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自" " Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather" " than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le," " Christopher D. Manning 发布。\n" ) __snake_case , __snake_case : Any = check_copies.convert_to_localized_md( lowerCamelCase , lowerCamelCase , localized_readme["format_model_list"] ) self.assertFalse(lowerCamelCase ) self.assertEqual(lowerCamelCase , lowerCamelCase ) __snake_case , __snake_case : Tuple = check_copies.convert_to_localized_md( lowerCamelCase , lowerCamelCase , localized_readme["format_model_list"] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(lowerCamelCase ) __snake_case : Optional[Any] = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the" " Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for" " Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong" " Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut." ) __snake_case : Optional[int] = ( "1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and" " the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) __snake_case : List[Any] = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) __snake_case , __snake_case : Tuple = check_copies.convert_to_localized_md( lowerCamelCase , lowerCamelCase , localized_readme["format_model_list"] ) # Check if the model link is synchronized. self.assertEqual(lowerCamelCase , lowerCamelCase )
123
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() _snake_case : int = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Any = DPTConfig(embedding_type="hybrid" ) if "large" in checkpoint_url: __snake_case : Optional[int] = 1_0_2_4 __snake_case : List[Any] = 4_0_9_6 __snake_case : List[Any] = 2_4 __snake_case : Optional[Any] = 1_6 __snake_case : str = [5, 1_1, 1_7, 2_3] __snake_case : List[str] = [2_5_6, 5_1_2, 1_0_2_4, 1_0_2_4] __snake_case : Union[str, Any] = (1, 3_8_4, 3_8_4) if "nyu" or "midas" in checkpoint_url: __snake_case : Tuple = 7_6_8 __snake_case : Any = [1, 1, 1, 0.5] __snake_case : Any = [2_5_6, 5_1_2, 7_6_8, 7_6_8] __snake_case : Any = 1_5_0 __snake_case : Optional[Any] = 1_6 __snake_case : List[str] = (1, 3_8_4, 3_8_4) __snake_case : Tuple = False __snake_case : Optional[Any] = "project" if "ade" in checkpoint_url: __snake_case : Optional[int] = True __snake_case : List[str] = 7_6_8 __snake_case : int = [1, 1, 1, 0.5] __snake_case : Any = 1_5_0 __snake_case : Tuple = 1_6 __snake_case : List[str] = "huggingface/label-files" __snake_case : Union[str, Any] = "ade20k-id2label.json" __snake_case : List[str] = json.load(open(cached_download(hf_hub_url(__lowerCamelCase , __lowerCamelCase , repo_type="dataset" ) ) , "r" ) ) __snake_case : int = {int(__lowerCamelCase ): v for k, v in idalabel.items()} __snake_case : Optional[Any] = idalabel __snake_case : str = {v: k for k, v in idalabel.items()} __snake_case : Tuple = [1, 1_5_0, 4_8_0, 4_8_0] return config, expected_shape def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Tuple = ["pretrained.model.head.weight", "pretrained.model.head.bias"] for k in ignore_keys: state_dict.pop(__lowerCamelCase , __lowerCamelCase ) def lowerCAmelCase_ ( __lowerCamelCase ): if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): __snake_case : Tuple = name.replace("pretrained.model" , "dpt.encoder" ) if "pretrained.model" in name: __snake_case : Tuple = name.replace("pretrained.model" , "dpt.embeddings" ) if "patch_embed" in name: __snake_case : Optional[Any] = name.replace("patch_embed" , "" ) if "pos_embed" in name: __snake_case : Optional[int] = name.replace("pos_embed" , "position_embeddings" ) if "attn.proj" in name: __snake_case : List[str] = name.replace("attn.proj" , "attention.output.dense" ) if "proj" in name and "project" not in name: __snake_case : Union[str, Any] = name.replace("proj" , "projection" ) if "blocks" in name: __snake_case : int = name.replace("blocks" , "layer" ) if "mlp.fc1" in name: __snake_case : Tuple = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __snake_case : Any = name.replace("mlp.fc2" , "output.dense" ) if "norm1" in name and "backbone" not in name: __snake_case : Optional[Any] = name.replace("norm1" , "layernorm_before" ) if "norm2" in name and "backbone" not in name: __snake_case : Any = name.replace("norm2" , "layernorm_after" ) if "scratch.output_conv" in name: __snake_case : Dict = name.replace("scratch.output_conv" , "head" ) if "scratch" in name: __snake_case : Union[str, Any] = name.replace("scratch" , "neck" ) if "layer1_rn" in name: __snake_case : List[Any] = name.replace("layer1_rn" , "convs.0" ) if "layer2_rn" in name: __snake_case : str = name.replace("layer2_rn" , "convs.1" ) if "layer3_rn" in name: __snake_case : List[str] = name.replace("layer3_rn" , "convs.2" ) if "layer4_rn" in name: __snake_case : Optional[int] = name.replace("layer4_rn" , "convs.3" ) if "refinenet" in name: __snake_case : Optional[int] = int(name[len("neck.refinenet" ) : len("neck.refinenet" ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 __snake_case : int = name.replace(F'refinenet{layer_idx}' , F'fusion_stage.layers.{abs(layer_idx-4 )}' ) if "out_conv" in name: __snake_case : Any = name.replace("out_conv" , "projection" ) if "resConfUnit1" in name: __snake_case : List[Any] = name.replace("resConfUnit1" , "residual_layer1" ) if "resConfUnit2" in name: __snake_case : Tuple = name.replace("resConfUnit2" , "residual_layer2" ) if "conv1" in name: __snake_case : List[str] = name.replace("conv1" , "convolution1" ) if "conv2" in name: __snake_case : str = name.replace("conv2" , "convolution2" ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess1.0.project.0" , "neck.reassemble_stage.readout_projects.0.0" ) if "pretrained.act_postprocess2.0.project.0" in name: __snake_case : Optional[int] = name.replace("pretrained.act_postprocess2.0.project.0" , "neck.reassemble_stage.readout_projects.1.0" ) if "pretrained.act_postprocess3.0.project.0" in name: __snake_case : List[str] = name.replace("pretrained.act_postprocess3.0.project.0" , "neck.reassemble_stage.readout_projects.2.0" ) if "pretrained.act_postprocess4.0.project.0" in name: __snake_case : Dict = name.replace("pretrained.act_postprocess4.0.project.0" , "neck.reassemble_stage.readout_projects.3.0" ) # resize blocks if "pretrained.act_postprocess1.3" in name: __snake_case : Tuple = name.replace("pretrained.act_postprocess1.3" , "neck.reassemble_stage.layers.0.projection" ) if "pretrained.act_postprocess1.4" in name: __snake_case : int = name.replace("pretrained.act_postprocess1.4" , "neck.reassemble_stage.layers.0.resize" ) if "pretrained.act_postprocess2.3" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess2.3" , "neck.reassemble_stage.layers.1.projection" ) if "pretrained.act_postprocess2.4" in name: __snake_case : Optional[Any] = name.replace("pretrained.act_postprocess2.4" , "neck.reassemble_stage.layers.1.resize" ) if "pretrained.act_postprocess3.3" in name: __snake_case : Optional[int] = name.replace("pretrained.act_postprocess3.3" , "neck.reassemble_stage.layers.2.projection" ) if "pretrained.act_postprocess4.3" in name: __snake_case : Dict = name.replace("pretrained.act_postprocess4.3" , "neck.reassemble_stage.layers.3.projection" ) if "pretrained.act_postprocess4.4" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess4.4" , "neck.reassemble_stage.layers.3.resize" ) if "pretrained" in name: __snake_case : Union[str, Any] = name.replace("pretrained" , "dpt" ) if "bn" in name: __snake_case : Tuple = name.replace("bn" , "batch_norm" ) if "head" in name: __snake_case : Dict = name.replace("head" , "head.head" ) if "encoder.norm" in name: __snake_case : Optional[int] = name.replace("encoder.norm" , "layernorm" ) if "auxlayer" in name: __snake_case : Tuple = name.replace("auxlayer" , "auxiliary_head.head" ) if "backbone" in name: __snake_case : str = name.replace("backbone" , "backbone.bit.encoder" ) if ".." in name: __snake_case : Tuple = name.replace(".." , "." ) if "stem.conv" in name: __snake_case : int = name.replace("stem.conv" , "bit.embedder.convolution" ) if "blocks" in name: __snake_case : Any = name.replace("blocks" , "layers" ) if "convolution" in name and "backbone" in name: __snake_case : Optional[int] = name.replace("convolution" , "conv" ) if "layer" in name and "backbone" in name: __snake_case : List[Any] = name.replace("layer" , "layers" ) if "backbone.bit.encoder.bit" in name: __snake_case : Optional[int] = name.replace("backbone.bit.encoder.bit" , "backbone.bit" ) if "embedder.conv" in name: __snake_case : int = name.replace("embedder.conv" , "embedder.convolution" ) if "backbone.bit.encoder.stem.norm" in name: __snake_case : Optional[Any] = name.replace("backbone.bit.encoder.stem.norm" , "backbone.bit.embedder.norm" ) return name def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __snake_case : int = state_dict.pop(F'dpt.encoder.layer.{i}.attn.qkv.weight' ) __snake_case : Any = state_dict.pop(F'dpt.encoder.layer.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict __snake_case : str = in_proj_weight[: config.hidden_size, :] __snake_case : List[Any] = in_proj_bias[: config.hidden_size] __snake_case : str = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __snake_case : Tuple = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] __snake_case : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] __snake_case : int = in_proj_bias[-config.hidden_size :] def lowerCAmelCase_ ( ): __snake_case : List[str] = "http://images.cocodataset.org/val2017/000000039769.jpg" __snake_case : int = Image.open(requests.get(__lowerCamelCase , stream=__lowerCamelCase ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): __snake_case , __snake_case : Optional[int] = get_dpt_config(__lowerCamelCase ) # load original state_dict from URL # state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, map_location="cpu") __snake_case : Optional[int] = torch.load(__lowerCamelCase , map_location="cpu" ) # remove certain keys remove_ignore_keys_(__lowerCamelCase ) # rename keys for key in state_dict.copy().keys(): __snake_case : Optional[int] = state_dict.pop(__lowerCamelCase ) __snake_case : Optional[Any] = val # read in qkv matrices read_in_q_k_v(__lowerCamelCase , __lowerCamelCase ) # load HuggingFace model __snake_case : Dict = DPTForSemanticSegmentation(__lowerCamelCase ) if "ade" in checkpoint_url else DPTForDepthEstimation(__lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) model.eval() # Check outputs on an image __snake_case : str = 4_8_0 if "ade" in checkpoint_url else 3_8_4 __snake_case : Any = DPTImageProcessor(size=__lowerCamelCase ) __snake_case : int = prepare_img() __snake_case : Union[str, Any] = image_processor(__lowerCamelCase , return_tensors="pt" ) # forward pass __snake_case : Dict = model(**__lowerCamelCase ).logits if "ade" in checkpoint_url else model(**__lowerCamelCase ).predicted_depth if show_prediction: __snake_case : int = ( torch.nn.functional.interpolate( outputs.unsqueeze(1 ) , size=(image.size[1], image.size[0]) , mode="bicubic" , align_corners=__lowerCamelCase , ) .squeeze() .cpu() .numpy() ) Image.fromarray((prediction / prediction.max()) * 2_5_5 ).show() if pytorch_dump_folder_path is not None: Path(__lowerCamelCase ).mkdir(exist_ok=__lowerCamelCase ) print(F'Saving model to {pytorch_dump_folder_path}' ) model.save_pretrained(__lowerCamelCase ) print(F'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(__lowerCamelCase ) if push_to_hub: model.push_to_hub("ybelkada/dpt-hybrid-midas" ) image_processor.push_to_hub("ybelkada/dpt-hybrid-midas" ) if __name__ == "__main__": _snake_case : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--checkpoint_url", default="https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt", type=str, help="URL of the original DPT checkpoint you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=False, help="Path to the output PyTorch model directory.", ) parser.add_argument( "--push_to_hub", action="store_true", ) parser.add_argument( "--model_name", default="dpt-large", type=str, help="Name of the model, in case you're pushing to the hub.", ) parser.add_argument( "--show_prediction", action="store_true", ) _snake_case : str = parser.parse_args() convert_dpt_checkpoint( args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name, args.show_prediction )
123
1
import warnings from functools import wraps from typing import Callable def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Callable: @wraps(SCREAMING_SNAKE_CASE__ ) def _inner_fn(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): warnings.warn( (f"'{fn.__name__}' is experimental and might be subject to breaking changes in the future.") , SCREAMING_SNAKE_CASE__ , ) return fn(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) return _inner_fn
285
import math from datetime import datetime, timedelta def _snake_case( SCREAMING_SNAKE_CASE__ ) -> datetime: lowercase : Any = year % 19 lowercase : Optional[int] = year % 4 lowercase : Any = year % 7 lowercase : str = math.floor(year / 100 ) lowercase : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) lowercase : Tuple = leap_day_inhibits / 4 lowercase : Any = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 lowercase : Union[str, Any] = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 lowercase : str = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon lowercase : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(SCREAMING_SNAKE_CASE__ , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(SCREAMING_SNAKE_CASE__ , 4 , 18 ) else: return datetime(SCREAMING_SNAKE_CASE__ , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): lowercase : List[str] = """will be""" if year > datetime.now().year else """was""" print(F'''Easter in {year} {tense} {gauss_easter(year)}''')
285
1
'''simple docstring''' import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def lowerCAmelCase_ ( snake_case_ : Optional[int] ) -> str: '''simple docstring''' return 1.0 / (1.0 + np.exp(-_outputs )) def lowerCAmelCase_ ( snake_case_ : Optional[int] ) -> Dict: '''simple docstring''' UpperCAmelCase_ = np.max(_outputs , axis=-1 , keepdims=snake_case_ ) UpperCAmelCase_ = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=snake_case_ ) class __A ( UpperCamelCase__ ): a__ : List[str] = """sigmoid""" a__ : Tuple = """softmax""" a__ : Union[str, Any] = """none""" @add_end_docstrings( UpperCamelCase__ , r""" return_all_scores (`bool`, *optional*, defaults to `False`): Whether to return all prediction scores or just the one of the predicted class. function_to_apply (`str`, *optional*, defaults to `\"default\"`): The function to apply to the model outputs in order to retrieve the scores. Accepts four different values: - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model has several labels, will apply the softmax function on the output. - `\"sigmoid\"`: Applies the sigmoid function on the output. - `\"softmax\"`: Applies the softmax function on the output. - `\"none\"`: Does not apply any function on the output. """ , ) class __A ( UpperCamelCase__ ): a__ : Optional[Any] = False a__ : List[str] = ClassificationFunction.NONE def __init__(self : Dict , **__a : Any ): super().__init__(**__a ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def _lowercase (self : Dict , __a : Dict=None , __a : Any=None , __a : int="" , **__a : Dict ): # Using "" as default argument because we're going to use `top_k=None` in user code to declare # "No top_k" UpperCAmelCase_ = tokenizer_kwargs UpperCAmelCase_ = {} if hasattr(self.model.config , "return_all_scores" ) and return_all_scores is None: UpperCAmelCase_ = self.model.config.return_all_scores if isinstance(__a , __a ) or top_k is None: UpperCAmelCase_ = top_k UpperCAmelCase_ = False elif return_all_scores is not None: warnings.warn( "`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of" " `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`." , __a , ) if return_all_scores: UpperCAmelCase_ = None else: UpperCAmelCase_ = 1 if isinstance(__a , __a ): UpperCAmelCase_ = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: UpperCAmelCase_ = function_to_apply return preprocess_params, {}, postprocess_params def __call__(self : Optional[int] , *__a : List[Any] , **__a : Tuple ): UpperCAmelCase_ = super().__call__(*__a , **__a ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. UpperCAmelCase_ = "top_k" not in kwargs if isinstance(args[0] , __a ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def _lowercase (self : Optional[Any] , __a : Dict , **__a : List[str] ): UpperCAmelCase_ = self.framework if isinstance(__a , __a ): return self.tokenizer(**__a , return_tensors=__a , **__a ) elif isinstance(__a , __a ) and len(__a ) == 1 and isinstance(inputs[0] , __a ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0] , text_pair=inputs[0][1] , return_tensors=__a , **__a ) elif isinstance(__a , __a ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( "The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a" " dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair." ) return self.tokenizer(__a , return_tensors=__a , **__a ) def _lowercase (self : Dict , __a : Optional[Any] ): return self.model(**__a ) def _lowercase (self : Dict , __a : int , __a : int=None , __a : List[str]=1 , __a : List[str]=True ): # `_legacy` is used to determine if we're running the naked pipeline and in backward # compatibility mode, or if running the pipeline with `pipeline(..., top_k=1)` we're running # the more natural result containing the list. # Default value before `set_parameters` if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: UpperCAmelCase_ = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: UpperCAmelCase_ = ClassificationFunction.SOFTMAX elif hasattr(self.model.config , "function_to_apply" ) and function_to_apply is None: UpperCAmelCase_ = self.model.config.function_to_apply else: UpperCAmelCase_ = ClassificationFunction.NONE UpperCAmelCase_ = model_outputs["logits"][0] UpperCAmelCase_ = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: UpperCAmelCase_ = sigmoid(__a ) elif function_to_apply == ClassificationFunction.SOFTMAX: UpperCAmelCase_ = softmax(__a ) elif function_to_apply == ClassificationFunction.NONE: UpperCAmelCase_ = outputs else: raise ValueError(f"""Unrecognized `function_to_apply` argument: {function_to_apply}""" ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} UpperCAmelCase_ = [ {"label": self.model.config.idalabel[i], "score": score.item()} for i, score in enumerate(__a ) ] if not _legacy: dict_scores.sort(key=lambda __a : x["score"] , reverse=__a ) if top_k is not None: UpperCAmelCase_ = dict_scores[:top_k] return dict_scores
1
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def __SCREAMING_SNAKE_CASE ( ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ = ArgumentParser( description=( """PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=__UpperCamelCase , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=__UpperCamelCase , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=__UpperCamelCase ) return parser.parse_args() def __SCREAMING_SNAKE_CASE ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ = parse_args() # Import training_script as a module. SCREAMING_SNAKE_CASE__ = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) SCREAMING_SNAKE_CASE__ = script_fpath.stem SCREAMING_SNAKE_CASE__ = importlib.import_module(__UpperCamelCase ) # Patch sys.argv SCREAMING_SNAKE_CASE__ = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
219
0
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { """google/pix2struct-textcaps-base""": ( """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json""" ), } class a__ ( snake_case__ ): _a : Any = """pix2struct_text_model""" _a : List[str] = ["""past_key_values"""] _a : Union[str, Any] = { """hidden_size""": """hidden_size""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self , _A=5_0_2_4_4 , _A=7_6_8 , _A=6_4 , _A=2_0_4_8 , _A=1_2 , _A=1_2 , _A=3_2 , _A=1_2_8 , _A=0.1 , _A=1E-6 , _A=1.0 , _A="gelu_new" , _A=0 , _A=False , _A=0 , _A=1 , _A=False , _A=True , **_A , ): """simple docstring""" __lowerCAmelCase = vocab_size __lowerCAmelCase = hidden_size __lowerCAmelCase = d_kv __lowerCAmelCase = d_ff __lowerCAmelCase = num_layers __lowerCAmelCase = num_heads __lowerCAmelCase = relative_attention_num_buckets __lowerCAmelCase = relative_attention_max_distance __lowerCAmelCase = dropout_rate __lowerCAmelCase = layer_norm_epsilon __lowerCAmelCase = initializer_factor __lowerCAmelCase = use_cache __lowerCAmelCase = eos_token_id __lowerCAmelCase = decoder_start_token_id # for backwards compatibility __lowerCAmelCase = dense_act_fn super().__init__( pad_token_id=_A , eos_token_id=_A , decoder_start_token_id=_A , tie_word_embeddings=_A , is_decoder=_A , **_A , ) @classmethod def __SCREAMING_SNAKE_CASE( cls , _A , **_A ): """simple docstring""" cls._set_token_in_kwargs(_A ) __lowerCAmelCase , __lowerCAmelCase = cls.get_config_dict(_A , **_A ) # get the text config dict if we are loading from Pix2StructConfig if config_dict.get("model_type" ) == "pix2struct": __lowerCAmelCase = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_A , **_A ) class a__ ( snake_case__ ): _a : int = """pix2struct_vision_model""" def __init__( self , _A=7_6_8 , _A=7_6_8 , _A=2_0_4_8 , _A=6_4 , _A=1_2 , _A=1_2 , _A="gelu_new" , _A=1E-6 , _A=0.0 , _A=0.0 , _A=1E-1_0 , _A=1.0 , _A=4_0_9_6 , _A=3_2 , _A=1_2_8 , **_A , ): """simple docstring""" super().__init__(**_A ) __lowerCAmelCase = hidden_size __lowerCAmelCase = patch_embed_hidden_size __lowerCAmelCase = d_ff __lowerCAmelCase = dropout_rate __lowerCAmelCase = num_hidden_layers __lowerCAmelCase = num_attention_heads __lowerCAmelCase = initializer_range __lowerCAmelCase = initializer_factor __lowerCAmelCase = attention_dropout __lowerCAmelCase = layer_norm_eps __lowerCAmelCase = dense_act_fn __lowerCAmelCase = seq_len __lowerCAmelCase = relative_attention_num_buckets __lowerCAmelCase = relative_attention_max_distance __lowerCAmelCase = d_kv @classmethod def __SCREAMING_SNAKE_CASE( cls , _A , **_A ): """simple docstring""" cls._set_token_in_kwargs(_A ) __lowerCAmelCase , __lowerCAmelCase = cls.get_config_dict(_A , **_A ) # get the vision config dict if we are loading from Pix2StructConfig if config_dict.get("model_type" ) == "pix2struct": __lowerCAmelCase = config_dict["vision_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_A , **_A ) class a__ ( snake_case__ ): _a : Any = """pix2struct""" _a : List[str] = True def __init__( self , _A=None , _A=None , _A=1.0 , _A=0.02 , _A=False , _A=False , _A=True , **_A , ): """simple docstring""" super().__init__(tie_word_embeddings=_A , is_encoder_decoder=_A , **_A ) if text_config is None: __lowerCAmelCase = {} logger.info("text_config is None. Initializing the Pix2StructTextConfig with default values." ) if vision_config is None: __lowerCAmelCase = {} logger.info("vision_config is None. Initializing the Pix2StructVisionConfig with default values." ) __lowerCAmelCase = PixaStructTextConfig(**_A ) __lowerCAmelCase = PixaStructVisionConfig(**_A ) __lowerCAmelCase = self.text_config.decoder_start_token_id __lowerCAmelCase = self.text_config.pad_token_id __lowerCAmelCase = self.text_config.eos_token_id __lowerCAmelCase = initializer_factor __lowerCAmelCase = initializer_range __lowerCAmelCase = self.initializer_range __lowerCAmelCase = self.initializer_range __lowerCAmelCase = is_vqa @classmethod def __SCREAMING_SNAKE_CASE( cls , _A , _A , **_A ): """simple docstring""" return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_A ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = copy.deepcopy(self.__dict__ ) __lowerCAmelCase = self.text_config.to_dict() __lowerCAmelCase = self.vision_config.to_dict() __lowerCAmelCase = self.__class__.model_type return output
361
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { """alibaba-damo/mgp-str-base""": """https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json""", } class a__ ( snake_case__ ): _a : Dict = """mgp-str""" def __init__( self , _A=[3_2, 1_2_8] , _A=4 , _A=3 , _A=2_7 , _A=3_8 , _A=5_0_2_5_7 , _A=3_0_5_2_2 , _A=7_6_8 , _A=1_2 , _A=1_2 , _A=4.0 , _A=True , _A=False , _A=1E-5 , _A=0.0 , _A=0.0 , _A=0.0 , _A=False , _A=0.02 , **_A , ): """simple docstring""" super().__init__(**_A ) __lowerCAmelCase = image_size __lowerCAmelCase = patch_size __lowerCAmelCase = num_channels __lowerCAmelCase = max_token_length __lowerCAmelCase = num_character_labels __lowerCAmelCase = num_bpe_labels __lowerCAmelCase = num_wordpiece_labels __lowerCAmelCase = hidden_size __lowerCAmelCase = num_hidden_layers __lowerCAmelCase = num_attention_heads __lowerCAmelCase = mlp_ratio __lowerCAmelCase = distilled __lowerCAmelCase = layer_norm_eps __lowerCAmelCase = drop_rate __lowerCAmelCase = qkv_bias __lowerCAmelCase = attn_drop_rate __lowerCAmelCase = drop_path_rate __lowerCAmelCase = output_aa_attentions __lowerCAmelCase = initializer_range
102
0
from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar lowerCamelCase__ : int = TypeVar('T') def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (position - 1) // 2 def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (2 * position) + 1 def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (2 * position) + 2 class lowerCamelCase_ ( Generic[T] ): '''simple docstring''' def __init__( self : int ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 0 def __len__( self : Union[str, Any] ): return self.elements def __repr__( self : Optional[int] ): return str(self.heap ) def lowerCAmelCase_ ( self : List[str] ): # Check if the priority queue is empty return self.elements == 0 def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Add an element with given priority to the queue self.heap.append((elem, weight) ) SCREAMING_SNAKE_CASE_ = self.elements self.elements += 1 self._bubble_up(_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[0] self._bubble_down(_lowerCAmelCase ) return elem def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Update the weight of the given key SCREAMING_SNAKE_CASE_ = self.position_map[elem] SCREAMING_SNAKE_CASE_ = (elem, weight) if position > 0: SCREAMING_SNAKE_CASE_ = get_parent_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[parent_position] if parent_weight > weight: self._bubble_up(_lowerCAmelCase ) else: self._bubble_down(_lowerCAmelCase ) else: self._bubble_down(_lowerCAmelCase ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : T ): # Place a node at the proper position (upward movement) [to be used internally # only] SCREAMING_SNAKE_CASE_ = self.position_map[elem] if curr_pos == 0: return None SCREAMING_SNAKE_CASE_ = get_parent_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[curr_pos] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_up(_lowerCAmelCase ) return None def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : T ): # Place a node at the proper position (downward movement) [to be used # internally only] SCREAMING_SNAKE_CASE_ = self.position_map[elem] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[curr_pos] SCREAMING_SNAKE_CASE_ = get_child_left_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = get_child_right_position(_lowerCAmelCase ) if child_left_position < self.elements and child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_left_position] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) if child_left_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) else: return None if child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) return None def lowerCAmelCase_ ( self : int , _lowerCAmelCase : int , _lowerCAmelCase : int ): # Swap the nodes at the given positions SCREAMING_SNAKE_CASE_ = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_ = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = ( self.heap[nodea_pos], self.heap[nodea_pos], ) SCREAMING_SNAKE_CASE_ = nodea_pos SCREAMING_SNAKE_CASE_ = nodea_pos class lowerCamelCase_ ( Generic[T] ): '''simple docstring''' def __init__( self : int ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 0 def __repr__( self : Tuple ): return str(self.connections ) def __len__( self : str ): return self.nodes def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : T ): # Add a node in the graph if it is not in the graph if node not in self.connections: SCREAMING_SNAKE_CASE_ = {} self.nodes += 1 def lowerCAmelCase_ ( self : List[str] , _lowerCAmelCase : T , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Add an edge between 2 nodes in the graph self.add_node(_lowerCAmelCase ) self.add_node(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = weight SCREAMING_SNAKE_CASE_ = weight def UpperCAmelCase_ ( __UpperCAmelCase : GraphUndirectedWeighted[T] , ) -> tuple[dict[T, int], dict[T, T | None]]: SCREAMING_SNAKE_CASE_ = {node: maxsize for node in graph.connections} SCREAMING_SNAKE_CASE_ = {node: None for node in graph.connections} SCREAMING_SNAKE_CASE_ = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(__UpperCAmelCase , __UpperCAmelCase ) if priority_queue.is_empty(): return dist, parent # initialization SCREAMING_SNAKE_CASE_ = priority_queue.extract_min() SCREAMING_SNAKE_CASE_ = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_ = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(__UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_ = node # running prim's algorithm while not priority_queue.is_empty(): SCREAMING_SNAKE_CASE_ = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_ = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(__UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_ = node return dist, parent
225
import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'vocab_file': 'vocab.json', 'tokenizer_config_file': 'tokenizer_config.json', 'merges_file': 'merges.txt', } lowerCamelCase__ : Union[str, Any] = { 'vocab_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json' ), }, 'tokenizer_config_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json' ), }, 'merges_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt' ), }, } lowerCamelCase__ : Optional[Any] = '</w>' lowerCamelCase__ : Union[str, Any] = '@@ ' def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = set() SCREAMING_SNAKE_CASE_ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_ = char return pairs # Speech2Text2 has no max input length lowerCamelCase__ : Any = {'facebook/s2t-wav2vec2-large-en-de': 1_024} class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["input_ids", "attention_mask"] def __init__( self : List[str] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any]="<s>" , _lowerCAmelCase : Any="<pad>" , _lowerCAmelCase : List[str]="</s>" , _lowerCAmelCase : int="<unk>" , _lowerCAmelCase : Optional[Any]=False , _lowerCAmelCase : Dict=None , **_lowerCAmelCase : Tuple , ): super().__init__( unk_token=_lowerCAmelCase , bos_token=_lowerCAmelCase , eos_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , **_lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = do_lower_case with open(_lowerCAmelCase , encoding='utf-8' ) as vocab_handle: SCREAMING_SNAKE_CASE_ = json.load(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(F"No merges files provided. {self.__class__.__name__} can only be used for decoding." ) SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None else: with open(_lowerCAmelCase , encoding='utf-8' ) as merges_handle: SCREAMING_SNAKE_CASE_ = merges_handle.read().split('\n' )[:-1] SCREAMING_SNAKE_CASE_ = [tuple(merge.split()[:2] ) for merge in merges] SCREAMING_SNAKE_CASE_ = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = {} @property def lowerCAmelCase_ ( self : List[str] ): return len(self.decoder ) def lowerCAmelCase_ ( self : Tuple ): return dict(self.encoder , **self.added_tokens_encoder ) def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : Tuple ): SCREAMING_SNAKE_CASE_ = tuple(token[:-1] ) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_ = get_pairs(_lowerCAmelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE_ = min(_lowerCAmelCase , key=lambda _lowerCAmelCase : self.bpe_ranks.get(_lowerCAmelCase , float('inf' ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = bigram SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 0 while i < len(_lowerCAmelCase ): try: SCREAMING_SNAKE_CASE_ = word.index(_lowerCAmelCase , _lowerCAmelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE_ = j if word[i] == first and i < len(_lowerCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE_ = tuple(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = new_word if len(_lowerCAmelCase ) == 1: break else: SCREAMING_SNAKE_CASE_ = get_pairs(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = ' '.join(_lowerCAmelCase ) if word == "\n " + BPE_TOKEN_MERGES: SCREAMING_SNAKE_CASE_ = '\n' + BPE_TOKEN_MERGES if word.endswith(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = word.replace(_lowerCAmelCase , '' ) SCREAMING_SNAKE_CASE_ = word.replace(' ' , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = word return word def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : Optional[int] ): if self.bpe_ranks is None: raise ValueError( 'This tokenizer was instantiated without a `merges.txt` file, so' ' that it can only be used for decoding, not for encoding.' 'Make sure to provide `merges.txt` file at instantiation to enable ' 'encoding.' ) if self.do_lower_case: SCREAMING_SNAKE_CASE_ = text.lower() SCREAMING_SNAKE_CASE_ = text.split() SCREAMING_SNAKE_CASE_ = [] for token in text: if token: split_tokens.extend(list(self.bpe(_lowerCAmelCase ).split(' ' ) ) ) return split_tokens def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : str ): return self.encoder.get(_lowerCAmelCase , self.encoder.get(self.unk_token ) ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = self.decoder.get(_lowerCAmelCase , self.unk_token ) return result def lowerCAmelCase_ ( self : int , _lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = ' '.join(_lowerCAmelCase ) # make sure @@ tokens are concatenated SCREAMING_SNAKE_CASE_ = ''.join(string.split(_lowerCAmelCase ) ) return string def lowerCAmelCase_ ( self : int , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(_lowerCAmelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ = os.path.join( _lowerCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(_lowerCAmelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_lowerCAmelCase , ensure_ascii=_lowerCAmelCase ) + '\n' ) SCREAMING_SNAKE_CASE_ = 0 if self.bpe_ranks is None: return (vocab_file,) with open(_lowerCAmelCase , 'w' , encoding='utf-8' ) as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _lowerCAmelCase : kv[1] ): if index != token_index: logger.warning( F"Saving vocabulary to {merges_file}: BPE merge indices are not consecutive." ' Please check that the tokenizer is not corrupted!' ) SCREAMING_SNAKE_CASE_ = token_index writer.write(' '.join(_lowerCAmelCase ) + '\n' ) index += 1 return (vocab_file, merges_file)
225
1
from __future__ import annotations def __lowerCamelCase ( __a :list[int | float] , __a :int , __a :int ) -> int | float: """simple docstring""" if len(__a ) == 0: raise ValueError("""find_max() arg is an empty sequence""" ) if ( left >= len(__a ) or left < -len(__a ) or right >= len(__a ) or right < -len(__a ) ): raise IndexError("""list index out of range""" ) if left == right: return nums[left] A__ = (left + right) >> 1 # the middle A__ = find_max(__a , __a , __a ) # find max in range[left, mid] A__ = find_max(__a , mid + 1 , __a ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
276
from maths.is_square_free import is_square_free from maths.prime_factors import prime_factors def __lowerCamelCase ( __a :int ) -> int: """simple docstring""" A__ = prime_factors(__a ) if is_square_free(__a ): return -1 if len(__a ) % 2 else 1 return 0 if __name__ == "__main__": import doctest doctest.testmod()
276
1
def _a ( SCREAMING_SNAKE_CASE : int = 4_00_00_00 ) -> int: """simple docstring""" __lowerCAmelCase: str = [] __lowerCAmelCase , __lowerCAmelCase: Union[str, Any] = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(SCREAMING_SNAKE_CASE ) __lowerCAmelCase , __lowerCAmelCase: Dict = b, a + b return sum(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(f"{solution() = }")
322
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A_ ( unittest.TestCase ): def __init__( self : List[str] , UpperCAmelCase : Tuple , UpperCAmelCase : Optional[Any]=1_3 , UpperCAmelCase : Optional[int]=7 , UpperCAmelCase : Tuple=True , UpperCAmelCase : str=True , UpperCAmelCase : List[Any]=True , UpperCAmelCase : Union[str, Any]=True , UpperCAmelCase : List[str]=9_9 , UpperCAmelCase : Optional[int]=3_2 , UpperCAmelCase : Dict=5 , UpperCAmelCase : int=4 , UpperCAmelCase : Optional[Any]=3_7 , UpperCAmelCase : List[str]="gelu" , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : int=0.1 , UpperCAmelCase : str=5_1_2 , UpperCAmelCase : Dict=1_6 , UpperCAmelCase : Union[str, Any]=2 , UpperCAmelCase : int=0.02 , UpperCAmelCase : List[Any]=4 , ) -> Optional[Any]: __lowerCAmelCase: str = parent __lowerCAmelCase: Dict = batch_size __lowerCAmelCase: Optional[int] = seq_length __lowerCAmelCase: Dict = is_training __lowerCAmelCase: Optional[Any] = use_attention_mask __lowerCAmelCase: List[Any] = use_token_type_ids __lowerCAmelCase: Optional[int] = use_labels __lowerCAmelCase: Optional[Any] = vocab_size __lowerCAmelCase: Optional[Any] = hidden_size __lowerCAmelCase: Tuple = num_hidden_layers __lowerCAmelCase: List[str] = num_attention_heads __lowerCAmelCase: int = intermediate_size __lowerCAmelCase: Union[str, Any] = hidden_act __lowerCAmelCase: List[Any] = hidden_dropout_prob __lowerCAmelCase: List[str] = attention_probs_dropout_prob __lowerCAmelCase: Optional[int] = max_position_embeddings __lowerCAmelCase: Union[str, Any] = type_vocab_size __lowerCAmelCase: int = type_sequence_label_size __lowerCAmelCase: Union[str, Any] = initializer_range __lowerCAmelCase: Any = num_choices def UpperCAmelCase ( self : Union[str, Any] ) -> Tuple: __lowerCAmelCase: List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase: List[Any] = None if self.use_attention_mask: __lowerCAmelCase: List[str] = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase: Optional[Any] = None if self.use_token_type_ids: __lowerCAmelCase: List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase: Optional[int] = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self : Dict ) -> Any: __lowerCAmelCase: Optional[int] = self.prepare_config_and_inputs() __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase: Optional[Any] = config_and_inputs __lowerCAmelCase: Tuple = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class A_ ( snake_case__ , unittest.TestCase ): _lowercase : Dict = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def UpperCAmelCase ( self : List[str] ) -> Optional[int]: __lowerCAmelCase: List[Any] = FlaxAlbertModelTester(self ) @slow def UpperCAmelCase ( self : Tuple ) -> Dict: for model_class_name in self.all_model_classes: __lowerCAmelCase: Optional[Any] = model_class_name.from_pretrained('albert-base-v2' ) __lowerCAmelCase: Dict = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCAmelCase ) @require_flax class A_ ( unittest.TestCase ): @slow def UpperCAmelCase ( self : Any ) -> Any: __lowerCAmelCase: List[Any] = FlaxAlbertModel.from_pretrained('albert-base-v2' ) __lowerCAmelCase: Optional[int] = np.array([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) __lowerCAmelCase: Tuple = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) __lowerCAmelCase: Tuple = model(UpperCAmelCase , attention_mask=UpperCAmelCase )[0] __lowerCAmelCase: str = (1, 1_1, 7_6_8) self.assertEqual(output.shape , UpperCAmelCase ) __lowerCAmelCase: List[str] = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , UpperCAmelCase , atol=1E-4 ) )
322
1
import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: lowerCamelCase_ = None lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCamelCase_ = { '''vocab_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/spiece.model''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/spiece.model''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/spiece.model''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/spiece.model''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/tokenizer.json''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/tokenizer.json''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/tokenizer.json''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/tokenizer.json''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/tokenizer.json''', }, } # TODO(PVP) - this should be removed in Transformers v5 lowerCamelCase_ = { '''t5-small''': 5_12, '''t5-base''': 5_12, '''t5-large''': 5_12, '''t5-3b''': 5_12, '''t5-11b''': 5_12, } class __A( a_ ): """simple docstring""" SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = ['''input_ids''', '''attention_mask'''] SCREAMING_SNAKE_CASE__ = TaTokenizer SCREAMING_SNAKE_CASE__ = [] def __init__(self , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_="</s>" , SCREAMING_SNAKE_CASE_="<unk>" , SCREAMING_SNAKE_CASE_="<pad>" , SCREAMING_SNAKE_CASE_=1_00 , SCREAMING_SNAKE_CASE_=None , **SCREAMING_SNAKE_CASE_ , ): if extra_ids > 0 and additional_special_tokens is None: UpperCamelCase__ = [F"<extra_id_{i}>" for i in range(lowercase_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens UpperCamelCase__ = len(set(filter(lambda SCREAMING_SNAKE_CASE_ : bool("""extra_id_""" in str(lowercase_ ) ) , lowercase_ ) ) ) if extra_tokens != extra_ids: raise ValueError( F"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids""" """ tokens""" ) super().__init__( lowercase_ , tokenizer_file=lowercase_ , eos_token=lowercase_ , unk_token=lowercase_ , pad_token=lowercase_ , extra_ids=lowercase_ , additional_special_tokens=lowercase_ , **lowercase_ , ) UpperCamelCase__ = vocab_file UpperCamelCase__ = False if not self.vocab_file else True UpperCamelCase__ = extra_ids @staticmethod def UpperCAmelCase_ (SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: UpperCamelCase__ = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( """This tokenizer was incorrectly instantiated with a model max length of""" F" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this" """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with""" """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on""" F" {pretrained_model_name_or_path} automatically truncating your input to" F" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences" F" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with" """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please""" """ instantiate this tokenizer with `model_max_length` set to your preferred value.""" , lowercase_ , ) return max_model_length def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(lowercase_ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return UpperCamelCase__ = os.path.join( lowercase_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ): copyfile(self.vocab_file , lowercase_ ) logger.info(F"Copy vocab file to {out_vocab_file}" ) return (out_vocab_file,) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): UpperCamelCase__ = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: UpperCamelCase__ = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): UpperCamelCase__ = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def UpperCAmelCase_ (self ): return list( set(filter(lambda SCREAMING_SNAKE_CASE_ : bool(re.search(r"""<extra_id_\d+>""" , lowercase_ ) ) is not None , self.additional_special_tokens ) ) ) def UpperCAmelCase_ (self ): return [self.convert_tokens_to_ids(lowercase_ ) for token in self.get_sentinel_tokens()]
364
from typing import TYPE_CHECKING from ...utils import _LazyModule lowerCamelCase_ = {'''processing_wav2vec2_with_lm''': ['''Wav2Vec2ProcessorWithLM''']} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
178
0
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets a_ = '''\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } ''' a_ = '''\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. ''' a_ = ''' Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: "accuracy": Accuracy "f1": F1 score "pearson": Pearson Correlation "spearmanr": Spearman Correlation "matthews_correlation": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\') # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\') # \'mrpc\' or \'qqp\' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)}) {\'pearson\': 1.0, \'spearmanr\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'cola\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def lowerCamelCase__ ( _a , _a): return float((preds == labels).mean()) def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : Optional[int] = simple_accuracy(_a , _a) SCREAMING_SNAKE_CASE : Tuple = float(fa_score(y_true=_a , y_pred=_a)) return { "accuracy": acc, "f1": fa, } def lowerCamelCase__ ( _a , _a): SCREAMING_SNAKE_CASE : Optional[Any] = float(pearsonr(_a , _a)[0]) SCREAMING_SNAKE_CASE : Tuple = float(spearmanr(_a , _a)[0]) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _UpperCamelCase ( datasets.Metric ): '''simple docstring''' def __UpperCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( "You should supply a configuration name selected in " "[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", " "\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int64" if self.config_name != "stsb" else "float32" ), "references": datasets.Value("int64" if self.config_name != "stsb" else "float32" ), } ) , codebase_urls=[] , reference_urls=[] , format="numpy" , ) def __UpperCamelCase ( self : Tuple , a : List[Any] , a : int ) -> int: """simple docstring""" if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(_A , _A )} elif self.config_name == "stsb": return pearson_and_spearman(_A , _A ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(_A , _A ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(_A , _A )} else: raise KeyError( "You should supply a configuration name selected in " "[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", " "\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]" )
76
def _SCREAMING_SNAKE_CASE ( a , a ) -> list[int]: __A : Optional[int] = int(a ) # Initialize Result __A : Optional[int] = [] # Traverse through all denomination for denomination in reversed(a ): # Find denominations while int(a ) >= int(a ): total_value -= int(a ) answer.append(a ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": UpperCAmelCase : List[str] = [] UpperCAmelCase : Optional[int] = '''0''' if ( input('''Do you want to enter your denominations ? (yY/n): ''').strip().lower() == "y" ): UpperCAmelCase : List[Any] = int(input('''Enter the number of denominations you want to add: ''').strip()) for i in range(0, n): denominations.append(int(input(F"""Denomination {i}: """).strip())) UpperCAmelCase : int = input('''Enter the change you want to make in Indian Currency: ''').strip() else: # All denominations of Indian Currency if user does not enter UpperCAmelCase : Optional[int] = [1, 2, 5, 10, 20, 50, 1_00, 5_00, 20_00] UpperCAmelCase : Tuple = input('''Enter the change you want to make: ''').strip() if int(value) == 0 or int(value) < 0: print('''The total value cannot be zero or negative.''') else: print(F"""Following is minimal change for {value}: """) UpperCAmelCase : Optional[int] = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=''' ''')
280
0
"""simple docstring""" import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py lowercase__ : Dict = """src/diffusers""" lowercase__ : Optional[Any] = """.""" # This is to make sure the diffusers module imported is the one in the repo. lowercase__ : List[Any] = importlib.util.spec_from_file_location( """diffusers""", os.path.join(DIFFUSERS_PATH, """__init__.py"""), submodule_search_locations=[DIFFUSERS_PATH], ) lowercase__ : Union[str, Any] = spec.loader.load_module() def UpperCamelCase_ ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict ) -> List[str]: """simple docstring""" return line.startswith(lowerCAmelCase__ ) or len(lowerCAmelCase__ ) <= 1 or re.search(R'^\s*\)(\s*->.*:|:)\s*$' , lowerCAmelCase__ ) is not None def UpperCamelCase_ ( lowerCAmelCase__ : List[Any] ) -> Optional[Any]: """simple docstring""" lowerCAmelCase_ : Dict = object_name.split('.' ) lowerCAmelCase_ : Any = 0 # First let's find the module where our object lives. lowerCAmelCase_ : Optional[Any] = parts[i] while i < len(lowerCAmelCase__ ) and not os.path.isfile(os.path.join(lowerCAmelCase__ , f"{module}.py" ) ): i += 1 if i < len(lowerCAmelCase__ ): lowerCAmelCase_ : Tuple = os.path.join(lowerCAmelCase__ , parts[i] ) if i >= len(lowerCAmelCase__ ): raise ValueError(f"`object_name` should begin with the name of a module of diffusers but got {object_name}." ) with open(os.path.join(lowerCAmelCase__ , f"{module}.py" ) , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCAmelCase_ : Optional[int] = f.readlines() # Now let's find the class / func in the code! lowerCAmelCase_ : Any = '' lowerCAmelCase_ : List[Any] = 0 for name in parts[i + 1 :]: while ( line_index < len(lowerCAmelCase__ ) and re.search(Rf"^{indent}(class|def)\s+{name}(\(|\:)" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(lowerCAmelCase__ ): raise ValueError(f" {object_name} does not match any function or class in {module}." ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). lowerCAmelCase_ : Any = line_index while line_index < len(lowerCAmelCase__ ) and _should_continue(lines[line_index] , lowerCAmelCase__ ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCAmelCase_ : Dict = lines[start_index:line_index] return "".join(lowerCAmelCase__ ) lowercase__ : str = re.compile(R"""^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)""") lowercase__ : Optional[int] = re.compile(R"""^\s*(\S+)->(\S+)(\s+.*|$)""") lowercase__ : Dict = re.compile(R"""<FILL\s+[^>]*>""") def UpperCamelCase_ ( lowerCAmelCase__ : str ) -> List[str]: """simple docstring""" lowerCAmelCase_ : List[Any] = code.split('\n' ) lowerCAmelCase_ : Union[str, Any] = 0 while idx < len(lowerCAmelCase__ ) and len(lines[idx] ) == 0: idx += 1 if idx < len(lowerCAmelCase__ ): return re.search(R'^(\s*)\S' , lines[idx] ).groups()[0] return "" def UpperCamelCase_ ( lowerCAmelCase__ : Dict ) -> Dict: """simple docstring""" lowerCAmelCase_ : List[Any] = len(get_indent(lowerCAmelCase__ ) ) > 0 if has_indent: lowerCAmelCase_ : List[str] = f"class Bla:\n{code}" lowerCAmelCase_ : Tuple = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=lowerCAmelCase__ ) lowerCAmelCase_ : Dict = black.format_str(lowerCAmelCase__ , mode=lowerCAmelCase__ ) lowerCAmelCase_ ,lowerCAmelCase_ : Dict = style_docstrings_in_code(lowerCAmelCase__ ) return result[len('class Bla:\n' ) :] if has_indent else result def UpperCamelCase_ ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Union[str, Any]=False ) -> str: """simple docstring""" with open(lowerCAmelCase__ , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCAmelCase_ : Dict = f.readlines() lowerCAmelCase_ : Any = [] lowerCAmelCase_ : str = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(lowerCAmelCase__ ): lowerCAmelCase_ : int = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. lowerCAmelCase_ ,lowerCAmelCase_ ,lowerCAmelCase_ : List[str] = search.groups() lowerCAmelCase_ : str = find_code_in_diffusers(lowerCAmelCase__ ) lowerCAmelCase_ : Tuple = get_indent(lowerCAmelCase__ ) lowerCAmelCase_ : Optional[int] = line_index + 1 if indent == theoretical_indent else line_index + 2 lowerCAmelCase_ : Dict = theoretical_indent lowerCAmelCase_ : Dict = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. lowerCAmelCase_ : Tuple = True while line_index < len(lowerCAmelCase__ ) and should_continue: line_index += 1 if line_index >= len(lowerCAmelCase__ ): break lowerCAmelCase_ : Optional[int] = lines[line_index] lowerCAmelCase_ : int = _should_continue(lowerCAmelCase__ , lowerCAmelCase__ ) and re.search(f"^{indent}# End copy" , lowerCAmelCase__ ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCAmelCase_ : str = lines[start_index:line_index] lowerCAmelCase_ : str = ''.join(lowerCAmelCase__ ) # Remove any nested `Copied from` comments to avoid circular copies lowerCAmelCase_ : Tuple = [line for line in theoretical_code.split('\n' ) if _re_copy_warning.search(lowerCAmelCase__ ) is None] lowerCAmelCase_ : Union[str, Any] = '\n'.join(lowerCAmelCase__ ) # Before comparing, use the `replace_pattern` on the original code. if len(lowerCAmelCase__ ) > 0: lowerCAmelCase_ : List[str] = replace_pattern.replace('with' , '' ).split(',' ) lowerCAmelCase_ : int = [_re_replace_pattern.search(lowerCAmelCase__ ) for p in patterns] for pattern in patterns: if pattern is None: continue lowerCAmelCase_ ,lowerCAmelCase_ ,lowerCAmelCase_ : Optional[int] = pattern.groups() lowerCAmelCase_ : Union[str, Any] = re.sub(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if option.strip() == "all-casing": lowerCAmelCase_ : int = re.sub(obja.lower() , obja.lower() , lowerCAmelCase__ ) lowerCAmelCase_ : Optional[int] = re.sub(obja.upper() , obja.upper() , lowerCAmelCase__ ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line lowerCAmelCase_ : int = blackify(lines[start_index - 1] + theoretical_code ) lowerCAmelCase_ : List[Any] = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: lowerCAmelCase_ : str = lines[:start_index] + [theoretical_code] + lines[line_index:] lowerCAmelCase_ : Optional[Any] = start_index + 1 if overwrite and len(lowerCAmelCase__ ) > 0: # Warn the user a file has been modified. print(f"Detected changes, rewriting {filename}." ) with open(lowerCAmelCase__ , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(lowerCAmelCase__ ) return diffs def UpperCamelCase_ ( lowerCAmelCase__ : bool = False ) -> Union[str, Any]: """simple docstring""" lowerCAmelCase_ : List[str] = glob.glob(os.path.join(lowerCAmelCase__ , '**/*.py' ) , recursive=lowerCAmelCase__ ) lowerCAmelCase_ : str = [] for filename in all_files: lowerCAmelCase_ : Union[str, Any] = is_copy_consistent(lowerCAmelCase__ , lowerCAmelCase__ ) diffs += [f"- {filename}: copy does not match {d[0]} at line {d[1]}" for d in new_diffs] if not overwrite and len(lowerCAmelCase__ ) > 0: lowerCAmelCase_ : Dict = '\n'.join(lowerCAmelCase__ ) raise Exception( 'Found the following copy inconsistencies:\n' + diff + '\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.' ) if __name__ == "__main__": lowercase__ : Optional[int] = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") lowercase__ : Any = parser.parse_args() check_copies(args.fix_and_overwrite)
289
"""simple docstring""" from __future__ import annotations def UpperCamelCase_ ( lowerCAmelCase__ : int ) -> list[int]: """simple docstring""" lowerCAmelCase_ : Any = 2 lowerCAmelCase_ : List[Any] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(lowerCAmelCase__ ) if n > 1: factors.append(lowerCAmelCase__ ) return factors if __name__ == "__main__": import doctest doctest.testmod()
289
1