code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel from diffusers.pipelines.pipeline_utils import DiffusionPipeline from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( enable_full_determinism, floats_tensor, load_image, load_numpy, require_torch_gpu, skip_mps, slow, torch_device, ) from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class a ( _lowerCamelCase, _lowerCamelCase, _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = StableUnCLIPImgaImgPipeline UpperCAmelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS UpperCAmelCase = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS UpperCAmelCase = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCAmelCase = frozenset([] ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = 32 A__ = embedder_hidden_size # image encoding components A__ = CLIPImageProcessor(crop_size=32 , size=32 ) torch.manual_seed(0 ) A__ = CLIPVisionModelWithProjection( CLIPVisionConfig( hidden_size=UpperCamelCase , projection_dim=UpperCamelCase , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) ) # regular denoising components torch.manual_seed(0 ) A__ = StableUnCLIPImageNormalizer(embedding_dim=UpperCamelCase ) A__ = DDPMScheduler(beta_schedule="""squaredcos_cap_v2""" ) torch.manual_seed(0 ) A__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) torch.manual_seed(0 ) A__ = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=UpperCamelCase , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) ) torch.manual_seed(0 ) A__ = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""CrossAttnDownBlock2D""", """DownBlock2D""") , up_block_types=("""UpBlock2D""", """CrossAttnUpBlock2D""") , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type="""projection""" , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=UpperCamelCase , layers_per_block=1 , upcast_attention=UpperCamelCase , use_linear_projection=UpperCamelCase , ) torch.manual_seed(0 ) A__ = DDIMScheduler( beta_schedule="""scaled_linear""" , beta_start=0.00_085 , beta_end=0.012 , prediction_type="""v_prediction""" , set_alpha_to_one=UpperCamelCase , steps_offset=1 , ) torch.manual_seed(0 ) A__ = AutoencoderKL() A__ = { # image encoding components """feature_extractor""": feature_extractor, """image_encoder""": image_encoder.eval(), # image noising components """image_normalizer""": image_normalizer.eval(), """image_noising_scheduler""": image_noising_scheduler, # regular denoising components """tokenizer""": tokenizer, """text_encoder""": text_encoder.eval(), """unet""": unet.eval(), """scheduler""": scheduler, """vae""": vae.eval(), } return components def UpperCamelCase ( self: Any , UpperCamelCase: Optional[int] , UpperCamelCase: str=0 , UpperCamelCase: int=True ): """simple docstring""" if str(UpperCamelCase ).startswith("""mps""" ): A__ = torch.manual_seed(UpperCamelCase ) else: A__ = torch.Generator(device=UpperCamelCase ).manual_seed(UpperCamelCase ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(UpperCamelCase ) ).to(UpperCamelCase ) if pil_image: A__ = input_image * 0.5 + 0.5 A__ = input_image.clamp(0 , 1 ) A__ = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() A__ = DiffusionPipeline.numpy_to_pil(UpperCamelCase )[0] return { "prompt": "An anime racoon running a marathon", "image": input_image, "generator": generator, "num_inference_steps": 2, "output_type": "np", } @skip_mps def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = """cpu""" # ensure determinism for the device-dependent torch.Generator A__ = self.get_dummy_components() A__ = StableUnCLIPImgaImgPipeline(**UpperCamelCase ) A__ = sd_pipe.to(UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = self.get_dummy_inputs(UpperCamelCase ) inputs.update({"""image_embeds""": None} ) A__ = sd_pipe(**UpperCamelCase ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A__ = np.array([0.3_872, 0.7_224, 0.5_601, 0.4_741, 0.6_872, 0.5_814, 0.4_636, 0.3_867, 0.5_078] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCamelCase ( self: Any ): """simple docstring""" A__ = torch_device in ["""cpu""", """mps"""] self._test_attention_slicing_forward_pass(test_max_difference=UpperCamelCase ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = torch_device in ["""cpu""", """mps"""] self._test_inference_batch_single_identical(test_max_difference=UpperCamelCase ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(test_max_difference=UpperCamelCase ) @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png""" ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy""" ) A__ = StableUnCLIPImgaImgPipeline.from_pretrained( """fusing/stable-unclip-2-1-l-img2img""" , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) # 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() A__ = torch.Generator(device="""cpu""" ).manual_seed(0 ) A__ = pipe(UpperCamelCase , """anime turle""" , generator=UpperCamelCase , output_type="""np""" ) A__ = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png""" ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy""" ) A__ = StableUnCLIPImgaImgPipeline.from_pretrained( """fusing/stable-unclip-2-1-h-img2img""" , torch_dtype=torch.floataa ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) # 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() A__ = torch.Generator(device="""cpu""" ).manual_seed(0 ) A__ = pipe(UpperCamelCase , """anime turle""" , generator=UpperCamelCase , output_type="""np""" ) A__ = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png""" ) torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() A__ = StableUnCLIPImgaImgPipeline.from_pretrained( """fusing/stable-unclip-2-1-h-img2img""" , torch_dtype=torch.floataa ) A__ = pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() A__ = pipe( UpperCamelCase , """anime turtle""" , num_inference_steps=2 , output_type="""np""" , ) A__ = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
335
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
1
"""simple docstring""" import sys SCREAMING_SNAKE_CASE_ : List[Any] = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _snake_case ( UpperCAmelCase_ : str ): A__ = 1 for digit in s: product *= int(UpperCAmelCase_ ) return product def _snake_case ( UpperCAmelCase_ : str = N ): A__ = -sys.maxsize - 1 A__ = n[:13] A__ = 13 while cur_index < len(UpperCAmelCase_ ) - 13: if int(n[cur_index] ) >= int(substr[0] ): A__ = substr[1:] + n[cur_index] cur_index += 1 else: A__ = max(UpperCAmelCase_ , str_eval(UpperCAmelCase_ ) ) A__ = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
335
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE_ : int = [ 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the' ' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe' ' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.', 'The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal' ' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s' ' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the' ' body.', 'Amnesty International releases its annual report on the death penalty. The report catalogs the use of' ' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the' ' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital' ' punishment.', ] SCREAMING_SNAKE_CASE_ : List[Any] = [ 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .' ' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz' ' had informed his Lufthansa training school of an episode of severe depression, airline says .', 'Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .' ' Israel and the United States opposed the move, which could open the door to war crimes investigations against' ' Israelis .', 'Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to' ' death . Organization claims that governments around the world are using the threat of terrorism to advance' ' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death' ' sentences up by 28% .', ] def _snake_case ( ): A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2""", """rougeL"""] ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2"""] ) assert ( pd.DataFrame(no_aggregation["""rouge2"""] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["""rouge2"""] ).fmeasure.mean() ) def _snake_case ( ): A__ = """rougeLsum""" A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _snake_case ( ): A__ = ["""rouge1""", """rouge2""", """rougeL"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) assert score_sep == score_no_sep def _snake_case ( ): A__ = [ """Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.""", """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""", ] A__ = [ """Margot Frank, died in 1945, a month earlier than previously thought.""", """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of""" """ the final seconds on board Flight 9525.""", ] assert calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) == calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) def _snake_case ( ): A__ = [ """\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" """ ] A__ = [ """ Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .""" ] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] , newline_sep=UpperCAmelCase_ )["""rougeLsum"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] )["""rougeLsum"""] assert new_score > prev_score def _snake_case ( ): A__ = Path("""examples/seq2seq/test_data/wmt_en_ro""" ) A__ = calculate_rouge_path(data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge_path( data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) , bootstrap_aggregation=UpperCAmelCase_ ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING SCREAMING_SNAKE_CASE_ : List[str] = logging.get_logger(__name__) @add_end_docstrings(_lowerCamelCase ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Tuple , *UpperCamelCase: Tuple , **UpperCamelCase: List[Any] ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) requires_backends(self , """vision""" ) self.check_model_type(UpperCamelCase ) def __call__( self: Tuple , UpperCamelCase: Union[str, List[str], "Image.Image", List["Image.Image"]] , **UpperCamelCase: List[str] ): """simple docstring""" return super().__call__(UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: int , **UpperCamelCase: List[str] ): """simple docstring""" return {}, {}, {} def UpperCamelCase ( self: Any , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = load_image(UpperCamelCase ) A__ = image.size A__ = self.image_processor(images=UpperCamelCase , return_tensors=self.framework ) return model_inputs def UpperCamelCase ( self: List[str] , UpperCamelCase: List[str] ): """simple docstring""" A__ = self.model(**UpperCamelCase ) return model_outputs def UpperCamelCase ( self: List[str] , UpperCamelCase: Tuple ): """simple docstring""" A__ = model_outputs.predicted_depth A__ = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode="""bicubic""" , align_corners=UpperCamelCase ) A__ = prediction.squeeze().cpu().numpy() A__ = (output * 2_55 / np.max(UpperCamelCase )).astype("""uint8""" ) A__ = Image.fromarray(UpperCamelCase ) A__ = {} A__ = predicted_depth A__ = depth return output_dict
335
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'MobileNetV1Config' # Base docstring SCREAMING_SNAKE_CASE_ : str = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : List[str] = [1, 1_0_2_4, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ : Optional[Any] = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : Tuple = 'tabby, tabby cat' SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None ): A__ = {} if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = model.mobilenet_va else: A__ = model A__ = """MobilenetV1/Conv2d_0/""" A__ = backbone.conv_stem.convolution.weight A__ = backbone.conv_stem.normalization.bias A__ = backbone.conv_stem.normalization.weight A__ = backbone.conv_stem.normalization.running_mean A__ = backbone.conv_stem.normalization.running_var for i in range(13 ): A__ = i + 1 A__ = i * 2 A__ = backbone.layer[pt_index] A__ = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var A__ = backbone.layer[pt_index + 1] A__ = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = """MobilenetV1/Logits/Conv2d_1c_1x1/""" A__ = model.classifier.weight A__ = model.classifier.bias return tf_to_pt_map def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model A__ = tf.train.list_variables(UpperCAmelCase_ ) A__ = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) A__ = tf.train.load_variable(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = array # Build TF to PyTorch weights loading map A__ = _build_tf_to_pytorch_map(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue A__ = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) A__ = np.transpose(UpperCAmelCase_ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer A__ = array.squeeze().transpose() else: A__ = np.transpose(UpperCAmelCase_ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) A__ = torch.from_numpy(UpperCAmelCase_ ) tf_weights.pop(UpperCAmelCase_ , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp""" , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp_1""" , UpperCAmelCase_ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , UpperCAmelCase_ ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _snake_case ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : nn.Convad ): A__ , A__ = features.shape[-2:] A__ , A__ = conv_layer.stride A__ , A__ = conv_layer.kernel_size if in_height % stride_height == 0: A__ = max(kernel_height - stride_height , 0 ) else: A__ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: A__ = max(kernel_width - stride_width , 0 ) else: A__ = max(kernel_width - (in_width % stride_width) , 0 ) A__ = pad_along_width // 2 A__ = pad_along_width - pad_left A__ = pad_along_height // 2 A__ = pad_along_height - pad_top A__ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(UpperCAmelCase_ , UpperCAmelCase_ , """constant""" , 0.0 ) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: bool = False , UpperCamelCase: Optional[bool] = True , UpperCamelCase: Optional[bool or str] = True , ): """simple docstring""" super().__init__() A__ = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) A__ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) A__ = nn.Convad( in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=UpperCamelCase , stride=UpperCamelCase , padding=UpperCamelCase , groups=UpperCamelCase , bias=UpperCamelCase , padding_mode="""zeros""" , ) if use_normalization: A__ = nn.BatchNormad( num_features=UpperCamelCase , eps=config.layer_norm_eps , momentum=0.9_997 , affine=UpperCamelCase , track_running_stats=UpperCamelCase , ) else: A__ = None if use_activation: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = ACTaFN[use_activation] elif isinstance(config.hidden_act , UpperCamelCase ): A__ = ACTaFN[config.hidden_act] else: A__ = config.hidden_act else: A__ = None def UpperCamelCase ( self: List[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" if self.config.tf_padding: A__ = apply_tf_padding(UpperCamelCase , self.convolution ) A__ = self.convolution(UpperCamelCase ) if self.normalization is not None: A__ = self.normalization(UpperCamelCase ) if self.activation is not None: A__ = self.activation(UpperCamelCase ) return features class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = MobileNetVaConfig UpperCAmelCase = load_tf_weights_in_mobilenet_va UpperCAmelCase = "mobilenet_v1" UpperCAmelCase = "pixel_values" UpperCAmelCase = False def UpperCamelCase ( self: Any , UpperCamelCase: Union[nn.Linear, nn.Convad] ): """simple docstring""" if isinstance(UpperCamelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(UpperCamelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: bool = True ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config A__ = 32 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) A__ = MobileNetVaConvLayer( UpperCamelCase , in_channels=config.num_channels , out_channels=UpperCamelCase , kernel_size=3 , stride=2 , ) A__ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] A__ = nn.ModuleList() for i in range(13 ): A__ = out_channels if strides[i] == 2 or i == 0: depth *= 2 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=3 , stride=strides[i] , groups=UpperCamelCase , ) ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=1 , ) ) A__ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) A__ = self.conv_stem(UpperCamelCase ) A__ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): A__ = layer_module(UpperCamelCase ) if output_hidden_states: A__ = all_hidden_states + (hidden_states,) A__ = hidden_states if self.pooler is not None: A__ = torch.flatten(self.pooler(UpperCamelCase ) , start_dim=1 ) else: A__ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase , pooler_output=UpperCamelCase , hidden_states=UpperCamelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = MobileNetVaModel(UpperCamelCase ) A__ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head A__ = nn.Dropout(config.classifier_dropout_prob , inplace=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.mobilenet_va(UpperCamelCase , output_hidden_states=UpperCamelCase , return_dict=UpperCamelCase ) A__ = outputs.pooler_output if return_dict else outputs[1] A__ = self.classifier(self.dropout(UpperCamelCase ) ) A__ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: A__ = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): A__ = """single_label_classification""" else: A__ = """multi_label_classification""" if self.config.problem_type == "regression": A__ = MSELoss() if self.num_labels == 1: A__ = loss_fct(logits.squeeze() , labels.squeeze() ) else: A__ = loss_fct(UpperCamelCase , UpperCamelCase ) elif self.config.problem_type == "single_label_classification": A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": A__ = BCEWithLogitsLoss() A__ = loss_fct(UpperCamelCase , UpperCamelCase ) if not return_dict: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=UpperCamelCase , logits=UpperCamelCase , hidden_states=outputs.hidden_states , )
335
1
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : set ): A__ , A__ = len(UpperCAmelCase_ ), len(grid[0] ) if ( min(UpperCAmelCase_ , UpperCAmelCase_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(UpperCAmelCase_ , row + 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , row - 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col + 1 , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col - 1 , UpperCAmelCase_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_m2m_100': ['M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP', 'M2M100Config', 'M2M100OnnxConfig'], 'tokenization_m2m_100': ['M2M100Tokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST', 'M2M100ForConditionalGeneration', 'M2M100Model', 'M2M100PreTrainedModel', ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): SCREAMING_SNAKE_CASE_ : str = 'pt' elif is_tf_available(): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'tf' else: SCREAMING_SNAKE_CASE_ : str = 'jax' class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = ByTaTokenizer UpperCAmelCase = False def UpperCamelCase ( self: Dict ): """simple docstring""" super().setUp() A__ = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def UpperCamelCase ( self: List[str] ): """simple docstring""" return ByTaTokenizer.from_pretrained("""google/byt5-small""" ) def UpperCamelCase ( self: Any , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return self.tokenizer_class.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: List[Any]=False , UpperCamelCase: List[str]=20 , UpperCamelCase: List[Any]=5 ): """simple docstring""" A__ = [] for i in range(len(UpperCamelCase ) ): try: A__ = tokenizer.decode([i] , clean_up_tokenization_spaces=UpperCamelCase ) except UnicodeDecodeError: pass toks.append((i, tok) ) A__ = list(filter(lambda UpperCamelCase : re.match(r"""^[ a-zA-Z]+$""" , t[1] ) , UpperCamelCase ) ) A__ = list(filter(lambda UpperCamelCase : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=UpperCamelCase ) , UpperCamelCase ) ) if max_length is not None and len(UpperCamelCase ) > max_length: A__ = toks[:max_length] if min_length is not None and len(UpperCamelCase ) < min_length and len(UpperCamelCase ) > 0: while len(UpperCamelCase ) < 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(UpperCamelCase , clean_up_tokenization_spaces=UpperCamelCase ) if " " not in output_txt and len(UpperCamelCase ) > 1: A__ = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=UpperCamelCase ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=UpperCamelCase ) ) if with_prefix_space: A__ = """ """ + output_txt A__ = tokenizer.encode(UpperCamelCase , add_special_tokens=UpperCamelCase ) return output_txt, output_ids def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = tokenizer(["""hi</s>""", """I went to the gym</s>""", """</s>"""] ) A__ = tokenizer(["""hi""", """I went to the gym""", """"""] ) self.assertListEqual(batch_with_eos_added["""input_ids"""] , batch_without_eos_added["""input_ids"""] ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = """Unicode €.""" A__ = tokenizer(UpperCamelCase ) A__ = [88, 1_13, 1_08, 1_02, 1_14, 1_03, 1_04, 35, 2_29, 1_33, 1_75, 49, 1] self.assertEqual(encoded["""input_ids"""] , UpperCamelCase ) # decoding A__ = tokenizer.decode(UpperCamelCase ) self.assertEqual(UpperCamelCase , """Unicode €.</s>""" ) A__ = tokenizer("""e è é ê ë""" ) A__ = [1_04, 35, 1_98, 1_71, 35, 1_98, 1_72, 35, 1_98, 1_73, 35, 1_98, 1_74, 1] self.assertEqual(encoded["""input_ids"""] , UpperCamelCase ) # decoding A__ = tokenizer.decode(UpperCamelCase ) self.assertEqual(UpperCamelCase , """e è é ê ë</s>""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) , """e è é ê ë</s>""" ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off A__ = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 1, 0] # fmt: on A__ = tokenizer(UpperCamelCase , padding=UpperCamelCase , return_tensors=UpperCamelCase ) self.assertIsInstance(UpperCamelCase , UpperCamelCase ) if FRAMEWORK != "jax": A__ = list(batch.input_ids.numpy()[0] ) else: A__ = list(batch.input_ids.tolist()[0] ) self.assertListEqual(UpperCamelCase , UpperCamelCase ) self.assertEqual((2, 37) , batch.input_ids.shape ) self.assertEqual((2, 37) , batch.attention_mask.shape ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] A__ = tokenizer(UpperCamelCase , padding=UpperCamelCase , return_tensors=UpperCamelCase ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" , UpperCamelCase ) self.assertIn("""attention_mask""" , UpperCamelCase ) self.assertNotIn("""decoder_input_ids""" , UpperCamelCase ) self.assertNotIn("""decoder_attention_mask""" , UpperCamelCase ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = [ """Summary of the text.""", """Another summary.""", ] A__ = tokenizer( text_target=UpperCamelCase , max_length=32 , padding="""max_length""" , truncation=UpperCamelCase , return_tensors=UpperCamelCase ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.ta_base_tokenizer A__ = ["""A long paragraph for summarization. </s>"""] A__ = ["""Summary of the text. </s>"""] # fmt: off A__ = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 35, 1] A__ = [86, 1_20, 1_12, 1_12, 1_00, 1_17, 1_24, 35, 1_14, 1_05, 35, 1_19, 1_07, 1_04, 35, 1_19, 1_04, 1_23, 1_19, 49, 35, 1] # fmt: on A__ = tokenizer(UpperCamelCase , text_target=UpperCamelCase ) self.assertEqual(UpperCamelCase , batch["""input_ids"""][0] ) self.assertEqual(UpperCamelCase , batch["""labels"""][0] ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): # Isolate this from the other tests because we save additional tokens/etc A__ = tempfile.mkdtemp() A__ = """ He is very happy, UNwant\u00E9d,running""" A__ = tokenizer.encode(UpperCamelCase , add_special_tokens=UpperCamelCase ) tokenizer.save_pretrained(UpperCamelCase ) A__ = tokenizer.__class__.from_pretrained(UpperCamelCase ) A__ = after_tokenizer.encode(UpperCamelCase , add_special_tokens=UpperCamelCase ) self.assertListEqual(UpperCamelCase , UpperCamelCase ) shutil.rmtree(UpperCamelCase ) A__ = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): # Isolate this from the other tests because we save additional tokens/etc A__ = tempfile.mkdtemp() A__ = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) A__ = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) A__ = tokenizer.encode(UpperCamelCase , add_special_tokens=UpperCamelCase ) tokenizer.save_pretrained(UpperCamelCase ) A__ = tokenizer.__class__.from_pretrained(UpperCamelCase ) A__ = after_tokenizer.encode(UpperCamelCase , add_special_tokens=UpperCamelCase ) self.assertListEqual(UpperCamelCase , UpperCamelCase ) self.assertIn("""new_additional_special_token""" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) A__ = tokenizer.__class__.from_pretrained(UpperCamelCase , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(UpperCamelCase ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(UpperCamelCase ) with open(os.path.join(UpperCamelCase , """special_tokens_map.json""" ) , encoding="""utf-8""" ) as json_file: A__ = json.load(UpperCamelCase ) with open(os.path.join(UpperCamelCase , """tokenizer_config.json""" ) , encoding="""utf-8""" ) as json_file: A__ = json.load(UpperCamelCase ) A__ = [f"""<extra_id_{i}>""" for i in range(1_25 )] A__ = added_tokens_extra_ids + [ """an_additional_special_token""" ] A__ = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(UpperCamelCase , """special_tokens_map.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(UpperCamelCase , UpperCamelCase ) with open(os.path.join(UpperCamelCase , """tokenizer_config.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(UpperCamelCase , UpperCamelCase ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files A__ = tokenizer_class.from_pretrained( UpperCamelCase , ) self.assertIn( """an_additional_special_token""" , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["""an_additional_special_token"""] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained A__ = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" , lstrip=UpperCamelCase )] A__ = tokenizer_class.from_pretrained( UpperCamelCase , additional_special_tokens=UpperCamelCase , ) self.assertIn("""a_new_additional_special_token""" , tokenizer.additional_special_tokens ) self.assertEqual( ["""a_new_additional_special_token"""] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""] ) ) , ) def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(UpperCamelCase ) A__ = tokenizer_class.from_pretrained(UpperCamelCase ) self.assertTrue(tokenizer.decode([2_55] ) == """""" ) def UpperCamelCase ( self: Any ): """simple docstring""" pass def UpperCamelCase ( self: Any ): """simple docstring""" pass def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" pass def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" pass def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.get_tokenizers(fast=UpperCamelCase , do_lower_case=UpperCamelCase ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A__ = ["""t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """x""", """t""", """</s>"""] A__ = tokenizer.convert_tokens_to_string(UpperCamelCase ) self.assertIsInstance(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A__ = [ """bos_token""", """eos_token""", """unk_token""", """sep_token""", """pad_token""", """cls_token""", """mask_token""", ] A__ = 0 A__ = tokenizer.convert_ids_to_tokens( UpperCamelCase , skip_special_tokens=UpperCamelCase ) for attr in attributes_list: setattr(UpperCamelCase , attr + """_id""" , UpperCamelCase ) self.assertEqual(getattr(UpperCamelCase , UpperCamelCase ) , UpperCamelCase ) self.assertEqual(getattr(UpperCamelCase , attr + """_id""" ) , UpperCamelCase ) setattr(UpperCamelCase , attr + """_id""" , UpperCamelCase ) self.assertEqual(getattr(UpperCamelCase , UpperCamelCase ) , UpperCamelCase ) self.assertEqual(getattr(UpperCamelCase , attr + """_id""" ) , UpperCamelCase ) setattr(UpperCamelCase , """additional_special_tokens_ids""" , [] ) self.assertListEqual(getattr(UpperCamelCase , """additional_special_tokens""" ) , [] ) self.assertListEqual(getattr(UpperCamelCase , """additional_special_tokens_ids""" ) , [] ) setattr(UpperCamelCase , """additional_special_tokens_ids""" , [token_id_to_test_setters] ) self.assertListEqual(getattr(UpperCamelCase , """additional_special_tokens""" ) , [token_to_test_setters] ) self.assertListEqual(getattr(UpperCamelCase , """additional_special_tokens_ids""" ) , [token_id_to_test_setters] )
335
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices SCREAMING_SNAKE_CASE_ : List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : Optional[int] = { 'microsoft/swin-tiny-patch4-window7-224': ( 'https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json' ), # See all Swin models at https://huggingface.co/models?filter=swin } class a ( _lowerCamelCase, _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "swin" UpperCAmelCase = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self: int , UpperCamelCase: str=2_24 , UpperCamelCase: Tuple=4 , UpperCamelCase: Tuple=3 , UpperCamelCase: Optional[Any]=96 , UpperCamelCase: Optional[Any]=[2, 2, 6, 2] , UpperCamelCase: Dict=[3, 6, 12, 24] , UpperCamelCase: Dict=7 , UpperCamelCase: Tuple=4.0 , UpperCamelCase: Optional[Any]=True , UpperCamelCase: Optional[Any]=0.0 , UpperCamelCase: List[str]=0.0 , UpperCamelCase: Optional[int]=0.1 , UpperCamelCase: Optional[int]="gelu" , UpperCamelCase: Dict=False , UpperCamelCase: Tuple=0.02 , UpperCamelCase: Dict=1e-5 , UpperCamelCase: Dict=32 , UpperCamelCase: List[str]=None , UpperCamelCase: Union[str, Any]=None , **UpperCamelCase: List[str] , ): """simple docstring""" super().__init__(**UpperCamelCase ) A__ = image_size A__ = patch_size A__ = num_channels A__ = embed_dim A__ = depths A__ = len(UpperCamelCase ) A__ = num_heads A__ = window_size A__ = mlp_ratio A__ = qkv_bias A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = drop_path_rate A__ = hidden_act A__ = use_absolute_embeddings A__ = layer_norm_eps A__ = initializer_range A__ = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A__ = int(embed_dim * 2 ** (len(UpperCamelCase ) - 1) ) A__ = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(UpperCamelCase ) + 1 )] A__ , A__ = get_aligned_output_features_output_indices( out_features=UpperCamelCase , out_indices=UpperCamelCase , stage_names=self.stage_names ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = version.parse("1.11" ) @property def UpperCamelCase ( self: str ): """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" return 1e-4
335
"""simple docstring""" import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC SCREAMING_SNAKE_CASE_ : str = parse(importlib.metadata.version('torch')) def _snake_case ( UpperCAmelCase_ : Union[str, Version] , UpperCAmelCase_ : str , UpperCAmelCase_ : str ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) A__ = STR_OPERATION_TO_FUNC[operation] if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = parse(importlib.metadata.version(UpperCAmelCase_ ) ) return operation(UpperCAmelCase_ , parse(UpperCAmelCase_ ) ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : str ): return compare_versions(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" class a : """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: List[Any] ): """simple docstring""" A__ = val A__ = None A__ = None def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] ): """simple docstring""" if self.val: if val < self.val: if self.left is None: A__ = Node(UpperCamelCase ) else: self.left.insert(UpperCamelCase ) elif val > self.val: if self.right is None: A__ = Node(UpperCamelCase ) else: self.right.insert(UpperCamelCase ) else: A__ = val def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple ): # Recursive traversal if root: inorder(root.left , UpperCAmelCase_ ) res.append(root.val ) inorder(root.right , UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : Optional[Any] ): # Build BST if len(UpperCAmelCase_ ) == 0: return arr A__ = Node(arr[0] ) for i in range(1 , len(UpperCAmelCase_ ) ): root.insert(arr[i] ) # Traverse BST in order. A__ = [] inorder(UpperCAmelCase_ , UpperCAmelCase_ ) return res if __name__ == "__main__": print(tree_sort([1_0, 1, 3, 2, 9, 1_4, 1_3]))
335
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , 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/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != 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(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE_ : Any = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Dict = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : str = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Optional[Any] = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple=None , UpperCamelCase: Tuple=None , **UpperCamelCase: Dict ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = eval_examples A__ = post_process_function def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Dataset] = None , UpperCamelCase: List[Any]=None , UpperCamelCase: Optional[List[str]] = None , UpperCamelCase: str = "eval" , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(UpperCamelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCamelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCamelCase ) return metrics def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Dict=None , UpperCamelCase: str = "test" , **UpperCamelCase: Optional[int] ): """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(UpperCamelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase , """predict""" ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase )
335
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = 42 class a ( _lowerCamelCase, _lowerCamelCase ): """simple docstring""" @register_to_config def __init__( self: Tuple , UpperCamelCase: int = 3 , UpperCamelCase: int = 3 , UpperCamelCase: Tuple[str] = ("DownEncoderBlock2D",) , UpperCamelCase: Tuple[str] = ("UpDecoderBlock2D",) , UpperCamelCase: Tuple[int] = (64,) , UpperCamelCase: int = 1 , UpperCamelCase: str = "silu" , UpperCamelCase: int = 3 , UpperCamelCase: int = 32 , UpperCamelCase: int = 2_56 , UpperCamelCase: int = 32 , UpperCamelCase: Optional[int] = None , UpperCamelCase: float = 0.18_215 , UpperCamelCase: str = "group" , ): """simple docstring""" super().__init__() # pass init params to Encoder A__ = Encoder( in_channels=UpperCamelCase , out_channels=UpperCamelCase , down_block_types=UpperCamelCase , block_out_channels=UpperCamelCase , layers_per_block=UpperCamelCase , act_fn=UpperCamelCase , norm_num_groups=UpperCamelCase , double_z=UpperCamelCase , ) A__ = vq_embed_dim if vq_embed_dim is not None else latent_channels A__ = nn.Convad(UpperCamelCase , UpperCamelCase , 1 ) A__ = VectorQuantizer(UpperCamelCase , UpperCamelCase , beta=0.25 , remap=UpperCamelCase , sane_index_shape=UpperCamelCase ) A__ = nn.Convad(UpperCamelCase , UpperCamelCase , 1 ) # pass init params to Decoder A__ = Decoder( in_channels=UpperCamelCase , out_channels=UpperCamelCase , up_block_types=UpperCamelCase , block_out_channels=UpperCamelCase , layers_per_block=UpperCamelCase , act_fn=UpperCamelCase , norm_num_groups=UpperCamelCase , norm_type=UpperCamelCase , ) @apply_forward_hook def UpperCamelCase ( self: str , UpperCamelCase: torch.FloatTensor , UpperCamelCase: bool = True ): """simple docstring""" A__ = self.encoder(UpperCamelCase ) A__ = self.quant_conv(UpperCamelCase ) if not return_dict: return (h,) return VQEncoderOutput(latents=UpperCamelCase ) @apply_forward_hook def UpperCamelCase ( self: str , UpperCamelCase: torch.FloatTensor , UpperCamelCase: bool = False , UpperCamelCase: bool = True ): """simple docstring""" if not force_not_quantize: A__ , A__ , A__ = self.quantize(UpperCamelCase ) else: A__ = h A__ = self.post_quant_conv(UpperCamelCase ) A__ = self.decoder(UpperCamelCase , quant if self.config.norm_type == """spatial""" else None ) if not return_dict: return (dec,) return DecoderOutput(sample=UpperCamelCase ) def UpperCamelCase ( self: List[str] , UpperCamelCase: torch.FloatTensor , UpperCamelCase: bool = True ): """simple docstring""" A__ = sample A__ = self.encode(UpperCamelCase ).latents A__ = self.decode(UpperCamelCase ).sample if not return_dict: return (dec,) return DecoderOutput(sample=UpperCamelCase )
335
"""simple docstring""" class a : """simple docstring""" def __init__( self: Dict ): """simple docstring""" A__ = {} def UpperCamelCase ( self: List[str] ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase , """ -> """ , """ -> """.join([str(UpperCamelCase ) for j in self.vertex[i]] ) ) def UpperCamelCase ( self: Any , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase ) else: # else make a new vertex A__ = [to_vertex] def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: list ): """simple docstring""" A__ = True print(UpperCamelCase , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[int] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
335
1
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 re from ..models.auto import AutoProcessor from ..models.vision_encoder_decoder import VisionEncoderDecoderModel from ..utils import is_vision_available from .base import PipelineTool if is_vision_available(): from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "naver-clova-ix/donut-base-finetuned-docvqa" UpperCAmelCase = ( "This is a tool that answers a question about an document (pdf). It takes an input named `document` which " "should be the document containing the information, as well as a `question` that is the question about the " "document. It returns a text that contains the answer to the question." ) UpperCAmelCase = "document_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = VisionEncoderDecoderModel UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: int , *UpperCamelCase: List[Any] , **UpperCamelCase: Dict ): """simple docstring""" if not is_vision_available(): raise ValueError("""Pillow must be installed to use the DocumentQuestionAnsweringTool.""" ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: List[str] , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" A__ = """<s_docvqa><s_question>{user_input}</s_question><s_answer>""" A__ = task_prompt.replace("""{user_input}""" , UpperCamelCase ) A__ = self.pre_processor.tokenizer( UpperCamelCase , add_special_tokens=UpperCamelCase , return_tensors="""pt""" ).input_ids A__ = self.pre_processor(UpperCamelCase , return_tensors="""pt""" ).pixel_values return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values} def UpperCamelCase ( self: str , UpperCamelCase: List[Any] ): """simple docstring""" return self.model.generate( inputs["""pixel_values"""].to(self.device ) , decoder_input_ids=inputs["""decoder_input_ids"""].to(self.device ) , max_length=self.model.decoder.config.max_position_embeddings , early_stopping=UpperCamelCase , pad_token_id=self.pre_processor.tokenizer.pad_token_id , eos_token_id=self.pre_processor.tokenizer.eos_token_id , use_cache=UpperCamelCase , num_beams=1 , bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]] , return_dict_in_generate=UpperCamelCase , ).sequences def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Dict ): """simple docstring""" A__ = self.pre_processor.batch_decode(UpperCamelCase )[0] A__ = sequence.replace(self.pre_processor.tokenizer.eos_token , """""" ) A__ = sequence.replace(self.pre_processor.tokenizer.pad_token , """""" ) A__ = re.sub(r"""<.*?>""" , """""" , UpperCamelCase , count=1 ).strip() # remove first task start token A__ = self.pre_processor.tokenajson(UpperCamelCase ) return sequence["answer"]
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
1
"""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 SCREAMING_SNAKE_CASE_ : Union[str, Any] = False class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str , UpperCamelCase: Tuple=32 ): """simple docstring""" set_seed(0 ) A__ = UNetaDModel(sample_size=UpperCamelCase , in_channels=3 , out_channels=3 ) A__ = torch.optim.SGD(model.parameters() , lr=0.0_001 ) return model, optimizer @slow def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = """cpu""" # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable A__ = DDPMScheduler( num_train_timesteps=10_00 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=UpperCamelCase , ) A__ = DDIMScheduler( num_train_timesteps=10_00 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule="""linear""" , clip_sample=UpperCamelCase , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) A__ = [torch.randn((4, 3, 32, 32) ).clip(-1 , 1 ).to(UpperCamelCase ) for _ in range(4 )] A__ = [torch.randn((4, 3, 32, 32) ).to(UpperCamelCase ) for _ in range(4 )] A__ = [torch.randint(0 , 10_00 , (4,) ).long().to(UpperCamelCase ) for _ in range(4 )] # train with a DDPM scheduler A__ , A__ = self.get_model_optimizer(resolution=32 ) model.train().to(UpperCamelCase ) for i in range(4 ): optimizer.zero_grad() A__ = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) A__ = model(UpperCamelCase , timesteps[i] ).sample A__ = torch.nn.functional.mse_loss(UpperCamelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM A__ , A__ = self.get_model_optimizer(resolution=32 ) model.train().to(UpperCamelCase ) for i in range(4 ): optimizer.zero_grad() A__ = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) A__ = model(UpperCamelCase , timesteps[i] ).sample A__ = torch.nn.functional.mse_loss(UpperCamelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-5 ) ) self.assertTrue(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-5 ) )
335
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad ): A__ = end_pointa[0] - end_pointa[0] A__ = end_pointa[1] - end_pointa[1] A__ = end_pointa[2] - end_pointa[2] return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : Vectorad ): A__ = ab[1] * ac[2] - ab[2] * ac[1] # *i A__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j A__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : int ): return tuple(round(UpperCAmelCase_ , UpperCAmelCase_ ) for x in vector ) == (0, 0, 0) def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : int = 10 ): A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ )
335
1
"""simple docstring""" from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class a ( yaml.SafeLoader ): """simple docstring""" def UpperCamelCase ( self: List[Any] , UpperCamelCase: Any ): """simple docstring""" A__ = [self.constructed_objects[key_node] for key_node, _ in node.value] A__ = [tuple(UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else key for key in keys] A__ = Counter(UpperCamelCase ) A__ = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(f"""Got duplicate yaml keys: {duplicate_keys}""" ) def UpperCamelCase ( self: Any , UpperCamelCase: Any , UpperCamelCase: Optional[Any]=False ): """simple docstring""" A__ = super().construct_mapping(UpperCamelCase , deep=UpperCamelCase ) self._check_no_duplicates_on_constructed_node(UpperCamelCase ) return mapping def _snake_case ( UpperCAmelCase_ : str ): A__ = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: A__ = full_content[1:].index("""---""" ) + 1 A__ = """\n""".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(UpperCAmelCase_ ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = {"train_eval_index"} # train-eval-index in the YAML metadata @classmethod def UpperCamelCase ( cls: Optional[Any] , UpperCamelCase: Path ): """simple docstring""" with open(UpperCamelCase , encoding="""utf-8""" ) as readme_file: A__ , A__ = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(UpperCamelCase ) else: return cls() def UpperCamelCase ( self: List[Any] , UpperCamelCase: Path ): """simple docstring""" if path.exists(): with open(UpperCamelCase , encoding="""utf-8""" ) as readme_file: A__ = readme_file.read() else: A__ = None A__ = self._to_readme(UpperCamelCase ) with open(UpperCamelCase , """w""" , encoding="""utf-8""" ) as readme_file: readme_file.write(UpperCamelCase ) def UpperCamelCase ( self: int , UpperCamelCase: Optional[str] = None ): """simple docstring""" if readme_content is not None: A__ , A__ = _split_yaml_from_readme(UpperCamelCase ) A__ = """---\n""" + self.to_yaml_string() + """---\n""" + content else: A__ = """---\n""" + self.to_yaml_string() + """---\n""" return full_content @classmethod def UpperCamelCase ( cls: Any , UpperCamelCase: str ): """simple docstring""" A__ = yaml.load(UpperCamelCase , Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields A__ = { (key.replace("""-""" , """_""" ) if key.replace("""-""" , """_""" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" return yaml.safe_dump( { (key.replace("""_""" , """-""" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } , sort_keys=UpperCamelCase , allow_unicode=UpperCamelCase , encoding="""utf-8""" , ).decode("""utf-8""" ) SCREAMING_SNAKE_CASE_ : List[str] = { 'image-classification': [], 'translation': [], 'image-segmentation': [], 'fill-mask': [], 'automatic-speech-recognition': [], 'token-classification': [], 'sentence-similarity': [], 'audio-classification': [], 'question-answering': [], 'summarization': [], 'zero-shot-classification': [], 'table-to-text': [], 'feature-extraction': [], 'other': [], 'multiple-choice': [], 'text-classification': [], 'text-to-image': [], 'text2text-generation': [], 'zero-shot-image-classification': [], 'tabular-classification': [], 'tabular-regression': [], 'image-to-image': [], 'tabular-to-text': [], 'unconditional-image-generation': [], 'text-retrieval': [], 'text-to-speech': [], 'object-detection': [], 'audio-to-audio': [], 'text-generation': [], 'conversational': [], 'table-question-answering': [], 'visual-question-answering': [], 'image-to-text': [], 'reinforcement-learning': [], 'voice-activity-detection': [], 'time-series-forecasting': [], 'document-question-answering': [], } if __name__ == "__main__": from argparse import ArgumentParser SCREAMING_SNAKE_CASE_ : Tuple = ArgumentParser(usage='Validate the yaml metadata block of a README.md file.') ap.add_argument('readme_filepath') SCREAMING_SNAKE_CASE_ : List[str] = ap.parse_args() SCREAMING_SNAKE_CASE_ : Optional[int] = Path(args.readme_filepath) SCREAMING_SNAKE_CASE_ : Optional[Any] = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_pegasus_x': ['PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusXConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST', 'PegasusXForConditionalGeneration', 'PegasusXModel', 'PegasusXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class a ( _lowerCamelCase ): """simple docstring""" @require_torch def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = """ from transformers import BertConfig, BertModel, BertTokenizer, pipeline """ A__ = """ mname = \"hf-internal-testing/tiny-random-bert\" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) BertTokenizer.from_pretrained(mname) pipe = pipeline(task=\"fill-mask\", model=mname) print(\"success\") """ A__ = """ import socket def offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\") socket.socket = offline_socket """ # Force fetching the files so that we can use the cache A__ = """hf-internal-testing/tiny-random-bert""" BertConfig.from_pretrained(UpperCamelCase ) BertModel.from_pretrained(UpperCamelCase ) BertTokenizer.from_pretrained(UpperCamelCase ) pipeline(task="""fill-mask""" , model=UpperCamelCase ) # baseline - just load from_pretrained with normal network A__ = [sys.executable, """-c""", """\n""".join([load, run, mock] )] # should succeed A__ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ = """1""" A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() ) @require_torch def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = """ from transformers import BertConfig, BertModel, BertTokenizer, pipeline """ A__ = """ mname = \"hf-internal-testing/tiny-random-bert\" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) BertTokenizer.from_pretrained(mname) pipe = pipeline(task=\"fill-mask\", model=mname) print(\"success\") """ A__ = """ import socket def offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\") socket.socket = offline_socket """ # Force fetching the files so that we can use the cache A__ = """hf-internal-testing/tiny-random-bert""" BertConfig.from_pretrained(UpperCamelCase ) BertModel.from_pretrained(UpperCamelCase ) BertTokenizer.from_pretrained(UpperCamelCase ) pipeline(task="""fill-mask""" , model=UpperCamelCase ) # baseline - just load from_pretrained with normal network A__ = [sys.executable, """-c""", """\n""".join([load, run, mock] )] # should succeed A__ = self.get_env() A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() ) @require_torch def UpperCamelCase ( self: str ): """simple docstring""" A__ = """ from transformers import BertConfig, BertModel, BertTokenizer """ A__ = """ mname = \"hf-internal-testing/tiny-random-bert-sharded\" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) print(\"success\") """ A__ = """ import socket def offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\") socket.socket = offline_socket """ # baseline - just load from_pretrained with normal network A__ = [sys.executable, """-c""", """\n""".join([load, run] )] # should succeed A__ = self.get_env() A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() ) # next emulate no network A__ = [sys.executable, """-c""", """\n""".join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ = """1""" A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() ) @require_torch def UpperCamelCase ( self: Any ): """simple docstring""" A__ = """ from transformers import pipeline """ A__ = """ mname = \"hf-internal-testing/tiny-random-bert\" pipe = pipeline(model=mname) """ A__ = """ import socket def offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\") socket.socket = offline_socket """ A__ = self.get_env() A__ = """1""" A__ = [sys.executable, """-c""", """\n""".join([load, mock, run] )] A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( """You cannot infer task automatically within `pipeline` when using offline mode""" , result.stderr.decode().replace("""\n""" , """""" ) , ) @require_torch def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = """ from transformers import AutoModel """ A__ = """ mname = \"hf-internal-testing/test_dynamic_model\" AutoModel.from_pretrained(mname, trust_remote_code=True) print(\"success\") """ # baseline - just load from_pretrained with normal network A__ = [sys.executable, """-c""", """\n""".join([load, run] )] # should succeed A__ = self.get_env() A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ = """1""" A__ = subprocess.run(UpperCamelCase , env=UpperCamelCase , check=UpperCamelCase , capture_output=UpperCamelCase ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn("""success""" , result.stdout.decode() )
335
"""simple docstring""" import math class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: List[str]=0 ): # a graph with Node 0,1,...,N-1 """simple docstring""" A__ = n A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # adjacency matrix for weight A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Tuple ): """simple docstring""" A__ = w def UpperCamelCase ( self: int ): """simple docstring""" for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): A__ = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase ( self: int , UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 1_0) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 1_0) 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)
335
1
"""simple docstring""" class a : # Public class to implement a graph """simple docstring""" def __init__( self: List[str] , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: list[list[bool]] ): """simple docstring""" A__ = row A__ = col A__ = graph def UpperCamelCase ( self: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: list[list[bool]] ): """simple docstring""" return ( 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] ) def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: list[list[bool]] ): """simple docstring""" A__ = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order A__ = [-1, 0, 1, -1, 1, -1, 0, 1] A__ = True # Make those cells visited for k in range(8 ): if self.is_safe(i + row_nbr[k] , j + col_nbr[k] , UpperCamelCase ): self.diffs(i + row_nbr[k] , j + col_nbr[k] , UpperCamelCase ) def UpperCamelCase ( self: Optional[int] ): # And finally, count all islands. """simple docstring""" A__ = [[False for j in range(self.COL )] for i in range(self.ROW )] A__ = 0 for i in range(self.ROW ): for j in range(self.COL ): if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(UpperCamelCase , UpperCamelCase , UpperCamelCase ) count += 1 return count
335
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def UpperCamelCase ( self: str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = (3, 32, 1_28) A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on A__ = dict(zip(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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(UpperCamelCase ) + """\n""" ) A__ = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 1_28}, } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta ) A__ = Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) return image_input def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """labels"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase ): processor() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.char_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) A__ = [seq.replace(""" """ , """""" ) for seq in decoded_tok] self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = None A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = torch.randn(1 , 27 , 38 ) A__ = torch.randn(1 , 27 , 5_02_57 ) A__ = torch.randn(1 , 27 , 3_05_22 ) A__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""] )
335
1
"""simple docstring""" import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): SCREAMING_SNAKE_CASE_ : int = True from torch.cuda.amp import autocast SCREAMING_SNAKE_CASE_ : Dict = logging.getLogger(__name__) @dataclass class a : """simple docstring""" UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to log verbose messages or not."}, ) UpperCAmelCase = field( default=2.0, metadata={"help": "Maximum temperature for gumbel softmax."} ) UpperCAmelCase = field( default=0.5, metadata={"help": "Minimum temperature for gumbel softmax."} ) UpperCAmelCase = field( default=0.9_9_9_9_9_5, metadata={"help": "Decay of gumbel temperature during training."} ) def _snake_case ( UpperCAmelCase_ : ModelArguments , UpperCAmelCase_ : TrainingArguments ): logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) A__ = logging.WARNING if model_args.verbose_logging: A__ = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): A__ = logging.INFO logger.setLevel(UpperCAmelCase_ ) @dataclass class a : """simple docstring""" UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "The name of the dataset to use (via the datasets library)."} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) UpperCAmelCase = field( default="train", metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" }, ) UpperCAmelCase = field( default="validation", metadata={ "help": ( "The name of the validation data set split to use (via the datasets library). Defaults to 'validation'" ) }, ) UpperCAmelCase = field( default="file", metadata={"help": "Column in the dataset that contains speech file path. Defaults to 'file'"}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Overwrite the cached preprocessed datasets or not."} ) UpperCAmelCase = field( default=1, metadata={ "help": "The percentage of the train set used as validation set in case there's no validation split" }, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "The number of processes to use for the preprocessing."}, ) UpperCAmelCase = field( default=2_0.0, metadata={"help": "Filter audio files that are longer than `max_duration_in_seconds` seconds"} ) @dataclass class a : """simple docstring""" UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = "longest" UpperCAmelCase = None UpperCAmelCase = None def __call__( self: Tuple , UpperCamelCase: List[Dict[str, Union[List[int], torch.Tensor]]] ): """simple docstring""" A__ = self.feature_extractor.pad( UpperCamelCase , max_length=self.max_length , padding=self.padding , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="""pt""" , ) A__ = self.model._get_feat_extract_output_lengths(batch["""input_values"""].shape[-1] ) A__ = batch["""input_values"""].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula A__ = self.model._get_feat_extract_output_lengths(batch["""attention_mask"""].sum(-1 ) ).to( torch.long ) A__ = torch.zeros( (batch_size, mask_indices_seq_length) , dtype=torch.long , device=batch["""input_values"""].device ) # these two operations makes sure that all values # before the output lengths indices are attended to A__ = 1 A__ = attention_mask.flip([-1] ).cumsum(-1 ).flip([-1] ).bool() # sample randomly masked indices A__ = _compute_mask_indices( (batch_size, mask_indices_seq_length) , self.model.config.mask_time_prob , self.model.config.mask_time_length , attention_mask=UpperCamelCase , min_masks=2 , ) return batch class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Any , UpperCamelCase: Optional[Any]=1 , UpperCamelCase: Optional[Any]=0 , UpperCamelCase: Dict=1.0 , **UpperCamelCase: Union[str, Any] ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = 0 A__ = max_gumbel_temp A__ = min_gumbel_temp A__ = gumbel_temp_decay def UpperCamelCase ( self: Tuple , UpperCamelCase: nn.Module , UpperCamelCase: Dict[str, Union[torch.Tensor, Any]] ): """simple docstring""" model.train() A__ = self._prepare_inputs(UpperCamelCase ) if self.use_amp: with autocast(): A__ = self.compute_loss(UpperCamelCase , UpperCamelCase ) else: A__ = self.compute_loss(UpperCamelCase , UpperCamelCase ) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": A__ = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": A__ = loss.sum() / (inputs["""mask_time_indices"""]).sum() else: raise ValueError(f"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']""" ) if self.args.gradient_accumulation_steps > 1: A__ = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(UpperCamelCase ).backward() elif self.use_apex: with amp.scale_loss(UpperCamelCase , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(UpperCamelCase ) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) return loss.detach() def _snake_case ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() configure_logger(UpperCAmelCase_ , UpperCAmelCase_ ) # Downloading and loading a dataset from the hub. A__ = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" A__ = DatasetDict() A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}[:{data_args.validation_split_percentage}%]""" , cache_dir=model_args.cache_dir , ) A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}[{data_args.validation_split_percentage}%:]""" , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" A__ = DatasetDict() A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split="""validation""" , cache_dir=model_args.cache_dir , ) A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}""" , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported A__ = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=UpperCAmelCase_ ) def prepare_dataset(UpperCAmelCase_ : int ): # check that all files have the correct sampling rate A__ , A__ = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays A__ = datasets.map( UpperCAmelCase_ , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets["""train"""].column_names ) # filter audio files that are too long A__ = vectorized_datasets.filter( lambda UpperCAmelCase_ : len(data["""speech"""] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(UpperCAmelCase_ : Dict ): return feature_extractor(batch["""speech"""] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` A__ = vectorized_datasets.map( UpperCAmelCase_ , batched=UpperCAmelCase_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets["""train"""].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 A__ = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( """PreTraining is only supported for ``config.do_stable_layer_norm=True`` and""" """ ``config.feat_extract_norm='layer'""" ) A__ = WavaVecaForPreTraining(UpperCAmelCase_ ) A__ = DataCollatorForWavaVecaPretraining(model=UpperCAmelCase_ , feature_extractor=UpperCAmelCase_ ) A__ = WavaVecaPreTrainer( model=UpperCAmelCase_ , data_collator=UpperCAmelCase_ , args=UpperCAmelCase_ , train_dataset=vectorized_datasets["""train"""] , eval_dataset=vectorized_datasets["""validation"""] , tokenizer=UpperCAmelCase_ , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
335
"""simple docstring""" import math def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase_ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : str = { 'configuration_blip_2': [ 'BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Blip2Config', 'Blip2QFormerConfig', 'Blip2VisionConfig', ], 'processing_blip_2': ['Blip2Processor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : List[str] = [ 'BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST', 'Blip2Model', 'Blip2QFormerModel', 'Blip2PreTrainedModel', 'Blip2ForConditionalGeneration', 'Blip2VisionModel', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys SCREAMING_SNAKE_CASE_ : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = 1 A__ = 3 A__ = (32, 32) A__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase ) return image @property def UpperCamelCase ( self: int ): """simple docstring""" torch.manual_seed(0 ) A__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) return model @property def UpperCamelCase ( self: Tuple ): """simple docstring""" torch.manual_seed(0 ) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) return model @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" torch.manual_seed(0 ) A__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_06 , ) return RobertaSeriesModelWithTransformation(UpperCamelCase ) @property def UpperCamelCase ( self: str ): """simple docstring""" def extract(*UpperCamelCase: List[str] , **UpperCamelCase: Any ): class a : """simple docstring""" def __init__( self: Any ): """simple docstring""" A__ = torch.ones([0] ) def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" self.pixel_values.to(UpperCamelCase ) return self return Out() return extract def UpperCamelCase ( self: str ): """simple docstring""" A__ = """cpu""" # ensure determinism for the device-dependent torch.Generator A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) A__ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ) A__ = output.images A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , return_dict=UpperCamelCase , )[0] A__ = image[0, -3:, -3:, -1] A__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A__ = np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) # put models in fp16 A__ = unet.half() A__ = vae.half() A__ = bert.half() # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) # resize to resolution that is divisible by 8 but not 16 or 32 A__ = init_image.resize((7_60, 5_04) ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] A__ = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) A__ = np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) A__ = init_image.resize((7_68, 5_12) ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy""" ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
335
1
"""simple docstring""" import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class a ( _lowerCamelCase, _lowerCamelCase ): """simple docstring""" @register_to_config def __init__( self: List[str] , *, UpperCamelCase: int = 4 , UpperCamelCase: int = 7_68 , UpperCamelCase: int , UpperCamelCase: List[Any] , ): """simple docstring""" super().__init__() A__ = nn.Parameter(torch.zeros(UpperCamelCase ) ) # parameters for additional clip time embeddings A__ = nn.Linear(UpperCamelCase , UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , UpperCamelCase ) # parameters for encoder hidden states A__ = clip_extra_context_tokens A__ = nn.Linear( UpperCamelCase , self.clip_extra_context_tokens * cross_attention_dim ) A__ = nn.Linear(UpperCamelCase , UpperCamelCase ) A__ = nn.LayerNorm(UpperCamelCase ) def UpperCamelCase ( self: List[str] , *, UpperCamelCase: str , UpperCamelCase: Any , UpperCamelCase: Optional[Any] , UpperCamelCase: Dict ): """simple docstring""" if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings A__ = image_embeddings.shape[0] A__ = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) A__ = classifier_free_guidance_embeddings.expand( UpperCamelCase , -1 ) A__ = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] A__ = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... A__ = self.embedding_proj(UpperCamelCase ) A__ = self.clip_image_embeddings_project_to_time_embeddings(UpperCamelCase ) A__ = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" A__ = self.clip_extra_context_tokens_proj(UpperCamelCase ) A__ = clip_extra_context_tokens.reshape(UpperCamelCase , -1 , self.clip_extra_context_tokens ) A__ = clip_extra_context_tokens.permute(0 , 2 , 1 ) A__ = self.encoder_hidden_states_proj(UpperCamelCase ) A__ = self.text_encoder_hidden_states_norm(UpperCamelCase ) A__ = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
335
"""simple docstring""" import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = 0.01 with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): A__ = time.time() locka.acquire(UpperCAmelCase_ ) assert time.time() - _start > timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = """a""" * 1000 + """.lock""" A__ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(""".lock""" ) assert not locka._lock_file.endswith(UpperCAmelCase_ ) assert len(os.path.basename(locka._lock_file ) ) <= 255 A__ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): locka.acquire(0 )
335
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE_ : List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : List[Any] = { 'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json', } class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "lxmert" UpperCAmelCase = {} def __init__( self: Dict , UpperCamelCase: Tuple=3_05_22 , UpperCamelCase: List[Any]=7_68 , UpperCamelCase: Optional[Any]=12 , UpperCamelCase: Any=95_00 , UpperCamelCase: Optional[Any]=16_00 , UpperCamelCase: Union[str, Any]=4_00 , UpperCamelCase: str=30_72 , UpperCamelCase: int="gelu" , UpperCamelCase: List[str]=0.1 , UpperCamelCase: int=0.1 , UpperCamelCase: List[str]=5_12 , UpperCamelCase: List[str]=2 , UpperCamelCase: Optional[Any]=0.02 , UpperCamelCase: Union[str, Any]=1e-1_2 , UpperCamelCase: Dict=9 , UpperCamelCase: Optional[int]=5 , UpperCamelCase: Optional[int]=5 , UpperCamelCase: Union[str, Any]=20_48 , UpperCamelCase: Tuple=4 , UpperCamelCase: Optional[int]=6.67 , UpperCamelCase: Dict=True , UpperCamelCase: int=True , UpperCamelCase: Dict=True , UpperCamelCase: Optional[int]=True , UpperCamelCase: List[str]=True , UpperCamelCase: str=True , UpperCamelCase: Any=True , **UpperCamelCase: str , ): """simple docstring""" A__ = vocab_size A__ = hidden_size A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = num_qa_labels A__ = num_object_labels A__ = num_attr_labels A__ = l_layers A__ = x_layers A__ = r_layers A__ = visual_feat_dim A__ = visual_pos_dim A__ = visual_loss_normalizer A__ = task_matched A__ = task_mask_lm A__ = task_obj_predict A__ = task_qa A__ = visual_obj_loss A__ = visual_attr_loss A__ = visual_feat_loss A__ = {"""vision""": r_layers, """cross_encoder""": x_layers, """language""": l_layers} super().__init__(**UpperCamelCase )
335
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase = "image_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = AutoModelForVisualQuestionAnswering UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" return self.pre_processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) def UpperCamelCase ( self: str , UpperCamelCase: str ): """simple docstring""" with torch.no_grad(): return self.model(**UpperCamelCase ).logits def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int ): """simple docstring""" A__ = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
335
1
"""simple docstring""" from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def _snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any] ): for param, grad_param in zip(model_a.parameters() , model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is False ), F"""Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})""" else: # Grads should be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is True ), F"""Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})""" def _snake_case ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Union[str, Any]=True ): model.train() A__ = model(UpperCAmelCase_ ) A__ = F.mse_loss(UpperCAmelCase_ , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any]=False ): set_seed(42 ) A__ = RegressionModel() A__ = deepcopy(UpperCAmelCase_ ) A__ = RegressionDataset(length=80 ) A__ = DataLoader(UpperCAmelCase_ , batch_size=16 ) model.to(accelerator.device ) if sched: A__ = AdamW(params=model.parameters() , lr=1e-3 ) A__ = AdamW(params=ddp_model.parameters() , lr=1e-3 ) A__ = LambdaLR(UpperCAmelCase_ , lr_lambda=lambda UpperCAmelCase_ : epoch**0.65 ) A__ = LambdaLR(UpperCAmelCase_ , lr_lambda=lambda UpperCAmelCase_ : epoch**0.65 ) # Make a copy of `model` if sched: A__ , A__ , A__ , A__ = accelerator.prepare(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) else: A__ , A__ = accelerator.prepare(UpperCAmelCase_ , UpperCAmelCase_ ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def _snake_case ( UpperCAmelCase_ : Union[str, Any] ): # Test when on a single CPU or GPU that the context manager does nothing A__ , A__ , A__ = get_training_setup(UpperCAmelCase_ ) # Use a single batch A__ , A__ = next(iter(UpperCAmelCase_ ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model A__ , A__ = accelerator.gather((ddp_input, ddp_target) ) A__ , A__ = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(UpperCAmelCase_ ): step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) else: # Sync grads step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad , ddp_param.grad ), F"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) A__ = ddp_input[torch.randperm(len(UpperCAmelCase_ ) )] def _snake_case ( UpperCAmelCase_ : Optional[Any] ): # Test on distributed setup that context manager behaves properly A__ , A__ , A__ = get_training_setup(UpperCAmelCase_ ) # Use a single batch A__ , A__ = next(iter(UpperCAmelCase_ ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model A__ , A__ = accelerator.gather((ddp_input, ddp_target) ) A__ , A__ = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(UpperCAmelCase_ ): step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) else: # Sync grads step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F"""Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})""" else: # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) A__ = ddp_input[torch.randperm(len(UpperCAmelCase_ ) )] def _snake_case ( UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : List[str]=False ): A__ = Accelerator( split_batches=UpperCAmelCase_ , dispatch_batches=UpperCAmelCase_ , gradient_accumulation_steps=2 ) # Test that context manager behaves properly A__ , A__ , A__ = get_training_setup(UpperCAmelCase_ ) for iteration, batch in enumerate(UpperCAmelCase_ ): A__ , A__ = batch.values() # Gather the distributed inputs and targs for the base model A__ , A__ = accelerator.gather((ddp_input, ddp_target) ) A__ , A__ = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Do "gradient accumulation" (noop) with accelerator.accumulate(UpperCAmelCase_ ): step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(UpperCAmelCase_ ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F"""Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" else: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F"""Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) A__ = ddp_input[torch.randperm(len(UpperCAmelCase_ ) )] GradientState._reset_state() def _snake_case ( UpperCAmelCase_ : Optional[Any]=False , UpperCAmelCase_ : Dict=False ): A__ = Accelerator( split_batches=UpperCAmelCase_ , dispatch_batches=UpperCAmelCase_ , gradient_accumulation_steps=2 ) # Test that context manager behaves properly A__ , A__ , A__ , A__ , A__ , A__ , A__ = get_training_setup(UpperCAmelCase_ , UpperCAmelCase_ ) for iteration, batch in enumerate(UpperCAmelCase_ ): A__ , A__ = batch.values() # Gather the distributed inputs and targs for the base model A__ , A__ = accelerator.gather((ddp_input, ddp_target) ) A__ , A__ = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(UpperCAmelCase_ )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(UpperCAmelCase_ ): step_model(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), F"""Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]['lr']}\nDDP opt: {ddp_opt.param_groups[0]['lr']}\n""" A__ = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(UpperCAmelCase_ )) if accelerator.num_processes > 1: check_model_parameters(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) GradientState._reset_state() def _snake_case ( ): A__ = Accelerator() A__ = RegressionDataset(length=80 ) A__ = DataLoader(UpperCAmelCase_ , batch_size=16 ) A__ = RegressionDataset(length=96 ) A__ = DataLoader(UpperCAmelCase_ , batch_size=16 ) A__ , A__ = accelerator.prepare(UpperCAmelCase_ , UpperCAmelCase_ ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(UpperCAmelCase_ ): assert id(accelerator.gradient_state.active_dataloader ) == id(UpperCAmelCase_ ) if iteration < len(UpperCAmelCase_ ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(UpperCAmelCase_ ): assert id(accelerator.gradient_state.active_dataloader ) == id(UpperCAmelCase_ ) if batch_num < len(UpperCAmelCase_ ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def _snake_case ( ): A__ = Accelerator() A__ = accelerator.state if state.local_process_index == 0: print("""**Test `accumulate` gradient accumulation with dataloader break**""" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("""**Test NOOP `no_sync` context manager**""" ) test_noop_sync(UpperCAmelCase_ ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("""**Test Distributed `no_sync` context manager**""" ) test_distributed_sync(UpperCAmelCase_ ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( """**Test `accumulate` gradient accumulation, """ , F"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**""" , ) test_gradient_accumulation(UpperCAmelCase_ , UpperCAmelCase_ ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("""<""" , """2.0""" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( """**Test `accumulate` gradient accumulation with optimizer and scheduler, """ , """`split_batches=False`, `dispatch_batches=False`**""" , ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( """**Test `accumulate` gradient accumulation with optimizer and scheduler, """ , F"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**""" , ) test_gradient_accumulation_with_opt_and_scheduler(UpperCAmelCase_ , UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : Union[str, Any] ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
335
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( 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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): if number < 0 or shift_amount < 0: raise ValueError("""both inputs must be positive integers""" ) A__ = str(bin(UpperCAmelCase_ ) ) binary_number += "0" * shift_amount return binary_number def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): if number < 0 or shift_amount < 0: raise ValueError("""both inputs must be positive integers""" ) A__ = str(bin(UpperCAmelCase_ ) )[2:] if shift_amount >= len(UpperCAmelCase_ ): return "0b0" A__ = binary_number[: len(UpperCAmelCase_ ) - shift_amount] return "0b" + shifted_binary_number def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): if number >= 0: # Get binary representation of positive number A__ = """0""" + str(bin(UpperCAmelCase_ ) ).strip("""-""" )[2:] else: # Get binary (2's complement) representation of negative number A__ = len(bin(UpperCAmelCase_ )[3:] ) # Find 2's complement of number A__ = bin(abs(UpperCAmelCase_ ) - (1 << binary_number_length) )[3:] A__ = ( """1""" + """0""" * (binary_number_length - len(UpperCAmelCase_ )) + binary_number ) if shift_amount >= len(UpperCAmelCase_ ): return "0b" + binary_number[0] * len(UpperCAmelCase_ ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(UpperCAmelCase_ ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : Dict ): # noqa: E741 A__ = len(UpperCAmelCase_ ) A__ = 0 A__ = [0] * n A__ = [False] * n A__ = [False] * n def dfs(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): if parent == root: out_edge_count += 1 A__ = True A__ = at for to in l[at]: if to == parent: pass elif not visited[to]: A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: A__ = True # AP found via cycle if at == low[to]: A__ = True else: A__ = min(low[at] , UpperCAmelCase_ ) return out_edge_count for i in range(UpperCAmelCase_ ): if not visited[i]: A__ = 0 A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , -1 , UpperCAmelCase_ ) A__ = out_edge_count > 1 for x in range(len(UpperCAmelCase_ ) ): if is_art[x] is True: print(UpperCAmelCase_ ) # Adjacency list of graph SCREAMING_SNAKE_CASE_ : Optional[int] = { 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)
335
1
"""simple docstring""" import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def _snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[int] ): A__ = XCLIPTextConfig() # derive patch size from model name A__ = model_name.find("""patch""" ) A__ = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) A__ = XCLIPVisionConfig(patch_size=UpperCAmelCase_ , num_frames=UpperCAmelCase_ ) if "large" in model_name: A__ = 768 A__ = 3072 A__ = 12 A__ = 1024 A__ = 4096 A__ = 16 A__ = 24 A__ = 768 A__ = 3072 if model_name == "xclip-large-patch14-16-frames": A__ = 336 A__ = XCLIPConfig.from_text_vision_configs(UpperCAmelCase_ , UpperCAmelCase_ ) if "large" in model_name: A__ = 768 return config def _snake_case ( UpperCAmelCase_ : Optional[Any] ): # text encoder if name == "token_embedding.weight": A__ = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": A__ = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: A__ = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: A__ = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: A__ = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: A__ = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): A__ = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: A__ = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: A__ = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": A__ = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": A__ = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): A__ = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: A__ = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: A__ = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: A__ = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: A__ = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: A__ = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: A__ = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: A__ = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": A__ = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): A__ = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): A__ = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def _snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Tuple ): for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(UpperCAmelCase_ ) if "attn.in_proj" in key: A__ = key.split(""".""" ) if key.startswith("""visual""" ): A__ = key_split[3] A__ = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: A__ = val[ :dim, : ] A__ = val[ dim : dim * 2, : ] A__ = val[ -dim:, : ] else: A__ = val[ :dim ] A__ = val[ dim : dim * 2 ] A__ = val[ -dim: ] else: if "weight" in key: A__ = val[ :dim, : ] A__ = val[ dim : dim * 2, : ] A__ = val[ -dim:, : ] else: A__ = val[:dim] A__ = val[ dim : dim * 2 ] A__ = val[-dim:] elif key.startswith("""mit""" ): A__ = key_split[2] A__ = config.vision_config.mit_hidden_size if "weight" in key: A__ = val[:dim, :] A__ = val[dim : dim * 2, :] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = key_split[2] A__ = config.text_config.hidden_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[ dim : dim * 2 ] A__ = val[-dim:] else: A__ = rename_key(UpperCAmelCase_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: A__ = val.T A__ = val return orig_state_dict def _snake_case ( UpperCAmelCase_ : Any ): if num_frames == 8: A__ = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: A__ = """eating_spaghetti.npy""" elif num_frames == 32: A__ = """eating_spaghetti_32_frames.npy""" A__ = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=UpperCAmelCase_ , repo_type="""dataset""" , ) A__ = np.load(UpperCAmelCase_ ) return list(UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : str=False ): A__ = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } A__ = model_to_url[model_name] A__ = 8 if "16-frames" in model_name: A__ = 16 elif "shot" in model_name: A__ = 32 A__ = get_xclip_config(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = XCLIPModel(UpperCAmelCase_ ) model.eval() if "drive" in checkpoint_url: A__ = """pytorch_model.bin""" gdown.cached_download(UpperCAmelCase_ , UpperCAmelCase_ , quiet=UpperCAmelCase_ ) A__ = torch.load(UpperCAmelCase_ , map_location="""cpu""" )["""model"""] else: A__ = torch.hub.load_state_dict_from_url(UpperCAmelCase_ )["""model"""] A__ = convert_state_dict(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = XCLIPModel(UpperCAmelCase_ ) A__ , A__ = model.load_state_dict(UpperCAmelCase_ , strict=UpperCAmelCase_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() A__ = 336 if model_name == """xclip-large-patch14-16-frames""" else 224 A__ = VideoMAEImageProcessor(size=UpperCAmelCase_ ) A__ = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) A__ = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) A__ = XCLIPProcessor(image_processor=UpperCAmelCase_ , tokenizer=UpperCAmelCase_ ) A__ = prepare_video(UpperCAmelCase_ ) A__ = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=UpperCAmelCase_ , return_tensors="""pt""" , padding=UpperCAmelCase_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): A__ = model(**UpperCAmelCase_ ) # Verify outputs A__ = outputs.logits_per_video A__ = logits_per_video.softmax(dim=1 ) print("""Probs:""" , UpperCAmelCase_ ) # kinetics-400 if model_name == "xclip-base-patch32": A__ = torch.tensor([[0.00_19, 0.99_51, 0.00_30]] ) elif model_name == "xclip-base-patch32-16-frames": A__ = torch.tensor([[7.09_99e-04, 9.98_83e-01, 4.55_80e-04]] ) elif model_name == "xclip-base-patch16": A__ = torch.tensor([[0.00_83, 0.96_81, 0.02_36]] ) elif model_name == "xclip-base-patch16-16-frames": A__ = torch.tensor([[7.69_37e-04, 9.97_28e-01, 1.94_73e-03]] ) elif model_name == "xclip-large-patch14": A__ = torch.tensor([[0.00_62, 0.98_64, 0.00_75]] ) elif model_name == "xclip-large-patch14-16-frames": A__ = torch.tensor([[3.38_77e-04, 9.99_37e-01, 2.88_88e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": A__ = torch.tensor([[0.05_55, 0.89_14, 0.05_31]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": A__ = torch.tensor([[3.85_54e-04, 9.99_29e-01, 3.27_54e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": A__ = torch.tensor([[0.00_36, 0.99_20, 0.00_45]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": A__ = torch.tensor([[7.18_90e-06, 9.99_94e-01, 5.65_59e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": A__ = torch.tensor([[1.03_20e-05, 9.99_93e-01, 6.24_35e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": A__ = torch.tensor([[4.13_77e-06, 9.99_90e-01, 9.83_86e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": A__ = torch.tensor([[4.13_47e-05, 9.99_62e-01, 3.34_11e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": A__ = torch.tensor([[8.58_57e-05, 9.99_28e-01, 6.32_91e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": A__ = torch.tensor([[8.58_57e-05, 9.99_28e-01, 6.32_91e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": A__ = torch.tensor([[0.00_27, 0.99_04, 0.00_70]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": A__ = torch.tensor([[9.82_19e-04, 9.95_93e-01, 3.08_63e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": A__ = torch.tensor([[3.50_82e-04, 9.97_85e-01, 1.79_66e-03]] ) else: raise ValueError(F"""Model name {model_name} not supported""" ) assert torch.allclose(UpperCAmelCase_ , UpperCAmelCase_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCAmelCase_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(UpperCAmelCase_ , organization="""nielsr""" ) processor.push_to_hub(UpperCAmelCase_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(UpperCAmelCase_ , organization="""nielsr""" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='xclip-base-patch32', type=str, help='Name of the model.', ) 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.' ) SCREAMING_SNAKE_CASE_ : List[Any] = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
335
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" A__ = get_activation("""swish""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""silu""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = get_activation("""mish""" ) self.assertIsInstance(UpperCamelCase , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""gelu""" ) self.assertIsInstance(UpperCamelCase , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
335
1
"""simple docstring""" import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin 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 MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: int , UpperCamelCase: List[str]=13 , UpperCamelCase: Optional[Any]=32 , UpperCamelCase: Any=2 , UpperCamelCase: int=3 , UpperCamelCase: Any=16 , UpperCamelCase: Union[str, Any]=[1, 2, 1] , UpperCamelCase: Optional[Any]=[2, 2, 4] , UpperCamelCase: Optional[int]=2 , UpperCamelCase: Dict=2.0 , UpperCamelCase: Dict=True , UpperCamelCase: Optional[Any]=0.0 , UpperCamelCase: Optional[Any]=0.0 , UpperCamelCase: Union[str, Any]=0.1 , UpperCamelCase: List[Any]="gelu" , UpperCamelCase: Dict=False , UpperCamelCase: List[str]=True , UpperCamelCase: List[str]=0.02 , UpperCamelCase: Union[str, Any]=1e-5 , UpperCamelCase: List[Any]=True , UpperCamelCase: str=None , UpperCamelCase: Dict=True , UpperCamelCase: Any=10 , UpperCamelCase: Optional[Any]=8 , UpperCamelCase: Dict=["stage1", "stage2", "stage3"] , UpperCamelCase: Dict=[1, 2, 3] , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = embed_dim 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 UpperCamelCase ( self: Tuple ): """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 UpperCamelCase ( self: Any ): """simple docstring""" return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def UpperCamelCase ( self: List[str] , UpperCamelCase: str , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] ): """simple docstring""" A__ = MaskFormerSwinModel(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 UpperCamelCase ( self: Optional[int] , UpperCamelCase: str , UpperCamelCase: Any , UpperCamelCase: str ): """simple docstring""" A__ = MaskFormerSwinBackbone(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 ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(UpperCamelCase ): A__ = ["""stem"""] A__ = MaskFormerSwinBackbone(config=UpperCamelCase ) def UpperCamelCase ( self: 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 ( _lowerCamelCase, _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) UpperCAmelCase = {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {} UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = MaskFormerSwinModelTester(self ) A__ = ConfigTester(self , config_class=UpperCamelCase , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with""" """ `nn.DataParallel`""" ) ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" pass def UpperCamelCase ( self: Tuple ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase ( self: Tuple ): """simple docstring""" return def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*UpperCamelCase ) @unittest.skip("""Swin does not use inputs_embeds""" ) def UpperCamelCase ( self: int ): """simple docstring""" pass @unittest.skip("""Swin does not support feedforward chunking""" ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" pass def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: 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 UpperCamelCase ( self: str ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: 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 ) @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions""" ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" pass @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone""" ) def UpperCamelCase ( self: Dict ): """simple docstring""" pass def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Any , UpperCamelCase: int ): """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 ) # Swin 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] , ) def UpperCamelCase ( self: 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: 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 UpperCamelCase ( self: str ): """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: 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) ) @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints""" ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def UpperCamelCase ( self: Tuple ): """simple docstring""" pass def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(UpperCamelCase: Any ): A__ = 0 return t def check_equivalence(UpperCamelCase: Any , UpperCamelCase: Optional[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: Dict={} ): with torch.no_grad(): A__ = model(**UpperCamelCase , return_dict=UpperCamelCase , **UpperCamelCase ) A__ = model(**UpperCamelCase , return_dict=UpperCamelCase , **UpperCamelCase ).to_tuple() def recursive_check(UpperCamelCase: List[str] , UpperCamelCase: Any ): if isinstance(UpperCamelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(UpperCamelCase , UpperCamelCase ): recursive_check(UpperCamelCase , UpperCamelCase ) elif isinstance(UpperCamelCase , UpperCamelCase ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(UpperCamelCase , UpperCamelCase ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(UpperCamelCase ) , set_nan_tensor_to_zero(UpperCamelCase ) , atol=1e-5 ) , msg=( """Tuple and dict output are not equal. Difference:""" f""" {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:""" f""" {torch.isnan(UpperCamelCase ).any()} and `inf`: {torch.isinf(UpperCamelCase )}. Dict has""" f""" `nan`: {torch.isnan(UpperCamelCase ).any()} and `inf`: {torch.isinf(UpperCamelCase )}.""" ) , ) recursive_check(UpperCamelCase , UpperCamelCase ) for model_class in self.all_model_classes: A__ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase ) check_equivalence(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase , return_labels=UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase , return_labels=UpperCamelCase ) check_equivalence(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase ) check_equivalence(UpperCamelCase , UpperCamelCase , UpperCamelCase , {"""output_hidden_states""": True} ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase , return_labels=UpperCamelCase ) A__ = self._prepare_for_class(UpperCamelCase , UpperCamelCase , return_labels=UpperCamelCase ) check_equivalence(UpperCamelCase , UpperCamelCase , UpperCamelCase , {"""output_hidden_states""": True} ) @require_torch class a ( unittest.TestCase, _lowerCamelCase ): """simple docstring""" UpperCAmelCase = (MaskFormerSwinBackbone,) if is_torch_available() else () UpperCAmelCase = MaskFormerSwinConfig def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = MaskFormerSwinModelTester(self ) def UpperCamelCase ( self: int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = inputs_dict["""pixel_values"""].shape[0] for backbone_class in self.all_model_classes: A__ = backbone_class(UpperCamelCase ) backbone.to(UpperCamelCase ) backbone.eval() A__ = backbone(**UpperCamelCase ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , UpperCamelCase ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True A__ = backbone(**UpperCamelCase , output_hidden_states=UpperCamelCase ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) A__ , A__ , A__ = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: A__ = backbone(**UpperCamelCase , output_attentions=UpperCamelCase ) self.assertIsNotNone(outputs.attentions )
335
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
1
"""simple docstring""" import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) SCREAMING_SNAKE_CASE_ : List[str] = logging.getLogger(__name__) def _snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : str ): A__ = np.argmax(UpperCAmelCase_ , axis=1 ) return np.sum(outputs == labels ) def _snake_case ( UpperCAmelCase_ : Any ): with open(UpperCAmelCase_ , encoding="""utf_8""" ) as f: A__ = csv.reader(UpperCAmelCase_ ) A__ = [] next(UpperCAmelCase_ ) # skip the first line for line in tqdm(UpperCAmelCase_ ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def _snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] ): A__ = [] for dataset in encoded_datasets: A__ = len(UpperCAmelCase_ ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(UpperCAmelCase_ ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(UpperCAmelCase_ ) - 1 A__ = len(UpperCAmelCase_ ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(UpperCAmelCase_ ) for t in all_inputs ) ) return tensor_datasets def _snake_case ( ): A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=UpperCAmelCase_ , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=UpperCAmelCase_ , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=UpperCAmelCase_ , default="""""" ) parser.add_argument("""--eval_dataset""" , type=UpperCAmelCase_ , default="""""" ) parser.add_argument("""--seed""" , type=UpperCAmelCase_ , default=42 ) parser.add_argument("""--num_train_epochs""" , type=UpperCAmelCase_ , default=3 ) parser.add_argument("""--train_batch_size""" , type=UpperCAmelCase_ , default=8 ) parser.add_argument("""--eval_batch_size""" , type=UpperCAmelCase_ , default=16 ) parser.add_argument("""--adam_epsilon""" , default=1e-8 , type=UpperCAmelCase_ , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=UpperCAmelCase_ , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=UpperCAmelCase_ , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=UpperCAmelCase_ , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=UpperCAmelCase_ , default=6.25e-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=UpperCAmelCase_ , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=UpperCAmelCase_ , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=UpperCAmelCase_ , default=0.01 ) parser.add_argument("""--lm_coef""" , type=UpperCAmelCase_ , default=0.9 ) parser.add_argument("""--n_valid""" , type=UpperCAmelCase_ , default=374 ) parser.add_argument("""--server_ip""" , type=UpperCAmelCase_ , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=UpperCAmelCase_ , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(UpperCAmelCase_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=UpperCAmelCase_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(UpperCAmelCase_ , UpperCAmelCase_ ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(UpperCAmelCase_ ) A__ = tokenizer.convert_tokens_to_ids(UpperCAmelCase_ ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(UpperCAmelCase_ ) ) model.to(UpperCAmelCase_ ) # Load and encode the datasets def tokenize_and_encode(UpperCAmelCase_ : Any ): if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(UpperCAmelCase_ ) ) elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): return obj return [tokenize_and_encode(UpperCAmelCase_ ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(UpperCAmelCase_ ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(UpperCAmelCase_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , *UpperCAmelCase_ ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*UpperCAmelCase_ ) A__ = RandomSampler(UpperCAmelCase_ ) A__ = DataLoader(UpperCAmelCase_ , sampler=UpperCAmelCase_ , batch_size=args.train_batch_size ) A__ = TensorDataset(*UpperCAmelCase_ ) A__ = SequentialSampler(UpperCAmelCase_ ) A__ = DataLoader(UpperCAmelCase_ , sampler=UpperCAmelCase_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(UpperCAmelCase_ ) // args.gradient_accumulation_steps) + 1 else: A__ = len(UpperCAmelCase_ ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(UpperCAmelCase_ , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( UpperCAmelCase_ , num_warmup_steps=args.warmup_steps , num_training_steps=UpperCAmelCase_ ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(UpperCAmelCase_ , desc="""Training""" ) for step, batch in enumerate(UpperCAmelCase_ ): A__ = tuple(t.to(UpperCAmelCase_ ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(UpperCAmelCase_ , mc_token_ids=UpperCAmelCase_ , lm_labels=UpperCAmelCase_ , mc_labels=UpperCAmelCase_ ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(UpperCAmelCase_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(UpperCAmelCase_ , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , UpperCAmelCase_ ) A__ = os.path.join(args.output_dir , UpperCAmelCase_ ) torch.save(model_to_save.state_dict() , UpperCAmelCase_ ) model_to_save.config.to_json_file(UpperCAmelCase_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(UpperCAmelCase_ ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(UpperCAmelCase_ , desc="""Evaluating""" ): A__ = tuple(t.to(UpperCAmelCase_ ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( UpperCAmelCase_ , mc_token_ids=UpperCAmelCase_ , lm_labels=UpperCAmelCase_ , mc_labels=UpperCAmelCase_ ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(UpperCAmelCase_ , UpperCAmelCase_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(UpperCAmelCase_ , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , UpperCAmelCase_ , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
335
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE_ : int = [ 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the' ' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe' ' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.', 'The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal' ' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s' ' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the' ' body.', 'Amnesty International releases its annual report on the death penalty. The report catalogs the use of' ' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the' ' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital' ' punishment.', ] SCREAMING_SNAKE_CASE_ : List[Any] = [ 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .' ' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz' ' had informed his Lufthansa training school of an episode of severe depression, airline says .', 'Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .' ' Israel and the United States opposed the move, which could open the door to war crimes investigations against' ' Israelis .', 'Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to' ' death . Organization claims that governments around the world are using the threat of terrorism to advance' ' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death' ' sentences up by 28% .', ] def _snake_case ( ): A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2""", """rougeL"""] ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2"""] ) assert ( pd.DataFrame(no_aggregation["""rouge2"""] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["""rouge2"""] ).fmeasure.mean() ) def _snake_case ( ): A__ = """rougeLsum""" A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _snake_case ( ): A__ = ["""rouge1""", """rouge2""", """rougeL"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) assert score_sep == score_no_sep def _snake_case ( ): A__ = [ """Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.""", """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""", ] A__ = [ """Margot Frank, died in 1945, a month earlier than previously thought.""", """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of""" """ the final seconds on board Flight 9525.""", ] assert calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) == calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) def _snake_case ( ): A__ = [ """\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" """ ] A__ = [ """ Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .""" ] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] , newline_sep=UpperCAmelCase_ )["""rougeLsum"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] )["""rougeLsum"""] assert new_score > prev_score def _snake_case ( ): A__ = Path("""examples/seq2seq/test_data/wmt_en_ro""" ) A__ = calculate_rouge_path(data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge_path( data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) , bootstrap_aggregation=UpperCAmelCase_ ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig SCREAMING_SNAKE_CASE_ : List[str] = { 'google/tapas-base-finetuned-sqa': ( 'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json' ), 'google/tapas-base-finetuned-wtq': ( 'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json' ), 'google/tapas-base-finetuned-wikisql-supervised': ( 'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json' ), 'google/tapas-base-finetuned-tabfact': ( 'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json' ), } class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "tapas" def __init__( self: str , UpperCamelCase: str=3_05_22 , UpperCamelCase: int=7_68 , UpperCamelCase: Union[str, Any]=12 , UpperCamelCase: List[str]=12 , UpperCamelCase: Union[str, Any]=30_72 , UpperCamelCase: str="gelu" , UpperCamelCase: Optional[int]=0.1 , UpperCamelCase: str=0.1 , UpperCamelCase: Union[str, Any]=10_24 , UpperCamelCase: Tuple=[3, 2_56, 2_56, 2, 2_56, 2_56, 10] , UpperCamelCase: Any=0.02 , UpperCamelCase: List[str]=1e-1_2 , UpperCamelCase: Tuple=0 , UpperCamelCase: str=10.0 , UpperCamelCase: int=0 , UpperCamelCase: Tuple=1.0 , UpperCamelCase: Tuple=None , UpperCamelCase: Any=1.0 , UpperCamelCase: int=False , UpperCamelCase: Optional[Any]=None , UpperCamelCase: int=1.0 , UpperCamelCase: List[Any]=1.0 , UpperCamelCase: Dict=False , UpperCamelCase: List[str]=False , UpperCamelCase: List[Any]="ratio" , UpperCamelCase: Any=None , UpperCamelCase: Optional[Any]=None , UpperCamelCase: Tuple=64 , UpperCamelCase: Union[str, Any]=32 , UpperCamelCase: Union[str, Any]=False , UpperCamelCase: List[str]=True , UpperCamelCase: str=False , UpperCamelCase: str=False , UpperCamelCase: Optional[int]=True , UpperCamelCase: int=False , UpperCamelCase: str=None , UpperCamelCase: str=None , **UpperCamelCase: List[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCamelCase , **UpperCamelCase ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) 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__ = type_vocab_sizes A__ = initializer_range A__ = layer_norm_eps # Fine-tuning task hyperparameters A__ = positive_label_weight A__ = num_aggregation_labels A__ = aggregation_loss_weight A__ = use_answer_as_supervision A__ = answer_loss_importance A__ = use_normalized_answer_loss A__ = huber_loss_delta A__ = temperature A__ = aggregation_temperature A__ = use_gumbel_for_cells A__ = use_gumbel_for_aggregation A__ = average_approximation_function A__ = cell_selection_preference A__ = answer_loss_cutoff A__ = max_num_rows A__ = max_num_columns A__ = average_logits_per_cell A__ = select_one_column A__ = allow_empty_column_selection A__ = init_cell_selection_weights_to_zero A__ = reset_position_index_per_cell A__ = disable_per_token_loss # Aggregation hyperparameters A__ = aggregation_labels A__ = no_aggregation_label_index if isinstance(self.aggregation_labels , UpperCamelCase ): A__ = {int(UpperCamelCase ): v for k, v in aggregation_labels.items()}
335
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'MobileNetV1Config' # Base docstring SCREAMING_SNAKE_CASE_ : str = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : List[str] = [1, 1_0_2_4, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ : Optional[Any] = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : Tuple = 'tabby, tabby cat' SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None ): A__ = {} if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = model.mobilenet_va else: A__ = model A__ = """MobilenetV1/Conv2d_0/""" A__ = backbone.conv_stem.convolution.weight A__ = backbone.conv_stem.normalization.bias A__ = backbone.conv_stem.normalization.weight A__ = backbone.conv_stem.normalization.running_mean A__ = backbone.conv_stem.normalization.running_var for i in range(13 ): A__ = i + 1 A__ = i * 2 A__ = backbone.layer[pt_index] A__ = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var A__ = backbone.layer[pt_index + 1] A__ = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = """MobilenetV1/Logits/Conv2d_1c_1x1/""" A__ = model.classifier.weight A__ = model.classifier.bias return tf_to_pt_map def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model A__ = tf.train.list_variables(UpperCAmelCase_ ) A__ = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) A__ = tf.train.load_variable(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = array # Build TF to PyTorch weights loading map A__ = _build_tf_to_pytorch_map(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue A__ = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) A__ = np.transpose(UpperCAmelCase_ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer A__ = array.squeeze().transpose() else: A__ = np.transpose(UpperCAmelCase_ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) A__ = torch.from_numpy(UpperCAmelCase_ ) tf_weights.pop(UpperCAmelCase_ , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp""" , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp_1""" , UpperCAmelCase_ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , UpperCAmelCase_ ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _snake_case ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : nn.Convad ): A__ , A__ = features.shape[-2:] A__ , A__ = conv_layer.stride A__ , A__ = conv_layer.kernel_size if in_height % stride_height == 0: A__ = max(kernel_height - stride_height , 0 ) else: A__ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: A__ = max(kernel_width - stride_width , 0 ) else: A__ = max(kernel_width - (in_width % stride_width) , 0 ) A__ = pad_along_width // 2 A__ = pad_along_width - pad_left A__ = pad_along_height // 2 A__ = pad_along_height - pad_top A__ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(UpperCAmelCase_ , UpperCAmelCase_ , """constant""" , 0.0 ) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: bool = False , UpperCamelCase: Optional[bool] = True , UpperCamelCase: Optional[bool or str] = True , ): """simple docstring""" super().__init__() A__ = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) A__ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) A__ = nn.Convad( in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=UpperCamelCase , stride=UpperCamelCase , padding=UpperCamelCase , groups=UpperCamelCase , bias=UpperCamelCase , padding_mode="""zeros""" , ) if use_normalization: A__ = nn.BatchNormad( num_features=UpperCamelCase , eps=config.layer_norm_eps , momentum=0.9_997 , affine=UpperCamelCase , track_running_stats=UpperCamelCase , ) else: A__ = None if use_activation: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = ACTaFN[use_activation] elif isinstance(config.hidden_act , UpperCamelCase ): A__ = ACTaFN[config.hidden_act] else: A__ = config.hidden_act else: A__ = None def UpperCamelCase ( self: List[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" if self.config.tf_padding: A__ = apply_tf_padding(UpperCamelCase , self.convolution ) A__ = self.convolution(UpperCamelCase ) if self.normalization is not None: A__ = self.normalization(UpperCamelCase ) if self.activation is not None: A__ = self.activation(UpperCamelCase ) return features class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = MobileNetVaConfig UpperCAmelCase = load_tf_weights_in_mobilenet_va UpperCAmelCase = "mobilenet_v1" UpperCAmelCase = "pixel_values" UpperCAmelCase = False def UpperCamelCase ( self: Any , UpperCamelCase: Union[nn.Linear, nn.Convad] ): """simple docstring""" if isinstance(UpperCamelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(UpperCamelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: bool = True ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config A__ = 32 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) A__ = MobileNetVaConvLayer( UpperCamelCase , in_channels=config.num_channels , out_channels=UpperCamelCase , kernel_size=3 , stride=2 , ) A__ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] A__ = nn.ModuleList() for i in range(13 ): A__ = out_channels if strides[i] == 2 or i == 0: depth *= 2 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=3 , stride=strides[i] , groups=UpperCamelCase , ) ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=1 , ) ) A__ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) A__ = self.conv_stem(UpperCamelCase ) A__ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): A__ = layer_module(UpperCamelCase ) if output_hidden_states: A__ = all_hidden_states + (hidden_states,) A__ = hidden_states if self.pooler is not None: A__ = torch.flatten(self.pooler(UpperCamelCase ) , start_dim=1 ) else: A__ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase , pooler_output=UpperCamelCase , hidden_states=UpperCamelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = MobileNetVaModel(UpperCamelCase ) A__ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head A__ = nn.Dropout(config.classifier_dropout_prob , inplace=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.mobilenet_va(UpperCamelCase , output_hidden_states=UpperCamelCase , return_dict=UpperCamelCase ) A__ = outputs.pooler_output if return_dict else outputs[1] A__ = self.classifier(self.dropout(UpperCamelCase ) ) A__ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: A__ = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): A__ = """single_label_classification""" else: A__ = """multi_label_classification""" if self.config.problem_type == "regression": A__ = MSELoss() if self.num_labels == 1: A__ = loss_fct(logits.squeeze() , labels.squeeze() ) else: A__ = loss_fct(UpperCamelCase , UpperCamelCase ) elif self.config.problem_type == "single_label_classification": A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": A__ = BCEWithLogitsLoss() A__ = loss_fct(UpperCamelCase , UpperCamelCase ) if not return_dict: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=UpperCamelCase , logits=UpperCamelCase , hidden_states=outputs.hidden_states , )
335
1
"""simple docstring""" from __future__ import annotations SCREAMING_SNAKE_CASE_ : str = 8.9_8_8E9 # units = N * m^s * C^-2 def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float ): A__ = abs(chargea * chargea ) if (force, chargea, chargea, distance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if distance < 0: raise ValueError("""Distance cannot be negative""" ) if force == 0: A__ = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: A__ = abs(UpperCAmelCase_ ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: A__ = abs(UpperCAmelCase_ ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: A__ = (COULOMBS_CONSTANT * charge_product / abs(UpperCAmelCase_ )) ** 0.5 return {"distance": distance} raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : set ): A__ , A__ = len(UpperCAmelCase_ ), len(grid[0] ) if ( min(UpperCAmelCase_ , UpperCAmelCase_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(UpperCAmelCase_ , row + 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , row - 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col + 1 , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col - 1 , UpperCAmelCase_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
335
1
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" from __future__ import annotations import pandas as pd def _snake_case ( UpperCAmelCase_ : list[int] , UpperCAmelCase_ : list[int] , UpperCAmelCase_ : int ): A__ = [0] * no_of_processes A__ = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(UpperCAmelCase_ ): A__ = burst_time[i] A__ = 0 A__ = 0 A__ = 9_9999_9999 A__ = 0 A__ = False # Process until all processes are completed while complete != no_of_processes: for j in range(UpperCAmelCase_ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: A__ = remaining_time[j] A__ = j A__ = True if not check: increment_time += 1 continue remaining_time[short] -= 1 A__ = remaining_time[short] if minm == 0: A__ = 9_9999_9999 if remaining_time[short] == 0: complete += 1 A__ = False # Find finish time of current process A__ = increment_time + 1 # Calculate waiting time A__ = finish_time - arrival_time[short] A__ = finar - burst_time[short] if waiting_time[short] < 0: A__ = 0 # Increment time increment_time += 1 return waiting_time def _snake_case ( UpperCAmelCase_ : list[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : list[int] ): A__ = [0] * no_of_processes for i in range(UpperCAmelCase_ ): A__ = burst_time[i] + waiting_time[i] return turn_around_time def _snake_case ( UpperCAmelCase_ : list[int] , UpperCAmelCase_ : list[int] , UpperCAmelCase_ : int ): A__ = 0 A__ = 0 for i in range(UpperCAmelCase_ ): A__ = total_waiting_time + waiting_time[i] A__ = total_turn_around_time + turn_around_time[i] print(F"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print("""Average turn around time =""" , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('Enter how many process you want to analyze') SCREAMING_SNAKE_CASE_ : str = int(input()) SCREAMING_SNAKE_CASE_ : List[Any] = [0] * no_of_processes SCREAMING_SNAKE_CASE_ : Dict = [0] * no_of_processes SCREAMING_SNAKE_CASE_ : Optional[Any] = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('Enter the arrival time and burst time for process:--' + str(i + 1)) SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ : Tuple = map(int, input().split()) SCREAMING_SNAKE_CASE_ : Optional[int] = calculate_waitingtime(arrival_time, burst_time, no_of_processes) SCREAMING_SNAKE_CASE_ : Dict = burst_time SCREAMING_SNAKE_CASE_ : Optional[Any] = no_of_processes SCREAMING_SNAKE_CASE_ : Optional[Any] = waiting_time SCREAMING_SNAKE_CASE_ : Optional[int] = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) SCREAMING_SNAKE_CASE_ : str = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ 'Process', 'BurstTime', 'ArrivalTime', 'WaitingTime', 'TurnAroundTime', ], ) # Printing the dataFrame pd.set_option('display.max_rows', fcfs.shape[0] + 1) print(fcfs)
335
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_pegasus_x': ['PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusXConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST', 'PegasusXForConditionalGeneration', 'PegasusXModel', 'PegasusXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC SCREAMING_SNAKE_CASE_ : str = parse(importlib.metadata.version('torch')) def _snake_case ( UpperCAmelCase_ : Union[str, Version] , UpperCAmelCase_ : str , UpperCAmelCase_ : str ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) A__ = STR_OPERATION_TO_FUNC[operation] if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = parse(importlib.metadata.version(UpperCAmelCase_ ) ) return operation(UpperCAmelCase_ , parse(UpperCAmelCase_ ) ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : str ): return compare_versions(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE_ : Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : str = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} SCREAMING_SNAKE_CASE_ : str = { 'vocab_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json' ), }, 'merges_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt' ), }, } SCREAMING_SNAKE_CASE_ : Optional[int] = { 'allenai/longformer-base-4096': 4_0_9_6, 'allenai/longformer-large-4096': 4_0_9_6, 'allenai/longformer-large-4096-finetuned-triviaqa': 4_0_9_6, 'allenai/longformer-base-4096-extra.pos.embd.only': 4_0_9_6, 'allenai/longformer-large-4096-extra.pos.embd.only': 4_0_9_6, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def _snake_case ( ): 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 _snake_case ( UpperCAmelCase_ : int ): A__ = set() A__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A__ = char return pairs class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] def __init__( self: str , UpperCamelCase: Optional[int] , UpperCamelCase: Any , UpperCamelCase: Optional[Any]="replace" , UpperCamelCase: Optional[Any]="<s>" , UpperCamelCase: Optional[Any]="</s>" , UpperCamelCase: Dict="</s>" , UpperCamelCase: Optional[Any]="<s>" , UpperCamelCase: Optional[int]="<unk>" , UpperCamelCase: List[str]="<pad>" , UpperCamelCase: Dict="<mask>" , UpperCamelCase: Tuple=False , **UpperCamelCase: List[Any] , ): """simple docstring""" A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else bos_token A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else eos_token A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else sep_token A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else cls_token A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else unk_token A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A__ = AddedToken(UpperCamelCase , lstrip=UpperCamelCase , rstrip=UpperCamelCase ) if isinstance(UpperCamelCase , UpperCamelCase ) else mask_token super().__init__( errors=UpperCamelCase , bos_token=UpperCamelCase , eos_token=UpperCamelCase , unk_token=UpperCamelCase , sep_token=UpperCamelCase , cls_token=UpperCamelCase , pad_token=UpperCamelCase , mask_token=UpperCamelCase , add_prefix_space=UpperCamelCase , **UpperCamelCase , ) with open(UpperCamelCase , encoding="""utf-8""" ) as vocab_handle: A__ = json.load(UpperCamelCase ) 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(UpperCamelCase , 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(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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 def UpperCamelCase ( self: str ): """simple docstring""" return len(self.encoder ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def UpperCamelCase ( self: Any , UpperCamelCase: Dict ): """simple docstring""" if token in self.cache: return self.cache[token] A__ = tuple(UpperCamelCase ) A__ = get_pairs(UpperCamelCase ) if not pairs: return token while True: A__ = min(UpperCamelCase , key=lambda UpperCamelCase : self.bpe_ranks.get(UpperCamelCase , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break A__ , A__ = bigram A__ = [] A__ = 0 while i < len(UpperCamelCase ): try: A__ = word.index(UpperCamelCase , UpperCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A__ = j if word[i] == first and i < len(UpperCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A__ = tuple(UpperCamelCase ) A__ = new_word if len(UpperCamelCase ) == 1: break else: A__ = get_pairs(UpperCamelCase ) A__ = """ """.join(UpperCamelCase ) A__ = word return word def UpperCamelCase ( self: str , UpperCamelCase: List[Any] ): """simple docstring""" A__ = [] for token in re.findall(self.pat , UpperCamelCase ): 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(UpperCamelCase ).split(""" """ ) ) return bpe_tokens def UpperCamelCase ( self: Any , UpperCamelCase: str ): """simple docstring""" return self.encoder.get(UpperCamelCase , self.encoder.get(self.unk_token ) ) def UpperCamelCase ( self: str , UpperCamelCase: Dict ): """simple docstring""" return self.decoder.get(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[Any] ): """simple docstring""" A__ = """""".join(UpperCamelCase ) A__ = bytearray([self.byte_decoder[c] for c in text] ).decode("""utf-8""" , errors=self.errors ) return text def UpperCamelCase ( self: List[str] , UpperCamelCase: str , UpperCamelCase: Optional[str] = None ): """simple docstring""" if not os.path.isdir(UpperCamelCase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A__ = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=UpperCamelCase , ensure_ascii=UpperCamelCase ) + """\n""" ) A__ = 0 with open(UpperCamelCase , """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(UpperCamelCase ) + """\n""" ) index += 1 return vocab_file, merge_file def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: List[int] , UpperCamelCase: Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A__ = [self.cls_token_id] A__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase ( self: List[Any] , UpperCamelCase: List[int] , UpperCamelCase: Optional[List[int]] = None , UpperCamelCase: bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase , token_ids_a=UpperCamelCase , already_has_special_tokens=UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(UpperCamelCase )) + [1] return [1] + ([0] * len(UpperCamelCase )) + [1, 1] + ([0] * len(UpperCamelCase )) + [1] def UpperCamelCase ( self: Dict , UpperCamelCase: List[int] , UpperCamelCase: Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def UpperCamelCase ( self: List[str] , UpperCamelCase: Union[str, Any] , UpperCamelCase: List[str]=False , **UpperCamelCase: List[str] ): """simple docstring""" A__ = kwargs.pop("""add_prefix_space""" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(UpperCamelCase ) > 0 and not text[0].isspace()): A__ = """ """ + text return (text, kwargs)
335
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , 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/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != 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(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
1
"""simple docstring""" import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["image_processor", "tokenizer"] UpperCAmelCase = "LayoutLMv2ImageProcessor" UpperCAmelCase = ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self: Any , UpperCamelCase: Dict=None , UpperCamelCase: str=None , **UpperCamelCase: List[str] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , UpperCamelCase , ) A__ = kwargs.pop("""feature_extractor""" ) A__ = 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__(UpperCamelCase , UpperCamelCase ) def __call__( self: Tuple , UpperCamelCase: int , UpperCamelCase: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , UpperCamelCase: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , UpperCamelCase: Union[List[List[int]], List[List[List[int]]]] = None , UpperCamelCase: Optional[Union[List[int], List[List[int]]]] = None , UpperCamelCase: bool = True , UpperCamelCase: Union[bool, str, PaddingStrategy] = False , UpperCamelCase: Union[bool, str, TruncationStrategy] = None , UpperCamelCase: Optional[int] = None , UpperCamelCase: int = 0 , UpperCamelCase: Optional[int] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = True , UpperCamelCase: Optional[Union[str, TensorType]] = None , **UpperCamelCase: int , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( """You cannot provide bounding boxes """ """if you initialized the image processor with apply_ocr set to True.""" ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( """You cannot provide word labels if you initialized the image processor with apply_ocr set to True.""" ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError("""You cannot return overflowing tokens without returning the offsets mapping.""" ) # first, apply the image processor A__ = self.image_processor(images=UpperCamelCase , return_tensors=UpperCamelCase ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = [text] # add batch dimension (as the image processor always adds a batch dimension) A__ = features["""words"""] A__ = self.tokenizer( text=text if text is not None else features["""words"""] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features["""boxes"""] , word_labels=UpperCamelCase , add_special_tokens=UpperCamelCase , padding=UpperCamelCase , truncation=UpperCamelCase , max_length=UpperCamelCase , stride=UpperCamelCase , pad_to_multiple_of=UpperCamelCase , return_token_type_ids=UpperCamelCase , return_attention_mask=UpperCamelCase , return_overflowing_tokens=UpperCamelCase , return_special_tokens_mask=UpperCamelCase , return_offsets_mapping=UpperCamelCase , return_length=UpperCamelCase , verbose=UpperCamelCase , return_tensors=UpperCamelCase , **UpperCamelCase , ) # add pixel values A__ = features.pop("""pixel_values""" ) if return_overflowing_tokens is True: A__ = self.get_overflowing_images(UpperCamelCase , encoded_inputs["""overflow_to_sample_mapping"""] ) A__ = images return encoded_inputs def UpperCamelCase ( self: List[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: List[Any] ): """simple docstring""" A__ = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(UpperCamelCase ) != len(UpperCamelCase ): raise ValueError( """Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got""" f""" {len(UpperCamelCase )} and {len(UpperCamelCase )}""" ) return images_with_overflow def UpperCamelCase ( self: str , *UpperCamelCase: Any , **UpperCamelCase: Dict ): """simple docstring""" return self.tokenizer.batch_decode(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: List[str] , *UpperCamelCase: List[str] , **UpperCamelCase: Tuple ): """simple docstring""" return self.tokenizer.decode(*UpperCamelCase , **UpperCamelCase ) @property def UpperCamelCase ( self: Any ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , UpperCamelCase , ) return self.image_processor_class @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , UpperCamelCase , ) return self.image_processor
335
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple=None , UpperCamelCase: Tuple=None , **UpperCamelCase: Dict ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = eval_examples A__ = post_process_function def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Dataset] = None , UpperCamelCase: List[Any]=None , UpperCamelCase: Optional[List[str]] = None , UpperCamelCase: str = "eval" , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(UpperCamelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCamelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCamelCase ) return metrics def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Dict=None , UpperCamelCase: str = "test" , **UpperCamelCase: Optional[int] ): """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(UpperCamelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase , """predict""" ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase )
335
1
"""simple docstring""" from __future__ import annotations import math class a : """simple docstring""" def __init__( self: List[str] , UpperCamelCase: int ): """simple docstring""" A__ = size # approximate the overall size of segment tree with given value A__ = [0 for i in range(0 , 4 * size )] # create array to store lazy update A__ = [0 for i in range(0 , 4 * size )] A__ = [0 for i in range(0 , 4 * size )] # flag for lazy update def UpperCamelCase ( self: Tuple , UpperCamelCase: int ): """simple docstring""" return idx * 2 def UpperCamelCase ( self: List[Any] , UpperCamelCase: int ): """simple docstring""" return idx * 2 + 1 def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: list[int] ): """simple docstring""" if left_element == right_element: A__ = a[left_element - 1] else: A__ = (left_element + right_element) // 2 self.build(self.left(UpperCamelCase ) , UpperCamelCase , UpperCamelCase , UpperCamelCase ) self.build(self.right(UpperCamelCase ) , mid + 1 , UpperCamelCase , UpperCamelCase ) A__ = max( self.segment_tree[self.left(UpperCamelCase )] , self.segment_tree[self.right(UpperCamelCase )] ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if self.flag[idx] is True: A__ = self.lazy[idx] A__ = False if left_element != right_element: A__ = self.lazy[idx] A__ = self.lazy[idx] A__ = True A__ = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: A__ = val if left_element != right_element: A__ = val A__ = val A__ = True A__ = True return True A__ = (left_element + right_element) // 2 self.update(self.left(UpperCamelCase ) , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) self.update(self.right(UpperCamelCase ) , mid + 1 , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = max( self.segment_tree[self.left(UpperCamelCase )] , self.segment_tree[self.right(UpperCamelCase )] ) return True def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if self.flag[idx] is True: A__ = self.lazy[idx] A__ = False if left_element != right_element: A__ = self.lazy[idx] A__ = self.lazy[idx] A__ = True A__ = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] A__ = (left_element + right_element) // 2 A__ = self.query(self.left(UpperCamelCase ) , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.query(self.right(UpperCamelCase ) , mid + 1 , UpperCamelCase , UpperCamelCase , UpperCamelCase ) return max(UpperCamelCase , UpperCamelCase ) def __str__( self: Tuple ): """simple docstring""" return str([self.query(1 , 1 , self.size , UpperCamelCase , UpperCamelCase ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8] SCREAMING_SNAKE_CASE_ : Optional[Any] = 1_5 SCREAMING_SNAKE_CASE_ : Optional[Any] = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 1_1)) print(segt.query(1, 1, size, 7, 1_2)) segt.update(1, 1, size, 1, 3, 1_1_1) print(segt.query(1, 1, size, 1, 1_5)) segt.update(1, 1, size, 7, 8, 2_3_5) print(segt)
335
"""simple docstring""" class a : """simple docstring""" def __init__( self: Dict ): """simple docstring""" A__ = {} def UpperCamelCase ( self: List[str] ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase , """ -> """ , """ -> """.join([str(UpperCamelCase ) for j in self.vertex[i]] ) ) def UpperCamelCase ( self: Any , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase ) else: # else make a new vertex A__ = [to_vertex] def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: list ): """simple docstring""" A__ = True print(UpperCamelCase , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[int] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 200 ): A__ = [1, 2, 5, 10, 20, 50, 100, 200] A__ = [0] * (pence + 1) A__ = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(UpperCAmelCase_ , pence + 1 , 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(2_0_0) == 7_3_6_8_2
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad ): A__ = end_pointa[0] - end_pointa[0] A__ = end_pointa[1] - end_pointa[1] A__ = end_pointa[2] - end_pointa[2] return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : Vectorad ): A__ = ab[1] * ac[2] - ab[2] * ac[1] # *i A__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j A__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : int ): return tuple(round(UpperCAmelCase_ , UpperCAmelCase_ ) for x in vector ) == (0, 0, 0) def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : int = 10 ): A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ )
335
1
"""simple docstring""" 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 SCREAMING_SNAKE_CASE_ : int = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : Union[str, Any] = {'vocab_file': 'vocab.txt', 'emoji_file': 'emoji.json'} SCREAMING_SNAKE_CASE_ : Optional[Any] = { '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', }, } SCREAMING_SNAKE_CASE_ : Tuple = { 'abeja/gpt-neox-japanese-2.7b': 2_0_4_8, } def _snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Tuple ): with open(UpperCAmelCase_ , """r""" , encoding="""utf-8""" ) as f: A__ = json.loads(f.read() ) A__ = collections.OrderedDict() A__ = collections.OrderedDict() A__ = collections.OrderedDict() with open(UpperCAmelCase_ , """r""" , encoding="""utf-8""" ) as f: A__ = f.readlines() A__ = [[t.rstrip("""\n""" )] if (t == """,""" or """,""" not in t) else t.rstrip("""\n""" ).split(""",""" ) for t in token] for idx, b in enumerate(UpperCAmelCase_ ): A__ = b A__ = idx for wd in b: A__ = idx return vocab, raw_vocab, ids_to_tokens, emoji class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] def __init__( self: Dict , UpperCamelCase: List[Any] , UpperCamelCase: int , UpperCamelCase: Tuple="<|endoftext|>" , UpperCamelCase: Any="<|endoftext|>" , UpperCamelCase: str="<|startoftext|>" , UpperCamelCase: int="<|endoftext|>" , UpperCamelCase: List[str]=False , **UpperCamelCase: Tuple , ): """simple docstring""" super().__init__( unk_token=UpperCamelCase , pad_token=UpperCamelCase , bos_token=UpperCamelCase , eos_token=UpperCamelCase , do_clean_text=UpperCamelCase , **UpperCamelCase , ) if not os.path.isfile(UpperCamelCase ): 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(UpperCamelCase ): 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)`""" ) A__ = do_clean_text A__ , A__ , A__ , A__ = load_vocab_and_emoji(UpperCamelCase , UpperCamelCase ) A__ = SubWordJapaneseTokenizer( vocab=self.vocab , ids_to_tokens=self.ids_to_tokens , emoji=self.emoji ) @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" return len(self.raw_vocab ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" return dict(self.raw_vocab , **self.added_tokens_encoder ) def UpperCamelCase ( self: Any , UpperCamelCase: Union[str, Any] ): """simple docstring""" return self.subword_tokenizer.tokenize(UpperCamelCase , clean=self.do_clean_text ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Any ): """simple docstring""" return self.vocab.get(UpperCamelCase , self.vocab.get(self.unk_token ) ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict ): """simple docstring""" return self.subword_tokenizer.convert_id_to_token(UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Any ): """simple docstring""" A__ = """""".join(UpperCamelCase ).strip() return out_string def UpperCamelCase ( self: Tuple , UpperCamelCase: "Conversation" ): """simple docstring""" A__ = [] 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__ = input_ids[-self.model_max_length :] return input_ids def UpperCamelCase ( self: Optional[int] , UpperCamelCase: str , UpperCamelCase: Optional[str] = None ): """simple docstring""" A__ = 0 if os.path.isdir(UpperCamelCase ): A__ = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""emoji_file"""] ) else: A__ = ( (filename_prefix + """-""" if filename_prefix else """""") + save_directory + VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = ( (filename_prefix + """-""" if filename_prefix else """""") + save_directory + VOCAB_FILES_NAMES["""emoji_file"""] ) with open(UpperCamelCase , """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!""" ) A__ = token_index writer.write(""",""".join(UpperCamelCase ) + """\n""" ) index += 1 with open(UpperCamelCase , """w""" , encoding="""utf-8""" ) as writer: json.dump(self.emoji , UpperCamelCase ) return vocab_file, emoji_file class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: List[str] , UpperCamelCase: List[Any] , UpperCamelCase: Tuple , UpperCamelCase: Union[str, Any] ): """simple docstring""" A__ = vocab # same as swe A__ = ids_to_tokens # same as bpe A__ = emoji A__ = np.max([len(UpperCamelCase ) for w in self.vocab.keys()] ) A__ = re.compile(r"""(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)""" ) A__ = re.compile(r"""[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*""" ) A__ = re.compile(r"""[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}""" ) A__ = 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}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*""" ) A__ = 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}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*""" ) A__ = 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)*""" ) A__ = """─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿""" A__ = """▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟""" A__ = str.maketrans({k: """<BLOCK>""" for k in keisen + blocks} ) def __len__( self: Optional[Any] ): """simple docstring""" return len(self.ids_to_tokens ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" A__ = self.content_repattera.sub("""<URL>""" , UpperCamelCase ) A__ = self.content_repattera.sub("""<EMAIL>""" , UpperCamelCase ) A__ = self.content_repattera.sub("""<TEL>""" , UpperCamelCase ) A__ = self.content_repattera.sub("""<DATE>""" , UpperCamelCase ) A__ = self.content_repattera.sub("""<DATE>""" , UpperCamelCase ) A__ = self.content_repattera.sub("""<PRICE>""" , UpperCamelCase ) A__ = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: A__ = content.replace("""<BLOCK><BLOCK>""" , """<BLOCK>""" ) return content def UpperCamelCase ( self: Dict , UpperCamelCase: Union[str, Any] , UpperCamelCase: List[Any]=False ): """simple docstring""" A__ = text.replace(""" """ , """<SP>""" ) A__ = text.replace(""" """ , """<SP>""" ) A__ = text.replace("""\r\n""" , """<BR>""" ) A__ = text.replace("""\n""" , """<BR>""" ) A__ = text.replace("""\r""" , """<BR>""" ) A__ = text.replace("""\t""" , """<TAB>""" ) A__ = text.replace("""—""" , """ー""" ) A__ = text.replace("""−""" , """ー""" ) for k, v in self.emoji["emoji"].items(): if k in text: A__ = text.replace(UpperCamelCase , UpperCamelCase ) if clean: A__ = self.clean_text(UpperCamelCase ) def check_simbol(UpperCamelCase: str ): A__ = x.encode() if len(UpperCamelCase ) == 1 and len(UpperCamelCase ) == 2: A__ = (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(UpperCamelCase: List[str] ): A__ = x.encode() if len(UpperCamelCase ) == 1 and len(UpperCamelCase ) == 3: A__ = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0Xe28080 and c <= 0Xe2b07f: return True return False A__ = 0 A__ = [] while pos < len(UpperCamelCase ): A__ = min(len(UpperCamelCase ) , pos + self.maxlen + 1 ) if text[pos] == """<""" else pos + 3 A__ = [] # (token_id, token, pos) for e in range(UpperCamelCase , UpperCamelCase , -1 ): A__ = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(UpperCamelCase ) > 2: A__ = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(UpperCamelCase ) > 0: # the smallest token_id is adopted A__ , A__ , A__ = sorted(UpperCamelCase , key=lambda UpperCamelCase : x[0] )[0] result.append(UpperCamelCase ) A__ = e else: A__ = pos + 1 A__ = text[pos:end] if check_simbol(UpperCamelCase ): result.append("""<KIGOU>""" ) elif checkuae(UpperCamelCase ): result.append("""<U2000U2BFF>""" ) else: for i in wd.encode("""utf-8""" ): result.append("""<|byte%d|>""" % i ) A__ = end return result def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Dict , UpperCamelCase: Tuple="\n" ): """simple docstring""" A__ = [] A__ = [] A__ = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(UpperCamelCase ) > 0: words.append(bytearray(UpperCamelCase ).decode("""utf-8""" , errors="""replace""" ) ) A__ = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji["""emoji_inv"""][word] ) elif word == "<SP>": words.append(""" """ ) elif word == "<BR>": words.append(UpperCamelCase ) elif word == "<TAB>": words.append("""\t""" ) elif word == "<BLOCK>": words.append("""▀""" ) elif word == "<KIGOU>": words.append("""ǀ""" ) elif word == "<U2000U2BFF>": words.append("""‖""" ) else: words.append(UpperCamelCase ) if len(UpperCamelCase ) > 0: words.append(bytearray(UpperCamelCase ).decode("""utf-8""" , errors="""replace""" ) ) A__ = """""".join(UpperCamelCase ) return text
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_pegasus_x': ['PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusXConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST', 'PegasusXForConditionalGeneration', 'PegasusXModel', 'PegasusXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def _snake_case ( ): A__ = 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 _snake_case ( ): A__ = parse_args() # Import training_script as a module. A__ = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) A__ = script_fpath.stem A__ = importlib.import_module(UpperCAmelCase_ ) # Patch sys.argv A__ = [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()
335
"""simple docstring""" import math class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: List[str]=0 ): # a graph with Node 0,1,...,N-1 """simple docstring""" A__ = n A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # adjacency matrix for weight A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Tuple ): """simple docstring""" A__ = w def UpperCamelCase ( self: int ): """simple docstring""" for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): A__ = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase ( self: int , UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 1_0) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 1_0) 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)
335
1
"""simple docstring""" import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants SCREAMING_SNAKE_CASE_ : Optional[int] = Mapping[str, np.ndarray] SCREAMING_SNAKE_CASE_ : Any = Mapping[str, Any] # Is a nested dict. SCREAMING_SNAKE_CASE_ : List[Any] = 0.01 @dataclasses.dataclass(frozen=_lowerCamelCase ) class a : """simple docstring""" UpperCAmelCase = 42 # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. UpperCAmelCase = 42 # [num_res] # Binary float mask to indicate presence of a particular atom. 1.0 if an atom # is present and 0.0 if not. This should be used for loss masking. UpperCAmelCase = 42 # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. UpperCAmelCase = 42 # [num_res] # B-factors, or temperature factors, of each residue (in sq. angstroms units), # representing the displacement of the residue from its ground truth mean # value. UpperCAmelCase = 42 # [num_res, num_atom_type] # Chain indices for multi-chain predictions UpperCAmelCase = None # Optional remark about the protein. Included as a comment in output PDB # files UpperCAmelCase = None # Templates used to generate this protein (prediction-only) UpperCAmelCase = None # Chain corresponding to each parent UpperCAmelCase = None def _snake_case ( UpperCAmelCase_ : str ): A__ = R"""(\[[A-Z]+\]\n)""" A__ = [tag.strip() for tag in re.split(UpperCAmelCase_ , UpperCAmelCase_ ) if len(UpperCAmelCase_ ) > 0] A__ = zip(tags[0::2] , [l.split("""\n""" ) for l in tags[1::2]] ) A__ = ["N", "CA", "C"] A__ = None A__ = None A__ = None for g in groups: if "[PRIMARY]" == g[0]: A__ = g[1][0].strip() for i in range(len(UpperCAmelCase_ ) ): if seq[i] not in residue_constants.restypes: A__ = """X""" # FIXME: strings are immutable A__ = np.array( [residue_constants.restype_order.get(UpperCAmelCase_ , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: A__ = [] for axis in range(3 ): tertiary.append(list(map(UpperCAmelCase_ , g[1][axis].split() ) ) ) A__ = np.array(UpperCAmelCase_ ) A__ = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(UpperCAmelCase_ ): A__ = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: A__ = np.array(list(map({"""-""": 0, """+""": 1}.get , g[1][0].strip() ) ) ) A__ = np.zeros( ( len(UpperCAmelCase_ ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(UpperCAmelCase_ ): A__ = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=UpperCAmelCase_ , atom_mask=UpperCAmelCase_ , aatype=UpperCAmelCase_ , residue_index=np.arange(len(UpperCAmelCase_ ) ) , b_factors=UpperCAmelCase_ , ) def _snake_case ( UpperCAmelCase_ : Protein , UpperCAmelCase_ : int = 0 ): A__ = [] A__ = prot.remark if remark is not None: pdb_headers.append(F"""REMARK {remark}""" ) A__ = prot.parents A__ = prot.parents_chain_index if parents is not None and parents_chain_index is not None: A__ = [p for i, p in zip(UpperCAmelCase_ , UpperCAmelCase_ ) if i == chain_id] if parents is None or len(UpperCAmelCase_ ) == 0: A__ = ["""N/A"""] pdb_headers.append(F"""PARENT {' '.join(UpperCAmelCase_ )}""" ) return pdb_headers def _snake_case ( UpperCAmelCase_ : Protein , UpperCAmelCase_ : str ): A__ = [] A__ = pdb_str.split("""\n""" ) A__ = prot.remark if remark is not None: out_pdb_lines.append(F"""REMARK {remark}""" ) A__ = 42 if prot.parents is not None and len(prot.parents ) > 0: A__ = [] if prot.parents_chain_index is not None: A__ = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(UpperCAmelCase_ ) , [] ) parent_dict[str(UpperCAmelCase_ )].append(UpperCAmelCase_ ) A__ = max([int(UpperCAmelCase_ ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): A__ = parent_dict.get(str(UpperCAmelCase_ ) , ["""N/A"""] ) parents_per_chain.append(UpperCAmelCase_ ) else: parents_per_chain.append(list(prot.parents ) ) else: A__ = [["""N/A"""]] def make_parent_line(UpperCAmelCase_ : Sequence[str] ) -> str: return F"""PARENT {' '.join(UpperCAmelCase_ )}""" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) A__ = 0 for i, l in enumerate(UpperCAmelCase_ ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(UpperCAmelCase_ ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(UpperCAmelCase_ ): A__ = parents_per_chain[chain_counter] else: A__ = ["""N/A"""] out_pdb_lines.append(make_parent_line(UpperCAmelCase_ ) ) return "\n".join(UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : Protein ): A__ = residue_constants.restypes + ["""X"""] def res_atoa(UpperCAmelCase_ : int ) -> str: return residue_constants.restype_atoa.get(restypes[r] , """UNK""" ) A__ = residue_constants.atom_types A__ = [] A__ = prot.atom_mask A__ = prot.aatype A__ = prot.atom_positions A__ = prot.residue_index.astype(np.intaa ) A__ = prot.b_factors A__ = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError("""Invalid aatypes.""" ) A__ = get_pdb_headers(UpperCAmelCase_ ) if len(UpperCAmelCase_ ) > 0: pdb_lines.extend(UpperCAmelCase_ ) A__ = aatype.shape[0] A__ = 1 A__ = 0 A__ = string.ascii_uppercase A__ = None # Add all atom sites. for i in range(UpperCAmelCase_ ): A__ = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(UpperCAmelCase_ , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue A__ = """ATOM""" A__ = atom_name if len(UpperCAmelCase_ ) == 4 else F""" {atom_name}""" A__ = """""" A__ = """""" A__ = 1.00 A__ = atom_name[0] # Protein supports only C, N, O, S, this works. A__ = """""" A__ = """A""" if chain_index is not None: A__ = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! A__ = ( F"""{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}""" F"""{res_name_a:>3} {chain_tag:>1}""" F"""{residue_index[i]:>4}{insertion_code:>1} """ F"""{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}""" F"""{occupancy:>6.2f}{b_factor:>6.2f} """ F"""{element:>2}{charge:>2}""" ) pdb_lines.append(UpperCAmelCase_ ) atom_index += 1 A__ = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: A__ = True A__ = chain_index[i + 1] if should_terminate: # Close the chain. A__ = """TER""" A__ = ( F"""{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}""" ) pdb_lines.append(UpperCAmelCase_ ) atom_index += 1 if i != n - 1: # "prev" is a misnomer here. This happens at the beginning of # each new chain. pdb_lines.extend(get_pdb_headers(UpperCAmelCase_ , UpperCAmelCase_ ) ) pdb_lines.append("""END""" ) pdb_lines.append("""""" ) return "\n".join(UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : Protein ): return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def _snake_case ( UpperCAmelCase_ : FeatureDict , UpperCAmelCase_ : ModelOutput , UpperCAmelCase_ : Optional[np.ndarray] = None , UpperCAmelCase_ : Optional[np.ndarray] = None , UpperCAmelCase_ : Optional[str] = None , UpperCAmelCase_ : Optional[Sequence[str]] = None , UpperCAmelCase_ : Optional[Sequence[int]] = None , ): return Protein( aatype=features["""aatype"""] , atom_positions=result["""final_atom_positions"""] , atom_mask=result["""final_atom_mask"""] , residue_index=features["""residue_index"""] + 1 , b_factors=b_factors if b_factors is not None else np.zeros_like(result["""final_atom_mask"""] ) , chain_index=UpperCAmelCase_ , remark=UpperCAmelCase_ , parents=UpperCAmelCase_ , parents_chain_index=UpperCAmelCase_ , )
335
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def UpperCamelCase ( self: str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = (3, 32, 1_28) A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on A__ = dict(zip(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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(UpperCamelCase ) + """\n""" ) A__ = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 1_28}, } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta ) A__ = Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) return image_input def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """labels"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase ): processor() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.char_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) A__ = [seq.replace(""" """ , """""" ) for seq in decoded_tok] self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = None A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = torch.randn(1 , 27 , 38 ) A__ = torch.randn(1 , 27 , 5_02_57 ) A__ = torch.randn(1 , 27 , 3_05_22 ) A__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""] )
335
1
"""simple docstring""" from __future__ import annotations import math SCREAMING_SNAKE_CASE_ : Optional[int] = '2020.9.26' SCREAMING_SNAKE_CASE_ : Optional[int] = 'xcodz-dot, cclaus, dhruvmanila' def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if not all(isinstance(UpperCAmelCase_ , (float, int) ) for val in locals().values() ): A__ = F"""Input values must either be float or int: {list(locals().values() )}""" raise TypeError(UpperCAmelCase_ ) A__ = ((x * distance) / (z + distance)) * scale A__ = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : str , UpperCAmelCase_ : float ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise TypeError("""Axis must be a str""" ) A__ = locals() del input_variables["axis"] if not all(isinstance(UpperCAmelCase_ , (float, int) ) for val in input_variables.values() ): A__ = ( """Input values except axis must either be float or int: """ F"""{list(input_variables.values() )}""" ) raise TypeError(UpperCAmelCase_ ) A__ = (angle % 360) / 450 * 180 / math.pi if axis == "z": A__ = x * math.cos(UpperCAmelCase_ ) - y * math.sin(UpperCAmelCase_ ) A__ = y * math.cos(UpperCAmelCase_ ) + x * math.sin(UpperCAmelCase_ ) A__ = z elif axis == "x": A__ = y * math.cos(UpperCAmelCase_ ) - z * math.sin(UpperCAmelCase_ ) A__ = z * math.cos(UpperCAmelCase_ ) + y * math.sin(UpperCAmelCase_ ) A__ = x elif axis == "y": A__ = x * math.cos(UpperCAmelCase_ ) - z * math.sin(UpperCAmelCase_ ) A__ = z * math.cos(UpperCAmelCase_ ) + x * math.sin(UpperCAmelCase_ ) A__ = 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, 10.0, 10.0) = }""") print(f"""{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }""")
335
"""simple docstring""" import math def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase_ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
335
1
"""simple docstring""" import unittest from datasets import load_dataset from transformers.pipelines import pipeline from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow @is_pipeline_test @require_torch class a ( unittest.TestCase ): """simple docstring""" @require_torch def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = pipeline( task="""zero-shot-audio-classification""" , model="""hf-internal-testing/tiny-clap-htsat-unfused""" ) A__ = load_dataset("""ashraq/esc50""" ) A__ = dataset["""train"""]["""audio"""][-1]["""array"""] A__ = audio_classifier(UpperCamelCase , candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] ) self.assertEqual( nested_simplify(UpperCamelCase ) , [{"""score""": 0.501, """label""": """Sound of a dog"""}, {"""score""": 0.499, """label""": """Sound of vaccum cleaner"""}] , ) @unittest.skip("""No models are available in TF""" ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" pass @slow @require_torch def UpperCamelCase ( self: str ): """simple docstring""" A__ = pipeline( task="""zero-shot-audio-classification""" , model="""laion/clap-htsat-unfused""" , ) # This is an audio of a dog A__ = load_dataset("""ashraq/esc50""" ) A__ = dataset["""train"""]["""audio"""][-1]["""array"""] A__ = audio_classifier(UpperCamelCase , candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] ) self.assertEqual( nested_simplify(UpperCamelCase ) , [ {"""score""": 0.999, """label""": """Sound of a dog"""}, {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""}, ] , ) A__ = audio_classifier([audio] * 5 , candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] ) self.assertEqual( nested_simplify(UpperCamelCase ) , [ [ {"""score""": 0.999, """label""": """Sound of a dog"""}, {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""}, ], ] * 5 , ) A__ = audio_classifier( [audio] * 5 , candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] , batch_size=5 ) self.assertEqual( nested_simplify(UpperCamelCase ) , [ [ {"""score""": 0.999, """label""": """Sound of a dog"""}, {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""}, ], ] * 5 , ) @unittest.skip("""No models are available in TF""" ) def UpperCamelCase ( self: Dict ): """simple docstring""" pass
335
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = 1 A__ = 3 A__ = (32, 32) A__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase ) return image @property def UpperCamelCase ( self: int ): """simple docstring""" torch.manual_seed(0 ) A__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) return model @property def UpperCamelCase ( self: Tuple ): """simple docstring""" torch.manual_seed(0 ) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) return model @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" torch.manual_seed(0 ) A__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_06 , ) return RobertaSeriesModelWithTransformation(UpperCamelCase ) @property def UpperCamelCase ( self: str ): """simple docstring""" def extract(*UpperCamelCase: List[str] , **UpperCamelCase: Any ): class a : """simple docstring""" def __init__( self: Any ): """simple docstring""" A__ = torch.ones([0] ) def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" self.pixel_values.to(UpperCamelCase ) return self return Out() return extract def UpperCamelCase ( self: str ): """simple docstring""" A__ = """cpu""" # ensure determinism for the device-dependent torch.Generator A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) A__ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ) A__ = output.images A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , return_dict=UpperCamelCase , )[0] A__ = image[0, -3:, -3:, -1] A__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A__ = np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) # put models in fp16 A__ = unet.half() A__ = vae.half() A__ = bert.half() # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) # resize to resolution that is divisible by 8 but not 16 or 32 A__ = init_image.resize((7_60, 5_04) ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] A__ = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) A__ = np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) A__ = init_image.resize((7_68, 5_12) ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy""" ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
335
1
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = 0 # The first color of the flag. SCREAMING_SNAKE_CASE_ : int = 1 # The second color of the flag. SCREAMING_SNAKE_CASE_ : Dict = 2 # The third color of the flag. SCREAMING_SNAKE_CASE_ : Tuple = (red, white, blue) def _snake_case ( UpperCAmelCase_ : list ): if not sequence: return [] if len(UpperCAmelCase_ ) == 1: return list(UpperCAmelCase_ ) A__ = 0 A__ = len(UpperCAmelCase_ ) - 1 A__ = 0 while mid <= high: if sequence[mid] == colors[0]: A__ , A__ = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: A__ , A__ = sequence[high], sequence[mid] high -= 1 else: A__ = F"""The elements inside the sequence must contains only {colors} values""" raise ValueError(UpperCAmelCase_ ) return sequence if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE_ : Union[str, Any] = input('Enter numbers separated by commas:\n').strip() SCREAMING_SNAKE_CASE_ : Tuple = [int(item.strip()) for item in user_input.split(',')] print(f"""{dutch_national_flag_sort(unsorted)}""")
335
"""simple docstring""" import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = 0.01 with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): A__ = time.time() locka.acquire(UpperCAmelCase_ ) assert time.time() - _start > timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = """a""" * 1000 + """.lock""" A__ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(""".lock""" ) assert not locka._lock_file.endswith(UpperCAmelCase_ ) assert len(os.path.basename(locka._lock_file ) ) <= 255 A__ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): locka.acquire(0 )
335
1
"""simple docstring""" import os SCREAMING_SNAKE_CASE_ : Tuple = {'I': 1, 'V': 5, 'X': 1_0, 'L': 5_0, 'C': 1_0_0, 'D': 5_0_0, 'M': 1_0_0_0} def _snake_case ( UpperCAmelCase_ : str ): A__ = 0 A__ = 0 while index < len(UpperCAmelCase_ ) - 1: A__ = SYMBOLS[numerals[index]] A__ = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def _snake_case ( UpperCAmelCase_ : int ): A__ = """""" A__ = num // 1000 numerals += m_count * "M" num %= 1000 A__ = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 A__ = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def _snake_case ( UpperCAmelCase_ : str = "/p089_roman.txt" ): A__ = 0 with open(os.path.dirname(UpperCAmelCase_ ) + roman_numerals_filename ) as filea: A__ = filea.readlines() for line in lines: A__ = line.strip() A__ = parse_roman_numerals(UpperCAmelCase_ ) A__ = generate_roman_numerals(UpperCAmelCase_ ) savings += len(UpperCAmelCase_ ) - len(UpperCAmelCase_ ) return savings if __name__ == "__main__": print(f"""{solution() = }""")
335
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase = "image_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = AutoModelForVisualQuestionAnswering UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" return self.pre_processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) def UpperCamelCase ( self: str , UpperCamelCase: str ): """simple docstring""" with torch.no_grad(): return self.model(**UpperCamelCase ).logits def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int ): """simple docstring""" A__ = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
335
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: List[str] , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: List[str] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Optional[int] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: Optional[int] , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: List[Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: str , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Tuple , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: str , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: List[Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: str , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: int , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: List[Any] , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: str , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Union[str, Any] , *UpperCamelCase: Dict , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: List[str] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[Any] , *UpperCamelCase: List[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Optional[int] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: int , *UpperCamelCase: Tuple , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Optional[int] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: int , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Dict , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : int ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Union[str, Any] ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : int ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Tuple ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : int ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) def _snake_case ( *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : int ): requires_backends(UpperCAmelCase_ , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: List[str] , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: List[Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: Any , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: List[Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: Tuple , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: Dict , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: int , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: str , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: int , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: List[str] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: List[Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: Dict , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Tuple , *UpperCamelCase: int , **UpperCamelCase: Any ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: str , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Tuple , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Tuple , *UpperCamelCase: List[str] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: Any , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: int , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Tuple , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Optional[int] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: int ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Optional[int] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Dict , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: str , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: List[Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: int , *UpperCamelCase: str , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Dict , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: str , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Union[str, Any] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: int , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: List[str] , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: str , *UpperCamelCase: str , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Tuple , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: Dict , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: int , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: List[str] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: int , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: Optional[Any] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: List[Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: Optional[int] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: Any , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Dict , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: int ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Tuple , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: List[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[Any] , *UpperCamelCase: str , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[Any] , *UpperCamelCase: Any , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[Any] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: int , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Tuple , *UpperCamelCase: Tuple , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Any , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Any , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Union[str, Any] , *UpperCamelCase: int , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: str , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[Any] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: str , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: Dict , **UpperCamelCase: Any ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Tuple , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: Any , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: List[str] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: Dict , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: List[Any] , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: Tuple , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Dict , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: int , *UpperCamelCase: Optional[int] , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: List[Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: Any , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: Optional[int] , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: int , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Any , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[str] , *UpperCamelCase: int , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: str , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: int , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: Any , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: str , *UpperCamelCase: int , **UpperCamelCase: Dict ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: List[Any] , *UpperCamelCase: str , **UpperCamelCase: int ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Any , *UpperCamelCase: List[str] , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[int] , *UpperCamelCase: Dict , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: int , *UpperCamelCase: Dict , **UpperCamelCase: Tuple ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: List[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: List[Any] , **UpperCamelCase: Any ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: List[str] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: str , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: List[str] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Union[str, Any] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: str ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Dict , *UpperCamelCase: str , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[str] , *UpperCamelCase: List[str] , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Dict , *UpperCamelCase: List[Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Optional[Any] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: int ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: List[Any] , *UpperCamelCase: Optional[Any] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) class a ( metaclass=_lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["torch"] def __init__( self: Tuple , *UpperCamelCase: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" requires_backends(self , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Union[str, Any] , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[int] ): """simple docstring""" requires_backends(cls , ["""torch"""] ) @classmethod def UpperCamelCase ( cls: Tuple , *UpperCamelCase: Union[str, Any] , **UpperCamelCase: Optional[Any] ): """simple docstring""" requires_backends(cls , ["""torch"""] )
335
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( 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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE_ : Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : List[Any] = { 'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json', 'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json', 'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json', 'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json', 'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json', 'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json', 'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json', 'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json', 'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json', } class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "xmod" def __init__( self: Any , UpperCamelCase: Any=3_05_22 , UpperCamelCase: Any=7_68 , UpperCamelCase: Union[str, Any]=12 , UpperCamelCase: List[str]=12 , UpperCamelCase: Dict=30_72 , UpperCamelCase: Union[str, Any]="gelu" , UpperCamelCase: Union[str, Any]=0.1 , UpperCamelCase: Union[str, Any]=0.1 , UpperCamelCase: str=5_12 , UpperCamelCase: Tuple=2 , UpperCamelCase: Optional[int]=0.02 , UpperCamelCase: Optional[Any]=1e-1_2 , UpperCamelCase: str=1 , UpperCamelCase: List[Any]=0 , UpperCamelCase: int=2 , UpperCamelCase: Optional[Any]="absolute" , UpperCamelCase: int=True , UpperCamelCase: Union[str, Any]=None , UpperCamelCase: Union[str, Any]=False , UpperCamelCase: Dict=2 , UpperCamelCase: List[Any]=False , UpperCamelCase: int=True , UpperCamelCase: List[Any]=True , UpperCamelCase: str=("en_XX",) , UpperCamelCase: List[Any]=None , **UpperCamelCase: List[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCamelCase , bos_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , **UpperCamelCase ) 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__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout A__ = pre_norm A__ = adapter_reduction_factor A__ = adapter_layer_norm A__ = adapter_reuse_layer_norm A__ = ln_before_adapter A__ = list(UpperCamelCase ) A__ = default_language class a ( _lowerCamelCase ): """simple docstring""" @property def UpperCamelCase ( self: Dict ): """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : Dict ): # noqa: E741 A__ = len(UpperCAmelCase_ ) A__ = 0 A__ = [0] * n A__ = [False] * n A__ = [False] * n def dfs(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): if parent == root: out_edge_count += 1 A__ = True A__ = at for to in l[at]: if to == parent: pass elif not visited[to]: A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: A__ = True # AP found via cycle if at == low[to]: A__ = True else: A__ = min(low[at] , UpperCAmelCase_ ) return out_edge_count for i in range(UpperCAmelCase_ ): if not visited[i]: A__ = 0 A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , -1 , UpperCAmelCase_ ) A__ = out_edge_count > 1 for x in range(len(UpperCAmelCase_ ) ): if is_art[x] is True: print(UpperCAmelCase_ ) # Adjacency list of graph SCREAMING_SNAKE_CASE_ : Optional[int] = { 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)
335
1
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( 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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" A__ = get_activation("""swish""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""silu""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = get_activation("""mish""" ) self.assertIsInstance(UpperCamelCase , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""gelu""" ) self.assertIsInstance(UpperCamelCase , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
335
1
"""simple docstring""" from typing import Callable, Optional from .. import Features from ..packaged_modules.generator.generator import Generator from .abc import AbstractDatasetInputStream class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Callable , UpperCamelCase: Optional[Features] = None , UpperCamelCase: str = None , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: Optional[dict] = None , UpperCamelCase: Optional[int] = None , **UpperCamelCase: List[str] , ): """simple docstring""" super().__init__( features=UpperCamelCase , cache_dir=UpperCamelCase , keep_in_memory=UpperCamelCase , streaming=UpperCamelCase , num_proc=UpperCamelCase , **UpperCamelCase , ) A__ = Generator( cache_dir=UpperCamelCase , features=UpperCamelCase , generator=UpperCamelCase , gen_kwargs=UpperCamelCase , **UpperCamelCase , ) def UpperCamelCase ( self: Any ): """simple docstring""" if self.streaming: A__ = self.builder.as_streaming_dataset(split="""train""" ) # Build regular (map-style) dataset else: A__ = None A__ = None A__ = None A__ = None self.builder.download_and_prepare( download_config=UpperCamelCase , download_mode=UpperCamelCase , verification_mode=UpperCamelCase , base_path=UpperCamelCase , num_proc=self.num_proc , ) A__ = self.builder.as_dataset( split="""train""" , verification_mode=UpperCamelCase , in_memory=self.keep_in_memory ) return dataset
335
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
1
"""simple docstring""" import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging SCREAMING_SNAKE_CASE_ : Tuple = logging.get_logger(__name__) def _snake_case ( UpperCAmelCase_ : str=None , UpperCAmelCase_ : Any=None ): return field(default_factory=lambda: default , metadata=UpperCAmelCase_ ) @dataclass class a : """simple docstring""" UpperCAmelCase = list_field( default=[], metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) }, ) UpperCAmelCase = list_field( default=[8], metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) UpperCAmelCase = list_field( default=[8, 3_2, 1_2_8, 5_1_2], metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Use FP16 to accelerate inference."} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Benchmark training of model"} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Verbose memory tracing"} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."}, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" }, ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Trace memory line by line"} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Save result to a CSV file"} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Save all print statements in a log file"} ) UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Whether to print environment information"} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) }, ) UpperCAmelCase = field( default=F'inference_time_{round(time() )}.csv', metadata={"help": "CSV filename used if saving time results to csv."}, ) UpperCAmelCase = field( default=F'inference_memory_{round(time() )}.csv', metadata={"help": "CSV filename used if saving memory results to csv."}, ) UpperCAmelCase = field( default=F'train_time_{round(time() )}.csv', metadata={"help": "CSV filename used if saving time results to csv for training."}, ) UpperCAmelCase = field( default=F'train_memory_{round(time() )}.csv', metadata={"help": "CSV filename used if saving memory results to csv for training."}, ) UpperCAmelCase = field( default=F'env_info_{round(time() )}.csv', metadata={"help": "CSV filename used if saving environment information."}, ) UpperCAmelCase = field( default=F'log_{round(time() )}.csv', metadata={"help": "Log filename used if print statements are saved in log."}, ) UpperCAmelCase = field(default=3, metadata={"help": "Times an experiment will be run."} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) }, ) def UpperCamelCase ( self: int ): """simple docstring""" warnings.warn( f"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils""" """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , UpperCamelCase , ) def UpperCamelCase ( self: int ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def UpperCamelCase ( self: List[str] ): """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def UpperCamelCase ( self: Tuple ): """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
335
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE_ : int = [ 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the' ' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe' ' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.', 'The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal' ' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s' ' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the' ' body.', 'Amnesty International releases its annual report on the death penalty. The report catalogs the use of' ' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the' ' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital' ' punishment.', ] SCREAMING_SNAKE_CASE_ : List[Any] = [ 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .' ' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz' ' had informed his Lufthansa training school of an episode of severe depression, airline says .', 'Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .' ' Israel and the United States opposed the move, which could open the door to war crimes investigations against' ' Israelis .', 'Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to' ' death . Organization claims that governments around the world are using the threat of terrorism to advance' ' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death' ' sentences up by 28% .', ] def _snake_case ( ): A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2""", """rougeL"""] ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2"""] ) assert ( pd.DataFrame(no_aggregation["""rouge2"""] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["""rouge2"""] ).fmeasure.mean() ) def _snake_case ( ): A__ = """rougeLsum""" A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _snake_case ( ): A__ = ["""rouge1""", """rouge2""", """rougeL"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) assert score_sep == score_no_sep def _snake_case ( ): A__ = [ """Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.""", """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""", ] A__ = [ """Margot Frank, died in 1945, a month earlier than previously thought.""", """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of""" """ the final seconds on board Flight 9525.""", ] assert calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) == calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) def _snake_case ( ): A__ = [ """\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" """ ] A__ = [ """ Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .""" ] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] , newline_sep=UpperCAmelCase_ )["""rougeLsum"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] )["""rougeLsum"""] assert new_score > prev_score def _snake_case ( ): A__ = Path("""examples/seq2seq/test_data/wmt_en_ro""" ) A__ = calculate_rouge_path(data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge_path( data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) , bootstrap_aggregation=UpperCAmelCase_ ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" from PIL import Image def _snake_case ( UpperCAmelCase_ : Image , UpperCAmelCase_ : float ): def brightness(UpperCAmelCase_ : int ) -> float: return 128 + level + (c - 128) if not -2_55.0 <= level <= 2_55.0: raise ValueError("""level must be between -255.0 (black) and 255.0 (white)""" ) return img.point(UpperCAmelCase_ ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 SCREAMING_SNAKE_CASE_ : Optional[int] = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
335
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'MobileNetV1Config' # Base docstring SCREAMING_SNAKE_CASE_ : str = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : List[str] = [1, 1_0_2_4, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ : Optional[Any] = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : Tuple = 'tabby, tabby cat' SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None ): A__ = {} if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = model.mobilenet_va else: A__ = model A__ = """MobilenetV1/Conv2d_0/""" A__ = backbone.conv_stem.convolution.weight A__ = backbone.conv_stem.normalization.bias A__ = backbone.conv_stem.normalization.weight A__ = backbone.conv_stem.normalization.running_mean A__ = backbone.conv_stem.normalization.running_var for i in range(13 ): A__ = i + 1 A__ = i * 2 A__ = backbone.layer[pt_index] A__ = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var A__ = backbone.layer[pt_index + 1] A__ = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = """MobilenetV1/Logits/Conv2d_1c_1x1/""" A__ = model.classifier.weight A__ = model.classifier.bias return tf_to_pt_map def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model A__ = tf.train.list_variables(UpperCAmelCase_ ) A__ = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) A__ = tf.train.load_variable(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = array # Build TF to PyTorch weights loading map A__ = _build_tf_to_pytorch_map(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue A__ = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) A__ = np.transpose(UpperCAmelCase_ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer A__ = array.squeeze().transpose() else: A__ = np.transpose(UpperCAmelCase_ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) A__ = torch.from_numpy(UpperCAmelCase_ ) tf_weights.pop(UpperCAmelCase_ , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp""" , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp_1""" , UpperCAmelCase_ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , UpperCAmelCase_ ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _snake_case ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : nn.Convad ): A__ , A__ = features.shape[-2:] A__ , A__ = conv_layer.stride A__ , A__ = conv_layer.kernel_size if in_height % stride_height == 0: A__ = max(kernel_height - stride_height , 0 ) else: A__ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: A__ = max(kernel_width - stride_width , 0 ) else: A__ = max(kernel_width - (in_width % stride_width) , 0 ) A__ = pad_along_width // 2 A__ = pad_along_width - pad_left A__ = pad_along_height // 2 A__ = pad_along_height - pad_top A__ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(UpperCAmelCase_ , UpperCAmelCase_ , """constant""" , 0.0 ) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: bool = False , UpperCamelCase: Optional[bool] = True , UpperCamelCase: Optional[bool or str] = True , ): """simple docstring""" super().__init__() A__ = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) A__ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) A__ = nn.Convad( in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=UpperCamelCase , stride=UpperCamelCase , padding=UpperCamelCase , groups=UpperCamelCase , bias=UpperCamelCase , padding_mode="""zeros""" , ) if use_normalization: A__ = nn.BatchNormad( num_features=UpperCamelCase , eps=config.layer_norm_eps , momentum=0.9_997 , affine=UpperCamelCase , track_running_stats=UpperCamelCase , ) else: A__ = None if use_activation: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = ACTaFN[use_activation] elif isinstance(config.hidden_act , UpperCamelCase ): A__ = ACTaFN[config.hidden_act] else: A__ = config.hidden_act else: A__ = None def UpperCamelCase ( self: List[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" if self.config.tf_padding: A__ = apply_tf_padding(UpperCamelCase , self.convolution ) A__ = self.convolution(UpperCamelCase ) if self.normalization is not None: A__ = self.normalization(UpperCamelCase ) if self.activation is not None: A__ = self.activation(UpperCamelCase ) return features class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = MobileNetVaConfig UpperCAmelCase = load_tf_weights_in_mobilenet_va UpperCAmelCase = "mobilenet_v1" UpperCAmelCase = "pixel_values" UpperCAmelCase = False def UpperCamelCase ( self: Any , UpperCamelCase: Union[nn.Linear, nn.Convad] ): """simple docstring""" if isinstance(UpperCamelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(UpperCamelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: bool = True ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config A__ = 32 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) A__ = MobileNetVaConvLayer( UpperCamelCase , in_channels=config.num_channels , out_channels=UpperCamelCase , kernel_size=3 , stride=2 , ) A__ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] A__ = nn.ModuleList() for i in range(13 ): A__ = out_channels if strides[i] == 2 or i == 0: depth *= 2 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=3 , stride=strides[i] , groups=UpperCamelCase , ) ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=1 , ) ) A__ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) A__ = self.conv_stem(UpperCamelCase ) A__ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): A__ = layer_module(UpperCamelCase ) if output_hidden_states: A__ = all_hidden_states + (hidden_states,) A__ = hidden_states if self.pooler is not None: A__ = torch.flatten(self.pooler(UpperCamelCase ) , start_dim=1 ) else: A__ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase , pooler_output=UpperCamelCase , hidden_states=UpperCamelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = MobileNetVaModel(UpperCamelCase ) A__ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head A__ = nn.Dropout(config.classifier_dropout_prob , inplace=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.mobilenet_va(UpperCamelCase , output_hidden_states=UpperCamelCase , return_dict=UpperCamelCase ) A__ = outputs.pooler_output if return_dict else outputs[1] A__ = self.classifier(self.dropout(UpperCamelCase ) ) A__ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: A__ = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): A__ = """single_label_classification""" else: A__ = """multi_label_classification""" if self.config.problem_type == "regression": A__ = MSELoss() if self.num_labels == 1: A__ = loss_fct(logits.squeeze() , labels.squeeze() ) else: A__ = loss_fct(UpperCamelCase , UpperCamelCase ) elif self.config.problem_type == "single_label_classification": A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": A__ = BCEWithLogitsLoss() A__ = loss_fct(UpperCamelCase , UpperCamelCase ) if not return_dict: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=UpperCamelCase , logits=UpperCamelCase , hidden_states=outputs.hidden_states , )
335
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , UpperCamelCase: Tuple=1 , UpperCamelCase: List[Any]=0 , UpperCamelCase: Optional[Any]=2 , UpperCamelCase: List[Any]=5_12 , UpperCamelCase: List[str]="cls" , UpperCamelCase: Dict=False , UpperCamelCase: List[Any]=True , **UpperCamelCase: str , ): """simple docstring""" super().__init__(pad_token_id=UpperCamelCase , bos_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , **UpperCamelCase ) A__ = project_dim A__ = pooler_fn A__ = learn_encoder A__ = use_attention_mask class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = [R"pooler", R"logit_scale"] UpperCAmelCase = [R"position_ids", R"predictions.decoder.bias"] UpperCAmelCase = "roberta" UpperCAmelCase = RobertaSeriesConfig def __init__( self: Any , UpperCamelCase: Optional[int] ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = XLMRobertaModel(UpperCamelCase ) A__ = nn.Linear(config.hidden_size , config.project_dim ) A__ = getattr(UpperCamelCase , """has_pre_transformation""" , UpperCamelCase ) if self.has_pre_transformation: A__ = nn.Linear(config.hidden_size , config.project_dim ) A__ = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps ) self.post_init() def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.base_model( input_ids=UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , output_attentions=UpperCamelCase , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=UpperCamelCase , ) if self.has_pre_transformation: A__ = outputs["""hidden_states"""][-2] A__ = self.pre_LN(UpperCamelCase ) A__ = self.transformation_pre(UpperCamelCase ) return TransformationModelOutput( projection_state=UpperCamelCase , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: A__ = self.transformation(outputs.last_hidden_state ) return TransformationModelOutput( projection_state=UpperCamelCase , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : set ): A__ , A__ = len(UpperCAmelCase_ ), len(grid[0] ) if ( min(UpperCAmelCase_ , UpperCAmelCase_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(UpperCAmelCase_ , row + 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , row - 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col + 1 , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col - 1 , UpperCAmelCase_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
335
1
"""simple docstring""" from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging SCREAMING_SNAKE_CASE_ : List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : str = { 'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json', # See all umt5 models at https://huggingface.co/models?filter=umt5 } class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "umt5" UpperCAmelCase = ["past_key_values"] def __init__( self: Dict , UpperCamelCase: Any=25_01_12 , UpperCamelCase: List[Any]=5_12 , UpperCamelCase: Optional[Any]=64 , UpperCamelCase: List[Any]=10_24 , UpperCamelCase: int=8 , UpperCamelCase: List[Any]=None , UpperCamelCase: List[str]=6 , UpperCamelCase: List[str]=32 , UpperCamelCase: int=1_28 , UpperCamelCase: Optional[int]=0.1 , UpperCamelCase: List[Any]=1e-6 , UpperCamelCase: Tuple=1.0 , UpperCamelCase: Optional[int]="gated-gelu" , UpperCamelCase: Dict=True , UpperCamelCase: Dict=True , UpperCamelCase: int="T5Tokenizer" , UpperCamelCase: Optional[int]=True , UpperCamelCase: int=0 , UpperCamelCase: str=1 , UpperCamelCase: int=0 , **UpperCamelCase: Optional[Any] , ): """simple docstring""" super().__init__( is_encoder_decoder=UpperCamelCase , tokenizer_class=UpperCamelCase , tie_word_embeddings=UpperCamelCase , pad_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , decoder_start_token_id=UpperCamelCase , **UpperCamelCase , ) A__ = vocab_size A__ = d_model A__ = d_kv A__ = d_ff A__ = num_layers A__ = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry A__ = num_heads A__ = relative_attention_num_buckets A__ = relative_attention_max_distance A__ = dropout_rate A__ = layer_norm_epsilon A__ = initializer_factor A__ = feed_forward_proj A__ = use_cache A__ = self.feed_forward_proj.split("""-""" ) A__ = act_info[-1] A__ = act_info[0] == """gated""" if len(UpperCamelCase ) > 1 and act_info[0] != "gated" or len(UpperCamelCase ) > 2: raise ValueError( f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.""" """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """ """'gated-gelu' or 'relu'""" ) if feed_forward_proj == "gated-gelu": A__ = """gelu_new""" @property def UpperCamelCase ( self: List[str] ): """simple docstring""" return self.d_model @property def UpperCamelCase ( self: str ): """simple docstring""" return self.num_heads @property def UpperCamelCase ( self: Any ): """simple docstring""" return self.num_layers class a ( _lowerCamelCase ): """simple docstring""" @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = { """input_ids""": {0: """batch""", 1: """encoder_sequence"""}, """attention_mask""": {0: """batch""", 1: """encoder_sequence"""}, } if self.use_past: A__ = """past_encoder_sequence + sequence""" A__ = {0: """batch"""} A__ = {0: """batch""", 1: """past_decoder_sequence + sequence"""} else: A__ = {0: """batch""", 1: """decoder_sequence"""} A__ = {0: """batch""", 1: """decoder_sequence"""} if self.use_past: self.fill_with_past_key_values_(UpperCamelCase , direction="""inputs""" ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return 13 @property def UpperCamelCase ( self: Any ): """simple docstring""" return 5e-4
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def _snake_case ( UpperCAmelCase_ : Optional[int] ): # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def _snake_case ( ): with parallel_backend("""spark""" ): assert ParallelBackendConfig.backend_name == "spark" A__ = [1, 2, 3] with pytest.raises(UpperCAmelCase_ ): with parallel_backend("""unsupported backend""" ): map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=2 ) with pytest.raises(UpperCAmelCase_ ): with parallel_backend("""unsupported backend""" ): map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("""num_proc""" , [2, -1] ) def _snake_case ( UpperCAmelCase_ : List[str] ): A__ = [1, 2] A__ = {"""a""": 1, """b""": 2} A__ = {"""a""": [1, 2], """b""": [3, 4]} A__ = {"""a""": {"""1""": 1}, """b""": 2} A__ = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} A__ = [2, 3] A__ = {"""a""": 2, """b""": 3} A__ = {"""a""": [2, 3], """b""": [4, 5]} A__ = {"""a""": {"""1""": 2}, """b""": 3} A__ = {"""a""": 2, """b""": 3, """c""": 4, """d""": 5} with parallel_backend("""spark""" ): assert map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=UpperCAmelCase_ ) == expected_map_nested_sa assert map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=UpperCAmelCase_ ) == expected_map_nested_sa assert map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=UpperCAmelCase_ ) == expected_map_nested_sa assert map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=UpperCAmelCase_ ) == expected_map_nested_sa assert map_nested(UpperCAmelCase_ , UpperCAmelCase_ , num_proc=UpperCAmelCase_ ) == expected_map_nested_sa
335
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
1
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Optional[Any]=13 , UpperCamelCase: List[Any]=7 , UpperCamelCase: List[Any]=True , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[str]=True , UpperCamelCase: Optional[int]=True , UpperCamelCase: List[Any]=99 , UpperCamelCase: List[str]=32 , UpperCamelCase: List[Any]=5 , UpperCamelCase: Dict=4 , UpperCamelCase: int=37 , UpperCamelCase: Optional[int]="gelu" , UpperCamelCase: List[Any]=0.1 , UpperCamelCase: Optional[int]=0.1 , UpperCamelCase: Optional[int]=5_12 , UpperCamelCase: Union[str, Any]=16 , UpperCamelCase: Optional[int]=2 , UpperCamelCase: List[Any]=0.02 , UpperCamelCase: Tuple=4 , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def UpperCamelCase ( self: str ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_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__ = RobertaConfig( 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: Any ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = True A__ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) A__ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = True UpperCAmelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = FlaxRobertaModelTester(self ) @slow def UpperCamelCase ( self: Optional[int] ): """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""roberta-base""" , from_pt=UpperCamelCase ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCamelCase )
335
"""simple docstring""" import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC SCREAMING_SNAKE_CASE_ : str = parse(importlib.metadata.version('torch')) def _snake_case ( UpperCAmelCase_ : Union[str, Version] , UpperCAmelCase_ : str , UpperCAmelCase_ : str ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) A__ = STR_OPERATION_TO_FUNC[operation] if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = parse(importlib.metadata.version(UpperCAmelCase_ ) ) return operation(UpperCAmelCase_ , parse(UpperCAmelCase_ ) ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : str ): return compare_versions(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""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 SCREAMING_SNAKE_CASE_ : Optional[int] = 'src/diffusers' SCREAMING_SNAKE_CASE_ : Tuple = '.' # This is to make sure the diffusers module imported is the one in the repo. SCREAMING_SNAKE_CASE_ : Tuple = importlib.util.spec_from_file_location( 'diffusers', os.path.join(DIFFUSERS_PATH, '__init__.py'), submodule_search_locations=[DIFFUSERS_PATH], ) SCREAMING_SNAKE_CASE_ : Optional[int] = spec.loader.load_module() def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] ): return line.startswith(UpperCAmelCase_ ) or len(UpperCAmelCase_ ) <= 1 or re.search(R"""^\s*\)(\s*->.*:|:)\s*$""" , UpperCAmelCase_ ) is not None def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = object_name.split(""".""" ) A__ = 0 # First let's find the module where our object lives. A__ = parts[i] while i < len(UpperCAmelCase_ ) and not os.path.isfile(os.path.join(UpperCAmelCase_ , F"""{module}.py""" ) ): i += 1 if i < len(UpperCAmelCase_ ): A__ = os.path.join(UpperCAmelCase_ , parts[i] ) if i >= len(UpperCAmelCase_ ): raise ValueError(F"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(UpperCAmelCase_ , F"""{module}.py""" ) , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: A__ = f.readlines() # Now let's find the class / func in the code! A__ = """""" A__ = 0 for name in parts[i + 1 :]: while ( line_index < len(UpperCAmelCase_ ) 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(UpperCAmelCase_ ): 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). A__ = line_index while line_index < len(UpperCAmelCase_ ) and _should_continue(lines[line_index] , UpperCAmelCase_ ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 A__ = lines[start_index:line_index] return "".join(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = re.compile(r'^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)') SCREAMING_SNAKE_CASE_ : int = re.compile(r'^\s*(\S+)->(\S+)(\s+.*|$)') SCREAMING_SNAKE_CASE_ : Union[str, Any] = re.compile(r'<FILL\s+[^>]*>') def _snake_case ( UpperCAmelCase_ : int ): A__ = code.split("""\n""" ) A__ = 0 while idx < len(UpperCAmelCase_ ) and len(lines[idx] ) == 0: idx += 1 if idx < len(UpperCAmelCase_ ): return re.search(R"""^(\s*)\S""" , lines[idx] ).groups()[0] return "" def _snake_case ( UpperCAmelCase_ : Dict ): A__ = len(get_indent(UpperCAmelCase_ ) ) > 0 if has_indent: A__ = F"""class Bla:\n{code}""" A__ = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=UpperCAmelCase_ ) A__ = black.format_str(UpperCAmelCase_ , mode=UpperCAmelCase_ ) A__ , A__ = style_docstrings_in_code(UpperCAmelCase_ ) return result[len("""class Bla:\n""" ) :] if has_indent else result def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str=False ): with open(UpperCAmelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: A__ = f.readlines() A__ = [] A__ = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(UpperCAmelCase_ ): A__ = _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. A__ , A__ , A__ = search.groups() A__ = find_code_in_diffusers(UpperCAmelCase_ ) A__ = get_indent(UpperCAmelCase_ ) A__ = line_index + 1 if indent == theoretical_indent else line_index + 2 A__ = theoretical_indent A__ = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. A__ = True while line_index < len(UpperCAmelCase_ ) and should_continue: line_index += 1 if line_index >= len(UpperCAmelCase_ ): break A__ = lines[line_index] A__ = _should_continue(UpperCAmelCase_ , UpperCAmelCase_ ) and re.search(F"""^{indent}# End copy""" , UpperCAmelCase_ ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 A__ = lines[start_index:line_index] A__ = """""".join(UpperCAmelCase_ ) # Remove any nested `Copied from` comments to avoid circular copies A__ = [line for line in theoretical_code.split("""\n""" ) if _re_copy_warning.search(UpperCAmelCase_ ) is None] A__ = """\n""".join(UpperCAmelCase_ ) # Before comparing, use the `replace_pattern` on the original code. if len(UpperCAmelCase_ ) > 0: A__ = replace_pattern.replace("""with""" , """""" ).split(""",""" ) A__ = [_re_replace_pattern.search(UpperCAmelCase_ ) for p in patterns] for pattern in patterns: if pattern is None: continue A__ , A__ , A__ = pattern.groups() A__ = re.sub(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) if option.strip() == "all-casing": A__ = re.sub(obja.lower() , obja.lower() , UpperCAmelCase_ ) A__ = re.sub(obja.upper() , obja.upper() , UpperCAmelCase_ ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line A__ = blackify(lines[start_index - 1] + theoretical_code ) A__ = 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: A__ = lines[:start_index] + [theoretical_code] + lines[line_index:] A__ = start_index + 1 if overwrite and len(UpperCAmelCase_ ) > 0: # Warn the user a file has been modified. print(F"""Detected changes, rewriting {filename}.""" ) with open(UpperCAmelCase_ , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(UpperCAmelCase_ ) return diffs def _snake_case ( UpperCAmelCase_ : bool = False ): A__ = glob.glob(os.path.join(UpperCAmelCase_ , """**/*.py""" ) , recursive=UpperCAmelCase_ ) A__ = [] for filename in all_files: A__ = is_copy_consistent(UpperCAmelCase_ , UpperCAmelCase_ ) diffs += [F"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(UpperCAmelCase_ ) > 0: A__ = """\n""".join(UpperCAmelCase_ ) 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__": SCREAMING_SNAKE_CASE_ : Dict = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') SCREAMING_SNAKE_CASE_ : Any = parser.parse_args() check_copies(args.fix_and_overwrite)
335
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , 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/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != 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(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
1
"""simple docstring""" import enum import shutil import sys SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ : str = shutil.get_terminal_size() SCREAMING_SNAKE_CASE_ : Dict = {'UP': 'A', 'DOWN': 'B', 'RIGHT': 'C', 'LEFT': 'D'} class a ( enum.Enum ): """simple docstring""" UpperCAmelCase = 0 UpperCAmelCase = 1 def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any="" ): sys.stdout.write(str(UpperCAmelCase_ ) + end ) sys.stdout.flush() def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any]="" ): forceWrite(F"""\u001b[{color}m{content}\u001b[0m""" , UpperCAmelCase_ ) def _snake_case ( ): forceWrite("""\r""" ) def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : str ): forceWrite(F"""\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}""" ) def _snake_case ( ): forceWrite(""" """ * TERMINAL_WIDTH ) reset_cursor() def _snake_case ( ): reset_cursor() forceWrite("""-""" * TERMINAL_WIDTH )
335
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple=None , UpperCamelCase: Tuple=None , **UpperCamelCase: Dict ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = eval_examples A__ = post_process_function def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Dataset] = None , UpperCamelCase: List[Any]=None , UpperCamelCase: Optional[List[str]] = None , UpperCamelCase: str = "eval" , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(UpperCamelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCamelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCamelCase ) return metrics def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Dict=None , UpperCamelCase: str = "test" , **UpperCamelCase: Optional[int] ): """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(UpperCamelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase , """predict""" ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase )
335
1
"""simple docstring""" import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : str ): # Initialise PyTorch model A__ = LxmertConfig.from_json_file(UpperCAmelCase_ ) print(F"""Building PyTorch model from configuration: {config}""" ) A__ = LxmertForPreTraining(UpperCAmelCase_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , UpperCAmelCase_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) SCREAMING_SNAKE_CASE_ : List[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
335
"""simple docstring""" class a : """simple docstring""" def __init__( self: Dict ): """simple docstring""" A__ = {} def UpperCamelCase ( self: List[str] ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase , """ -> """ , """ -> """.join([str(UpperCamelCase ) for j in self.vertex[i]] ) ) def UpperCamelCase ( self: Any , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase ) else: # else make a new vertex A__ = [to_vertex] def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: list ): """simple docstring""" A__ = True print(UpperCamelCase , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[int] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 6008_5147_5143 ): try: A__ = int(UpperCAmelCase_ ) except (TypeError, ValueError): raise TypeError("""Parameter n must be int or castable to int.""" ) if n <= 0: raise ValueError("""Parameter n must be greater than or equal to one.""" ) A__ = 1 A__ = 2 while i * i <= n: while n % i == 0: A__ = i n //= i i += 1 if n > 1: A__ = n return int(UpperCAmelCase_ ) if __name__ == "__main__": print(f"""{solution() = }""")
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
1
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DeformableDetrImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[int] , UpperCamelCase: List[str] , UpperCamelCase: List[Any]=7 , UpperCamelCase: Tuple=3 , UpperCamelCase: Any=30 , UpperCamelCase: Union[str, Any]=4_00 , UpperCamelCase: Optional[int]=True , UpperCamelCase: int=None , UpperCamelCase: List[str]=True , UpperCamelCase: Tuple=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: int=True , UpperCamelCase: Optional[Any]=1 / 2_55 , UpperCamelCase: str=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( self: Tuple ): """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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = DeformableDetrImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Any ): """simple docstring""" A__ = DeformableDetrImageProcessingTester(self ) @property def UpperCamelCase ( self: Any ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_rescale""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_pad""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: Tuple ): """simple docstring""" pass def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = DeformableDetrImageProcessor() A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = DeformableDetrImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad ): A__ = end_pointa[0] - end_pointa[0] A__ = end_pointa[1] - end_pointa[1] A__ = end_pointa[2] - end_pointa[2] return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : Vectorad ): A__ = ab[1] * ac[2] - ab[2] * ac[1] # *i A__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j A__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : int ): return tuple(round(UpperCAmelCase_ , UpperCAmelCase_ ) for x in vector ) == (0, 0, 0) def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : int = 10 ): A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ )
335
1
"""simple docstring""" 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 ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: Optional[NestedDataStructureLike[PathLike]] = None , UpperCamelCase: Optional[NamedSplit] = None , UpperCamelCase: Optional[Features] = None , UpperCamelCase: str = None , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: Optional[int] = None , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = path_or_paths A__ = split if split or isinstance(UpperCamelCase , UpperCamelCase ) else """train""" A__ = features A__ = cache_dir A__ = keep_in_memory A__ = streaming A__ = num_proc A__ = kwargs @abstractmethod def UpperCamelCase ( self: List[str] ): """simple docstring""" pass class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: Optional[Features] = None , UpperCamelCase: str = None , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: Optional[int] = None , **UpperCamelCase: Dict , ): """simple docstring""" A__ = features A__ = cache_dir A__ = keep_in_memory A__ = streaming A__ = num_proc A__ = kwargs @abstractmethod def UpperCamelCase ( self: int ): """simple docstring""" pass
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_pegasus_x': ['PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusXConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST', 'PegasusXForConditionalGeneration', 'PegasusXModel', 'PegasusXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Dict = {'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Optional[Any] = [ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" import math class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: List[str]=0 ): # a graph with Node 0,1,...,N-1 """simple docstring""" A__ = n A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # adjacency matrix for weight A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Tuple ): """simple docstring""" A__ = w def UpperCamelCase ( self: int ): """simple docstring""" for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): A__ = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase ( self: int , UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 1_0) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 1_0) 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)
335
1
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE_ : Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : Tuple = {'vocab_file': 'vocab.json'} SCREAMING_SNAKE_CASE_ : Union[str, Any] = { 'vocab_file': { 'mgp-str': 'https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json', } } SCREAMING_SNAKE_CASE_ : Optional[int] = {'mgp-str': 2_7} class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self: Union[str, Any] , UpperCamelCase: Any , UpperCamelCase: str="[GO]" , UpperCamelCase: List[str]="[GO]" , UpperCamelCase: Optional[int]="[s]" , UpperCamelCase: Any="[GO]" , **UpperCamelCase: Optional[Any] ): """simple docstring""" super().__init__( unk_token=UpperCamelCase , bos_token=UpperCamelCase , eos_token=UpperCamelCase , pad_token=UpperCamelCase , **UpperCamelCase , ) with open(UpperCamelCase , encoding="""utf-8""" ) as vocab_handle: A__ = json.load(UpperCamelCase ) A__ = {v: k for k, v in self.vocab.items()} @property def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" return len(self.vocab ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict ): """simple docstring""" A__ = [] for s in text: char_tokens.extend(UpperCamelCase ) return char_tokens def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[int] ): """simple docstring""" return self.vocab.get(UpperCamelCase , self.vocab.get(self.unk_token ) ) def UpperCamelCase ( self: Tuple , UpperCamelCase: str ): """simple docstring""" return self.decoder.get(UpperCamelCase ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: str , UpperCamelCase: Optional[str] = None ): """simple docstring""" if not os.path.isdir(UpperCamelCase ): logger.error("""Vocabulary path ({}) should be a directory""".format(UpperCamelCase ) ) return A__ = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) with open(UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=UpperCamelCase , ensure_ascii=UpperCamelCase ) + """\n""" ) return (vocab_file,)
335
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def UpperCamelCase ( self: str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = (3, 32, 1_28) A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on A__ = dict(zip(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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(UpperCamelCase ) + """\n""" ) A__ = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 1_28}, } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta ) A__ = Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) return image_input def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """labels"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase ): processor() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.char_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) A__ = [seq.replace(""" """ , """""" ) for seq in decoded_tok] self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = None A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = torch.randn(1 , 27 , 38 ) A__ = torch.randn(1 , 27 , 5_02_57 ) A__ = torch.randn(1 , 27 , 3_05_22 ) A__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""] )
335
1
"""simple docstring""" import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : List[str] = [ ['attention', 'attn'], ['encoder_attention', 'encoder_attn'], ['q_lin', 'q_proj'], ['k_lin', 'k_proj'], ['v_lin', 'v_proj'], ['out_lin', 'out_proj'], ['norm_embeddings', 'layernorm_embedding'], ['position_embeddings', 'embed_positions'], ['embeddings', 'embed_tokens'], ['ffn.lin', 'fc'], ] def _snake_case ( UpperCAmelCase_ : List[str] ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: A__ = k.replace(UpperCAmelCase_ , UpperCAmelCase_ ) if k.startswith("""encoder""" ): A__ = k.replace(""".attn""" , """.self_attn""" ) A__ = k.replace("""norm1""" , """self_attn_layer_norm""" ) A__ = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): A__ = k.replace("""norm1""" , """self_attn_layer_norm""" ) A__ = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) A__ = k.replace("""norm3""" , """final_layer_norm""" ) return k def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: A__ = sd.pop(UpperCAmelCase_ ) A__ = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd A__ = v SCREAMING_SNAKE_CASE_ : Any = ['START'] @torch.no_grad() def _snake_case ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : int ): A__ = torch.load(UpperCAmelCase_ , map_location="""cpu""" ) A__ = model["""model"""] A__ = BlenderbotConfig.from_json_file(UpperCAmelCase_ ) A__ = BlenderbotForConditionalGeneration(UpperCAmelCase_ ) A__ = m.model.state_dict().keys() A__ = [] A__ = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue A__ = rename_state_dict_key(UpperCAmelCase_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: A__ = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(UpperCAmelCase_ ) m.model.load_state_dict(UpperCAmelCase_ , strict=UpperCAmelCase_ ) m.half() m.save_pretrained(UpperCAmelCase_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--src_path', type=str, help='like blenderbot-model.bin') parser.add_argument('--save_dir', default='hf_blenderbot', type=str, help='Where to save converted model.') parser.add_argument( '--hf_config_json', default='blenderbot-3b-config.json', type=str, help='Path to config to use' ) SCREAMING_SNAKE_CASE_ : int = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
335
"""simple docstring""" import math def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase_ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int ): A__ = [1] A__ , A__ , A__ = 0, 0, 0 A__ = ugly_nums[ia] * 2 A__ = ugly_nums[ia] * 3 A__ = ugly_nums[ia] * 5 for _ in range(1 , UpperCAmelCase_ ): A__ = min(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) ugly_nums.append(UpperCAmelCase_ ) if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(f"""{ugly_numbers(2_0_0) = }""")
335
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = 1 A__ = 3 A__ = (32, 32) A__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase ) return image @property def UpperCamelCase ( self: int ): """simple docstring""" torch.manual_seed(0 ) A__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) return model @property def UpperCamelCase ( self: Tuple ): """simple docstring""" torch.manual_seed(0 ) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) return model @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" torch.manual_seed(0 ) A__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_06 , ) return RobertaSeriesModelWithTransformation(UpperCamelCase ) @property def UpperCamelCase ( self: str ): """simple docstring""" def extract(*UpperCamelCase: List[str] , **UpperCamelCase: Any ): class a : """simple docstring""" def __init__( self: Any ): """simple docstring""" A__ = torch.ones([0] ) def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" self.pixel_values.to(UpperCamelCase ) return self return Out() return extract def UpperCamelCase ( self: str ): """simple docstring""" A__ = """cpu""" # ensure determinism for the device-dependent torch.Generator A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) A__ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ) A__ = output.images A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , return_dict=UpperCamelCase , )[0] A__ = image[0, -3:, -3:, -1] A__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A__ = np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) # put models in fp16 A__ = unet.half() A__ = vae.half() A__ = bert.half() # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) # resize to resolution that is divisible by 8 but not 16 or 32 A__ = init_image.resize((7_60, 5_04) ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] A__ = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) A__ = np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) A__ = init_image.resize((7_68, 5_12) ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy""" ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
335
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Dict , UpperCamelCase: List[Any] , UpperCamelCase: List[str]=7 , UpperCamelCase: str=3 , UpperCamelCase: Optional[int]=30 , UpperCamelCase: Dict=4_00 , UpperCamelCase: Tuple=True , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=0.9 , UpperCamelCase: Optional[Any]=None , UpperCamelCase: Optional[Any]=True , UpperCamelCase: str=[0.5, 0.5, 0.5] , UpperCamelCase: int=[0.5, 0.5, 0.5] , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 30} A__ = crop_size if crop_size is not None else {"""height""": 30, """width""": 30} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize_and_center_crop A__ = size A__ = crop_pct A__ = crop_size A__ = do_normalize A__ = image_mean A__ = image_std def UpperCamelCase ( self: List[Any] ): """simple docstring""" return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = PoolFormerImageProcessor if is_vision_available() else None def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = PoolFormerImageProcessingTester(self ) @property def UpperCamelCase ( self: Dict ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """do_resize_and_center_crop""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) self.assertTrue(hasattr(UpperCamelCase , """crop_pct""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 30} ) self.assertEqual(image_processor.crop_size , {"""height""": 30, """width""": 30} ) A__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" pass def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
335
"""simple docstring""" import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = 0.01 with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): A__ = time.time() locka.acquire(UpperCAmelCase_ ) assert time.time() - _start > timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = """a""" * 1000 + """.lock""" A__ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(""".lock""" ) assert not locka._lock_file.endswith(UpperCAmelCase_ ) assert len(os.path.basename(locka._lock_file ) ) <= 255 A__ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): locka.acquire(0 )
335
1
"""simple docstring""" import inspect import unittest from transformers import YolosConfig 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 from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class a : """simple docstring""" def __init__( self: Any , UpperCamelCase: Any , UpperCamelCase: List[str]=13 , UpperCamelCase: List[str]=[30, 30] , UpperCamelCase: Union[str, Any]=2 , UpperCamelCase: Any=3 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Any=True , UpperCamelCase: Union[str, Any]=32 , UpperCamelCase: Optional[int]=5 , UpperCamelCase: Union[str, Any]=4 , UpperCamelCase: int=37 , UpperCamelCase: List[str]="gelu" , UpperCamelCase: Tuple=0.1 , UpperCamelCase: Any=0.1 , UpperCamelCase: Optional[int]=10 , UpperCamelCase: int=0.02 , UpperCamelCase: List[str]=3 , UpperCamelCase: str=None , UpperCamelCase: str=8 , UpperCamelCase: Tuple=10 , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = num_labels A__ = scope A__ = n_targets A__ = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A__ = (image_size[1] // patch_size) * (image_size[0] // patch_size) A__ = num_patches + 1 + self.num_detection_tokens def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A__ = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A__ = [] for i in range(self.batch_size ): A__ = {} A__ = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCamelCase ) A__ = torch.rand(self.n_targets , 4 , device=UpperCamelCase ) labels.append(UpperCamelCase ) A__ = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return YolosConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCamelCase , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def UpperCamelCase ( self: str , UpperCamelCase: Union[str, Any] , UpperCamelCase: Any , UpperCamelCase: List[Any] ): """simple docstring""" A__ = YolosModel(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Dict , UpperCamelCase: List[Any] , UpperCamelCase: Dict ): """simple docstring""" A__ = YolosForObjectDetection(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(pixel_values=UpperCamelCase ) A__ = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A__ = model(pixel_values=UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def UpperCamelCase ( self: 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 ( _lowerCamelCase, _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = (YolosModel, YolosForObjectDetection) if is_torch_available() else () UpperCAmelCase = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase ( self: Optional[int] , UpperCamelCase: int , UpperCamelCase: str , UpperCamelCase: Dict=False ): """simple docstring""" A__ = super()._prepare_for_class(UpperCamelCase , UpperCamelCase , return_labels=UpperCamelCase ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A__ = [] for i in range(self.model_tester.batch_size ): A__ = {} A__ = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCamelCase , dtype=torch.long ) A__ = torch.ones( self.model_tester.n_targets , 4 , device=UpperCamelCase , dtype=torch.float ) labels.append(UpperCamelCase ) A__ = labels return inputs_dict def UpperCamelCase ( self: int ): """simple docstring""" A__ = YolosModelTester(self ) A__ = ConfigTester(self , config_class=UpperCamelCase , has_text_modality=UpperCamelCase , hidden_size=37 ) def UpperCamelCase ( self: str ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase ( self: Any ): """simple docstring""" pass def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: 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 UpperCamelCase ( self: Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: 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 UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True # in YOLOS, the seq_len is different A__ = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) A__ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) A__ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A__ = len(UpperCamelCase ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) A__ = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCamelCase ) ) A__ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" def check_hidden_states_output(UpperCamelCase: Optional[int] , UpperCamelCase: Any , UpperCamelCase: Any ): 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""" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase ) , UpperCamelCase ) # YOLOS has a different seq_length A__ = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) 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(UpperCamelCase , UpperCamelCase , UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(UpperCamelCase , UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCamelCase ) @slow def UpperCamelCase ( self: Optional[int] ): """simple docstring""" for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = YolosModel.from_pretrained(UpperCamelCase ) self.assertIsNotNone(UpperCamelCase ) def _snake_case ( ): A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return AutoImageProcessor.from_pretrained("""hustvl/yolos-small""" ) if is_vision_available() else None @slow def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = YolosForObjectDetection.from_pretrained("""hustvl/yolos-small""" ).to(UpperCamelCase ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=UpperCamelCase , return_tensors="""pt""" ).to(UpperCamelCase ) # forward pass with torch.no_grad(): A__ = model(inputs.pixel_values ) # verify outputs A__ = torch.Size((1, 1_00, 92) ) self.assertEqual(outputs.logits.shape , UpperCamelCase ) A__ = torch.tensor( [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]] , device=UpperCamelCase , ) A__ = torch.tensor( [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]] , device=UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCamelCase , atol=1e-4 ) ) # verify postprocessing A__ = image_processor.post_process_object_detection( UpperCamelCase , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A__ = torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861] ).to(UpperCamelCase ) A__ = [75, 75, 17, 63, 17] A__ = torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495] ).to(UpperCamelCase ) self.assertEqual(len(results["""scores"""] ) , 5 ) self.assertTrue(torch.allclose(results["""scores"""] , UpperCamelCase , atol=1e-4 ) ) self.assertSequenceEqual(results["""labels"""].tolist() , UpperCamelCase ) self.assertTrue(torch.allclose(results["""boxes"""][0, :] , UpperCamelCase ) )
335
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase = "image_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = AutoModelForVisualQuestionAnswering UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" return self.pre_processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) def UpperCamelCase ( self: str , UpperCamelCase: str ): """simple docstring""" with torch.no_grad(): return self.model(**UpperCamelCase ).logits def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int ): """simple docstring""" A__ = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
335
1
"""simple docstring""" import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings SCREAMING_SNAKE_CASE_ : Any = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = field(default=_lowerCamelCase, metadata={"help": "Whether to use SortishSampler or not."} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={"help": "Whether to use generate to calculate generative metrics (ROUGE, BLEU)."} ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": ( "The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default " "to the `max_length` value of the model configuration." ) }, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": ( "The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default " "to the `num_beams` value of the model configuration." ) }, ) UpperCAmelCase = field( default=_lowerCamelCase, metadata={ "help": "Model id, file path or url pointing to a GenerationConfig json file, to use during prediction." }, ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = super().to_dict() for k, v in d.items(): if isinstance(UpperCamelCase , UpperCamelCase ): A__ = v.to_dict() return d
335
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( 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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
1
"""simple docstring""" from packaging import version from .import_utils import is_accelerate_available if is_accelerate_available(): import accelerate def _snake_case ( UpperCAmelCase_ : Dict ): if not is_accelerate_available(): return method A__ = version.parse(accelerate.__version__ ).base_version if version.parse(UpperCAmelCase_ ) < version.parse("""0.17.0""" ): return method def wrapper(self : int , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Dict ): if hasattr(self , """_hf_hook""" ) and hasattr(self._hf_hook , """pre_forward""" ): self._hf_hook.pre_forward(self ) return method(self , *UpperCAmelCase_ , **UpperCAmelCase_ ) return wrapper
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : Dict ): # noqa: E741 A__ = len(UpperCAmelCase_ ) A__ = 0 A__ = [0] * n A__ = [False] * n A__ = [False] * n def dfs(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): if parent == root: out_edge_count += 1 A__ = True A__ = at for to in l[at]: if to == parent: pass elif not visited[to]: A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: A__ = True # AP found via cycle if at == low[to]: A__ = True else: A__ = min(low[at] , UpperCAmelCase_ ) return out_edge_count for i in range(UpperCAmelCase_ ): if not visited[i]: A__ = 0 A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , -1 , UpperCAmelCase_ ) A__ = out_edge_count > 1 for x in range(len(UpperCAmelCase_ ) ): if is_art[x] is True: print(UpperCAmelCase_ ) # Adjacency list of graph SCREAMING_SNAKE_CASE_ : Optional[int] = { 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)
335
1
"""simple docstring""" import math import sys def _snake_case ( UpperCAmelCase_ : int ): if number != int(UpperCAmelCase_ ): raise ValueError("""the value of input must be a natural number""" ) if number < 0: raise ValueError("""the value of input must not be a negative number""" ) if number == 0: return 1 A__ = [-1] * (number + 1) A__ = 0 for i in range(1 , number + 1 ): A__ = sys.maxsize A__ = int(math.sqrt(UpperCAmelCase_ ) ) for j in range(1 , root + 1 ): A__ = 1 + answers[i - (j**2)] A__ = min(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
335
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" A__ = get_activation("""swish""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""silu""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = get_activation("""mish""" ) self.assertIsInstance(UpperCamelCase , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""gelu""" ) self.assertIsInstance(UpperCamelCase , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): A__ = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _snake_case ( ): print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
335
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
1
"""simple docstring""" from __future__ import annotations import queue class a : """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: str ): """simple docstring""" A__ = data A__ = None A__ = None def _snake_case ( ): print("""\n********Press N to stop entering at any point of time********\n""" ) A__ = input("""Enter the value of the root node: """ ).strip().lower() A__ = queue.Queue() A__ = TreeNode(int(UpperCAmelCase_ ) ) q.put(UpperCAmelCase_ ) while not q.empty(): A__ = q.get() A__ = F"""Enter the left node of {node_found.data}: """ A__ = input(UpperCAmelCase_ ).strip().lower() or """n""" if check == "n": return tree_node A__ = TreeNode(int(UpperCAmelCase_ ) ) A__ = left_node q.put(UpperCAmelCase_ ) A__ = F"""Enter the right node of {node_found.data}: """ A__ = input(UpperCAmelCase_ ).strip().lower() or """n""" if check == "n": return tree_node A__ = TreeNode(int(UpperCAmelCase_ ) ) A__ = right_node q.put(UpperCAmelCase_ ) raise def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return print(node.data , end=""",""" ) pre_order(node.left ) pre_order(node.right ) def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return in_order(node.left ) print(node.data , end=""",""" ) in_order(node.right ) def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=""",""" ) def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return A__ = queue.Queue() q.put(UpperCAmelCase_ ) while not q.empty(): A__ = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return A__ = queue.Queue() q.put(UpperCAmelCase_ ) while not q.empty(): A__ = [] while not q.empty(): A__ = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return A__ = [] A__ = node while n or stack: while n: # start from root node, find its left child print(n.data , end=""",""" ) stack.append(UpperCAmelCase_ ) A__ = n.left # end of while means current node doesn't have left child A__ = stack.pop() # start to traverse its right child A__ = n.right def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return A__ = [] A__ = node while n or stack: while n: stack.append(UpperCAmelCase_ ) A__ = n.left A__ = stack.pop() print(n.data , end=""",""" ) A__ = n.right def _snake_case ( UpperCAmelCase_ : TreeNode ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not node: return A__ , A__ = [], [] A__ = node stacka.append(UpperCAmelCase_ ) while stacka: # to find the reversed order of post order, store it in stack2 A__ = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(UpperCAmelCase_ ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=""",""" ) def _snake_case ( UpperCAmelCase_ : str = "" , UpperCAmelCase_ : Dict=50 , UpperCAmelCase_ : str="*" ): if not s: return "\n" + width * char A__ , A__ = divmod(width - len(UpperCAmelCase_ ) - 2 , 2 ) return F"""{left * char} {s} {(left + extra) * char}""" if __name__ == "__main__": import doctest doctest.testmod() print(prompt('Binary Tree Traversals')) SCREAMING_SNAKE_CASE_ : TreeNode = build_tree() print(prompt('Pre Order Traversal')) pre_order(node) print(prompt() + '\n') print(prompt('In Order Traversal')) in_order(node) print(prompt() + '\n') print(prompt('Post Order Traversal')) post_order(node) print(prompt() + '\n') print(prompt('Level Order Traversal')) level_order(node) print(prompt() + '\n') print(prompt('Actual Level Order Traversal')) level_order_actual(node) print('*' * 5_0 + '\n') print(prompt('Pre Order Traversal - Iteration Version')) pre_order_iter(node) print(prompt() + '\n') print(prompt('In Order Traversal - Iteration Version')) in_order_iter(node) print(prompt() + '\n') print(prompt('Post Order Traversal - Iteration Version')) post_order_iter(node) print(prompt())
335
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE_ : int = [ 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the' ' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe' ' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.', 'The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal' ' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s' ' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the' ' body.', 'Amnesty International releases its annual report on the death penalty. The report catalogs the use of' ' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the' ' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital' ' punishment.', ] SCREAMING_SNAKE_CASE_ : List[Any] = [ 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .' ' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz' ' had informed his Lufthansa training school of an episode of severe depression, airline says .', 'Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .' ' Israel and the United States opposed the move, which could open the door to war crimes investigations against' ' Israelis .', 'Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to' ' death . Organization claims that governments around the world are using the threat of terrorism to advance' ' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death' ' sentences up by 28% .', ] def _snake_case ( ): A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2""", """rougeL"""] ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2"""] ) assert ( pd.DataFrame(no_aggregation["""rouge2"""] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["""rouge2"""] ).fmeasure.mean() ) def _snake_case ( ): A__ = """rougeLsum""" A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _snake_case ( ): A__ = ["""rouge1""", """rouge2""", """rougeL"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) assert score_sep == score_no_sep def _snake_case ( ): A__ = [ """Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.""", """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""", ] A__ = [ """Margot Frank, died in 1945, a month earlier than previously thought.""", """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of""" """ the final seconds on board Flight 9525.""", ] assert calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) == calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) def _snake_case ( ): A__ = [ """\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" """ ] A__ = [ """ Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .""" ] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] , newline_sep=UpperCAmelCase_ )["""rougeLsum"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] )["""rougeLsum"""] assert new_score > prev_score def _snake_case ( ): A__ = Path("""examples/seq2seq/test_data/wmt_en_ro""" ) A__ = calculate_rouge_path(data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge_path( data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) , bootstrap_aggregation=UpperCAmelCase_ ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE_ : Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ : Tuple = { 'asapp/sew-d-tiny-100k': 'https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json', # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "sew-d" def __init__( self: Dict , UpperCamelCase: List[Any]=32 , UpperCamelCase: Optional[int]=7_68 , UpperCamelCase: int=12 , UpperCamelCase: int=12 , UpperCamelCase: str=30_72 , UpperCamelCase: Dict=2 , UpperCamelCase: List[str]=5_12 , UpperCamelCase: str=2_56 , UpperCamelCase: List[Any]=True , UpperCamelCase: int=True , UpperCamelCase: List[Any]=("p2c", "c2p") , UpperCamelCase: Optional[int]="layer_norm" , UpperCamelCase: int="gelu_python" , UpperCamelCase: Any=0.1 , UpperCamelCase: List[str]=0.1 , UpperCamelCase: List[Any]=0.1 , UpperCamelCase: str=0.0 , UpperCamelCase: Optional[Any]=0.1 , UpperCamelCase: List[str]=0.02 , UpperCamelCase: Any=1e-7 , UpperCamelCase: List[Any]=1e-5 , UpperCamelCase: str="group" , UpperCamelCase: Any="gelu" , UpperCamelCase: Union[str, Any]=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12) , UpperCamelCase: List[Any]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , UpperCamelCase: Any=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , UpperCamelCase: Union[str, Any]=False , UpperCamelCase: Tuple=1_28 , UpperCamelCase: List[Any]=16 , UpperCamelCase: int=True , UpperCamelCase: int=0.05 , UpperCamelCase: List[str]=10 , UpperCamelCase: List[Any]=2 , UpperCamelCase: List[str]=0.0 , UpperCamelCase: Tuple=10 , UpperCamelCase: Tuple=0 , UpperCamelCase: int="mean" , UpperCamelCase: Optional[int]=False , UpperCamelCase: Optional[Any]=False , UpperCamelCase: str=2_56 , UpperCamelCase: List[Any]=0 , UpperCamelCase: int=1 , UpperCamelCase: List[str]=2 , **UpperCamelCase: Dict , ): """simple docstring""" super().__init__(**UpperCamelCase , pad_token_id=UpperCamelCase , bos_token_id=UpperCamelCase , eos_token_id=UpperCamelCase ) A__ = hidden_size A__ = feat_extract_norm A__ = feat_extract_activation A__ = list(UpperCamelCase ) A__ = list(UpperCamelCase ) A__ = list(UpperCamelCase ) A__ = conv_bias A__ = num_conv_pos_embeddings A__ = num_conv_pos_embedding_groups A__ = len(self.conv_dim ) A__ = num_hidden_layers A__ = intermediate_size A__ = squeeze_factor A__ = max_position_embeddings A__ = position_buckets A__ = share_att_key A__ = relative_attention A__ = norm_rel_ebd A__ = list(UpperCamelCase ) A__ = hidden_act A__ = num_attention_heads A__ = hidden_dropout A__ = attention_dropout A__ = activation_dropout A__ = feat_proj_dropout A__ = final_dropout A__ = layer_norm_eps A__ = feature_layer_norm_eps A__ = initializer_range A__ = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A__ = apply_spec_augment A__ = mask_time_prob A__ = mask_time_length A__ = mask_time_min_masks A__ = mask_feature_prob A__ = mask_feature_length A__ = mask_feature_min_masks # ctc loss A__ = ctc_loss_reduction A__ = ctc_zero_infinity # sequence classification A__ = use_weighted_layer_sum A__ = classifier_proj_size @property def UpperCamelCase ( self: Any ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
335
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'MobileNetV1Config' # Base docstring SCREAMING_SNAKE_CASE_ : str = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : List[str] = [1, 1_0_2_4, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ : Optional[Any] = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : Tuple = 'tabby, tabby cat' SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None ): A__ = {} if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = model.mobilenet_va else: A__ = model A__ = """MobilenetV1/Conv2d_0/""" A__ = backbone.conv_stem.convolution.weight A__ = backbone.conv_stem.normalization.bias A__ = backbone.conv_stem.normalization.weight A__ = backbone.conv_stem.normalization.running_mean A__ = backbone.conv_stem.normalization.running_var for i in range(13 ): A__ = i + 1 A__ = i * 2 A__ = backbone.layer[pt_index] A__ = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var A__ = backbone.layer[pt_index + 1] A__ = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = """MobilenetV1/Logits/Conv2d_1c_1x1/""" A__ = model.classifier.weight A__ = model.classifier.bias return tf_to_pt_map def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model A__ = tf.train.list_variables(UpperCAmelCase_ ) A__ = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) A__ = tf.train.load_variable(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = array # Build TF to PyTorch weights loading map A__ = _build_tf_to_pytorch_map(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue A__ = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) A__ = np.transpose(UpperCAmelCase_ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer A__ = array.squeeze().transpose() else: A__ = np.transpose(UpperCAmelCase_ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) A__ = torch.from_numpy(UpperCAmelCase_ ) tf_weights.pop(UpperCAmelCase_ , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp""" , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp_1""" , UpperCAmelCase_ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , UpperCAmelCase_ ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _snake_case ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : nn.Convad ): A__ , A__ = features.shape[-2:] A__ , A__ = conv_layer.stride A__ , A__ = conv_layer.kernel_size if in_height % stride_height == 0: A__ = max(kernel_height - stride_height , 0 ) else: A__ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: A__ = max(kernel_width - stride_width , 0 ) else: A__ = max(kernel_width - (in_width % stride_width) , 0 ) A__ = pad_along_width // 2 A__ = pad_along_width - pad_left A__ = pad_along_height // 2 A__ = pad_along_height - pad_top A__ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(UpperCAmelCase_ , UpperCAmelCase_ , """constant""" , 0.0 ) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: bool = False , UpperCamelCase: Optional[bool] = True , UpperCamelCase: Optional[bool or str] = True , ): """simple docstring""" super().__init__() A__ = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) A__ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) A__ = nn.Convad( in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=UpperCamelCase , stride=UpperCamelCase , padding=UpperCamelCase , groups=UpperCamelCase , bias=UpperCamelCase , padding_mode="""zeros""" , ) if use_normalization: A__ = nn.BatchNormad( num_features=UpperCamelCase , eps=config.layer_norm_eps , momentum=0.9_997 , affine=UpperCamelCase , track_running_stats=UpperCamelCase , ) else: A__ = None if use_activation: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = ACTaFN[use_activation] elif isinstance(config.hidden_act , UpperCamelCase ): A__ = ACTaFN[config.hidden_act] else: A__ = config.hidden_act else: A__ = None def UpperCamelCase ( self: List[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" if self.config.tf_padding: A__ = apply_tf_padding(UpperCamelCase , self.convolution ) A__ = self.convolution(UpperCamelCase ) if self.normalization is not None: A__ = self.normalization(UpperCamelCase ) if self.activation is not None: A__ = self.activation(UpperCamelCase ) return features class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = MobileNetVaConfig UpperCAmelCase = load_tf_weights_in_mobilenet_va UpperCAmelCase = "mobilenet_v1" UpperCAmelCase = "pixel_values" UpperCAmelCase = False def UpperCamelCase ( self: Any , UpperCamelCase: Union[nn.Linear, nn.Convad] ): """simple docstring""" if isinstance(UpperCamelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(UpperCamelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: bool = True ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config A__ = 32 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) A__ = MobileNetVaConvLayer( UpperCamelCase , in_channels=config.num_channels , out_channels=UpperCamelCase , kernel_size=3 , stride=2 , ) A__ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] A__ = nn.ModuleList() for i in range(13 ): A__ = out_channels if strides[i] == 2 or i == 0: depth *= 2 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=3 , stride=strides[i] , groups=UpperCamelCase , ) ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=1 , ) ) A__ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) A__ = self.conv_stem(UpperCamelCase ) A__ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): A__ = layer_module(UpperCamelCase ) if output_hidden_states: A__ = all_hidden_states + (hidden_states,) A__ = hidden_states if self.pooler is not None: A__ = torch.flatten(self.pooler(UpperCamelCase ) , start_dim=1 ) else: A__ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase , pooler_output=UpperCamelCase , hidden_states=UpperCamelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = MobileNetVaModel(UpperCamelCase ) A__ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head A__ = nn.Dropout(config.classifier_dropout_prob , inplace=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.mobilenet_va(UpperCamelCase , output_hidden_states=UpperCamelCase , return_dict=UpperCamelCase ) A__ = outputs.pooler_output if return_dict else outputs[1] A__ = self.classifier(self.dropout(UpperCamelCase ) ) A__ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: A__ = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): A__ = """single_label_classification""" else: A__ = """multi_label_classification""" if self.config.problem_type == "regression": A__ = MSELoss() if self.num_labels == 1: A__ = loss_fct(logits.squeeze() , labels.squeeze() ) else: A__ = loss_fct(UpperCamelCase , UpperCamelCase ) elif self.config.problem_type == "single_label_classification": A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": A__ = BCEWithLogitsLoss() A__ = loss_fct(UpperCamelCase , UpperCamelCase ) if not return_dict: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=UpperCamelCase , logits=UpperCamelCase , hidden_states=outputs.hidden_states , )
335
1
"""simple docstring""" SCREAMING_SNAKE_CASE_ : int = { 'a': 'AAAAA', 'b': 'AAAAB', 'c': 'AAABA', 'd': 'AAABB', 'e': 'AABAA', 'f': 'AABAB', 'g': 'AABBA', 'h': 'AABBB', 'i': 'ABAAA', 'j': 'BBBAA', 'k': 'ABAAB', 'l': 'ABABA', 'm': 'ABABB', 'n': 'ABBAA', 'o': 'ABBAB', 'p': 'ABBBA', 'q': 'ABBBB', 'r': 'BAAAA', 's': 'BAAAB', 't': 'BAABA', 'u': 'BAABB', 'v': 'BBBAB', 'w': 'BABAA', 'x': 'BABAB', 'y': 'BABBA', 'z': 'BABBB', ' ': ' ', } SCREAMING_SNAKE_CASE_ : str = {value: key for key, value in encode_dict.items()} def _snake_case ( UpperCAmelCase_ : str ): A__ = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def _snake_case ( UpperCAmelCase_ : str ): if set(UpperCAmelCase_ ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) A__ = """""" for word in coded.split(): while len(UpperCAmelCase_ ) != 0: decoded += decode_dict[word[:5]] A__ = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : set ): A__ , A__ = len(UpperCAmelCase_ ), len(grid[0] ) if ( min(UpperCAmelCase_ , UpperCAmelCase_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(UpperCAmelCase_ , row + 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , row - 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col + 1 , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col - 1 , UpperCAmelCase_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : Dict ): # noqa: E741 A__ = len(UpperCAmelCase_ ) A__ = 0 A__ = [0] * n A__ = [False] * n A__ = [False] * n def dfs(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): if parent == root: out_edge_count += 1 A__ = True A__ = at for to in l[at]: if to == parent: pass elif not visited[to]: A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: A__ = True # AP found via cycle if at == low[to]: A__ = True else: A__ = min(low[at] , UpperCAmelCase_ ) return out_edge_count for i in range(UpperCAmelCase_ ): if not visited[i]: A__ = 0 A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , -1 , UpperCAmelCase_ ) A__ = out_edge_count > 1 for x in range(len(UpperCAmelCase_ ) ): if is_art[x] is True: print(UpperCAmelCase_ ) # Adjacency list of graph SCREAMING_SNAKE_CASE_ : Optional[int] = { 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)
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup SCREAMING_SNAKE_CASE_ : Any = 'https://www.indeed.co.in/jobs?q=mobile+app+development&l=' def _snake_case ( UpperCAmelCase_ : str = "mumbai" ): A__ = BeautifulSoup(requests.get(url + location ).content , """html.parser""" ) # This attribute finds out all the specifics listed in a job for job in soup.find_all("""div""" , attrs={"""data-tn-component""": """organicJob"""} ): A__ = job.find("""a""" , attrs={"""data-tn-element""": """jobTitle"""} ).text.strip() A__ = job.find("""span""" , {"""class""": """company"""} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs('Bangalore'), 1): print(f"""Job {i:>2} is {job[0]} at {job[1]}""")
335
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
1
"""simple docstring""" # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def _snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=0 ): # Format the message. if name is None: A__ = None else: A__ = """.""" * max(0 , spaces - 2 ) + """# {:""" + str(50 - spaces ) + """s}""" A__ = fmt.format(UpperCAmelCase_ ) # Print and recurse (if needed). if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): if msg is not None: print(UpperCAmelCase_ ) for k in val.keys(): recursive_print(UpperCAmelCase_ , val[k] , spaces + 2 ) elif isinstance(UpperCAmelCase_ , torch.Tensor ): print(UpperCAmelCase_ , """:""" , val.size() ) else: print(UpperCAmelCase_ , """:""" , UpperCAmelCase_ ) def _snake_case ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. A__ = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] A__ = (num_heads, hidden_size, num_splits) + input_shape[1:] A__ = param.view(*UpperCAmelCase_ ) A__ = param.transpose(0 , 2 ) A__ = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] A__ = (num_heads, num_splits, hidden_size) + input_shape[1:] A__ = param.view(*UpperCAmelCase_ ) A__ = param.transpose(0 , 1 ).contiguous() A__ = param.view(*UpperCAmelCase_ ) return param def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] ): # The converted output model. A__ = {} # old versions did not store training args A__ = input_state_dict.get("""args""" , UpperCAmelCase_ ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) A__ = ds_args.padded_vocab_size A__ = ds_args.max_position_embeddings A__ = ds_args.hidden_size A__ = ds_args.num_layers A__ = ds_args.num_attention_heads A__ = ds_args.ffn_hidden_size # pprint(config) # The number of heads. A__ = config.n_head # The hidden_size per head. A__ = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): A__ = input_state_dict["""checkpoint_version"""] else: A__ = 0.0 # The model. A__ = input_state_dict["""model"""] # The language model. A__ = model["""language_model"""] # The embeddings. A__ = lm["""embedding"""] # The word embeddings. A__ = embeddings["""word_embeddings"""]["""weight"""] # Truncate the embedding table to vocab_size rows. A__ = word_embeddings[: config.vocab_size, :] A__ = word_embeddings # The position embeddings. A__ = embeddings["""position_embeddings"""]["""weight"""] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] A__ = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( F"""pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match""" ) # Store the position embeddings. A__ = pos_embeddings # The transformer. A__ = lm["""transformer"""] if """transformer""" in lm.keys() else lm["""encoder"""] # The regex to extract layer names. A__ = re.compile(R"""layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)""" ) # The simple map of names for "automated" rules. A__ = { """attention.dense""": """.attn.c_proj.""", """self_attention.dense""": """.attn.c_proj.""", """mlp.dense_h_to_4h""": """.mlp.c_fc.""", """mlp.dense_4h_to_h""": """.mlp.c_proj.""", } # Extract the layers. for key, val in transformer.items(): # Match the name. A__ = layer_re.match(UpperCAmelCase_ ) # Stop if that's not a layer if m is None: break # The index of the layer. A__ = int(m.group(1 ) ) # The name of the operation. A__ = m.group(2 ) # Is it a weight or a bias? A__ = m.group(3 ) # The name of the layer. A__ = F"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith("""layernorm""" ): A__ = """ln_1""" if op_name.startswith("""input""" ) else """ln_2""" A__ = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. A__ = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = causal_mask # Insert a "dummy" tensor for masked_bias. A__ = torch.tensor(-1e4 , dtype=torch.floataa ) A__ = masked_bias A__ = fix_query_key_value_ordering(UpperCAmelCase_ , UpperCAmelCase_ , 3 , UpperCAmelCase_ , UpperCAmelCase_ ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. A__ = out_val.transpose(0 , 1 ).contiguous() # Store. A__ = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": A__ = fix_query_key_value_ordering(UpperCAmelCase_ , UpperCAmelCase_ , 3 , UpperCAmelCase_ , UpperCAmelCase_ ) # Store. No change of shape. A__ = out_val # Transpose the weights. elif weight_or_bias == "weight": A__ = megatron_to_transformers[op_name] A__ = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": A__ = megatron_to_transformers[op_name] A__ = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. A__ = transformer["""final_layernorm.weight"""] A__ = transformer["""final_layernorm.bias"""] # For LM head, transformers' wants the matrix to weight embeddings. A__ = word_embeddings # It should be done! return output_state_dict def _snake_case ( ): # Create the argument parser. A__ = argparse.ArgumentParser() parser.add_argument("""--print-checkpoint-structure""" , action="""store_true""" ) parser.add_argument( """path_to_checkpoint""" , type=UpperCAmelCase_ , help="""Path to the checkpoint file (.zip archive or direct .pt file)""" , ) parser.add_argument( """--config_file""" , default="""""" , type=UpperCAmelCase_ , help="""An optional config json file describing the pre-trained model.""" , ) A__ = parser.parse_args() # Extract the basename. A__ = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(F"""Extracting PyTorch state dictionary from {args.path_to_checkpoint}""" ) if args.path_to_checkpoint.endswith(""".zip""" ): with zipfile.ZipFile(args.path_to_checkpoint , """r""" ) as checkpoint: with checkpoint.open("""release/mp_rank_00/model_optim_rng.pt""" ) as pytorch_dict: A__ = torch.load(UpperCAmelCase_ , map_location="""cpu""" ) else: A__ = torch.load(args.path_to_checkpoint , map_location="""cpu""" ) A__ = input_state_dict.get("""args""" , UpperCAmelCase_ ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: A__ = """gelu_fast""" elif ds_args.openai_gelu: A__ = """gelu_new""" else: A__ = """gelu""" else: # in the very early days this used to be "gelu_new" A__ = """gelu_new""" # Spell out all parameters in case the defaults change. A__ = GPTaConfig( vocab_size=5_0257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=UpperCAmelCase_ , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type="""cls_index""" , summary_use_proj=UpperCAmelCase_ , summary_activation=UpperCAmelCase_ , summary_proj_to_labels=UpperCAmelCase_ , summary_first_dropout=0.1 , scale_attn_weights=UpperCAmelCase_ , use_cache=UpperCAmelCase_ , bos_token_id=5_0256 , eos_token_id=5_0256 , ) else: A__ = GPTaConfig.from_json_file(args.config_file ) A__ = ["""GPT2LMHeadModel"""] # Convert. print("""Converting""" ) A__ = convert_megatron_checkpoint(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(UpperCAmelCase_ , UpperCAmelCase_ ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: A__ = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": A__ = """gpt2""" elif tokenizer_type == "PretrainedFromHF": A__ = ds_args.tokenizer_name_or_path else: raise ValueError(F"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: A__ = """gpt2""" A__ = AutoTokenizer.from_pretrained(UpperCAmelCase_ ) A__ = type(UpperCAmelCase_ ).__name__ A__ = tokenizer_class # Store the config to file. print("""Saving config""" ) config.save_pretrained(UpperCAmelCase_ ) # Save tokenizer based on args print(F"""Adding {tokenizer_class} tokenizer files""" ) tokenizer.save_pretrained(UpperCAmelCase_ ) # Store the state_dict to file. A__ = os.path.join(UpperCAmelCase_ , """pytorch_model.bin""" ) print(F"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(UpperCAmelCase_ , UpperCAmelCase_ ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
335
"""simple docstring""" import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC SCREAMING_SNAKE_CASE_ : str = parse(importlib.metadata.version('torch')) def _snake_case ( UpperCAmelCase_ : Union[str, Version] , UpperCAmelCase_ : str , UpperCAmelCase_ : str ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) A__ = STR_OPERATION_TO_FUNC[operation] if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = parse(importlib.metadata.version(UpperCAmelCase_ ) ) return operation(UpperCAmelCase_ , parse(UpperCAmelCase_ ) ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : str ): return compare_versions(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging SCREAMING_SNAKE_CASE_ : str = logging.get_logger(__name__) if is_vision_available(): import PIL class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["pixel_values"] def __init__( self: Optional[Any] , UpperCamelCase: bool = True , UpperCamelCase: Dict[str, int] = None , UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase: bool = True , UpperCamelCase: Dict[str, int] = None , UpperCamelCase: bool = True , UpperCamelCase: Union[int, float] = 1 / 2_55 , UpperCamelCase: bool = True , UpperCamelCase: Optional[Union[float, List[float]]] = None , UpperCamelCase: Optional[Union[float, List[float]]] = None , UpperCamelCase: bool = True , **UpperCamelCase: List[Any] , ): """simple docstring""" super().__init__(**UpperCamelCase ) A__ = size if size is not None else {"""shortest_edge""": 2_24} A__ = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) A__ = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} A__ = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase , param_name="""crop_size""" ) A__ = do_resize A__ = size A__ = resample A__ = do_center_crop A__ = crop_size A__ = do_rescale A__ = rescale_factor A__ = do_normalize A__ = image_mean if image_mean is not None else OPENAI_CLIP_MEAN A__ = image_std if image_std is not None else OPENAI_CLIP_STD A__ = do_convert_rgb def UpperCamelCase ( self: Dict , UpperCamelCase: np.ndarray , UpperCamelCase: Dict[str, int] , UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC , UpperCamelCase: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase: Any , ): """simple docstring""" A__ = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) A__ = get_resize_output_image_size(UpperCamelCase , size=size["""shortest_edge"""] , default_to_square=UpperCamelCase ) return resize(UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: Tuple , UpperCamelCase: np.ndarray , UpperCamelCase: Dict[str, int] , UpperCamelCase: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase: str , ): """simple docstring""" A__ = get_size_dict(UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(UpperCamelCase , size=(size["""height"""], size["""width"""]) , data_format=UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: np.ndarray , UpperCamelCase: Union[int, float] , UpperCamelCase: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase: int , ): """simple docstring""" return rescale(UpperCamelCase , scale=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: np.ndarray , UpperCamelCase: Union[float, List[float]] , UpperCamelCase: Union[float, List[float]] , UpperCamelCase: Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase: List[str] , ): """simple docstring""" return normalize(UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: ImageInput , UpperCamelCase: bool = None , UpperCamelCase: Dict[str, int] = None , UpperCamelCase: PILImageResampling = None , UpperCamelCase: bool = None , UpperCamelCase: int = None , UpperCamelCase: bool = None , UpperCamelCase: float = None , UpperCamelCase: bool = None , UpperCamelCase: Optional[Union[float, List[float]]] = None , UpperCamelCase: Optional[Union[float, List[float]]] = None , UpperCamelCase: bool = None , UpperCamelCase: Optional[Union[str, TensorType]] = None , UpperCamelCase: Optional[ChannelDimension] = ChannelDimension.FIRST , **UpperCamelCase: Tuple , ): """simple docstring""" 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(UpperCamelCase , param_name="""size""" , default_to_square=UpperCamelCase ) A__ = resample if resample is not None else self.resample A__ = do_center_crop if do_center_crop is not None else self.do_center_crop A__ = crop_size if crop_size is not None else self.crop_size A__ = get_size_dict(UpperCamelCase , param_name="""crop_size""" , default_to_square=UpperCamelCase ) A__ = do_rescale if do_rescale is not None else self.do_rescale A__ = rescale_factor if rescale_factor is not None else self.rescale_factor A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = image_mean if image_mean is not None else self.image_mean A__ = image_std if image_std is not None else self.image_std A__ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A__ = 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.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: A__ = [convert_to_rgb(UpperCamelCase ) for image in images] # All transformations expect numpy arrays. A__ = [to_numpy_array(UpperCamelCase ) for image in images] if do_resize: A__ = [self.resize(image=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase ) for image in images] if do_center_crop: A__ = [self.center_crop(image=UpperCamelCase , size=UpperCamelCase ) for image in images] if do_rescale: A__ = [self.rescale(image=UpperCamelCase , scale=UpperCamelCase ) for image in images] if do_normalize: A__ = [self.normalize(image=UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase ) for image in images] A__ = [to_channel_dimension_format(UpperCamelCase , UpperCamelCase ) for image in images] A__ = {"""pixel_values""": images} return BatchFeature(data=UpperCamelCase , tensor_type=UpperCamelCase )
335
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , 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/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != 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(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
1
"""simple docstring""" from math import sqrt def _snake_case ( UpperCAmelCase_ : int ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( number >= 0 ), "'number' must been an int and positive" A__ = True # 0 and 1 are none primes. if number <= 1: A__ = False for divisor in range(2 , int(round(sqrt(UpperCAmelCase_ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: A__ = False break # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'status' must been from type bool" return status def _snake_case ( UpperCAmelCase_ : str ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N A__ = list(range(2 , n + 1 ) ) A__ = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(UpperCAmelCase_ ) ): for j in range(i + 1 , len(UpperCAmelCase_ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): A__ = 0 # filters actual prime numbers. A__ = [x for x in begin_list if x != 0] # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'ans' must been from type list" return ans def _snake_case ( UpperCAmelCase_ : Any ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n > 2), "'N' must been an int and > 2" A__ = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(UpperCAmelCase_ ): ans.append(UpperCAmelCase_ ) # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'ans' must been from type list" return ans def _snake_case ( UpperCAmelCase_ : Dict ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and number >= 0, "'number' must been an int and >= 0" A__ = [] # this list will be returns of the function. # potential prime number factors. A__ = 2 A__ = number if number == 0 or number == 1: ans.append(UpperCAmelCase_ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(UpperCAmelCase_ ): while quotient != 1: if is_prime(UpperCAmelCase_ ) and (quotient % factor == 0): ans.append(UpperCAmelCase_ ) quotient /= factor else: factor += 1 else: ans.append(UpperCAmelCase_ ) # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'ans' must been from type list" return ans def _snake_case ( UpperCAmelCase_ : Optional[Any] ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( number >= 0 ), "'number' bust been an int and >= 0" A__ = 0 # prime factorization of 'number' A__ = prime_factorization(UpperCAmelCase_ ) A__ = max(UpperCAmelCase_ ) # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'ans' must been from type int" return ans def _snake_case ( UpperCAmelCase_ : Union[str, Any] ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( number >= 0 ), "'number' bust been an int and >= 0" A__ = 0 # prime factorization of 'number' A__ = prime_factorization(UpperCAmelCase_ ) A__ = min(UpperCAmelCase_ ) # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'ans' must been from type int" return ans def _snake_case ( UpperCAmelCase_ : Any ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'number' must been an int" assert isinstance(number % 2 == 0 , UpperCAmelCase_ ), "compare bust been from type bool" return number % 2 == 0 def _snake_case ( UpperCAmelCase_ : List[Any] ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ), "'number' must been an int" assert isinstance(number % 2 != 0 , UpperCAmelCase_ ), "compare bust been from type bool" return number % 2 != 0 def _snake_case ( UpperCAmelCase_ : Union[str, Any] ): assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (number > 2) and is_even(UpperCAmelCase_ ) ), "'number' must been an int, even and > 2" A__ = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' A__ = get_prime_numbers(UpperCAmelCase_ ) A__ = len(UpperCAmelCase_ ) # run variable for while-loops. A__ = 0 A__ = None # exit variable. for break up the loops A__ = True while i < len_pn and loop: A__ = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: A__ = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (len(UpperCAmelCase_ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _snake_case ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[Any] ): assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." A__ = 0 while numbera != 0: A__ = numbera % numbera A__ = numbera A__ = rest # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Any] ): assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." A__ = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' A__ = prime_factorization(UpperCAmelCase_ ) A__ = prime_factorization(UpperCAmelCase_ ) elif numbera == 1 or numbera == 1: A__ = [] A__ = [] A__ = max(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = 0 A__ = 0 A__ = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: A__ = prime_fac_a.count(UpperCAmelCase_ ) A__ = prime_fac_a.count(UpperCAmelCase_ ) for _ in range(max(UpperCAmelCase_ , UpperCAmelCase_ ) ): ans *= n else: A__ = prime_fac_a.count(UpperCAmelCase_ ) for _ in range(UpperCAmelCase_ ): ans *= n done.append(UpperCAmelCase_ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: A__ = prime_fac_a.count(UpperCAmelCase_ ) for _ in range(UpperCAmelCase_ ): ans *= n done.append(UpperCAmelCase_ ) # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _snake_case ( UpperCAmelCase_ : Optional[Any] ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n >= 0), "'number' must been a positive int" A__ = 0 A__ = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(UpperCAmelCase_ ): ans += 1 # precondition assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and is_prime( UpperCAmelCase_ ), "'ans' must been a prime number and from type int" return ans def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple ): assert ( is_prime(UpperCAmelCase_ ) and is_prime(UpperCAmelCase_ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" A__ = p_number_a + 1 # jump to the next number A__ = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(UpperCAmelCase_ ): number += 1 while number < p_number_a: ans.append(UpperCAmelCase_ ) number += 1 # fetch the next prime number. while not is_prime(UpperCAmelCase_ ): number += 1 # precondition assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ans[0] != p_number_a and ans[len(UpperCAmelCase_ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _snake_case ( UpperCAmelCase_ : Tuple ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n >= 1), "'n' must been int and >= 1" A__ = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(UpperCAmelCase_ ) # precondition assert ans[0] == 1 and ans[len(UpperCAmelCase_ ) - 1] == n, "Error in function getDivisiors(...)" return ans def _snake_case ( UpperCAmelCase_ : str ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and ( number > 1 ), "'number' must been an int and >= 1" A__ = get_divisors(UpperCAmelCase_ ) # precondition assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (divisors[0] == 1) and (divisors[len(UpperCAmelCase_ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any] ): assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. A__ = gcd(abs(UpperCAmelCase_ ) , abs(UpperCAmelCase_ ) ) # precondition assert ( isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _snake_case ( UpperCAmelCase_ : Any ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n >= 0), "'n' must been a int and >= 0" A__ = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _snake_case ( UpperCAmelCase_ : Optional[Any] ): assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and (n >= 0), "'n' must been an int and >= 0" A__ = 0 A__ = 1 A__ = 1 # this will be return for _ in range(n - 1 ): A__ = ans ans += fiba A__ = tmp return ans
335
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple=None , UpperCamelCase: Tuple=None , **UpperCamelCase: Dict ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = eval_examples A__ = post_process_function def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Dataset] = None , UpperCamelCase: List[Any]=None , UpperCamelCase: Optional[List[str]] = None , UpperCamelCase: str = "eval" , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(UpperCamelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCamelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCamelCase ) return metrics def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Dict=None , UpperCamelCase: str = "test" , **UpperCamelCase: Optional[int] ): """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(UpperCamelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase , """predict""" ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase )
335
1
"""simple docstring""" import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Union[str, Any] ): """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result["""bs"""] , model_result["""ss"""] ): A__ = model_result["""result"""][batch_size][sequence_length] self.assertIsNotNone(UpperCamelCase ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=UpperCamelCase , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = """sgugger/tiny-distilbert-classification""" A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , only_pretrain_model=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = AutoConfig.from_pretrained(UpperCamelCase ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=UpperCamelCase , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = AutoConfig.from_pretrained(UpperCamelCase ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = AutoConfig.from_pretrained(UpperCamelCase ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = """patrickvonplaten/t5-tiny-random""" A__ = AutoConfig.from_pretrained(UpperCamelCase ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase , configs=[config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , """Cannot do xla on CPU.""" ) def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=UpperCamelCase , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , use_xla=UpperCamelCase , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" with tempfile.TemporaryDirectory() as tmp_dir: A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=UpperCamelCase , save_to_csv=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(UpperCamelCase , """inf_time.csv""" ) , inference_memory_csv_file=os.path.join(UpperCamelCase , """inf_mem.csv""" ) , env_info_csv_file=os.path.join(UpperCamelCase , """env.csv""" ) , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) benchmark.run() self.assertTrue(Path(os.path.join(UpperCamelCase , """inf_time.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(UpperCamelCase , """inf_mem.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(UpperCamelCase , """env.csv""" ) ).exists() ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = """sshleifer/tiny-gpt2""" def _check_summary_is_not_empty(UpperCamelCase: List[str] ): self.assertTrue(hasattr(UpperCamelCase , """sequential""" ) ) self.assertTrue(hasattr(UpperCamelCase , """cumulative""" ) ) self.assertTrue(hasattr(UpperCamelCase , """current""" ) ) self.assertTrue(hasattr(UpperCamelCase , """total""" ) ) with tempfile.TemporaryDirectory() as tmp_dir: A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=UpperCamelCase , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(UpperCamelCase , """log.txt""" ) , log_print=UpperCamelCase , trace_memory_line_by_line=UpperCamelCase , eager_mode=UpperCamelCase , multi_process=UpperCamelCase , ) A__ = TensorFlowBenchmark(UpperCamelCase ) A__ = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(UpperCamelCase , """log.txt""" ) ).exists() )
335
"""simple docstring""" class a : """simple docstring""" def __init__( self: Dict ): """simple docstring""" A__ = {} def UpperCamelCase ( self: List[str] ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase , """ -> """ , """ -> """.join([str(UpperCamelCase ) for j in self.vertex[i]] ) ) def UpperCamelCase ( self: Any , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase ) else: # else make a new vertex A__ = [to_vertex] def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: list ): """simple docstring""" A__ = True print(UpperCamelCase , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[int] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
335
1
"""simple docstring""" import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller SCREAMING_SNAKE_CASE_ : List[str] = 3 def _snake_case ( UpperCAmelCase_ : int ): print("""Generating primitive root of p""" ) while True: A__ = random.randrange(3 , UpperCAmelCase_ ) if pow(UpperCAmelCase_ , 2 , UpperCAmelCase_ ) == 1: continue if pow(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) == 1: continue return g def _snake_case ( UpperCAmelCase_ : int ): print("""Generating prime p...""" ) A__ = rabin_miller.generate_large_prime(UpperCAmelCase_ ) # select large prime number. A__ = primitive_root(UpperCAmelCase_ ) # one primitive root on modulo p. A__ = random.randrange(3 , UpperCAmelCase_ ) # private_key -> have to be greater than 2 for safety. A__ = cryptomath.find_mod_inverse(pow(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ ) A__ = (key_size, e_a, e_a, p) A__ = (key_size, d) return public_key, private_key def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : int ): 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(UpperCAmelCase_ ) 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 _snake_case ( ): print("""Making key files...""" ) make_key_files("""elgamal""" , 2048 ) print("""Key files generation successful""" ) if __name__ == "__main__": main()
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
1
"""simple docstring""" import datasets from .evaluate import evaluate SCREAMING_SNAKE_CASE_ : Any = '\\n@inproceedings{Rajpurkar2016SQuAD10,\n title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n booktitle={EMNLP},\n year={2016}\n}\n' SCREAMING_SNAKE_CASE_ : Tuple = '\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nComputes SQuAD scores (F1 and EM).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': the text of the answer\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the SQuAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\nExamples:\n\n >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}]\n >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]\n >>> squad_metric = datasets.load_metric("squad")\n >>> results = squad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": {"""id""": datasets.Value("""string""" ), """prediction_text""": datasets.Value("""string""" )}, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://rajpurkar.github.io/SQuAD-explorer/"""] , reference_urls=["""https://rajpurkar.github.io/SQuAD-explorer/"""] , ) def UpperCamelCase ( self: Any , UpperCamelCase: Union[str, Any] , UpperCamelCase: Union[str, Any] ): """simple docstring""" A__ = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} A__ = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] A__ = evaluate(dataset=UpperCamelCase , predictions=UpperCamelCase ) return score
335
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad ): A__ = end_pointa[0] - end_pointa[0] A__ = end_pointa[1] - end_pointa[1] A__ = end_pointa[2] - end_pointa[2] return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : Vectorad ): A__ = ab[1] * ac[2] - ab[2] * ac[1] # *i A__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j A__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : int ): return tuple(round(UpperCAmelCase_ , UpperCAmelCase_ ) for x in vector ) == (0, 0, 0) def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : int = 10 ): A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ )
335
1
"""simple docstring""" import os from pathlib import Path def _snake_case ( ): from torch.utils.cpp_extension import load A__ = Path(UpperCAmelCase_ ).resolve().parent.parent.parent / """kernels""" / """deformable_detr""" A__ = [ root / filename for filename in [ """vision.cpp""", os.path.join("""cpu""" , """ms_deform_attn_cpu.cpp""" ), os.path.join("""cuda""" , """ms_deform_attn_cuda.cu""" ), ] ] load( """MultiScaleDeformableAttention""" , UpperCAmelCase_ , with_cuda=UpperCAmelCase_ , extra_include_paths=[str(UpperCAmelCase_ )] , extra_cflags=["""-DWITH_CUDA=1"""] , extra_cuda_cflags=[ """-DCUDA_HAS_FP16=1""", """-D__CUDA_NO_HALF_OPERATORS__""", """-D__CUDA_NO_HALF_CONVERSIONS__""", """-D__CUDA_NO_HALF2_OPERATORS__""", ] , ) import MultiScaleDeformableAttention as MSDA return MSDA
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_pegasus_x': ['PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusXConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST', 'PegasusXForConditionalGeneration', 'PegasusXModel', 'PegasusXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" A__ = inspect.getfile(accelerate.test_utils ) A__ = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_script.py"""] ) A__ = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_distributed_data_loop.py"""] ) A__ = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_ops.py"""] ) @require_multi_gpu def UpperCamelCase ( self: List[Any] ): """simple docstring""" print(f"""Found {torch.cuda.device_count()} devices.""" ) A__ = ["""torchrun""", f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" print(f"""Found {torch.cuda.device_count()} devices.""" ) A__ = ["""torchrun""", f"""--nproc_per_node={torch.cuda.device_count()}""", self.operation_file_path] print(f"""Command: {cmd}""" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def UpperCamelCase ( self: int ): """simple docstring""" A__ = ["""torchrun""", f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def UpperCamelCase ( self: str ): """simple docstring""" print(f"""Found {torch.cuda.device_count()} devices, using 2 devices only""" ) A__ = ["""torchrun""", f"""--nproc_per_node={torch.cuda.device_count()}""", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="""0,1""" ): execute_subprocess_async(UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Union[str, Any] = Accelerator() SCREAMING_SNAKE_CASE_ : Optional[Any] = (accelerator.state.process_index + 2, 1_0) SCREAMING_SNAKE_CASE_ : Dict = torch.randint(0, 1_0, shape).to(accelerator.device) SCREAMING_SNAKE_CASE_ : int = '' SCREAMING_SNAKE_CASE_ : Any = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." SCREAMING_SNAKE_CASE_ : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." SCREAMING_SNAKE_CASE_ : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
335
"""simple docstring""" import math class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: List[str]=0 ): # a graph with Node 0,1,...,N-1 """simple docstring""" A__ = n A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # adjacency matrix for weight A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Tuple ): """simple docstring""" A__ = w def UpperCamelCase ( self: int ): """simple docstring""" for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): A__ = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase ( self: int , UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 1_0) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 1_0) 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)
335
1
"""simple docstring""" import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors SCREAMING_SNAKE_CASE_ : List[str] = logging.getLogger(__name__) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "sequence-classification" def __init__( self: Dict , UpperCamelCase: List[str] ): """simple docstring""" if type(UpperCamelCase ) == dict: A__ = Namespace(**UpperCamelCase ) A__ = glue_output_modes[hparams.task] A__ = glue_tasks_num_labels[hparams.task] super().__init__(UpperCamelCase , UpperCamelCase , self.mode ) def UpperCamelCase ( self: Any , **UpperCamelCase: List[Any] ): """simple docstring""" return self.model(**UpperCamelCase ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[Any] ): """simple docstring""" A__ = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: A__ = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None A__ = self(**UpperCamelCase ) A__ = outputs[0] A__ = self.trainer.lr_schedulers[0]["""scheduler"""] A__ = {"""loss""": loss, """rate""": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.hparams A__ = processors[args.task]() A__ = processor.get_labels() for mode in ["train", "dev"]: A__ = self._feature_file(UpperCamelCase ) if os.path.exists(UpperCamelCase ) and not args.overwrite_cache: logger.info("""Loading features from cached file %s""" , UpperCamelCase ) else: logger.info("""Creating features from dataset file at %s""" , args.data_dir ) A__ = ( processor.get_dev_examples(args.data_dir ) if mode == """dev""" else processor.get_train_examples(args.data_dir ) ) A__ = convert_examples_to_features( UpperCamelCase , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info("""Saving features into cached file %s""" , UpperCamelCase ) torch.save(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: str , UpperCamelCase: int , UpperCamelCase: bool = False ): """simple docstring""" A__ = """dev""" if mode == """test""" else mode A__ = self._feature_file(UpperCamelCase ) logger.info("""Loading features from cached file %s""" , UpperCamelCase ) A__ = torch.load(UpperCamelCase ) A__ = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) A__ = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) A__ = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) if self.hparams.glue_output_mode == "classification": A__ = torch.tensor([f.label for f in features] , dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": A__ = torch.tensor([f.label for f in features] , dtype=torch.float ) return DataLoader( TensorDataset(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) , batch_size=UpperCamelCase , shuffle=UpperCamelCase , ) def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: A__ = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None A__ = self(**UpperCamelCase ) A__ , A__ = outputs[:2] A__ = logits.detach().cpu().numpy() A__ = inputs["""labels"""].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def UpperCamelCase ( self: str , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = torch.stack([x["""val_loss"""] for x in outputs] ).mean().detach().cpu().item() A__ = np.concatenate([x["""pred"""] for x in outputs] , axis=0 ) if self.hparams.glue_output_mode == "classification": A__ = np.argmax(UpperCamelCase , axis=1 ) elif self.hparams.glue_output_mode == "regression": A__ = np.squeeze(UpperCamelCase ) A__ = np.concatenate([x["""target"""] for x in outputs] , axis=0 ) A__ = [[] for _ in range(out_label_ids.shape[0] )] A__ = [[] for _ in range(out_label_ids.shape[0] )] A__ = {**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task , UpperCamelCase , UpperCamelCase )} A__ = dict(results.items() ) A__ = results return ret, preds_list, out_label_list def UpperCamelCase ( self: int , UpperCamelCase: list ): """simple docstring""" A__ , A__ , A__ = self._eval_end(UpperCamelCase ) A__ = ret["""log"""] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def UpperCamelCase ( self: List[str] , UpperCamelCase: Any ): """simple docstring""" A__ , A__ , A__ = self._eval_end(UpperCamelCase ) A__ = ret["""log"""] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def UpperCamelCase ( UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" BaseTransformer.add_model_specific_args(UpperCamelCase , UpperCamelCase ) parser.add_argument( """--max_seq_length""" , default=1_28 , type=UpperCamelCase , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--task""" , default="""""" , type=UpperCamelCase , required=UpperCamelCase , help="""The GLUE task to run""" , ) parser.add_argument( """--gpus""" , default=0 , type=UpperCamelCase , help="""The number of GPUs allocated for this, it is by default 0 meaning none""" , ) parser.add_argument( """--overwrite_cache""" , action="""store_true""" , help="""Overwrite the cached training and evaluation sets""" ) return parser def _snake_case ( ): A__ = argparse.ArgumentParser() add_generic_args(UpperCAmelCase_ , os.getcwd() ) A__ = GLUETransformer.add_model_specific_args(UpperCAmelCase_ , os.getcwd() ) A__ = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: A__ = os.path.join( """./results""" , F"""{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}""" , ) os.makedirs(args.output_dir ) A__ = GLUETransformer(UpperCAmelCase_ ) A__ = generic_train(UpperCAmelCase_ , UpperCAmelCase_ ) # Optionally, predict on dev set and write to output_dir if args.do_predict: A__ = sorted(glob.glob(os.path.join(args.output_dir , """checkpoint-epoch=*.ckpt""" ) , recursive=UpperCAmelCase_ ) ) A__ = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(UpperCAmelCase_ ) if __name__ == "__main__": main()
335
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def UpperCamelCase ( self: str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = (3, 32, 1_28) A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on A__ = dict(zip(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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(UpperCamelCase ) + """\n""" ) A__ = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 1_28}, } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta ) A__ = Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) return image_input def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """labels"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase ): processor() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.char_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) A__ = [seq.replace(""" """ , """""" ) for seq in decoded_tok] self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = None A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = torch.randn(1 , 27 , 38 ) A__ = torch.randn(1 , 27 , 5_02_57 ) A__ = torch.randn(1 , 27 , 3_05_22 ) A__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""] )
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : float ): if edge <= 0 or not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise ValueError("""Length must be a positive.""" ) return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2) def _snake_case ( UpperCAmelCase_ : float ): if edge <= 0 or not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise ValueError("""Length must be a positive.""" ) return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3) if __name__ == "__main__": import doctest doctest.testmod()
335
"""simple docstring""" import math def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase_ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
335
1
"""simple docstring""" import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params SCREAMING_SNAKE_CASE_ : List[Any] = getLogger(__name__) SCREAMING_SNAKE_CASE_ : str = 'cuda' if torch.cuda.is_available() else 'cpu' def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : int = 8 , UpperCAmelCase_ : str = DEFAULT_DEVICE , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Dict="summarization" , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): A__ = Path(UpperCAmelCase_ ).open("""w""" , encoding="""utf-8""" ) A__ = str(UpperCAmelCase_ ) A__ = AutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase_ ).to(UpperCAmelCase_ ) if fpaa: A__ = model.half() A__ = AutoTokenizer.from_pretrained(UpperCAmelCase_ ) logger.info(F"""Inferred tokenizer type: {tokenizer.__class__}""" ) # if this is wrong, check config.model_type. A__ = time.time() # update config with task specific params use_task_specific_params(UpperCAmelCase_ , UpperCAmelCase_ ) if prefix is None: A__ = prefix or getattr(model.config , """prefix""" , """""" ) or """""" for examples_chunk in tqdm(list(chunks(UpperCAmelCase_ , UpperCAmelCase_ ) ) ): A__ = [prefix + text for text in examples_chunk] A__ = tokenizer(UpperCAmelCase_ , return_tensors="""pt""" , truncation=UpperCAmelCase_ , padding="""longest""" ).to(UpperCAmelCase_ ) A__ = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **UpperCAmelCase_ , ) A__ = tokenizer.batch_decode(UpperCAmelCase_ , skip_special_tokens=UpperCAmelCase_ , clean_up_tokenization_spaces=UpperCAmelCase_ ) for hypothesis in dec: fout.write(hypothesis + """\n""" ) fout.flush() fout.close() A__ = int(time.time() - start_time ) # seconds A__ = len(UpperCAmelCase_ ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def _snake_case ( ): return datetime.datetime.now().strftime("""%Y-%m-%d %H:%M:%S""" ) def _snake_case ( UpperCAmelCase_ : Any=True ): A__ = argparse.ArgumentParser() parser.add_argument("""model_name""" , type=UpperCAmelCase_ , help="""like facebook/bart-large-cnn,t5-base, etc.""" ) parser.add_argument("""input_path""" , type=UpperCAmelCase_ , help="""like cnn_dm/test.source""" ) parser.add_argument("""save_path""" , type=UpperCAmelCase_ , help="""where to save summaries""" ) parser.add_argument("""--reference_path""" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help="""like cnn_dm/test.target""" ) parser.add_argument("""--score_path""" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , default="""metrics.json""" , help="""where to save metrics""" ) parser.add_argument("""--device""" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , default=UpperCAmelCase_ , help="""cuda, cuda:1, cpu etc.""" ) parser.add_argument( """--prefix""" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , default=UpperCAmelCase_ , help="""will be added to the begininng of src examples""" ) parser.add_argument("""--task""" , type=UpperCAmelCase_ , default="""summarization""" , help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""" , type=UpperCAmelCase_ , default=8 , required=UpperCAmelCase_ , help="""batch size""" ) parser.add_argument( """--n_obs""" , type=UpperCAmelCase_ , default=-1 , required=UpperCAmelCase_ , help="""How many observations. Defaults to all.""" ) parser.add_argument("""--fp16""" , action="""store_true""" ) parser.add_argument("""--dump-args""" , action="""store_true""" , help="""print the custom hparams with the results""" ) parser.add_argument( """--info""" , nargs="""?""" , type=UpperCAmelCase_ , const=datetime_now() , help=( """use in conjunction w/ --dump-args to print with the results whatever other info you'd like, e.g.""" """ lang=en-ru. If no value is passed, the current datetime string will be used.""" ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate A__ , A__ = parser.parse_known_args() A__ = parse_numeric_n_bool_cl_kwargs(UpperCAmelCase_ ) if parsed_args and verbose: print(F"""parsed the following generate kwargs: {parsed_args}""" ) A__ = [""" """ + x.rstrip() if """t5""" in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: A__ = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=UpperCAmelCase_ ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F"""score_path {args.score_path} will be overwritten unless you type ctrl-c.""" ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError("""Can't mix --fp16 and --device cpu""" ) A__ = generate_summaries_or_translations( UpperCAmelCase_ , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **UpperCAmelCase_ , ) if args.reference_path is None: return {} # Compute scores A__ = calculate_bleu if """translation""" in args.task else calculate_rouge A__ = [x.rstrip() for x in open(args.save_path ).readlines()] A__ = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(UpperCAmelCase_ )] A__ = score_fn(UpperCAmelCase_ , UpperCAmelCase_ ) scores.update(UpperCAmelCase_ ) if args.dump_args: scores.update(UpperCAmelCase_ ) if args.info: A__ = args.info if verbose: print(UpperCAmelCase_ ) if args.score_path is not None: json.dump(UpperCAmelCase_ , open(args.score_path , """w""" ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
335
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = 1 A__ = 3 A__ = (32, 32) A__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(UpperCamelCase ) return image @property def UpperCamelCase ( self: int ): """simple docstring""" torch.manual_seed(0 ) A__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) return model @property def UpperCamelCase ( self: Tuple ): """simple docstring""" torch.manual_seed(0 ) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) return model @property def UpperCamelCase ( self: List[Any] ): """simple docstring""" torch.manual_seed(0 ) A__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_06 , ) return RobertaSeriesModelWithTransformation(UpperCamelCase ) @property def UpperCamelCase ( self: str ): """simple docstring""" def extract(*UpperCamelCase: List[str] , **UpperCamelCase: Any ): class a : """simple docstring""" def __init__( self: Any ): """simple docstring""" A__ = torch.ones([0] ) def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" self.pixel_values.to(UpperCamelCase ) return self return Out() return extract def UpperCamelCase ( self: str ): """simple docstring""" A__ = """cpu""" # ensure determinism for the device-dependent torch.Generator A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) A__ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ) A__ = output.images A__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , return_dict=UpperCamelCase , )[0] A__ = image[0, -3:, -3:, -1] A__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A__ = np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: int ): """simple docstring""" A__ = self.dummy_cond_unet A__ = PNDMScheduler(skip_prk_steps=UpperCamelCase ) A__ = self.dummy_vae A__ = self.dummy_text_encoder A__ = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) A__ = 77 A__ = self.dummy_image.to(UpperCamelCase ) # put models in fp16 A__ = unet.half() A__ = vae.half() A__ = bert.half() # make sure here that pndm scheduler skips prk A__ = AltDiffusionImgaImgPipeline( unet=UpperCamelCase , scheduler=UpperCamelCase , vae=UpperCamelCase , text_encoder=UpperCamelCase , tokenizer=UpperCamelCase , safety_checker=UpperCamelCase , feature_extractor=self.dummy_extractor , ) A__ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=UpperCamelCase ) A__ = alt_pipe.to(UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase ) A__ = """A painting of a squirrel eating a burger""" A__ = torch.manual_seed(0 ) A__ = alt_pipe( [prompt] , generator=UpperCamelCase , num_inference_steps=2 , output_type="""np""" , image=UpperCamelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) # resize to resolution that is divisible by 8 but not 16 or 32 A__ = init_image.resize((7_60, 5_04) ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] A__ = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) A__ = np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) A__ = init_image.resize((7_68, 5_12) ) A__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy""" ) A__ = """BAAI/AltDiffusion""" A__ = AltDiffusionImgaImgPipeline.from_pretrained( UpperCamelCase , safety_checker=UpperCamelCase , ) pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) pipe.enable_attention_slicing() A__ = """A fantasy landscape, trending on artstation""" A__ = torch.manual_seed(0 ) A__ = pipe( prompt=UpperCamelCase , image=UpperCamelCase , strength=0.75 , guidance_scale=7.5 , generator=UpperCamelCase , output_type="""np""" , ) A__ = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
335
1
"""simple docstring""" import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 'hf-internal-testing/tiny-random-bert' SCREAMING_SNAKE_CASE_ : int = os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert') SCREAMING_SNAKE_CASE_ : int = '9b8c223d42b2188cb49d29af482996f9d0f3e5a6' class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" A__ = cached_file(UpperCamelCase , UpperCamelCase ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(UpperCamelCase ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(UpperCamelCase , UpperCamelCase ) ) ) with open(os.path.join(UpperCamelCase , """refs""" , """main""" ) ) as f: A__ = f.read() self.assertEqual(UpperCamelCase , os.path.join(UpperCamelCase , """snapshots""" , UpperCamelCase , UpperCamelCase ) ) self.assertTrue(os.path.isfile(UpperCamelCase ) ) # File is cached at the same place the second time. A__ = cached_file(UpperCamelCase , UpperCamelCase ) self.assertEqual(UpperCamelCase , UpperCamelCase ) # Using a specific revision to test the full commit hash. A__ = cached_file(UpperCamelCase , UpperCamelCase , revision="""9b8c223""" ) self.assertEqual(UpperCamelCase , os.path.join(UpperCamelCase , """snapshots""" , UpperCamelCase , UpperCamelCase ) ) def UpperCamelCase ( self: str ): """simple docstring""" with self.assertRaisesRegex(UpperCamelCase , """is not a valid model identifier""" ): A__ = cached_file("""tiny-random-bert""" , UpperCamelCase ) with self.assertRaisesRegex(UpperCamelCase , """is not a valid git identifier""" ): A__ = cached_file(UpperCamelCase , UpperCamelCase , revision="""aaaa""" ) with self.assertRaisesRegex(UpperCamelCase , """does not appear to have a file named""" ): A__ = cached_file(UpperCamelCase , """conf""" ) def UpperCamelCase ( self: Tuple ): """simple docstring""" with self.assertRaisesRegex(UpperCamelCase , """does not appear to have a file named""" ): A__ = cached_file(UpperCamelCase , """conf""" ) with open(os.path.join(UpperCamelCase , """refs""" , """main""" ) ) as f: A__ = f.read() self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase , """.no_exist""" , UpperCamelCase , """conf""" ) ) ) A__ = cached_file(UpperCamelCase , """conf""" , _raise_exceptions_for_missing_entries=UpperCamelCase ) self.assertIsNone(UpperCamelCase ) A__ = cached_file(UpperCamelCase , """conf""" , local_files_only=UpperCamelCase , _raise_exceptions_for_missing_entries=UpperCamelCase ) self.assertIsNone(UpperCamelCase ) A__ = mock.Mock() A__ = 5_00 A__ = {} A__ = HTTPError A__ = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("""requests.Session.request""" , return_value=UpperCamelCase ) as mock_head: A__ = cached_file(UpperCamelCase , """conf""" , _raise_exceptions_for_connection_errors=UpperCamelCase ) self.assertIsNone(UpperCamelCase ) # This check we did call the fake head request mock_head.assert_called() def UpperCamelCase ( self: int ): """simple docstring""" self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only""" , UpperCamelCase ) ) self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" , UpperCamelCase ) ) self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" , UpperCamelCase ) ) def UpperCamelCase ( self: Dict ): """simple docstring""" self.assertIsNone(get_file_from_repo("""bert-base-cased""" , """ahah.txt""" ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(UpperCamelCase , """is not a valid model identifier""" ): get_file_from_repo("""bert-base-case""" , UpperCamelCase ) # The function raises if the revision does not exist. with self.assertRaisesRegex(UpperCamelCase , """is not a valid git identifier""" ): get_file_from_repo("""bert-base-cased""" , UpperCamelCase , revision="""ahaha""" ) A__ = get_file_from_repo("""bert-base-cased""" , UpperCamelCase ) # The name is the cached name which is not very easy to test, so instead we load the content. A__ = json.loads(open(UpperCamelCase , """r""" ).read() ) self.assertEqual(config["""hidden_size"""] , 7_68 ) def UpperCamelCase ( self: int ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: A__ = Path(UpperCamelCase ) / """a.txt""" filename.touch() self.assertEqual(get_file_from_repo(UpperCamelCase , """a.txt""" ) , str(UpperCamelCase ) ) self.assertIsNone(get_file_from_repo(UpperCamelCase , """b.txt""" ) )
335
"""simple docstring""" import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = FileLock(str(tmpdir / """foo.lock""" ) ) A__ = 0.01 with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): A__ = time.time() locka.acquire(UpperCAmelCase_ ) assert time.time() - _start > timeout def _snake_case ( UpperCAmelCase_ : List[Any] ): A__ = """a""" * 1000 + """.lock""" A__ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(""".lock""" ) assert not locka._lock_file.endswith(UpperCAmelCase_ ) assert len(os.path.basename(locka._lock_file ) ) <= 255 A__ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(UpperCAmelCase_ ): locka.acquire(0 )
335
1
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase = "image_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = AutoModelForVisualQuestionAnswering UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" return self.pre_processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) def UpperCamelCase ( self: str , UpperCamelCase: str ): """simple docstring""" with torch.no_grad(): return self.model(**UpperCamelCase ).logits def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int ): """simple docstring""" A__ = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
335
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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 import torch from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = "dandelin/vilt-b32-finetuned-vqa" UpperCAmelCase = ( "This is a tool that answers a question about an image. It takes an input named `image` which should be the " "image containing the information, as well as a `question` which should be the question in English. It " "returns a text that is the answer to the question." ) UpperCAmelCase = "image_qa" UpperCAmelCase = AutoProcessor UpperCAmelCase = AutoModelForVisualQuestionAnswering UpperCAmelCase = ["image", "text"] UpperCAmelCase = ["text"] def __init__( self: List[str] , *UpperCamelCase: Dict , **UpperCamelCase: List[str] ): """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: "Image" , UpperCamelCase: str ): """simple docstring""" return self.pre_processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) def UpperCamelCase ( self: str , UpperCamelCase: str ): """simple docstring""" with torch.no_grad(): return self.model(**UpperCamelCase ).logits def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: int ): """simple docstring""" A__ = outputs.argmax(-1 ).item() return self.model.config.idalabel[idx]
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) SCREAMING_SNAKE_CASE_ : Optional[int] = { 'configuration_resnet': ['RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ResNetConfig', 'ResNetOnnxConfig'] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : str = [ 'RESNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'ResNetForImageClassification', 'ResNetModel', 'ResNetPreTrainedModel', 'ResNetBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFResNetForImageClassification', 'TFResNetModel', 'TFResNetPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Any = [ 'FlaxResNetForImageClassification', 'FlaxResNetModel', 'FlaxResNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_resnet import RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ResNetConfig, ResNetOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_resnet import ( RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, ResNetBackbone, ResNetForImageClassification, ResNetModel, ResNetPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_resnet import ( TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFResNetForImageClassification, TFResNetModel, TFResNetPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel, FlaxResNetPreTrainedModel else: import sys SCREAMING_SNAKE_CASE_ : Any = _LazyModule(__name__, globals()['__file__'], _import_structure)
335
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( 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, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
1
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def UpperCamelCase ( self: str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = (3, 32, 1_28) A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on A__ = dict(zip(UpperCamelCase , range(len(UpperCamelCase ) ) ) ) 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(UpperCamelCase ) + """\n""" ) A__ = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 1_28}, } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: Union[str, Any] ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[Any] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta ) A__ = Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) return image_input def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , UpperCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """test""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """labels"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase ): processor() def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.char_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) A__ = [seq.replace(""" """ , """""" ) for seq in decoded_tok] self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = None A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = MgpstrProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = torch.randn(1 , 27 , 38 ) A__ = torch.randn(1 , 27 , 5_02_57 ) A__ = torch.randn(1 , 27 , 3_05_22 ) A__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""] )
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : Dict ): # noqa: E741 A__ = len(UpperCAmelCase_ ) A__ = 0 A__ = [0] * n A__ = [False] * n A__ = [False] * n def dfs(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): if parent == root: out_edge_count += 1 A__ = True A__ = at for to in l[at]: if to == parent: pass elif not visited[to]: A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: A__ = True # AP found via cycle if at == low[to]: A__ = True else: A__ = min(low[at] , UpperCAmelCase_ ) return out_edge_count for i in range(UpperCAmelCase_ ): if not visited[i]: A__ = 0 A__ = dfs(UpperCAmelCase_ , UpperCAmelCase_ , -1 , UpperCAmelCase_ ) A__ = out_edge_count > 1 for x in range(len(UpperCAmelCase_ ) ): if is_art[x] is True: print(UpperCAmelCase_ ) # Adjacency list of graph SCREAMING_SNAKE_CASE_ : Optional[int] = { 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)
335
1
"""simple docstring""" from __future__ import annotations SCREAMING_SNAKE_CASE_ : Optional[Any] = [] def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): for i in range(len(UpperCAmelCase_ ) ): if board[row][i] == 1: return False for i in range(len(UpperCAmelCase_ ) ): if board[i][column] == 1: return False for i, j in zip(range(UpperCAmelCase_ , -1 , -1 ) , range(UpperCAmelCase_ , -1 , -1 ) ): if board[i][j] == 1: return False for i, j in zip(range(UpperCAmelCase_ , -1 , -1 ) , range(UpperCAmelCase_ , len(UpperCAmelCase_ ) ) ): if board[i][j] == 1: return False return True def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int ): if row >= len(UpperCAmelCase_ ): solution.append(UpperCAmelCase_ ) printboard(UpperCAmelCase_ ) print() return True for i in range(len(UpperCAmelCase_ ) ): if is_safe(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): A__ = 1 solve(UpperCAmelCase_ , row + 1 ) A__ = 0 return False def _snake_case ( UpperCAmelCase_ : list[list[int]] ): for i in range(len(UpperCAmelCase_ ) ): for j in range(len(UpperCAmelCase_ ) ): if board[i][j] == 1: print("""Q""" , end=""" """ ) else: print(""".""" , end=""" """ ) print() # n=int(input("The no. of queens")) SCREAMING_SNAKE_CASE_ : Union[str, Any] = 8 SCREAMING_SNAKE_CASE_ : Union[str, Any] = [[0 for i in range(n)] for j in range(n)] solve(board, 0) print('The total no. of solutions are :', len(solution))
335
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: str ): """simple docstring""" A__ = get_activation("""swish""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""silu""" ) self.assertIsInstance(UpperCamelCase , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = get_activation("""mish""" ) self.assertIsInstance(UpperCamelCase , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = get_activation("""gelu""" ) self.assertIsInstance(UpperCamelCase , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
335
1
"""simple docstring""" import torch from diffusers import DiffusionPipeline class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: Optional[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCamelCase , scheduler=UpperCamelCase ) def __call__( self: str ): """simple docstring""" A__ = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , ) A__ = 1 A__ = self.unet(UpperCamelCase , UpperCamelCase ).sample A__ = self.scheduler.step(UpperCamelCase , UpperCamelCase , UpperCamelCase ).prev_sample A__ = scheduler_output - scheduler_output + torch.ones_like(UpperCamelCase ) return result
335
"""simple docstring""" from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( "The RoBERTa Model transformer with early exiting (DeeRoBERTa). ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Union[str, Any] , UpperCamelCase: Any ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = RobertaEmbeddings(UpperCamelCase ) self.init_weights() @add_start_docstrings( "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = RobertaConfig UpperCAmelCase = "roberta" def __init__( self: Optional[Any] , UpperCamelCase: int ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = config.num_hidden_layers A__ = DeeRobertaModel(UpperCamelCase ) A__ = nn.Dropout(config.hidden_dropout_prob ) A__ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int]=None , UpperCamelCase: str=None , UpperCamelCase: str=None , UpperCamelCase: List[str]=None , UpperCamelCase: Dict=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None , UpperCamelCase: Optional[int]=-1 , UpperCamelCase: Optional[Any]=False , ): """simple docstring""" A__ = self.num_layers try: A__ = self.roberta( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , position_ids=UpperCamelCase , head_mask=UpperCamelCase , inputs_embeds=UpperCamelCase , ) A__ = outputs[1] A__ = self.dropout(UpperCamelCase ) A__ = self.classifier(UpperCamelCase ) A__ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A__ = e.message A__ = e.exit_layer A__ = outputs[0] if not self.training: A__ = entropy(UpperCamelCase ) A__ = [] A__ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A__ = [] for highway_exit in outputs[-1]: A__ = highway_exit[0] if not self.training: highway_logits_all.append(UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A__ = MSELoss() A__ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A__ = CrossEntropyLoss() A__ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(UpperCamelCase ) if train_highway: A__ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A__ = (loss,) + outputs if not self.training: A__ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A__ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
335
1
"""simple docstring""" import torch from torch import nn class a ( nn.Module ): """simple docstring""" def __init__( self: Tuple , UpperCamelCase: Dict , UpperCamelCase: str , UpperCamelCase: int , UpperCamelCase: Dict , UpperCamelCase: List[str]=1 , UpperCamelCase: Optional[Any]=False ): """simple docstring""" super().__init__() A__ = n_token A__ = d_embed A__ = d_proj A__ = cutoffs + [n_token] A__ = [0] + self.cutoffs A__ = div_val A__ = self.cutoffs[0] A__ = len(self.cutoffs ) - 1 A__ = self.shortlist_size + self.n_clusters if self.n_clusters > 0: A__ = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) A__ = nn.Parameter(torch.zeros(self.n_clusters ) ) A__ = nn.ModuleList() A__ = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(UpperCamelCase , UpperCamelCase ) ) ) else: self.out_projs.append(UpperCamelCase ) self.out_layers.append(nn.Linear(UpperCamelCase , UpperCamelCase ) ) else: for i in range(len(self.cutoffs ) ): A__ , A__ = self.cutoff_ends[i], self.cutoff_ends[i + 1] A__ = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(UpperCamelCase , UpperCamelCase ) ) ) self.out_layers.append(nn.Linear(UpperCamelCase , r_idx - l_idx ) ) A__ = keep_order def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Dict , UpperCamelCase: Tuple , UpperCamelCase: Optional[Any] , UpperCamelCase: Dict ): """simple docstring""" if proj is None: A__ = nn.functional.linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: A__ = nn.functional.linear(UpperCamelCase , proj.t().contiguous() ) A__ = nn.functional.linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def UpperCamelCase ( self: int , UpperCamelCase: Optional[Any] , UpperCamelCase: Dict=None , UpperCamelCase: Optional[int]=False ): """simple docstring""" if labels is not None: # Shift so that tokens < n predict n A__ = hidden[..., :-1, :].contiguous() A__ = labels[..., 1:].contiguous() A__ = hidden.view(-1 , hidden.size(-1 ) ) A__ = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError("""Input and labels should have the same size in the batch dimension.""" ) else: A__ = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: A__ = self._compute_logit(UpperCamelCase , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: A__ = labels != -1_00 A__ = torch.zeros_like(UpperCamelCase , dtype=hidden.dtype , device=hidden.device ) A__ = ( -nn.functional.log_softmax(UpperCamelCase , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: A__ = nn.functional.log_softmax(UpperCamelCase , dim=-1 ) else: # construct weights and biases A__ , A__ = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: A__ , A__ = self.cutoff_ends[i], self.cutoff_ends[i + 1] A__ = self.out_layers[0].weight[l_idx:r_idx] A__ = self.out_layers[0].bias[l_idx:r_idx] else: A__ = self.out_layers[i].weight A__ = self.out_layers[i].bias if i == 0: A__ = torch.cat([weight_i, self.cluster_weight] , dim=0 ) A__ = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(UpperCamelCase ) biases.append(UpperCamelCase ) A__ , A__ , A__ = weights[0], biases[0], self.out_projs[0] A__ = self._compute_logit(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = nn.functional.log_softmax(UpperCamelCase , dim=1 ) if labels is None: A__ = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: A__ = torch.zeros_like(UpperCamelCase , dtype=hidden.dtype , device=hidden.device ) A__ = 0 A__ = [0] + self.cutoffs for i in range(len(UpperCamelCase ) - 1 ): A__ , A__ = cutoff_values[i], cutoff_values[i + 1] if labels is not None: A__ = (labels >= l_idx) & (labels < r_idx) A__ = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue A__ = labels.index_select(0 , UpperCamelCase ) - l_idx A__ = head_logprob.index_select(0 , UpperCamelCase ) A__ = hidden.index_select(0 , UpperCamelCase ) else: A__ = hidden if i == 0: if labels is not None: A__ = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: A__ = head_logprob[:, : self.cutoffs[0]] else: A__ , A__ , A__ = weights[i], biases[i], self.out_projs[i] A__ = self._compute_logit(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = nn.functional.log_softmax(UpperCamelCase , dim=1 ) A__ = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: A__ = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: A__ = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i A__ = logprob_i if labels is not None: if (hasattr(self , """keep_order""" ) and self.keep_order) or keep_order: out.index_copy_(0 , UpperCamelCase , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def UpperCamelCase ( self: Any , UpperCamelCase: int ): """simple docstring""" if self.n_clusters == 0: A__ = self._compute_logit(UpperCamelCase , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(UpperCamelCase , dim=-1 ) else: # construct weights and biases A__ , A__ = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: A__ , A__ = self.cutoff_ends[i], self.cutoff_ends[i + 1] A__ = self.out_layers[0].weight[l_idx:r_idx] A__ = self.out_layers[0].bias[l_idx:r_idx] else: A__ = self.out_layers[i].weight A__ = self.out_layers[i].bias if i == 0: A__ = torch.cat([weight_i, self.cluster_weight] , dim=0 ) A__ = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(UpperCamelCase ) biases.append(UpperCamelCase ) A__ , A__ , A__ = weights[0], biases[0], self.out_projs[0] A__ = self._compute_logit(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = hidden.new_empty((head_logit.size(0 ), self.n_token) ) A__ = nn.functional.log_softmax(UpperCamelCase , dim=1 ) A__ = [0] + self.cutoffs for i in range(len(UpperCamelCase ) - 1 ): A__ , A__ = cutoff_values[i], cutoff_values[i + 1] if i == 0: A__ = head_logprob[:, : self.cutoffs[0]] else: A__ , A__ , A__ = weights[i], biases[i], self.out_projs[i] A__ = self._compute_logit(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = nn.functional.log_softmax(UpperCamelCase , dim=1 ) A__ = head_logprob[:, -i] + tail_logprob_i A__ = logprob_i return out
335
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE_ : int = [ 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the' ' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe' ' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.', 'The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal' ' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s' ' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the' ' body.', 'Amnesty International releases its annual report on the death penalty. The report catalogs the use of' ' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the' ' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital' ' punishment.', ] SCREAMING_SNAKE_CASE_ : List[Any] = [ 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .' ' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz' ' had informed his Lufthansa training school of an episode of severe depression, airline says .', 'Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .' ' Israel and the United States opposed the move, which could open the door to war crimes investigations against' ' Israelis .', 'Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to' ' death . Organization claims that governments around the world are using the threat of terrorism to advance' ' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death' ' sentences up by 28% .', ] def _snake_case ( ): A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2""", """rougeL"""] ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , bootstrap_aggregation=UpperCAmelCase_ , rouge_keys=["""rouge2"""] ) assert ( pd.DataFrame(no_aggregation["""rouge2"""] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["""rouge2"""] ).fmeasure.mean() ) def _snake_case ( ): A__ = """rougeLsum""" A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _snake_case ( ): A__ = ["""rouge1""", """rouge2""", """rougeL"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ , rouge_keys=UpperCAmelCase_ ) assert score_sep == score_no_sep def _snake_case ( ): A__ = [ """Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.""", """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""", ] A__ = [ """Margot Frank, died in 1945, a month earlier than previously thought.""", """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of""" """ the final seconds on board Flight 9525.""", ] assert calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) == calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , newline_sep=UpperCAmelCase_ ) def _snake_case ( ): A__ = [ """\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" """ ] A__ = [ """ Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .""" ] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] , newline_sep=UpperCAmelCase_ )["""rougeLsum"""] A__ = calculate_rouge(UpperCAmelCase_ , UpperCAmelCase_ , rouge_keys=["""rougeLsum"""] )["""rougeLsum"""] assert new_score > prev_score def _snake_case ( ): A__ = Path("""examples/seq2seq/test_data/wmt_en_ro""" ) A__ = calculate_rouge_path(data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = calculate_rouge_path( data_dir.joinpath("""test.source""" ) , data_dir.joinpath("""test.target""" ) , bootstrap_aggregation=UpperCAmelCase_ ) assert isinstance(UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" import math class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: List[str]=0 ): # a graph with Node 0,1,...,N-1 """simple docstring""" A__ = n A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # adjacency matrix for weight A__ = [ [math.inf for j in range(0 , UpperCamelCase )] for i in range(0 , UpperCamelCase ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Tuple ): """simple docstring""" A__ = w def UpperCamelCase ( self: int ): """simple docstring""" for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): A__ = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase ( self: int , UpperCamelCase: List[str] , UpperCamelCase: Dict ): """simple docstring""" return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : List[Any] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 1_0) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 1_0) 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)
335
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig SCREAMING_SNAKE_CASE_ : Union[str, Any] = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'MobileNetV1Config' # Base docstring SCREAMING_SNAKE_CASE_ : str = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : List[str] = [1, 1_0_2_4, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ : Optional[Any] = 'google/mobilenet_v1_1.0_224' SCREAMING_SNAKE_CASE_ : Tuple = 'tabby, tabby cat' SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None ): A__ = {} if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = model.mobilenet_va else: A__ = model A__ = """MobilenetV1/Conv2d_0/""" A__ = backbone.conv_stem.convolution.weight A__ = backbone.conv_stem.normalization.bias A__ = backbone.conv_stem.normalization.weight A__ = backbone.conv_stem.normalization.running_mean A__ = backbone.conv_stem.normalization.running_var for i in range(13 ): A__ = i + 1 A__ = i * 2 A__ = backbone.layer[pt_index] A__ = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var A__ = backbone.layer[pt_index + 1] A__ = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" A__ = pointer.convolution.weight A__ = pointer.normalization.bias A__ = pointer.normalization.weight A__ = pointer.normalization.running_mean A__ = pointer.normalization.running_var if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = """MobilenetV1/Logits/Conv2d_1c_1x1/""" A__ = model.classifier.weight A__ = model.classifier.bias return tf_to_pt_map def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model A__ = tf.train.list_variables(UpperCAmelCase_ ) A__ = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) A__ = tf.train.load_variable(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = array # Build TF to PyTorch weights loading map A__ = _build_tf_to_pytorch_map(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue A__ = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) A__ = np.transpose(UpperCAmelCase_ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer A__ = array.squeeze().transpose() else: A__ = np.transpose(UpperCAmelCase_ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) A__ = torch.from_numpy(UpperCAmelCase_ ) tf_weights.pop(UpperCAmelCase_ , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp""" , UpperCAmelCase_ ) tf_weights.pop(name + """/RMSProp_1""" , UpperCAmelCase_ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , UpperCAmelCase_ ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _snake_case ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : nn.Convad ): A__ , A__ = features.shape[-2:] A__ , A__ = conv_layer.stride A__ , A__ = conv_layer.kernel_size if in_height % stride_height == 0: A__ = max(kernel_height - stride_height , 0 ) else: A__ = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: A__ = max(kernel_width - stride_width , 0 ) else: A__ = max(kernel_width - (in_width % stride_width) , 0 ) A__ = pad_along_width // 2 A__ = pad_along_width - pad_left A__ = pad_along_height // 2 A__ = pad_along_height - pad_top A__ = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(UpperCAmelCase_ , UpperCAmelCase_ , """constant""" , 0.0 ) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: int , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: Optional[int] = 1 , UpperCamelCase: bool = False , UpperCamelCase: Optional[bool] = True , UpperCamelCase: Optional[bool or str] = True , ): """simple docstring""" super().__init__() A__ = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) A__ = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) A__ = nn.Convad( in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=UpperCamelCase , stride=UpperCamelCase , padding=UpperCamelCase , groups=UpperCamelCase , bias=UpperCamelCase , padding_mode="""zeros""" , ) if use_normalization: A__ = nn.BatchNormad( num_features=UpperCamelCase , eps=config.layer_norm_eps , momentum=0.9_997 , affine=UpperCamelCase , track_running_stats=UpperCamelCase , ) else: A__ = None if use_activation: if isinstance(UpperCamelCase , UpperCamelCase ): A__ = ACTaFN[use_activation] elif isinstance(config.hidden_act , UpperCamelCase ): A__ = ACTaFN[config.hidden_act] else: A__ = config.hidden_act else: A__ = None def UpperCamelCase ( self: List[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" if self.config.tf_padding: A__ = apply_tf_padding(UpperCamelCase , self.convolution ) A__ = self.convolution(UpperCamelCase ) if self.normalization is not None: A__ = self.normalization(UpperCamelCase ) if self.activation is not None: A__ = self.activation(UpperCamelCase ) return features class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = MobileNetVaConfig UpperCAmelCase = load_tf_weights_in_mobilenet_va UpperCAmelCase = "mobilenet_v1" UpperCAmelCase = "pixel_values" UpperCAmelCase = False def UpperCamelCase ( self: Any , UpperCamelCase: Union[nn.Linear, nn.Convad] ): """simple docstring""" if isinstance(UpperCamelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(UpperCamelCase , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ : Optional[Any] = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Any , UpperCamelCase: MobileNetVaConfig , UpperCamelCase: bool = True ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config A__ = 32 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) A__ = MobileNetVaConvLayer( UpperCamelCase , in_channels=config.num_channels , out_channels=UpperCamelCase , kernel_size=3 , stride=2 , ) A__ = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] A__ = nn.ModuleList() for i in range(13 ): A__ = out_channels if strides[i] == 2 or i == 0: depth *= 2 A__ = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=3 , stride=strides[i] , groups=UpperCamelCase , ) ) self.layer.append( MobileNetVaConvLayer( UpperCamelCase , in_channels=UpperCamelCase , out_channels=UpperCamelCase , kernel_size=1 , ) ) A__ = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCamelCase ( self: Dict , UpperCamelCase: Optional[Any] ): """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) A__ = self.conv_stem(UpperCamelCase ) A__ = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): A__ = layer_module(UpperCamelCase ) if output_hidden_states: A__ = all_hidden_states + (hidden_states,) A__ = hidden_states if self.pooler is not None: A__ = torch.flatten(self.pooler(UpperCamelCase ) , start_dim=1 ) else: A__ = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase , pooler_output=UpperCamelCase , hidden_states=UpperCamelCase , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ", _lowerCamelCase, ) class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: MobileNetVaConfig ): """simple docstring""" super().__init__(UpperCamelCase ) A__ = config.num_labels A__ = MobileNetVaModel(UpperCamelCase ) A__ = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head A__ = nn.Dropout(config.classifier_dropout_prob , inplace=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , UpperCamelCase: Optional[torch.Tensor] = None , UpperCamelCase: Optional[bool] = None , ): """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = self.mobilenet_va(UpperCamelCase , output_hidden_states=UpperCamelCase , return_dict=UpperCamelCase ) A__ = outputs.pooler_output if return_dict else outputs[1] A__ = self.classifier(self.dropout(UpperCamelCase ) ) A__ = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: A__ = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): A__ = """single_label_classification""" else: A__ = """multi_label_classification""" if self.config.problem_type == "regression": A__ = MSELoss() if self.num_labels == 1: A__ = loss_fct(logits.squeeze() , labels.squeeze() ) else: A__ = loss_fct(UpperCamelCase , UpperCamelCase ) elif self.config.problem_type == "single_label_classification": A__ = CrossEntropyLoss() A__ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": A__ = BCEWithLogitsLoss() A__ = loss_fct(UpperCamelCase , UpperCamelCase ) if not return_dict: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=UpperCamelCase , logits=UpperCamelCase , hidden_states=outputs.hidden_states , )
335
1
"""simple docstring""" import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class a : """simple docstring""" def __init__( self: List[Any] , UpperCamelCase: Optional[int] , UpperCamelCase: int=sys.maxsize ): """simple docstring""" A__ = """bilinear""" A__ = max_size A__ = short_edge_length def __call__( self: str , UpperCamelCase: List[str] ): """simple docstring""" A__ = [] for img in imgs: A__ , A__ = img.shape[:2] # later: provide list and randomly choose index for resize A__ = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1 ) if size == 0: return img A__ = size * 1.0 / min(UpperCamelCase , UpperCamelCase ) if h < w: A__ , A__ = size, scale * w else: A__ , A__ = scale * h, size if max(UpperCamelCase , UpperCamelCase ) > self.max_size: A__ = self.max_size * 1.0 / max(UpperCamelCase , UpperCamelCase ) A__ = newh * scale A__ = neww * scale A__ = int(neww + 0.5 ) A__ = int(newh + 0.5 ) if img.dtype == np.uinta: A__ = Image.fromarray(UpperCamelCase ) A__ = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR ) A__ = np.asarray(UpperCamelCase ) else: A__ = img.permute(2 , 0 , 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw A__ = nn.functional.interpolate( UpperCamelCase , (newh, neww) , mode=self.interp_method , align_corners=UpperCamelCase ).squeeze(0 ) img_augs.append(UpperCamelCase ) return img_augs class a : """simple docstring""" def __init__( self: List[str] , UpperCamelCase: Tuple ): """simple docstring""" A__ = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST ) A__ = cfg.INPUT.FORMAT A__ = cfg.SIZE_DIVISIBILITY A__ = cfg.PAD_VALUE A__ = cfg.INPUT.MAX_SIZE_TEST A__ = cfg.MODEL.DEVICE A__ = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) A__ = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) A__ = lambda UpperCamelCase : (x - self.pixel_mean) / self.pixel_std def UpperCamelCase ( self: List[str] , UpperCamelCase: Tuple ): """simple docstring""" A__ = tuple(max(UpperCamelCase ) for s in zip(*[img.shape for img in images] ) ) A__ = [im.shape[-2:] for im in images] A__ = [ nn.functional.pad( UpperCamelCase , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(UpperCamelCase , UpperCamelCase ) ] return torch.stack(UpperCamelCase ), torch.tensor(UpperCamelCase ) def __call__( self: Optional[int] , UpperCamelCase: str , UpperCamelCase: Union[str, Any]=False ): """simple docstring""" with torch.no_grad(): if not isinstance(UpperCamelCase , UpperCamelCase ): A__ = [images] if single_image: assert len(UpperCamelCase ) == 1 for i in range(len(UpperCamelCase ) ): if isinstance(images[i] , torch.Tensor ): images.insert(UpperCamelCase , images.pop(UpperCamelCase ).to(self.device ).float() ) elif not isinstance(images[i] , torch.Tensor ): images.insert( UpperCamelCase , torch.as_tensor(img_tensorize(images.pop(UpperCamelCase ) , input_format=self.input_format ) ) .to(self.device ) .float() , ) # resize smallest edge A__ = torch.tensor([im.shape[:2] for im in images] ) A__ = self.aug(UpperCamelCase ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic A__ = [self.normalizer(UpperCamelCase ) for x in images] # now pad them to do the following operations A__ , A__ = self.pad(UpperCamelCase ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad A__ = torch.true_divide(UpperCamelCase , UpperCamelCase ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def _snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Any] ): boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple[int, int] ): assert torch.isfinite(UpperCAmelCase_ ).all(), "Box tensor contains infinite or NaN!" A__ , A__ = box_size tensor[:, 0].clamp_(min=0 , max=UpperCAmelCase_ ) tensor[:, 1].clamp_(min=0 , max=UpperCAmelCase_ ) tensor[:, 2].clamp_(min=0 , max=UpperCAmelCase_ ) tensor[:, 3].clamp_(min=0 , max=UpperCAmelCase_ )
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : set ): A__ , A__ = len(UpperCAmelCase_ ), len(grid[0] ) if ( min(UpperCAmelCase_ , UpperCAmelCase_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(UpperCAmelCase_ , row + 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , row - 1 , UpperCAmelCase_ , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col + 1 , UpperCAmelCase_ ) count += depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , col - 1 , UpperCAmelCase_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
335
1
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def _snake_case ( UpperCAmelCase_ : Any ): A__ = SwinConfig(image_size=192 ) if "base" in model_name: A__ = 6 A__ = 128 A__ = (2, 2, 18, 2) A__ = (4, 8, 16, 32) elif "large" in model_name: A__ = 12 A__ = 192 A__ = (2, 2, 18, 2) A__ = (6, 12, 24, 48) else: raise ValueError("""Model not supported, only supports base and large variants""" ) A__ = window_size A__ = embed_dim A__ = depths A__ = num_heads return config def _snake_case ( UpperCAmelCase_ : Optional[int] ): if "encoder.mask_token" in name: A__ = name.replace("""encoder.mask_token""" , """embeddings.mask_token""" ) if "encoder.patch_embed.proj" in name: A__ = name.replace("""encoder.patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "encoder.patch_embed.norm" in name: A__ = name.replace("""encoder.patch_embed.norm""" , """embeddings.norm""" ) if "attn.proj" in name: A__ = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: A__ = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: A__ = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: A__ = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: A__ = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: A__ = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "encoder.norm.weight": A__ = """layernorm.weight""" if name == "encoder.norm.bias": A__ = """layernorm.bias""" if "decoder" in name: pass else: A__ = """swin.""" + name return name def _snake_case ( UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any ): for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(UpperCAmelCase_ ) if "attn_mask" in key: pass elif "qkv" in key: A__ = key.split(""".""" ) A__ = int(key_split[2] ) A__ = int(key_split[4] ) A__ = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[ :dim ] A__ = val[ dim : dim * 2 ] A__ = val[ -dim: ] else: A__ = val return orig_state_dict def _snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict ): A__ = torch.load(UpperCAmelCase_ , map_location="""cpu""" )["""model"""] A__ = get_swin_config(UpperCAmelCase_ ) A__ = SwinForMaskedImageModeling(UpperCAmelCase_ ) model.eval() A__ = convert_state_dict(UpperCAmelCase_ , UpperCAmelCase_ ) model.load_state_dict(UpperCAmelCase_ ) A__ = """http://images.cocodataset.org/val2017/000000039769.jpg""" A__ = ViTImageProcessor(size={"""height""": 192, """width""": 192} ) A__ = Image.open(requests.get(UpperCAmelCase_ , stream=UpperCAmelCase_ ).raw ) A__ = image_processor(images=UpperCAmelCase_ , return_tensors="""pt""" ) with torch.no_grad(): A__ = model(**UpperCAmelCase_ ).logits print(outputs.keys() ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCAmelCase_ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(UpperCAmelCase_ ) if push_to_hub: print(F"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(F"""microsoft/{model_name}""" ) image_processor.push_to_hub(F"""microsoft/{model_name}""" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='swin-base-simmim-window6-192', type=str, choices=['swin-base-simmim-window6-192', 'swin-large-simmim-window12-192'], help='Name of the Swin SimMIM model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default='/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth', type=str, help='Path to the original PyTorch checkpoint (.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.' ) SCREAMING_SNAKE_CASE_ : int = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
335
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ : int = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
1
"""simple docstring""" import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class a ( _lowerCamelCase, _lowerCamelCase ): """simple docstring""" @register_to_config def __init__( self: str , UpperCamelCase: int = 1_28 , UpperCamelCase: int = 2_56 , UpperCamelCase: float = 2_000.0 , UpperCamelCase: int = 7_68 , UpperCamelCase: int = 12 , UpperCamelCase: int = 12 , UpperCamelCase: int = 64 , UpperCamelCase: int = 20_48 , UpperCamelCase: float = 0.1 , ): """simple docstring""" super().__init__() A__ = nn.Sequential( nn.Linear(UpperCamelCase , d_model * 4 , bias=UpperCamelCase ) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=UpperCamelCase ) , nn.SiLU() , ) A__ = nn.Embedding(UpperCamelCase , UpperCamelCase ) A__ = False A__ = nn.Linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) A__ = nn.Dropout(p=UpperCamelCase ) A__ = nn.ModuleList() for lyr_num in range(UpperCamelCase ): # FiLM conditional T5 decoder A__ = DecoderLayer(d_model=UpperCamelCase , d_kv=UpperCamelCase , num_heads=UpperCamelCase , d_ff=UpperCamelCase , dropout_rate=UpperCamelCase ) self.decoders.append(UpperCamelCase ) A__ = TaLayerNorm(UpperCamelCase ) A__ = nn.Dropout(p=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) def UpperCamelCase ( self: Dict , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" A__ = torch.mul(query_input.unsqueeze(-1 ) , key_input.unsqueeze(-2 ) ) return mask.unsqueeze(-3 ) def UpperCamelCase ( self: Tuple , UpperCamelCase: Optional[Any] , UpperCamelCase: List[Any] , UpperCamelCase: Any ): """simple docstring""" A__ , A__ , A__ = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. A__ = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype ) A__ = self.conditioning_emb(UpperCamelCase ).unsqueeze(1 ) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) A__ = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. A__ = torch.broadcast_to( torch.arange(UpperCamelCase , device=decoder_input_tokens.device ) , (batch, seq_length) , ) A__ = self.position_encoding(UpperCamelCase ) A__ = self.continuous_inputs_projection(UpperCamelCase ) inputs += position_encodings A__ = self.dropout(UpperCamelCase ) # decoder: No padding present. A__ = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype ) # Translate encoding masks to encoder-decoder masks. A__ = [(x, self.encoder_decoder_mask(UpperCamelCase , UpperCamelCase )) for x, y in encodings_and_masks] # cross attend style: concat encodings A__ = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1 ) A__ = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1 ) for lyr in self.decoders: A__ = lyr( UpperCamelCase , conditioning_emb=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , )[0] A__ = self.decoder_norm(UpperCamelCase ) A__ = self.post_dropout(UpperCamelCase ) A__ = self.spec_out(UpperCamelCase ) return spec_out class a ( nn.Module ): """simple docstring""" def __init__( self: int , UpperCamelCase: Any , UpperCamelCase: Optional[int] , UpperCamelCase: str , UpperCamelCase: str , UpperCamelCase: Any , UpperCamelCase: Tuple=1e-6 ): """simple docstring""" super().__init__() A__ = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=UpperCamelCase , d_kv=UpperCamelCase , num_heads=UpperCamelCase , dropout_rate=UpperCamelCase ) ) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=UpperCamelCase , d_kv=UpperCamelCase , num_heads=UpperCamelCase , dropout_rate=UpperCamelCase , layer_norm_epsilon=UpperCamelCase , ) ) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=UpperCamelCase , d_ff=UpperCamelCase , dropout_rate=UpperCamelCase , layer_norm_epsilon=UpperCamelCase ) ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Optional[Any] , UpperCamelCase: Union[str, Any]=None , UpperCamelCase: Any=None , UpperCamelCase: Tuple=None , UpperCamelCase: List[Any]=None , UpperCamelCase: List[Any]=None , ): """simple docstring""" A__ = self.layer[0]( UpperCamelCase , conditioning_emb=UpperCamelCase , attention_mask=UpperCamelCase , ) if encoder_hidden_states is not None: A__ = torch.where(encoder_attention_mask > 0 , 0 , -1e1_0 ).to( encoder_hidden_states.dtype ) A__ = self.layer[1]( UpperCamelCase , key_value_states=UpperCamelCase , attention_mask=UpperCamelCase , ) # Apply Film Conditional Feed Forward layer A__ = self.layer[-1](UpperCamelCase , UpperCamelCase ) return (hidden_states,) class a ( nn.Module ): """simple docstring""" def __init__( self: Union[str, Any] , UpperCamelCase: int , UpperCamelCase: str , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] ): """simple docstring""" super().__init__() A__ = TaLayerNorm(UpperCamelCase ) A__ = TaFiLMLayer(in_features=d_model * 4 , out_features=UpperCamelCase ) A__ = Attention(query_dim=UpperCamelCase , heads=UpperCamelCase , dim_head=UpperCamelCase , out_bias=UpperCamelCase , scale_qk=UpperCamelCase ) A__ = nn.Dropout(UpperCamelCase ) def UpperCamelCase ( self: int , UpperCamelCase: Optional[int] , UpperCamelCase: str=None , UpperCamelCase: str=None , ): """simple docstring""" A__ = self.layer_norm(UpperCamelCase ) if conditioning_emb is not None: A__ = self.FiLMLayer(UpperCamelCase , UpperCamelCase ) # Self-attention block A__ = self.attention(UpperCamelCase ) A__ = hidden_states + self.dropout(UpperCamelCase ) return hidden_states class a ( nn.Module ): """simple docstring""" def __init__( self: Any , UpperCamelCase: int , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: List[str] , UpperCamelCase: int ): """simple docstring""" super().__init__() A__ = Attention(query_dim=UpperCamelCase , heads=UpperCamelCase , dim_head=UpperCamelCase , out_bias=UpperCamelCase , scale_qk=UpperCamelCase ) A__ = TaLayerNorm(UpperCamelCase , eps=UpperCamelCase ) A__ = nn.Dropout(UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: str , UpperCamelCase: List[Any]=None , UpperCamelCase: List[Any]=None , ): """simple docstring""" A__ = self.layer_norm(UpperCamelCase ) A__ = self.attention( UpperCamelCase , encoder_hidden_states=UpperCamelCase , attention_mask=attention_mask.squeeze(1 ) , ) A__ = hidden_states + self.dropout(UpperCamelCase ) return layer_output class a ( nn.Module ): """simple docstring""" def __init__( self: int , UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple , UpperCamelCase: Any , UpperCamelCase: List[str] ): """simple docstring""" super().__init__() A__ = TaDenseGatedActDense(d_model=UpperCamelCase , d_ff=UpperCamelCase , dropout_rate=UpperCamelCase ) A__ = TaFiLMLayer(in_features=d_model * 4 , out_features=UpperCamelCase ) A__ = TaLayerNorm(UpperCamelCase , eps=UpperCamelCase ) A__ = nn.Dropout(UpperCamelCase ) def UpperCamelCase ( self: Dict , UpperCamelCase: Tuple , UpperCamelCase: Optional[Any]=None ): """simple docstring""" A__ = self.layer_norm(UpperCamelCase ) if conditioning_emb is not None: A__ = self.film(UpperCamelCase , UpperCamelCase ) A__ = self.DenseReluDense(UpperCamelCase ) A__ = hidden_states + self.dropout(UpperCamelCase ) return hidden_states class a ( nn.Module ): """simple docstring""" def __init__( self: str , UpperCamelCase: Dict , UpperCamelCase: int , UpperCamelCase: Tuple ): """simple docstring""" super().__init__() A__ = nn.Linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) A__ = nn.Linear(UpperCamelCase , UpperCamelCase , bias=UpperCamelCase ) A__ = nn.Dropout(UpperCamelCase ) A__ = NewGELUActivation() def UpperCamelCase ( self: Any , UpperCamelCase: Any ): """simple docstring""" A__ = self.act(self.wi_a(UpperCamelCase ) ) A__ = self.wi_a(UpperCamelCase ) A__ = hidden_gelu * hidden_linear A__ = self.dropout(UpperCamelCase ) A__ = self.wo(UpperCamelCase ) return hidden_states class a ( nn.Module ): """simple docstring""" def __init__( self: Any , UpperCamelCase: Optional[int] , UpperCamelCase: Optional[Any]=1e-6 ): """simple docstring""" super().__init__() A__ = nn.Parameter(torch.ones(UpperCamelCase ) ) A__ = eps def UpperCamelCase ( self: Any , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = hidden_states.to(torch.floataa ).pow(2 ).mean(-1 , keepdim=UpperCamelCase ) A__ = hidden_states * torch.rsqrt(variance + self.variance_epsilon ) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: A__ = hidden_states.to(self.weight.dtype ) return self.weight * hidden_states class a ( nn.Module ): """simple docstring""" def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: torch.Tensor ): """simple docstring""" return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.044_715 * torch.pow(UpperCamelCase , 3.0 )) )) class a ( nn.Module ): """simple docstring""" def __init__( self: Dict , UpperCamelCase: int , UpperCamelCase: Optional[Any] ): """simple docstring""" super().__init__() A__ = nn.Linear(UpperCamelCase , out_features * 2 , bias=UpperCamelCase ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: List[Any] , UpperCamelCase: List[str] ): """simple docstring""" A__ = self.scale_bias(UpperCamelCase ) A__ , A__ = torch.chunk(UpperCamelCase , 2 , -1 ) A__ = x * (1 + scale) + shift return x
335
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(UpperCAmelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int ): A__ = _distribute_shards(**UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] ): A__ = _split_gen_kwargs(UpperCAmelCase_ , UpperCAmelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _snake_case ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] ): if expected is RuntimeError: with pytest.raises(UpperCAmelCase_ ): _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) else: A__ = _number_of_shards_in_gen_kwargs(UpperCAmelCase_ ) assert out == expected
335
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE_ : Any = { 'configuration_squeezebert': [ 'SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SqueezeBertConfig', 'SqueezeBertOnnxConfig', ], 'tokenization_squeezebert': ['SqueezeBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Tuple = ['SqueezeBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ 'SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'SqueezeBertForMaskedLM', 'SqueezeBertForMultipleChoice', 'SqueezeBertForQuestionAnswering', 'SqueezeBertForSequenceClassification', 'SqueezeBertForTokenClassification', 'SqueezeBertModel', 'SqueezeBertModule', 'SqueezeBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
335
"""simple docstring""" import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC SCREAMING_SNAKE_CASE_ : str = parse(importlib.metadata.version('torch')) def _snake_case ( UpperCAmelCase_ : Union[str, Version] , UpperCAmelCase_ : str , UpperCAmelCase_ : str ): if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) A__ = STR_OPERATION_TO_FUNC[operation] if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): A__ = parse(importlib.metadata.version(UpperCAmelCase_ ) ) return operation(UpperCAmelCase_ , parse(UpperCAmelCase_ ) ) def _snake_case ( UpperCAmelCase_ : str , UpperCAmelCase_ : str ): return compare_versions(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ )
335
1
"""simple docstring""" import argparse import torch from transformers import BertForMaskedLM if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : str = argparse.ArgumentParser( description=( 'Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned' ' Distillation' ) ) parser.add_argument('--model_type', default='bert', choices=['bert']) parser.add_argument('--model_name', default='bert-base-uncased', type=str) parser.add_argument('--dump_checkpoint', default='serialization_dir/tf_bert-base-uncased_0247911.pth', type=str) parser.add_argument('--vocab_transform', action='store_true') SCREAMING_SNAKE_CASE_ : List[Any] = parser.parse_args() if args.model_type == "bert": SCREAMING_SNAKE_CASE_ : Dict = BertForMaskedLM.from_pretrained(args.model_name) SCREAMING_SNAKE_CASE_ : str = 'bert' else: raise ValueError('args.model_type should be "bert".') SCREAMING_SNAKE_CASE_ : List[Any] = model.state_dict() SCREAMING_SNAKE_CASE_ : Tuple = {} for w in ["word_embeddings", "position_embeddings"]: SCREAMING_SNAKE_CASE_ : Tuple = state_dict[f"""{prefix}.embeddings.{w}.weight"""] for w in ["weight", "bias"]: SCREAMING_SNAKE_CASE_ : List[Any] = state_dict[f"""{prefix}.embeddings.LayerNorm.{w}"""] SCREAMING_SNAKE_CASE_ : List[str] = 0 for teacher_idx in [0, 2, 4, 7, 9, 1_1]: for w in ["weight", "bias"]: SCREAMING_SNAKE_CASE_ : int = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}""" ] SCREAMING_SNAKE_CASE_ : List[Any] = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}""" ] SCREAMING_SNAKE_CASE_ : Union[str, Any] = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}""" ] SCREAMING_SNAKE_CASE_ : Any = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}""" ] SCREAMING_SNAKE_CASE_ : Optional[Any] = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}""" ] SCREAMING_SNAKE_CASE_ : str = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}""" ] SCREAMING_SNAKE_CASE_ : Any = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}""" ] SCREAMING_SNAKE_CASE_ : List[str] = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}""" ] std_idx += 1 SCREAMING_SNAKE_CASE_ : List[str] = state_dict['cls.predictions.decoder.weight'] SCREAMING_SNAKE_CASE_ : Optional[int] = state_dict['cls.predictions.bias'] if args.vocab_transform: for w in ["weight", "bias"]: SCREAMING_SNAKE_CASE_ : Optional[Any] = state_dict[f"""cls.predictions.transform.dense.{w}"""] SCREAMING_SNAKE_CASE_ : int = state_dict[f"""cls.predictions.transform.LayerNorm.{w}"""] print(f"""N layers selected for distillation: {std_idx}""") print(f"""Number of params transferred for distillation: {len(compressed_sd.keys())}""") print(f"""Save transferred checkpoint to {args.dump_checkpoint}.""") torch.save(compressed_sd, args.dump_checkpoint)
335
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , 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/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != 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(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
1
"""simple docstring""" # Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def _snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] ): A__ = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, nicht wahr?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] A__ = { """wmt16-en-de-dist-12-1""": [28.3, 27.52], """wmt16-en-de-dist-6-1""": [27.4, 27.11], """wmt16-en-de-12-1""": [26.9, 25.75], } A__ = F"""{src_lang}-{tgt_lang}""" A__ = F""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt16 - allenai license: apache-2.0 datasets: - wmt16 metrics: - bleu --- # FSMT ## Model description This is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}. For more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369). All 3 models are available: * [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1) * [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1) * [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"allenai/{model_name}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias ## Training data Pretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369). ## Eval results Here are the BLEU scores: model | fairseq | transformers -------|---------|---------- {model_name} | {scores[model_name][0]} | {scores[model_name][1]} The score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs. The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=5 mkdir -p $DATA_DIR sacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` ## Data Sources - [training, etc.](http://www.statmt.org/wmt16/) - [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372) ### BibTeX entry and citation info ``` @misc{{kasai2020deep, title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}}, author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}}, year={{2020}}, eprint={{2006.10369}}, archivePrefix={{arXiv}}, primaryClass={{cs.CL}} }} ``` """ model_card_dir.mkdir(parents=UpperCAmelCase_ , exist_ok=UpperCAmelCase_ ) A__ = os.path.join(UpperCAmelCase_ , """README.md""" ) print(F"""Generating {path}""" ) with open(UpperCAmelCase_ , """w""" , encoding="""utf-8""" ) as f: f.write(UpperCAmelCase_ ) # make sure we are under the root of the project SCREAMING_SNAKE_CASE_ : List[Any] = Path(__file__).resolve().parent.parent.parent SCREAMING_SNAKE_CASE_ : int = repo_dir / 'model_cards' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: SCREAMING_SNAKE_CASE_ : Optional[int] = model_cards_dir / 'allenai' / model_name write_model_card(model_card_dir, src_lang='en', tgt_lang='de', model_name=model_name)
335
"""simple docstring""" import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( _lowerCamelCase ): """simple docstring""" def __init__( self: Optional[int] , *UpperCamelCase: Optional[Any] , UpperCamelCase: Tuple=None , UpperCamelCase: Tuple=None , **UpperCamelCase: Dict ): """simple docstring""" super().__init__(*UpperCamelCase , **UpperCamelCase ) A__ = eval_examples A__ = post_process_function def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Dataset] = None , UpperCamelCase: List[Any]=None , UpperCamelCase: Optional[List[str]] = None , UpperCamelCase: str = "eval" , **UpperCamelCase: Optional[int] , ): """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(UpperCamelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCamelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCamelCase ) return metrics def UpperCamelCase ( self: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Dict=None , UpperCamelCase: str = "test" , **UpperCamelCase: Optional[int] ): """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(UpperCamelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( UpperCamelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase , metric_key_prefix=UpperCamelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( UpperCamelCase , UpperCamelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(UpperCamelCase , UpperCamelCase , UpperCamelCase , """predict""" ) A__ = self.compute_metrics(UpperCamelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): A__ = metrics.pop(UpperCamelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase )
335
1
"""simple docstring""" import os def _snake_case ( UpperCAmelCase_ : str = "matrix.txt" ): with open(os.path.join(os.path.dirname(UpperCAmelCase_ ) , UpperCAmelCase_ ) ) as in_file: A__ = in_file.read() A__ = [[int(UpperCAmelCase_ ) for cell in row.split(""",""" )] for row in data.strip().splitlines()] A__ = [[0 for cell in row] for row in grid] A__ = len(grid[0] ) A__ = [[0 for i in range(UpperCAmelCase_ )] for j in range(UpperCAmelCase_ )] A__ = grid[0][0] for i in range(1 , UpperCAmelCase_ ): A__ = grid[0][i] + dp[0][i - 1] for i in range(1 , UpperCAmelCase_ ): A__ = grid[i][0] + dp[i - 1][0] for i in range(1 , UpperCAmelCase_ ): for j in range(1 , UpperCAmelCase_ ): A__ = grid[i][j] + min(dp[i - 1][j] , dp[i][j - 1] ) return dp[-1][-1] if __name__ == "__main__": print(f"""{solution() = }""")
335
"""simple docstring""" class a : """simple docstring""" def __init__( self: Dict ): """simple docstring""" A__ = {} def UpperCamelCase ( self: List[str] ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase , """ -> """ , """ -> """.join([str(UpperCamelCase ) for j in self.vertex[i]] ) ) def UpperCamelCase ( self: Any , UpperCamelCase: int , UpperCamelCase: int ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase ) else: # else make a new vertex A__ = [to_vertex] def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: str , UpperCamelCase: int , UpperCamelCase: list ): """simple docstring""" A__ = True print(UpperCamelCase , end=""" """ ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ : Optional[int] = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
335
1
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): while b: A__ , A__ = b, a % b return a def _snake_case ( UpperCAmelCase_ : int , UpperCAmelCase_ : int ): return a if b == 0 else euclidean_gcd_recursive(UpperCAmelCase_ , a % b ) def _snake_case ( ): print(F"""euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}""" ) print(F"""euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}""" ) print(F"""euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}""" ) print(F"""euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}""" ) print(F"""euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}""" ) print(F"""euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}""" ) print(F"""euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}""" ) print(F"""euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}""" ) print(F"""euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}""" ) print(F"""euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}""" ) if __name__ == "__main__": main()
335
"""simple docstring""" def _snake_case ( UpperCAmelCase_ : int = 10 ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or n < 0: raise ValueError("""Invalid input""" ) A__ = 10**n A__ = 2_8433 * (pow(2 , 783_0457 , UpperCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(1_0) = }""")
335
1
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class a ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self: Any ): """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) A__ = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } A__ = os.path.join(self.tmpdirname , UpperCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: Tuple , **UpperCamelCase: int ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: List[str] , **UpperCamelCase: str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase ) def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(UpperCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_image_processor() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) A__ = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" A__ = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=UpperCamelCase , padding_value=1.0 ) A__ = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase ) def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(UpperCamelCase , return_tensors="""np""" ) A__ = processor(images=UpperCamelCase , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """lower newer""" A__ = processor(text=UpperCamelCase ) A__ = tokenizer(UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(UpperCamelCase ): processor() def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(UpperCamelCase ) A__ = tokenizer.batch_decode(UpperCamelCase ) self.assertListEqual(UpperCamelCase , UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase , image_processor=UpperCamelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=UpperCamelCase , images=UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
335
"""simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] SCREAMING_SNAKE_CASE_ : Optional[int] = tuple[float, float, float] def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad ): A__ = end_pointa[0] - end_pointa[0] A__ = end_pointa[1] - end_pointa[1] A__ = end_pointa[2] - end_pointa[2] return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : Vectorad ): A__ = ab[1] * ac[2] - ab[2] * ac[1] # *i A__ = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j A__ = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _snake_case ( UpperCAmelCase_ : Vectorad , UpperCAmelCase_ : int ): return tuple(round(UpperCAmelCase_ , UpperCAmelCase_ ) for x in vector ) == (0, 0, 0) def _snake_case ( UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : Pointad , UpperCAmelCase_ : int = 10 ): A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = create_vector(UpperCAmelCase_ , UpperCAmelCase_ ) return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ )
335
1