code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
from __future__ import annotations def lowercase ( __A : list[float] , __A : list[float] ) -> float: '''simple docstring''' snake_case : str = sorted(numsa + numsa ) snake_case , snake_case : Any = divmod(len(__A ) , 2 ) if mod == 1: return all_numbers[div] else: return (all_numbers[div] + all_numbers[div - 1]) / 2 if __name__ == "__main__": import doctest doctest.testmod() __lowercase : Any = [float(x) for x in input('''Enter the elements of first array: ''').split()] __lowercase : List[str] = [float(x) for x in input('''Enter the elements of second array: ''').split()] print(f'''The median of two arrays is: {median_of_two_arrays(array_a, array_a)}''')
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
def lowercase ( __A : int = 400_0000 ) -> int: '''simple docstring''' snake_case : int = [] snake_case , snake_case : Any = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__A ) snake_case , snake_case : Optional[Any] = b, a + b return sum(__A ) if __name__ == "__main__": print(f'''{solution() = }''')
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _A : '''simple docstring''' @staticmethod def snake_case_ ( *SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass @is_pipeline_test @require_vision class _A ( unittest.TestCase ): '''simple docstring''' @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,) snake_case : Tuple = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case : List[str] = image_classifier(SCREAMING_SNAKE_CASE_ ,candidate_labels=["""a""", """b""", """c"""] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ [{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """b"""}, {"""score""": 0.3_33, """label""": """c"""}], [{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """c"""}, {"""score""": 0.3_33, """label""": """b"""}], ] ,) snake_case : Optional[Any] = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], ] ,) @require_tf def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,framework="""tf""" ) snake_case : Optional[int] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case : Any = image_classifier(SCREAMING_SNAKE_CASE_ ,candidate_labels=["""a""", """b""", """c"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """b"""}, {"""score""": 0.3_33, """label""": """c"""}] ,) snake_case : int = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": 0.3_33, """label""": ANY(SCREAMING_SNAKE_CASE_ )}, ], ] ,) @slow @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,) # This is an image of 2 cats with remotes and no planes snake_case : Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case : str = image_classifier(SCREAMING_SNAKE_CASE_ ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ] ,) snake_case : Optional[Any] = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ [ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ], ] * 5 ,) @slow @require_tf def snake_case_ ( self ): '''simple docstring''' snake_case : Any = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,framework="""tf""" ) # This is an image of 2 cats with remotes and no planes snake_case : List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case : Dict = image_classifier(SCREAMING_SNAKE_CASE_ ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ] ,) snake_case : Optional[int] = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) ,[ [ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ], ] * 5 ,)
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : Optional[Any] = { '''google/vivit-b-16x2-kinetics400''': ( '''https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json''' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vivit''' def __init__( self ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=[2, 16, 16] ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=768 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=3072 ,SCREAMING_SNAKE_CASE_="gelu_fast" ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-06 ,SCREAMING_SNAKE_CASE_=True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Dict = hidden_size snake_case : Union[str, Any] = num_hidden_layers snake_case : Dict = num_attention_heads snake_case : List[str] = intermediate_size snake_case : int = hidden_act snake_case : Tuple = hidden_dropout_prob snake_case : Optional[int] = attention_probs_dropout_prob snake_case : Optional[int] = initializer_range snake_case : str = layer_norm_eps snake_case : str = image_size snake_case : Optional[Any] = num_frames snake_case : List[str] = tubelet_size snake_case : int = num_channels snake_case : Optional[int] = qkv_bias super().__init__(**SCREAMING_SNAKE_CASE_ )
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __lowercase : List[Any] = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class _A ( snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = XLMRobertaTokenizer __lowerCamelCase : Dict = XLMRobertaTokenizerFast __lowerCamelCase : Optional[Any] = True __lowerCamelCase : List[str] = True def snake_case_ ( self ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing snake_case : Optional[Any] = XLMRobertaTokenizer(SCREAMING_SNAKE_CASE_ ,keep_accents=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(self.tmpdirname ) def snake_case_ ( self ): '''simple docstring''' snake_case : str = """<pad>""" snake_case : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"""<s>""" ) self.assertEqual(vocab_keys[1] ,"""<pad>""" ) self.assertEqual(vocab_keys[-1] ,"""<mask>""" ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,1002 ) def snake_case_ ( self ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size ,1002 ) def snake_case_ ( self ): '''simple docstring''' snake_case : str = XLMRobertaTokenizer(SCREAMING_SNAKE_CASE_ ,keep_accents=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) ,[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] ,) snake_case : Union[str, Any] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( SCREAMING_SNAKE_CASE_ ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] ,) snake_case : str = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) self.assertListEqual( SCREAMING_SNAKE_CASE_ ,[ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] ,) snake_case : List[str] = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual( SCREAMING_SNAKE_CASE_ ,[ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] ,) def snake_case_ ( self ): '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return snake_case : Dict = (self.rust_tokenizer_class, """hf-internal-testing/tiny-xlm-roberta""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): snake_case : List[Any] = self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Dict = self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Dict = tempfile.mkdtemp() snake_case : str = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Any = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) snake_case : str = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Checks everything loads correctly in the same way snake_case : str = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) # Save tokenizer rust, legacy_format=True snake_case : Union[str, Any] = tempfile.mkdtemp() snake_case : Optional[Any] = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ ,legacy_format=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it save with the same files self.assertSequenceEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Checks everything loads correctly in the same way snake_case : int = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) # Save tokenizer rust, legacy_format=False snake_case : Union[str, Any] = tempfile.mkdtemp() snake_case : List[Any] = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ ,legacy_format=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way snake_case : List[str] = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) @cached_property def snake_case_ ( self ): '''simple docstring''' return XLMRobertaTokenizer.from_pretrained("""xlm-roberta-base""" ) def snake_case_ ( self ): '''simple docstring''' with tempfile.NamedTemporaryFile() as f: shutil.copyfile(SCREAMING_SNAKE_CASE_ ,f.name ) snake_case : Dict = XLMRobertaTokenizer(f.name ,keep_accents=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = pickle.dumps(SCREAMING_SNAKE_CASE_ ) pickle.loads(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' if not self.test_rust_tokenizer: return snake_case : Optional[Any] = self.get_tokenizer() snake_case : str = self.get_rust_tokenizer() snake_case : Optional[int] = """I was born in 92000, and this is falsé.""" snake_case : Any = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = self.get_rust_tokenizer() snake_case : Dict = tokenizer.encode(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = """Hello World!""" snake_case : Any = [0, 35378, 6661, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,self.big_tokenizer.encode(SCREAMING_SNAKE_CASE_ ) ) @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) snake_case : Union[str, Any] = [ 0, 3293, 83, 10, 4552, 4989, 7986, 678, 10, 5915, 111, 179459, 124850, 4, 6044, 237, 12, 6, 5, 6, 4, 6780, 705, 15, 1388, 44, 378, 10114, 711, 152, 20, 6, 5, 22376, 642, 1221, 15190, 34153, 450, 5608, 959, 1119, 57702, 136, 186, 47, 1098, 29367, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6044, 237, 6284, 50901, 528, 31, 90, 34, 927, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,self.big_tokenizer.encode(SCREAMING_SNAKE_CASE_ ) ) @slow def snake_case_ ( self ): '''simple docstring''' # fmt: off snake_case : List[Any] = {"""input_ids""": [[0, 11062, 82772, 7, 15, 82772, 538, 51529, 237, 17198, 1290, 206, 9, 215175, 1314, 136, 17198, 1290, 206, 9, 56359, 42, 122009, 9, 16466, 16, 87344, 4537, 9, 4717, 78381, 6, 159958, 7, 15, 24480, 618, 4, 527, 22693, 5428, 4, 2777, 24480, 9874, 4, 43523, 594, 4, 803, 18392, 33189, 18, 4, 43523, 24447, 12399, 100, 24955, 83658, 9626, 144057, 15, 839, 22335, 16, 136, 24955, 83658, 83479, 15, 39102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 122009, 115774, 23, 805, 1328, 46876, 7, 136, 53894, 1940, 42227, 41159, 17721, 823, 425, 4, 27512, 98722, 206, 136, 5531, 4970, 919, 17336, 5, 2], [0, 20080, 618, 83, 82775, 47, 479, 9, 1517, 73, 53894, 333, 80581, 110117, 18811, 5256, 1295, 51, 152526, 297, 7986, 390, 124416, 538, 35431, 214, 98, 15044, 25737, 136, 7108, 43701, 23, 756, 135355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 63773, 119455, 6, 147797, 88203, 7, 645, 70, 21, 3285, 10269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=SCREAMING_SNAKE_CASE_ ,model_name="""xlm-roberta-base""" ,revision="""d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3""" ,)
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging __lowercase : Optional[int] = logging.get_logger(__name__) __lowercase : List[str] = { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''', # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[Any] = '''blenderbot-small''' __lowerCamelCase : List[str] = ['''past_key_values'''] __lowerCamelCase : Tuple = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self ,SCREAMING_SNAKE_CASE_=50265 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_=2048 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_=2048 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=2 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : int = vocab_size snake_case : List[Any] = max_position_embeddings snake_case : int = d_model snake_case : List[Any] = encoder_ffn_dim snake_case : str = encoder_layers snake_case : List[Any] = encoder_attention_heads snake_case : Tuple = decoder_ffn_dim snake_case : List[Any] = decoder_layers snake_case : List[Any] = decoder_attention_heads snake_case : str = dropout snake_case : Union[str, Any] = attention_dropout snake_case : Any = activation_dropout snake_case : str = activation_function snake_case : str = init_std snake_case : Union[str, Any] = encoder_layerdrop snake_case : List[Any] = decoder_layerdrop snake_case : List[str] = use_cache snake_case : Optional[Any] = encoder_layers snake_case : Dict = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,is_encoder_decoder=SCREAMING_SNAKE_CASE_ ,decoder_start_token_id=SCREAMING_SNAKE_CASE_ ,forced_eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case : str = OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}), ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}), ] ) if self.use_past: snake_case : int = {0: """batch"""} snake_case : List[Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} else: snake_case : Any = {0: """batch""", 1: """decoder_sequence"""} snake_case : List[Any] = {0: """batch""", 1: """decoder_sequence"""} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ ,direction="""inputs""" ) elif self.task == "causal-lm": # TODO: figure this case out. snake_case : int = OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}), ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}), ] ) if self.use_past: snake_case , snake_case : int = self.num_layers for i in range(SCREAMING_SNAKE_CASE_ ): snake_case : List[str] = {0: """batch""", 2: """past_sequence + sequence"""} snake_case : Any = {0: """batch""", 2: """past_sequence + sequence"""} else: snake_case : Union[str, Any] = OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}), ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}), ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}), ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}), ] ) return common_inputs @property def snake_case_ ( self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case : Union[str, Any] = super().outputs else: snake_case : Optional[Any] = super(SCREAMING_SNAKE_CASE_ ,self ).outputs if self.use_past: snake_case , snake_case : List[str] = self.num_layers for i in range(SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = {0: """batch""", 2: """past_sequence + sequence"""} snake_case : str = {0: """batch""", 2: """past_sequence + sequence"""} return common_outputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' snake_case : Union[str, Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Generate decoder inputs snake_case : Optional[int] = seq_length if not self.use_past else 1 snake_case : Union[str, Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()} snake_case : int = dict(**SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch snake_case , snake_case : List[Any] = common_inputs["""input_ids"""].shape snake_case : Tuple = common_inputs["""decoder_input_ids"""].shape[1] snake_case , snake_case : Optional[int] = self.num_attention_heads snake_case : Optional[int] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) snake_case : Optional[Any] = decoder_seq_length + 3 snake_case : Dict = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) snake_case : Dict = torch.cat( [common_inputs["""decoder_attention_mask"""], torch.ones(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )] ,dim=1 ) snake_case : Union[str, Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered snake_case , snake_case : List[str] = self.num_layers snake_case : Dict = min(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = max(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) - min_num_layers snake_case : Union[str, Any] = """encoder""" if num_encoder_layers > num_decoder_layers else """decoder""" for _ in range(SCREAMING_SNAKE_CASE_ ): common_inputs["past_key_values"].append( ( torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), ) ) # TODO: test this. snake_case : List[Any] = encoder_shape if remaining_side_name == """encoder""" else decoder_shape for _ in range(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ )) ) return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' snake_case : int = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch snake_case , snake_case : Optional[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values snake_case : List[Any] = seqlen + 2 snake_case , snake_case : Tuple = self.num_layers snake_case , snake_case : Optional[int] = self.num_attention_heads snake_case : Any = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) snake_case : Any = common_inputs["""attention_mask"""].dtype snake_case : int = torch.cat( [common_inputs["""attention_mask"""], torch.ones(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,dtype=SCREAMING_SNAKE_CASE_ )] ,dim=1 ) snake_case : Dict = [ (torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ )) for _ in range(SCREAMING_SNAKE_CASE_ ) ] return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX snake_case : Tuple = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE_ ,fixed_dimension=OnnxConfig.default_fixed_batch ,num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX snake_case : List[str] = tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE_ ,fixed_dimension=OnnxConfig.default_fixed_sequence ,num_token_to_add=SCREAMING_SNAKE_CASE_ ) # Generate dummy inputs according to compute batch and sequence snake_case : str = [""" """.join([tokenizer.unk_token] ) * seq_length] * batch_size snake_case : Optional[int] = dict(tokenizer(SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ) ) return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case : str = self._generate_dummy_inputs_for_default_and_seqaseq_lm( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) elif self.task == "causal-lm": snake_case : List[Any] = self._generate_dummy_inputs_for_causal_lm( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) else: snake_case : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case : int = super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = super(SCREAMING_SNAKE_CASE_ ,self )._flatten_past_key_values_( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def lowercase ( __A : dict ) -> tuple: '''simple docstring''' return (data["data"], data["target"]) def lowercase ( __A : np.ndarray , __A : np.ndarray ) -> XGBClassifier: '''simple docstring''' snake_case : List[str] = XGBClassifier() classifier.fit(__A , __A ) return classifier def lowercase ( ) -> None: '''simple docstring''' snake_case : Any = load_iris() snake_case , snake_case : str = data_handling(__A ) snake_case , snake_case , snake_case , snake_case : Optional[int] = train_test_split( __A , __A , test_size=0.25 ) snake_case : Union[str, Any] = iris["""target_names"""] # Create an XGBoost Classifier from the training data snake_case : int = xgboost(__A , __A ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( __A , __A , __A , display_labels=__A , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) __lowercase : Optional[Any] = logging.getLogger() __lowercase : List[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = {"""source""": """What is love ?""", """target""": """life"""} snake_case : List[str] = {"""train""": 12, """val""": 2, """test""": 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: snake_case : Optional[int] = """\n""".join([contents[field]] * n_lines[split] ) with open(os.path.join(SCREAMING_SNAKE_CASE_ ,F"""{split}.{field}""" ) ,"""w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "pytorch" ): '''simple docstring''' snake_case : List[Any] = self.get_auto_remove_tmp_dir() snake_case : Tuple = os.path.join(SCREAMING_SNAKE_CASE_ ,"""output""" ) snake_case : Any = os.path.join(SCREAMING_SNAKE_CASE_ ,"""data""" ) self._create_dummy_data(data_dir=SCREAMING_SNAKE_CASE_ ) snake_case : int = F""" --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ """.split() if gpus > 0: testargs.append(F"""--gpus={gpus}""" ) if is_apex_available(): testargs.append("""--fp16""" ) else: testargs.append("""--gpus=0""" ) testargs.append("""--distributed_backend=ddp_cpu""" ) testargs.append("""--num_processes=2""" ) snake_case : Dict = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(SCREAMING_SNAKE_CASE_ ,env=self.get_env() ) snake_case : Optional[Any] = os.path.join(SCREAMING_SNAKE_CASE_ ,"""metrics.json""" ) with open(SCREAMING_SNAKE_CASE_ ) as f: snake_case : Any = json.load(SCREAMING_SNAKE_CASE_ ) return result @require_torch_gpu def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""] ,0.2 ) @require_torch_multi_gpu def snake_case_ ( self ): '''simple docstring''' snake_case : str = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""] ,0.2 ) @require_torch_gpu @require_ray def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self._run_finetune(gpus=1 ,distributed_retriever="""ray""" ) self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""] ,0.2 ) @require_torch_multi_gpu @require_ray def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self._run_finetune(gpus=1 ,distributed_retriever="""ray""" ) self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""] ,0.2 )
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__( SCREAMING_SNAKE_CASE_ ,split=SCREAMING_SNAKE_CASE_ ,features=SCREAMING_SNAKE_CASE_ ,cache_dir=SCREAMING_SNAKE_CASE_ ,keep_in_memory=SCREAMING_SNAKE_CASE_ ,streaming=SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) snake_case : Any = path_or_paths if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else {self.split: path_or_paths} snake_case : str = Text( cache_dir=SCREAMING_SNAKE_CASE_ ,data_files=SCREAMING_SNAKE_CASE_ ,features=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) def snake_case_ ( self ): '''simple docstring''' # Build iterable dataset if self.streaming: snake_case : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: snake_case : int = None snake_case : Tuple = None snake_case : str = None snake_case : List[str] = None self.builder.download_and_prepare( download_config=SCREAMING_SNAKE_CASE_ ,download_mode=SCREAMING_SNAKE_CASE_ ,verification_mode=SCREAMING_SNAKE_CASE_ ,base_path=SCREAMING_SNAKE_CASE_ ,num_proc=self.num_proc ,) snake_case : Any = self.builder.as_dataset( split=self.split ,verification_mode=SCREAMING_SNAKE_CASE_ ,in_memory=self.keep_in_memory ) return dataset
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
from __future__ import annotations __lowercase : List[Any] = [True] * 1_000_001 __lowercase : Union[str, Any] = 2 while i * i <= 1_000_000: if seive[i]: for j in range(i * i, 1_000_001, i): __lowercase : List[Any] = False i += 1 def lowercase ( __A : int ) -> bool: '''simple docstring''' return seive[n] def lowercase ( __A : int ) -> bool: '''simple docstring''' return any(digit in """02468""" for digit in str(__A ) ) def lowercase ( __A : int = 100_0000 ) -> list[int]: '''simple docstring''' snake_case : Optional[int] = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(__A ) and not contains_an_even_digit(__A ): snake_case : Any = str(__A ) snake_case : Tuple = [int(str_num[j:] + str_num[:j] ) for j in range(len(__A ) )] if all(is_prime(__A ) for i in list_nums ): result.append(__A ) return result def lowercase ( ) -> int: '''simple docstring''' return len(find_circular_primes() ) if __name__ == "__main__": print(f'''{len(find_circular_primes()) = }''')
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
import time from dataclasses import dataclass from multiprocessing import Pool from unittest import TestCase from unittest.mock import patch import multiprocess import numpy as np import pytest from datasets.utils.py_utils import ( NestedDataStructure, asdict, iflatmap_unordered, map_nested, temp_seed, temporary_assignment, zip_dict, ) from .utils import require_tf, require_torch def lowercase ( __A : List[Any] ) -> int: # picklable for multiprocessing '''simple docstring''' return x.sum() def lowercase ( __A : Any ) -> int: # picklable for multiprocessing '''simple docstring''' return i + 1 @dataclass class _A : '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : str class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = {} snake_case : List[str] = [] snake_case : List[Any] = 1 snake_case : Tuple = [1, 2] snake_case : Optional[int] = {"""a""": 1, """b""": 2} snake_case : Union[str, Any] = {"""a""": [1, 2], """b""": [3, 4]} snake_case : Union[str, Any] = {"""a""": {"""1""": 1}, """b""": 2} snake_case : Union[str, Any] = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} snake_case : str = {} snake_case : Optional[Any] = [] snake_case : Optional[int] = 2 snake_case : str = [2, 3] snake_case : Optional[Any] = {"""a""": 2, """b""": 3} snake_case : List[str] = {"""a""": [2, 3], """b""": [4, 5]} snake_case : Optional[Any] = {"""a""": {"""1""": 2}, """b""": 3} snake_case : Tuple = {"""a""": 2, """b""": 3, """c""": 4, """d""": 5} self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) snake_case : int = 2 self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = {"""a""": np.eye(2 ), """b""": np.zeros(3 ), """c""": np.ones(2 )} snake_case : Union[str, Any] = {"""a""": 2, """b""": 0, """c""": 2} snake_case : Optional[Any] = { """a""": np.eye(2 ).astype(SCREAMING_SNAKE_CASE_ ), """b""": np.zeros(3 ).astype(SCREAMING_SNAKE_CASE_ ), """c""": np.ones(2 ).astype(SCREAMING_SNAKE_CASE_ ), } self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,map_numpy=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual( {k: v.tolist() for k, v in map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,map_numpy=SCREAMING_SNAKE_CASE_ ).items()} ,{k: v.tolist() for k, v in expected_map_nested_sna_int.items()} ,) self.assertEqual(map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,map_numpy=SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual( {k: v.tolist() for k, v in map_nested(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,map_numpy=SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ).items()} ,{k: v.tolist() for k, v in expected_map_nested_sna_int.items()} ,) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): # can't pickle a local lambda map_nested(lambda SCREAMING_SNAKE_CASE_ : x + 1 ,SCREAMING_SNAKE_CASE_ ,num_proc=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = {"""a""": 1, """b""": 2} snake_case : List[Any] = {"""a""": 3, """b""": 4} snake_case : List[Any] = {"""a""": 5, """b""": 6} snake_case : Optional[int] = sorted([("""a""", (1, 3, 5)), ("""b""", (2, 4, 6))] ) self.assertEqual(sorted(zip_dict(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' class _A : '''simple docstring''' __lowerCamelCase : Tuple = '''bar''' snake_case : Optional[Any] = Foo() self.assertEqual(foo.my_attr ,"""bar""" ) with temporary_assignment(SCREAMING_SNAKE_CASE_ ,"""my_attr""" ,"""BAR""" ): self.assertEqual(foo.my_attr ,"""BAR""" ) self.assertEqual(foo.my_attr ,"""bar""" ) @pytest.mark.parametrize( """iterable_length, num_proc, expected_num_proc""" , [ (1, None, 1), (1, 1, 1), (2, None, 1), (2, 1, 1), (2, 2, 1), (2, 3, 1), (3, 2, 1), (16, 16, 16), (16, 17, 16), (17, 16, 16), ] , ) def lowercase ( __A : List[Any] , __A : Optional[Any] , __A : Dict ) -> Tuple: '''simple docstring''' with patch("""datasets.utils.py_utils._single_map_nested""" ) as mock_single_map_nested, patch( """datasets.parallel.parallel.Pool""" ) as mock_multiprocessing_pool: snake_case : int = {f"""{i}""": i for i in range(__A )} snake_case : str = map_nested(lambda __A : x + 10 , __A , num_proc=__A , parallel_min_length=16 ) if expected_num_proc == 1: assert mock_single_map_nested.called assert not mock_multiprocessing_pool.called else: assert not mock_single_map_nested.called assert mock_multiprocessing_pool.called assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc class _A ( snake_case ): '''simple docstring''' @require_tf def snake_case_ ( self ): '''simple docstring''' import tensorflow as tf from tensorflow.keras import layers snake_case : Any = layers.Dense(2 ) def gen_random_output(): snake_case : List[Any] = tf.random.uniform((1, 3) ) return model(SCREAMING_SNAKE_CASE_ ).numpy() with temp_seed(42 ,set_tensorflow=SCREAMING_SNAKE_CASE_ ): snake_case : Tuple = gen_random_output() with temp_seed(42 ,set_tensorflow=SCREAMING_SNAKE_CASE_ ): snake_case : int = gen_random_output() snake_case : Tuple = gen_random_output() np.testing.assert_equal(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertGreater(np.abs(outa - outa ).sum() ,0 ) @require_torch def snake_case_ ( self ): '''simple docstring''' import torch def gen_random_output(): snake_case : Optional[Any] = torch.nn.Linear(3 ,2 ) snake_case : Union[str, Any] = torch.rand(1 ,3 ) return model(SCREAMING_SNAKE_CASE_ ).detach().numpy() with temp_seed(42 ,set_pytorch=SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = gen_random_output() with temp_seed(42 ,set_pytorch=SCREAMING_SNAKE_CASE_ ): snake_case : Tuple = gen_random_output() snake_case : Any = gen_random_output() np.testing.assert_equal(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertGreater(np.abs(outa - outa ).sum() ,0 ) def snake_case_ ( self ): '''simple docstring''' def gen_random_output(): return np.random.rand(1 ,3 ) with temp_seed(42 ): snake_case : Union[str, Any] = gen_random_output() with temp_seed(42 ): snake_case : Union[str, Any] = gen_random_output() snake_case : List[Any] = gen_random_output() np.testing.assert_equal(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertGreater(np.abs(outa - outa ).sum() ,0 ) @pytest.mark.parametrize("""input_data""" , [{}] ) def lowercase ( __A : Any ) -> Any: '''simple docstring''' snake_case : Tuple = NestedDataStructure(__A ).data assert output_data == input_data @pytest.mark.parametrize( """data, expected_output""" , [ ({}, []), ([], []), ("""foo""", ["""foo"""]), (["""foo""", """bar"""], ["""foo""", """bar"""]), ([["""foo""", """bar"""]], ["""foo""", """bar"""]), ([[["""foo"""], ["""bar"""]]], ["""foo""", """bar"""]), ([[["""foo"""], """bar"""]], ["""foo""", """bar"""]), ({"""a""": 1, """b""": 2}, [1, 2]), ({"""a""": [1, 2], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[[3], [4]]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, [4]]}, [1, 2, 3, 4]), ({"""a""": {"""1""": 1}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": [2]}, [1, 2]), ] , ) def lowercase ( __A : str , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[Any] = NestedDataStructure(__A ).flatten() assert output == expected_output def lowercase ( ) -> Any: '''simple docstring''' snake_case : str = A(x=1 , y="""foobar""" ) snake_case : Tuple = {"""x""": 1, """y""": """foobar"""} assert asdict(__A ) == expected_output snake_case : Any = {"""a""": {"""b""": A(x=10 , y="""foo""" )}, """c""": [A(x=20 , y="""bar""" )]} snake_case : Union[str, Any] = {"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]} assert asdict(__A ) == expected_output with pytest.raises(__A ): asdict([1, A(x=10 , y="""foo""" )] ) def lowercase ( __A : str ) -> Union[str, Any]: '''simple docstring''' return text.split() def lowercase ( __A : Any ) -> List[Any]: '''simple docstring''' yield (time.time(), content) time.sleep(2 ) yield (time.time(), content) def lowercase ( ) -> int: '''simple docstring''' with Pool(2 ) as pool: snake_case : int = list(iflatmap_unordered(__A , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(__A ) == 20 # check multiprocess from pathos (uses dill for pickling) with multiprocess.Pool(2 ) as pool: snake_case : Optional[Any] = list(iflatmap_unordered(__A , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(__A ) == 20 # check that we get items as fast as possible with Pool(2 ) as pool: snake_case : str = [] for yield_time, content in iflatmap_unordered( __A , _aseconds_generator_of_aitems_with_timing , kwargs_iterable=[{"""content""": """a"""}, {"""content""": """b"""}] ): assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded" out.append(__A ) assert out.count("""a""" ) == 2 assert out.count("""b""" ) == 2 assert len(__A ) == 4
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import time import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers.generation import ( MaxLengthCriteria, MaxNewTokensCriteria, MaxTimeCriteria, StoppingCriteriaList, validate_stopping_criteria, ) @require_torch class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = 3 snake_case : Optional[Any] = 250 snake_case : Union[str, Any] = ids_tensor((batch_size, length) ,SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = torch.ones((batch_size, length) ,device=SCREAMING_SNAKE_CASE_ ,dtype=torch.float ) / length return input_ids, scores def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : str = self._get_tensors(5 ) snake_case : Optional[Any] = StoppingCriteriaList( [ MaxLengthCriteria(max_length=10 ), MaxTimeCriteria(max_time=0.1 ), ] ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : List[str] = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : int = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = MaxLengthCriteria(max_length=10 ) snake_case , snake_case : int = self._get_tensors(5 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : List[str] = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : List[str] = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : str = MaxNewTokensCriteria(start_length=5 ,max_new_tokens=5 ) snake_case , snake_case : int = self._get_tensors(5 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : Union[str, Any] = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case : Tuple = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case : Optional[Any] = StoppingCriteriaList([criteria] ) self.assertEqual(criteria_list.max_length ,10 ) def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Dict = self._get_tensors(5 ) snake_case : Optional[Any] = MaxTimeCriteria(max_time=0.1 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) snake_case : Optional[int] = MaxTimeCriteria(max_time=0.1 ,initial_timestamp=time.time() - 0.2 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) ,10 ) with self.assertWarns(SCREAMING_SNAKE_CASE_ ): validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) ,11 ) snake_case : Union[str, Any] = validate_stopping_criteria(StoppingCriteriaList() ,11 ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,1 )
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
def lowercase ( __A : int ) -> "list[int]": '''simple docstring''' if upper_limit < 0: raise ValueError("""Limit for the Catalan sequence must be ≥ 0""" ) snake_case : Dict = [0] * (upper_limit + 1) # Base case: C(0) = C(1) = 1 snake_case : Dict = 1 if upper_limit > 0: snake_case : Optional[Any] = 1 # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i for i in range(2 , upper_limit + 1 ): for j in range(__A ): catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1] return catalan_list if __name__ == "__main__": print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''') print('''\n*** Enter -1 at any time to quit ***''') print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''') try: while True: __lowercase : Union[str, Any] = int(input().strip()) if N < 0: print('''\n********* Goodbye!! ************''') break else: print(f'''The Catalan numbers from 0 through {N} are:''') print(catalan_numbers(N)) print('''Try another upper limit for the sequence: ''', end='''''') except (NameError, ValueError): print('''\n********* Invalid input, goodbye! ************\n''') import doctest doctest.testmod()
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = "geglu" ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = "layer_norm" ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' super().__init__() snake_case : Union[str, Any] = only_cross_attention snake_case : List[Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case : Optional[int] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to""" F""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case : Optional[int] = AdaLayerNorm(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: snake_case : str = AdaLayerNormZero(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else: snake_case : List[Any] = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ,elementwise_affine=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = Attention( query_dim=SCREAMING_SNAKE_CASE_ ,heads=SCREAMING_SNAKE_CASE_ ,dim_head=SCREAMING_SNAKE_CASE_ ,dropout=SCREAMING_SNAKE_CASE_ ,bias=SCREAMING_SNAKE_CASE_ ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=SCREAMING_SNAKE_CASE_ ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case : List[str] = ( AdaLayerNorm(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else nn.LayerNorm(SCREAMING_SNAKE_CASE_ ,elementwise_affine=SCREAMING_SNAKE_CASE_ ) ) snake_case : Union[str, Any] = Attention( query_dim=SCREAMING_SNAKE_CASE_ ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=SCREAMING_SNAKE_CASE_ ,dim_head=SCREAMING_SNAKE_CASE_ ,dropout=SCREAMING_SNAKE_CASE_ ,bias=SCREAMING_SNAKE_CASE_ ,upcast_attention=SCREAMING_SNAKE_CASE_ ,) # is self-attn if encoder_hidden_states is none else: snake_case : str = None snake_case : Any = None # 3. Feed-forward snake_case : Tuple = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ,elementwise_affine=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = FeedForward(SCREAMING_SNAKE_CASE_ ,dropout=SCREAMING_SNAKE_CASE_ ,activation_fn=SCREAMING_SNAKE_CASE_ ,final_dropout=SCREAMING_SNAKE_CASE_ ) # let chunk size default to None snake_case : Optional[int] = None snake_case : Optional[int] = 0 def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' # Sets chunk feed-forward snake_case : Any = chunk_size snake_case : List[Any] = dim def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case : List[Any] = self.norma(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: snake_case , snake_case , snake_case , snake_case , snake_case : int = self.norma( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,hidden_dtype=hidden_states.dtype ) else: snake_case : str = self.norma(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case : Tuple = self.attna( SCREAMING_SNAKE_CASE_ ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) if self.use_ada_layer_norm_zero: snake_case : Tuple = gate_msa.unsqueeze(1 ) * attn_output snake_case : Dict = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case : int = ( self.norma(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else self.norma(SCREAMING_SNAKE_CASE_ ) ) snake_case : Any = self.attna( SCREAMING_SNAKE_CASE_ ,encoder_hidden_states=SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) snake_case : Optional[Any] = attn_output + hidden_states # 3. Feed-forward snake_case : Dict = self.norma(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: snake_case : int = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" ) snake_case : Dict = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case : Union[str, Any] = torch.cat( [self.ff(SCREAMING_SNAKE_CASE_ ) for hid_slice in norm_hidden_states.chunk(SCREAMING_SNAKE_CASE_ ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case : int = self.ff(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: snake_case : List[Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case : Tuple = ff_output + hidden_states return hidden_states class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = 4 ,SCREAMING_SNAKE_CASE_ = 0.0 ,SCREAMING_SNAKE_CASE_ = "geglu" ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' super().__init__() snake_case : List[Any] = int(dim * mult ) snake_case : int = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case : Optional[Any] = GELU(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if activation_fn == "gelu-approximate": snake_case : List[str] = GELU(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case : str = GEGLU(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) elif activation_fn == "geglu-approximate": snake_case : str = ApproximateGELU(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = nn.ModuleList([] ) # project in self.net.append(SCREAMING_SNAKE_CASE_ ) # project dropout self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) # project out self.net.append(nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' for module in self.net: snake_case : str = module(SCREAMING_SNAKE_CASE_ ) return hidden_states class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "none" ): '''simple docstring''' super().__init__() snake_case : Optional[Any] = nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = approximate def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : str = self.proj(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = self.gelu(SCREAMING_SNAKE_CASE_ ) return hidden_states class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = nn.Linear(SCREAMING_SNAKE_CASE_ ,dim_out * 2 ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case , snake_case : Optional[int] = self.proj(SCREAMING_SNAKE_CASE_ ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(SCREAMING_SNAKE_CASE_ ) class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : str = nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = self.proj(SCREAMING_SNAKE_CASE_ ) return x * torch.sigmoid(1.7_02 * x ) class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : List[Any] = nn.Embedding(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = nn.SiLU() snake_case : str = nn.Linear(SCREAMING_SNAKE_CASE_ ,embedding_dim * 2 ) snake_case : str = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ,elementwise_affine=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ ) ) ) snake_case , snake_case : Tuple = torch.chunk(SCREAMING_SNAKE_CASE_ ,2 ) snake_case : int = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale) + shift return x class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : int = CombinedTimestepLabelEmbeddings(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Dict = nn.SiLU() snake_case : Dict = nn.Linear(SCREAMING_SNAKE_CASE_ ,6 * embedding_dim ,bias=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ,elementwise_affine=SCREAMING_SNAKE_CASE_ ,eps=1E-6 ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' snake_case : Any = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,hidden_dtype=SCREAMING_SNAKE_CASE_ ) ) ) snake_case , snake_case , snake_case , snake_case , snake_case , snake_case : Optional[Any] = emb.chunk(6 ,dim=1 ) snake_case : int = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = 1E-5 ): '''simple docstring''' super().__init__() snake_case : Optional[Any] = num_groups snake_case : Dict = eps if act_fn is None: snake_case : Tuple = None else: snake_case : Tuple = get_activation(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = nn.Linear(SCREAMING_SNAKE_CASE_ ,out_dim * 2 ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.act: snake_case : int = self.act(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = self.linear(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = emb[:, :, None, None] snake_case , snake_case : Optional[int] = emb.chunk(2 ,dim=1 ) snake_case : Union[str, Any] = F.group_norm(SCREAMING_SNAKE_CASE_ ,self.num_groups ,eps=self.eps ) snake_case : List[Any] = x * (1 + scale) + shift return x
36
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
import baseaa def lowercase ( __A : str ) -> bytes: '''simple docstring''' return baseaa.baaencode(string.encode("""utf-8""" ) ) def lowercase ( __A : bytes ) -> str: '''simple docstring''' return baseaa.baadecode(__A ).decode("""utf-8""" ) if __name__ == "__main__": __lowercase : int = '''Hello World!''' __lowercase : Union[str, Any] = baseaa_encode(test) print(encoded) __lowercase : Any = baseaa_decode(encoded) print(decoded)
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
from typing import Dict import numpy as np import torch from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map def lowercase ( __A : Dict[str, torch.Tensor] ) -> Dict[str, torch.Tensor]: '''simple docstring''' snake_case : str = [] snake_case : Optional[int] = [] snake_case : str = [] for rt in rc.restypes: snake_case : List[Any] = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]] restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] ) snake_case : List[str] = {name: i for i, name in enumerate(__A )} restype_atomaa_to_atomaa_list.append( [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] ) restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] ) # Add dummy mapping for restype 'UNK' restype_atomaa_to_atomaa_list.append([0] * 14 ) restype_atomaa_to_atomaa_list.append([0] * 37 ) restype_atomaa_mask_list.append([0.0] * 14 ) snake_case : List[Any] = torch.tensor( __A , dtype=torch.intaa , device=protein["""aatype"""].device , ) snake_case : int = torch.tensor( __A , dtype=torch.intaa , device=protein["""aatype"""].device , ) snake_case : Tuple = torch.tensor( __A , dtype=torch.floataa , device=protein["""aatype"""].device , ) snake_case : List[str] = protein["""aatype"""].to(torch.long ) # create the mapping for (residx, atom14) --> atom37, i.e. an array # with shape (num_res, 14) containing the atom37 indices for this protein snake_case : Any = restype_atomaa_to_atomaa[protein_aatype] snake_case : Dict = restype_atomaa_mask[protein_aatype] snake_case : str = residx_atomaa_mask snake_case : Optional[int] = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back snake_case : Tuple = restype_atomaa_to_atomaa[protein_aatype] snake_case : Optional[Any] = residx_atomaa_to_atomaa.long() # create the corresponding mask snake_case : int = torch.zeros([21, 37] , dtype=torch.floataa , device=protein["""aatype"""].device ) for restype, restype_letter in enumerate(rc.restypes ): snake_case : Optional[Any] = rc.restype_atoa[restype_letter] snake_case : Union[str, Any] = rc.residue_atoms[restype_name] for atom_name in atom_names: snake_case : int = rc.atom_order[atom_name] snake_case : Union[str, Any] = 1 snake_case : Any = restype_atomaa_mask[protein_aatype] snake_case : Union[str, Any] = residx_atomaa_mask return protein def lowercase ( __A : Dict[str, torch.Tensor] ) -> Dict[str, np.ndarray]: '''simple docstring''' snake_case : Tuple = tree_map(lambda __A : torch.tensor(__A , device=batch["""aatype"""].device ) , __A , np.ndarray ) snake_case : int = tensor_tree_map(lambda __A : np.array(__A ) , make_atomaa_masks(__A ) ) return out
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
def lowercase ( __A : int = 1 , __A : int = 1000 ) -> int: '''simple docstring''' snake_case : int = 1 snake_case : str = 0 for divide_by_number in range(__A , digit + 1 ): snake_case : list[int] = [] snake_case : Dict = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__A ): snake_case : List[str] = len(__A ) snake_case : str = divide_by_number else: has_been_divided.append(__A ) snake_case : Optional[int] = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" 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 - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking 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=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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 ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
from __future__ import annotations import math class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = size # approximate the overall size of segment tree with given value snake_case : Dict = [0 for i in range(0 ,4 * size )] # create array to store lazy update snake_case : List[Any] = [0 for i in range(0 ,4 * size )] snake_case : Any = [0 for i in range(0 ,4 * size )] # flag for lazy update def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return idx * 2 def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return idx * 2 + 1 def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if left_element == right_element: snake_case : int = a[left_element - 1] else: snake_case : List[str] = (left_element + right_element) // 2 self.build(self.left(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.build(self.right(SCREAMING_SNAKE_CASE_ ) ,mid + 1 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : str = max( self.segment_tree[self.left(SCREAMING_SNAKE_CASE_ )] ,self.segment_tree[self.right(SCREAMING_SNAKE_CASE_ )] ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.flag[idx] is True: snake_case : int = self.lazy[idx] snake_case : List[str] = False if left_element != right_element: snake_case : int = self.lazy[idx] snake_case : List[str] = self.lazy[idx] snake_case : List[Any] = True snake_case : Union[str, Any] = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: snake_case : Optional[Any] = val if left_element != right_element: snake_case : str = val snake_case : Optional[Any] = val snake_case : Optional[Any] = True snake_case : List[Any] = True return True snake_case : List[str] = (left_element + right_element) // 2 self.update(self.left(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.update(self.right(SCREAMING_SNAKE_CASE_ ) ,mid + 1 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = max( self.segment_tree[self.left(SCREAMING_SNAKE_CASE_ )] ,self.segment_tree[self.right(SCREAMING_SNAKE_CASE_ )] ) return True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.flag[idx] is True: snake_case : List[Any] = self.lazy[idx] snake_case : List[Any] = False if left_element != right_element: snake_case : List[str] = self.lazy[idx] snake_case : int = self.lazy[idx] snake_case : int = True snake_case : str = 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] snake_case : List[Any] = (left_element + right_element) // 2 snake_case : List[str] = self.query(self.left(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = self.query(self.right(SCREAMING_SNAKE_CASE_ ) ,mid + 1 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) return max(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def __str__( self ): '''simple docstring''' return str([self.query(1 ,1 ,self.size ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for i in range(1 ,self.size + 1 )] ) if __name__ == "__main__": __lowercase : Union[str, Any] = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] __lowercase : Union[str, Any] = 15 __lowercase : Union[str, Any] = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 111) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 235) print(segt)
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __lowercase : Optional[int] = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : int = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[Any] = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys __lowercase : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
def lowercase ( __A : list ) -> bool: '''simple docstring''' if not isinstance(__A , __A ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(__A ) == 0: raise ValueError("""Input list must be a non empty list""" ) if len(__A ) == 1: return True snake_case : int = series[1] - series[0] for index in range(len(__A ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def lowercase ( __A : list ) -> float: '''simple docstring''' if not isinstance(__A , __A ): raise ValueError("""Input series is not valid, valid series - [2, 4, 6]""" ) if len(__A ) == 0: raise ValueError("""Input list must be a non empty list""" ) snake_case : Any = 0 for val in series: answer += val return answer / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
def lowercase ( __A : int = 400_0000 ) -> int: '''simple docstring''' snake_case : str = [0, 1] snake_case : List[str] = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 snake_case : Tuple = 0 for j in range(len(__A ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f'''{solution() = }''')
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
import time import warnings from abc import ABC from copy import deepcopy from typing import Optional import torch from ..utils import add_start_docstrings, logging __lowercase : Tuple = logging.get_logger(__name__) __lowercase : Union[str, Any] = r''' Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax or scores for each vocabulary token after SoftMax. kwargs (`Dict[str, Any]`, *optional*): Additional stopping criteria specific kwargs. Return: `bool`. `False` indicates we should continue, `True` indicates we should stop. ''' class _A ( snake_case ): '''simple docstring''' @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' raise NotImplementedError("""StoppingCriteria needs to be subclassed""" ) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : List[Any] = max_length snake_case : Union[str, Any] = max_position_embeddings @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = input_ids.shape[-1] snake_case : List[Any] = cur_len >= self.max_length if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings: logger.warning_once( """This is a friendly reminder - the current text generation call will exceed the model's predefined """ F"""maximum length ({self.max_position_embeddings}). Depending on the model, you may observe """ """exceptions, performance degradation, or nothing at all.""" ) return is_done class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """The class `MaxNewTokensCriteria` is deprecated. """ F"""Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` """ """with `max_length = start_length + max_new_tokens` instead.""" ,SCREAMING_SNAKE_CASE_ ,) snake_case : List[str] = start_length snake_case : str = max_new_tokens snake_case : Union[str, Any] = start_length + max_new_tokens @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return input_ids.shape[-1] >= self.max_length class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Any = max_time snake_case : Dict = time.time() if initial_timestamp is None else initial_timestamp @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return time.time() - self.initial_timestamp > self.max_time class _A ( snake_case ): '''simple docstring''' @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return any(criteria(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for criteria in self ) @property def snake_case_ ( self ): '''simple docstring''' for stopping_criterium in self: if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): return stopping_criterium.max_length elif isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): return stopping_criterium.max_length return None def lowercase ( __A : StoppingCriteriaList , __A : int ) -> StoppingCriteriaList: '''simple docstring''' snake_case : List[str] = stopping_criteria.max_length snake_case : int = deepcopy(__A ) if stopping_max_length is not None and stopping_max_length != max_length: warnings.warn("""You set different `max_length` for stopping criteria and `max_length` parameter""" , __A ) elif stopping_max_length is None: new_stopping_criteria.append(MaxLengthCriteria(max_length=__A ) ) return new_stopping_criteria
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": __lowercase : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, help='''The YAML config file corresponding to the original architecture.''', ) parser.add_argument( '''--num_in_channels''', default=None, type=int, help='''The number of input channels. If `None` number of input channels will be automatically inferred.''', ) parser.add_argument( '''--image_size''', default=512, type=int, help=( '''The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2''' ''' Base. Use 768 for Stable Diffusion v2.''' ), ) parser.add_argument( '''--extract_ema''', action='''store_true''', help=( '''Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights''' ''' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield''' ''' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.''' ), ) parser.add_argument( '''--upcast_attention''', action='''store_true''', help=( '''Whether the attention computation should always be upcasted. This is necessary when running stable''' ''' diffusion 2.1.''' ), ) parser.add_argument( '''--from_safetensors''', action='''store_true''', help='''If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.''', ) parser.add_argument( '''--to_safetensors''', action='''store_true''', help='''Whether to store pipeline in safetensors format or not.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') parser.add_argument('''--device''', type=str, help='''Device to use (e.g. cpu, cuda:0, cuda:1, etc.)''') def lowercase ( __A : Tuple ) -> Union[str, Any]: '''simple docstring''' if string == "True": return True elif string == "False": return False else: raise ValueError(f"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) __lowercase : Union[str, Any] = parser.parse_args() __lowercase : Optional[int] = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=13 ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=99 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=37 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="None" ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=None ,): '''simple docstring''' snake_case : List[Any] = parent snake_case : Dict = batch_size snake_case : Dict = seq_length snake_case : Optional[Any] = is_training snake_case : Optional[Any] = use_input_mask snake_case : Any = use_token_type_ids snake_case : str = use_labels snake_case : int = vocab_size snake_case : Optional[int] = hidden_size snake_case : Optional[int] = num_hidden_layers snake_case : str = num_attention_heads snake_case : List[Any] = intermediate_size snake_case : str = hidden_act snake_case : Optional[Any] = hidden_dropout_prob snake_case : Any = attention_probs_dropout_prob snake_case : Union[str, Any] = max_position_embeddings snake_case : Tuple = type_vocab_size snake_case : Optional[Any] = type_sequence_label_size snake_case : List[Any] = initializer_range snake_case : Tuple = num_labels snake_case : int = num_choices snake_case : List[Any] = relative_attention snake_case : int = position_biased_input snake_case : Any = pos_att_type snake_case : Optional[int] = scope def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case : List[Any] = None if self.use_input_mask: snake_case : Any = random_attention_mask([self.batch_size, self.seq_length] ) snake_case : List[str] = None if self.use_token_type_ids: snake_case : Tuple = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) snake_case : Any = None snake_case : Optional[Any] = None snake_case : str = None if self.use_labels: snake_case : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) snake_case : int = DebertaVaConfig( 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 ,relative_attention=self.relative_attention ,position_biased_input=self.position_biased_input ,initializer_range=self.initializer_range ,return_dict=SCREAMING_SNAKE_CASE_ ,) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[Any] = TFDebertaVaModel(config=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} snake_case : Dict = [input_ids, input_mask] snake_case : List[Any] = model(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = TFDebertaVaForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) snake_case : int = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Any = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = self.num_labels snake_case : Any = TFDebertaVaForSequenceClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : List[str] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = self.num_labels snake_case : Union[str, Any] = TFDebertaVaForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = TFDebertaVaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Tuple = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.prepare_config_and_inputs() ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Optional[Any] = config_and_inputs snake_case : List[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _A ( snake_case , snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) __lowerCamelCase : Any = ( { '''feature-extraction''': TFDebertaVaModel, '''fill-mask''': TFDebertaVaForMaskedLM, '''question-answering''': TFDebertaVaForQuestionAnswering, '''text-classification''': TFDebertaVaForSequenceClassification, '''token-classification''': TFDebertaVaForTokenClassification, '''zero-shot''': TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) __lowerCamelCase : Optional[int] = False __lowerCamelCase : Dict = False def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = TFDebertaVaModelTester(self ) snake_case : Dict = ConfigTester(self ,config_class=SCREAMING_SNAKE_CASE_ ,hidden_size=37 ) def snake_case_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def snake_case_ ( self ): '''simple docstring''' snake_case : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Any = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @require_tf class _A ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason="""Model not available yet""" ) def snake_case_ ( self ): '''simple docstring''' pass @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) snake_case : Dict = tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] ) snake_case : Tuple = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case : Optional[Any] = model(SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ )[0] snake_case : Optional[Any] = tf.constant( [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] ,SCREAMING_SNAKE_CASE_ ,atol=1E-4 )
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger __lowercase : Optional[int] = get_logger(__name__) class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Optional[int] = ( os.path.join(SCREAMING_SNAKE_CASE_ ,config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) snake_case : Union[str, Any] = Extractor def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" snake_case : str = os.path.abspath(SCREAMING_SNAKE_CASE_ ) return os.path.join(self.extract_dir ,hash_url_to_filename(SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return force_extract or ( not os.path.isfile(SCREAMING_SNAKE_CASE_ ) and not (os.path.isdir(SCREAMING_SNAKE_CASE_ ) and os.listdir(SCREAMING_SNAKE_CASE_ )) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' snake_case : Optional[Any] = self.extractor.infer_extractor_format(SCREAMING_SNAKE_CASE_ ) if not extractor_format: return input_path snake_case : int = self._get_output_path(SCREAMING_SNAKE_CASE_ ) if self._do_extract(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): self.extractor.extract(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) return output_path class _A ( snake_case ): '''simple docstring''' @classmethod @abstractmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' ... @staticmethod @abstractmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' ... class _A ( snake_case , snake_case ): '''simple docstring''' __lowerCamelCase : List[bytes] = [] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as f: return f.read(SCREAMING_SNAKE_CASE_ ) @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = b"" ): '''simple docstring''' if not magic_number: snake_case : str = max(len(SCREAMING_SNAKE_CASE_ ) for cls_magic_number in cls.magic_numbers ) try: snake_case : Dict = cls.read_magic_number(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) except OSError: return False return any(magic_number.startswith(SCREAMING_SNAKE_CASE_ ) for cls_magic_number in cls.magic_numbers ) class _A ( snake_case ): '''simple docstring''' @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return tarfile.is_tarfile(SCREAMING_SNAKE_CASE_ ) @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def resolved(SCREAMING_SNAKE_CASE_ ) -> str: return os.path.realpath(os.path.abspath(SCREAMING_SNAKE_CASE_ ) ) def badpath(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ).startswith(SCREAMING_SNAKE_CASE_ ) def badlink(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) -> bool: # Links are interpreted relative to the directory containing the link snake_case : Optional[Any] = resolved(os.path.join(SCREAMING_SNAKE_CASE_ ,os.path.dirname(info.name ) ) ) return badpath(info.linkname ,base=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = resolved(SCREAMING_SNAKE_CASE_ ) for finfo in members: if badpath(finfo.name ,SCREAMING_SNAKE_CASE_ ): logger.error(F"""Extraction of {finfo.name} is blocked (illegal path)""" ) elif finfo.issym() and badlink(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): logger.error(F"""Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}""" ) elif finfo.islnk() and badlink(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): logger.error(F"""Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}""" ) else: yield finfo @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = tarfile.open(SCREAMING_SNAKE_CASE_ ) tar_file.extractall(SCREAMING_SNAKE_CASE_ ,members=TarExtractor.safemembers(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) tar_file.close() class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : str = [B'''\x1F\x8B'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with gzip.open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as gzip_file: with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = [ B'''PK\x03\x04''', B'''PK\x05\x06''', # empty archive B'''PK\x07\x08''', # spanned archive ] @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = b"" ): '''simple docstring''' if super().is_extractable(SCREAMING_SNAKE_CASE_ ,magic_number=SCREAMING_SNAKE_CASE_ ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as fp: snake_case : List[Any] = _EndRecData(SCREAMING_SNAKE_CASE_ ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: snake_case : List[Any] = fp.read(SCREAMING_SNAKE_CASE_ ) # CD is where we expect it to be if len(SCREAMING_SNAKE_CASE_ ) == sizeCentralDir: snake_case : str = struct.unpack(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ ,"""r""" ) as zip_file: zip_file.extractall(SCREAMING_SNAKE_CASE_ ) zip_file.close() class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = [B'''\xFD\x37\x7A\x58\x5A\x00'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with lzma.open(SCREAMING_SNAKE_CASE_ ) as compressed_file: with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = [B'''Rar!\x1a\x07\x00''', B'''Rar!\x1a\x07\x01\x00'''] # RAR_ID # RAR5_ID @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if not config.RARFILE_AVAILABLE: raise ImportError("""Please pip install rarfile""" ) import rarfile os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = rarfile.RarFile(SCREAMING_SNAKE_CASE_ ) rf.extractall(SCREAMING_SNAKE_CASE_ ) rf.close() class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = [B'''\x28\xb5\x2F\xFD'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if not config.ZSTANDARD_AVAILABLE: raise ImportError("""Please pip install zstandard""" ) import zstandard as zstd snake_case : Any = zstd.ZstdDecompressor() with open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as ifh, open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as ofh: dctx.copy_stream(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[Any] = [B'''\x42\x5A\x68'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with bza.open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as compressed_file: with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = [B'''\x37\x7A\xBC\xAF\x27\x1C'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if not config.PY7ZR_AVAILABLE: raise ImportError("""Please pip install py7zr""" ) import pyazr os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) with pyazr.SevenZipFile(SCREAMING_SNAKE_CASE_ ,"""r""" ) as archive: archive.extractall(SCREAMING_SNAKE_CASE_ ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Any = [B'''\x04\x22\x4D\x18'''] @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if not config.LZ4_AVAILABLE: raise ImportError("""Please pip install lz4""" ) import lza.frame with lza.frame.open(SCREAMING_SNAKE_CASE_ ,"""rb""" ) as compressed_file: with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) class _A : '''simple docstring''' __lowerCamelCase : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def snake_case_ ( cls ): '''simple docstring''' return max( len(SCREAMING_SNAKE_CASE_ ) for extractor in cls.extractors.values() if issubclass(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' try: return MagicNumberBaseExtractor.read_magic_number(SCREAMING_SNAKE_CASE_ ,magic_number_length=SCREAMING_SNAKE_CASE_ ) except OSError: return b"" @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' warnings.warn( """Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. """ """Use 'infer_extractor_format' instead.""" ,category=SCREAMING_SNAKE_CASE_ ,) snake_case : Union[str, Any] = cls.infer_extractor_format(SCREAMING_SNAKE_CASE_ ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ): # <Added version="2.4.0"/> '''simple docstring''' snake_case : str = cls._get_magic_number_max_length() snake_case : Optional[Any] = cls._read_magic_number(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(SCREAMING_SNAKE_CASE_ ,magic_number=SCREAMING_SNAKE_CASE_ ): return extractor_format @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = "deprecated" ,): '''simple docstring''' os.makedirs(os.path.dirname(SCREAMING_SNAKE_CASE_ ) ,exist_ok=SCREAMING_SNAKE_CASE_ ) # Prevent parallel extractions snake_case : str = str(Path(SCREAMING_SNAKE_CASE_ ).with_suffix(""".lock""" ) ) with FileLock(SCREAMING_SNAKE_CASE_ ): shutil.rmtree(SCREAMING_SNAKE_CASE_ ,ignore_errors=SCREAMING_SNAKE_CASE_ ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): # passed as positional arg warnings.warn( """Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. """ """Use 'extractor_format' instead.""" ,category=SCREAMING_SNAKE_CASE_ ,) snake_case : Dict = extractor if extractor != """deprecated""" else extractor_format else: snake_case : Optional[Any] = cls.extractors[extractor_format] return extractor.extract(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else: warnings.warn( """Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an """ """exception in 3.0.0.""" ,category=SCREAMING_SNAKE_CASE_ ,) for extractor in cls.extractors.values(): if extractor.is_extractable(SCREAMING_SNAKE_CASE_ ): return extractor.extract(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
def lowercase ( __A : str , __A : str ) -> bool: '''simple docstring''' snake_case : str = len(__A ) snake_case : Union[str, Any] = len(__A ) snake_case : Union[str, Any] = [[False for _ in range(m + 1 )] for _ in range(n + 1 )] snake_case : List[Any] = True for i in range(__A ): for j in range(m + 1 ): if dp[i][j]: if j < m and a[i].upper() == b[j]: snake_case : Any = True if a[i].islower(): snake_case : Dict = True return dp[n][m] if __name__ == "__main__": import doctest doctest.testmod()
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
def lowercase ( __A : int = 100 ) -> int: '''simple docstring''' snake_case : Tuple = 0 snake_case : Tuple = 0 for i in range(1 , n + 1 ): sum_of_squares += i**2 sum_of_ints += i return sum_of_ints**2 - sum_of_squares if __name__ == "__main__": print(f'''{solution() = }''')
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
import inspect import unittest import warnings from transformers import DeiTConfig from transformers.models.auto import get_values from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_MAPPING, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, ) from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=13 ,SCREAMING_SNAKE_CASE_=30 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=5 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=37 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=10 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=2 ,): '''simple docstring''' snake_case : List[Any] = parent snake_case : Optional[Any] = batch_size snake_case : List[str] = image_size snake_case : Dict = patch_size snake_case : Any = num_channels snake_case : Dict = is_training snake_case : Dict = use_labels snake_case : Union[str, Any] = hidden_size snake_case : int = num_hidden_layers snake_case : str = num_attention_heads snake_case : Dict = intermediate_size snake_case : Tuple = hidden_act snake_case : List[str] = hidden_dropout_prob snake_case : Union[str, Any] = attention_probs_dropout_prob snake_case : Optional[Any] = type_sequence_label_size snake_case : Tuple = initializer_range snake_case : Optional[Any] = scope snake_case : int = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) snake_case : Union[str, Any] = (image_size // patch_size) ** 2 snake_case : str = num_patches + 2 def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case : List[Any] = None if self.use_labels: snake_case : Optional[int] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case : List[Any] = self.get_config() return config, pixel_values, labels def snake_case_ ( self ): '''simple docstring''' return DeiTConfig( 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=SCREAMING_SNAKE_CASE_ ,initializer_range=self.initializer_range ,encoder_stride=self.encoder_stride ,) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = DeiTModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() snake_case : Optional[int] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Union[str, Any] = DeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() snake_case : List[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.reconstruction.shape ,(self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images snake_case : str = 1 snake_case : Union[str, Any] = DeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() snake_case : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case : Optional[int] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.reconstruction.shape ,(self.batch_size, 1, self.image_size, self.image_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = self.type_sequence_label_size snake_case : Optional[int] = DeiTForImageClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() snake_case : Any = model(SCREAMING_SNAKE_CASE_ ,labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images snake_case : Any = 1 snake_case : List[str] = DeiTForImageClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() snake_case : Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case : Tuple = model(SCREAMING_SNAKE_CASE_ ,labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = self.prepare_config_and_inputs() ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : str = config_and_inputs snake_case : List[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _A ( snake_case , snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[str] = ( ( DeiTModel, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, ) if is_torch_available() else () ) __lowerCamelCase : int = ( { '''feature-extraction''': DeiTModel, '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher), } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : List[Any] = False def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = DeiTModelTester(self ) snake_case : Union[str, Any] = ConfigTester(self ,config_class=SCREAMING_SNAKE_CASE_ ,has_text_modality=SCREAMING_SNAKE_CASE_ ,hidden_size=37 ) def snake_case_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""DeiT does not use inputs_embeds""" ) def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case : str = model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) snake_case : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ ,nn.Linear ) ) def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case : str = model_class(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case : Any = [*signature.parameters.keys()] snake_case : Optional[int] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' snake_case : Any = super()._prepare_for_class(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) if return_labels: if model_class.__name__ == "DeiTForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def snake_case_ ( self ): '''simple docstring''' if not self.model_tester.is_training: return snake_case , snake_case : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case : str = True for model_class in self.all_model_classes: # DeiTForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(SCREAMING_SNAKE_CASE_ ) or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue snake_case : Tuple = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.train() snake_case : Union[str, Any] = self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : int = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return snake_case : Tuple = False snake_case : Tuple = True for model_class in self.all_model_classes: if model_class in get_values(SCREAMING_SNAKE_CASE_ ) or not model_class.supports_gradient_checkpointing: continue # DeiTForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "DeiTForImageClassificationWithTeacher": continue snake_case : Optional[Any] = model_class(SCREAMING_SNAKE_CASE_ ) model.gradient_checkpointing_enable() model.to(SCREAMING_SNAKE_CASE_ ) model.train() snake_case : Union[str, Any] = self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Tuple = self.model_tester.prepare_config_and_inputs_for_common() snake_case : List[str] = [ {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float}, {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long}, {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(SCREAMING_SNAKE_CASE_ ), *get_values(SCREAMING_SNAKE_CASE_ ), ] or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}""" ): snake_case : Any = problem_type["""title"""] snake_case : str = problem_type["""num_labels"""] snake_case : str = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.train() snake_case : Tuple = self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) if problem_type["num_labels"] > 1: snake_case : Optional[Any] = inputs["""labels"""].unsqueeze(1 ).repeat(1 ,problem_type["""num_labels"""] ) snake_case : List[Any] = inputs["""labels"""].to(problem_type["""dtype"""] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=SCREAMING_SNAKE_CASE_ ) as warning_list: snake_case : Any = model(**SCREAMING_SNAKE_CASE_ ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( F"""Something is going wrong in the regression problem: intercepted {w.message}""" ) loss.backward() @slow def snake_case_ ( self ): '''simple docstring''' for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case : int = DeiTModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def lowercase ( ) -> List[Any]: '''simple docstring''' snake_case : Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class _A ( unittest.TestCase ): '''simple docstring''' @cached_property def snake_case_ ( self ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ) if is_vision_available() else None ) @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ).to( SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = self.default_image_processor snake_case : List[str] = prepare_img() snake_case : Tuple = image_processor(images=SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): snake_case : int = model(**SCREAMING_SNAKE_CASE_ ) # verify the logits snake_case : Dict = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape ,SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = torch.tensor([-1.02_66, 0.19_12, -1.28_61] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,SCREAMING_SNAKE_CASE_ ,atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = DeiTModel.from_pretrained( """facebook/deit-base-distilled-patch16-224""" ,torch_dtype=torch.floataa ,device_map="""auto""" ) snake_case : List[Any] = self.default_image_processor snake_case : Tuple = prepare_img() snake_case : List[str] = image_processor(images=SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ) snake_case : List[str] = inputs.pixel_values.to(SCREAMING_SNAKE_CASE_ ) # forward pass to make sure inference works in fp16 with torch.no_grad(): snake_case : Optional[Any] = model(SCREAMING_SNAKE_CASE_ )
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import logging import os from dataclasses import dataclass, field from functools import partial from pathlib import Path from tempfile import TemporaryDirectory from typing import List, Optional import faiss import torch from datasets import Features, Sequence, Value, load_dataset from transformers import DPRContextEncoder, DPRContextEncoderTokenizerFast, HfArgumentParser __lowercase : int = logging.getLogger(__name__) torch.set_grad_enabled(False) __lowercase : Optional[Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' def lowercase ( __A : str , __A : Union[str, Any]=100 , __A : Optional[Any]=" " ) -> List[str]: '''simple docstring''' snake_case : Dict = text.split(__A ) return [character.join(text[i : i + n] ).strip() for i in range(0 , len(__A ) , __A )] def lowercase ( __A : dict ) -> dict: '''simple docstring''' snake_case , snake_case : Optional[Any] = [], [] for title, text in zip(documents["""title"""] , documents["""text"""] ): if text is not None: for passage in split_text(__A ): titles.append(title if title is not None else """""" ) texts.append(__A ) return {"title": titles, "text": texts} def lowercase ( __A : dict , __A : DPRContextEncoder , __A : DPRContextEncoderTokenizerFast ) -> dict: '''simple docstring''' snake_case : List[str] = ctx_tokenizer( documents["""title"""] , documents["""text"""] , truncation=__A , padding="""longest""" , return_tensors="""pt""" )["""input_ids"""] snake_case : int = ctx_encoder(input_ids.to(device=__A ) , return_dict=__A ).pooler_output return {"embeddings": embeddings.detach().cpu().numpy()} def lowercase ( __A : "RagExampleArguments" , __A : "ProcessingArguments" , __A : "IndexHnswArguments" , ) -> int: '''simple docstring''' logger.info("""Step 1 - Create the dataset""" ) ###################################### # The dataset needed for RAG must have three columns: # - title (string): title of the document # - text (string): text of a passage of the document # - embeddings (array of dimension d): DPR representation of the passage # Let's say you have documents in tab-separated csv files with columns "title" and "text" assert os.path.isfile(rag_example_args.csv_path ), "Please provide a valid path to a csv file" # You can load a Dataset object this way snake_case : Any = load_dataset( """csv""" , data_files=[rag_example_args.csv_path] , split="""train""" , delimiter="""\t""" , column_names=["""title""", """text"""] ) # More info about loading csv files in the documentation: https://huggingface.co/docs/datasets/loading_datasets.html?highlight=csv#csv-files # Then split the documents into passages of 100 words snake_case : Optional[Any] = dataset.map(__A , batched=__A , num_proc=processing_args.num_proc ) # And compute the embeddings snake_case : Optional[Any] = DPRContextEncoder.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ).to(device=__A ) snake_case : Union[str, Any] = DPRContextEncoderTokenizerFast.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ) snake_case : List[str] = Features( {"""text""": Value("""string""" ), """title""": Value("""string""" ), """embeddings""": Sequence(Value("""float32""" ) )} ) # optional, save as float32 instead of float64 to save space snake_case : Optional[int] = dataset.map( partial(__A , ctx_encoder=__A , ctx_tokenizer=__A ) , batched=__A , batch_size=processing_args.batch_size , features=__A , ) # And finally save your dataset snake_case : Optional[Any] = os.path.join(rag_example_args.output_dir , """my_knowledge_dataset""" ) dataset.save_to_disk(__A ) # from datasets import load_from_disk # dataset = load_from_disk(passages_path) # to reload the dataset ###################################### logger.info("""Step 2 - Index the dataset""" ) ###################################### # Let's use the Faiss implementation of HNSW for fast approximate nearest neighbor search snake_case : Union[str, Any] = faiss.IndexHNSWFlat(index_hnsw_args.d , index_hnsw_args.m , faiss.METRIC_INNER_PRODUCT ) dataset.add_faiss_index("""embeddings""" , custom_index=__A ) # And save the index snake_case : Optional[Any] = os.path.join(rag_example_args.output_dir , """my_knowledge_dataset_hnsw_index.faiss""" ) dataset.get_index("""embeddings""" ).save(__A ) # dataset.load_faiss_index("embeddings", index_path) # to reload the index @dataclass class _A : '''simple docstring''' __lowerCamelCase : str = field( default=str(Path(snake_case ).parent / '''test_run''' / '''dummy-kb''' / '''my_knowledge_dataset.csv''' ) , metadata={'''help''': '''Path to a tab-separated csv file with columns \'title\' and \'text\''''} , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''Question that is passed as input to RAG. Default is \'What does Moses\' rod turn into ?\'.'''} , ) __lowerCamelCase : str = field( default='''facebook/rag-sequence-nq''' , metadata={'''help''': '''The RAG model to use. Either \'facebook/rag-sequence-nq\' or \'facebook/rag-token-nq\''''} , ) __lowerCamelCase : str = field( default='''facebook/dpr-ctx_encoder-multiset-base''' , metadata={ '''help''': ( '''The DPR context encoder model to use. Either \'facebook/dpr-ctx_encoder-single-nq-base\' or''' ''' \'facebook/dpr-ctx_encoder-multiset-base\'''' ) } , ) __lowerCamelCase : Optional[str] = field( default=str(Path(snake_case ).parent / '''test_run''' / '''dummy-kb''' ) , metadata={'''help''': '''Path to a directory where the dataset passages and the index will be saved'''} , ) @dataclass class _A : '''simple docstring''' __lowerCamelCase : Optional[int] = field( default=snake_case , metadata={ '''help''': '''The number of processes to use to split the documents into passages. Default is single process.''' } , ) __lowerCamelCase : int = field( default=1_6 , metadata={ '''help''': '''The batch size to use when computing the passages embeddings using the DPR context encoder.''' } , ) @dataclass class _A : '''simple docstring''' __lowerCamelCase : int = field( default=7_6_8 , metadata={'''help''': '''The dimension of the embeddings to pass to the HNSW Faiss index.'''} , ) __lowerCamelCase : int = field( default=1_2_8 , metadata={ '''help''': ( '''The number of bi-directional links created for every new element during the HNSW index construction.''' ) } , ) if __name__ == "__main__": logging.basicConfig(level=logging.WARNING) logger.setLevel(logging.INFO) __lowercase : Dict = HfArgumentParser((RagExampleArguments, ProcessingArguments, IndexHnswArguments)) __lowercase , __lowercase , __lowercase : Dict = parser.parse_args_into_dataclasses() with TemporaryDirectory() as tmp_dir: __lowercase : Union[str, Any] = rag_example_args.output_dir or tmp_dir main(rag_example_args, processing_args, index_hnsw_args)
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
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, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __lowercase : int = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PIL.Image.BICUBIC ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = size if size is not None else {"""height""": 256, """width""": 256} snake_case : Any = get_size_dict(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} snake_case : List[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Union[str, Any] = do_resize snake_case : Dict = size snake_case : Optional[int] = resample snake_case : Union[str, Any] = do_center_crop snake_case : Union[str, Any] = crop_size snake_case : Optional[int] = do_rescale snake_case : Dict = rescale_factor snake_case : Dict = do_normalize snake_case : int = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN snake_case : Optional[int] = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BICUBIC ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : int = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return resize( SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return normalize(SCREAMING_SNAKE_CASE_ ,mean=SCREAMING_SNAKE_CASE_ ,std=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Optional[Any] = do_resize if do_resize is not None else self.do_resize snake_case : Dict = resample if resample is not None else self.resample snake_case : List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : List[str] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Any = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize snake_case : Any = image_mean if image_mean is not None else self.image_mean snake_case : int = image_std if image_std is not None else self.image_std snake_case : str = size if size is not None else self.size snake_case : Tuple = get_size_dict(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = crop_size if crop_size is not None else self.crop_size snake_case : Dict = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : str = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_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.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Tuple = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : int = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] if do_normalize: snake_case : str = [self.normalize(image=SCREAMING_SNAKE_CASE_ ,mean=SCREAMING_SNAKE_CASE_ ,std=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : Dict = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : Union[str, Any] = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ )
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import string def lowercase ( __A : str ) -> str: '''simple docstring''' snake_case : Union[str, Any] = """""" for i in sequence: snake_case : Optional[int] = ord(__A ) if 65 <= extract <= 90: output += chr(155 - extract ) elif 97 <= extract <= 122: output += chr(219 - extract ) else: output += i return output def lowercase ( __A : str ) -> str: '''simple docstring''' snake_case : Optional[Any] = string.ascii_letters snake_case : str = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1] return "".join( letters_reversed[letters.index(__A )] if c in letters else c for c in sequence ) def lowercase ( ) -> None: '''simple docstring''' from timeit import timeit print("""Running performance benchmarks...""" ) snake_case : List[Any] = """from string import printable ; from __main__ import atbash, atbash_slow""" print(f"""> atbash_slow(): {timeit("atbash_slow(printable)" , setup=__A )} seconds""" ) print(f"""> atbash(): {timeit("atbash(printable)" , setup=__A )} seconds""" ) if __name__ == "__main__": for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"): print(f'''{example} encrypted in atbash: {atbash(example)}''') benchmark()
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
from math import pi, sqrt def lowercase ( __A : float ) -> float: '''simple docstring''' if num <= 0: raise ValueError("""math domain error""" ) if num > 171.5: raise OverflowError("""math range error""" ) elif num - int(__A ) not in (0, 0.5): raise NotImplementedError("""num must be an integer or a half-integer""" ) elif num == 0.5: return sqrt(__A ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def lowercase ( ) -> None: '''simple docstring''' assert gamma(0.5 ) == sqrt(__A ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() __lowercase : Optional[int] = 1.0 while num: __lowercase : Dict = float(input('''Gamma of: ''')) print(f'''gamma({num}) = {gamma(num)}''') print('''\nEnter 0 to exit...''')
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
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 lowercase ( __A : Union[str, Any] , __A : int , __A : Dict , __A : Optional[int] ) -> str: '''simple docstring''' 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 lowercase ( __A : str , __A : List[str] , __A : Tuple , __A : Dict , __A : Tuple=True ) -> Tuple: '''simple docstring''' model.train() snake_case : List[Any] = model(__A ) snake_case : int = F.mse_loss(__A , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(__A ) def lowercase ( __A : List[str] , __A : int=False ) -> Union[str, Any]: '''simple docstring''' set_seed(42 ) snake_case : Optional[Any] = RegressionModel() snake_case : Dict = deepcopy(__A ) snake_case : Optional[Any] = RegressionDataset(length=80 ) snake_case : Optional[Any] = DataLoader(__A , batch_size=16 ) model.to(accelerator.device ) if sched: snake_case : List[str] = AdamW(params=model.parameters() , lr=1E-3 ) snake_case : List[str] = AdamW(params=ddp_model.parameters() , lr=1E-3 ) snake_case : int = LambdaLR(__A , lr_lambda=lambda __A : epoch**0.65 ) snake_case : Any = LambdaLR(__A , lr_lambda=lambda __A : epoch**0.65 ) # Make a copy of `model` if sched: snake_case , snake_case , snake_case , snake_case : Optional[int] = accelerator.prepare(__A , __A , __A , __A ) else: snake_case , snake_case : Any = accelerator.prepare(__A , __A ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def lowercase ( __A : List[Any] ) -> Any: '''simple docstring''' snake_case , snake_case , snake_case : Union[str, Any] = get_training_setup(__A ) # Use a single batch snake_case , snake_case : str = next(iter(__A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model snake_case , snake_case : List[Any] = accelerator.gather((ddp_input, ddp_target) ) snake_case , snake_case : int = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(__A ): step_model(__A , __A , __A , __A ) else: # Sync grads step_model(__A , __A , __A , __A ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(__A , __A , __A , __A ) 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 ) snake_case : Tuple = ddp_input[torch.randperm(len(__A ) )] def lowercase ( __A : Optional[int] ) -> List[str]: '''simple docstring''' snake_case , snake_case , snake_case : str = get_training_setup(__A ) # Use a single batch snake_case , snake_case : Any = next(iter(__A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model snake_case , snake_case : List[str] = accelerator.gather((ddp_input, ddp_target) ) snake_case , snake_case : Optional[int] = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(__A ): step_model(__A , __A , __A , __A ) else: # Sync grads step_model(__A , __A , __A , __A ) # 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 ) snake_case : Optional[int] = ddp_input[torch.randperm(len(__A ) )] def lowercase ( __A : Union[str, Any]=False , __A : Any=False ) -> Tuple: '''simple docstring''' snake_case : Optional[Any] = Accelerator( split_batches=__A , dispatch_batches=__A , gradient_accumulation_steps=2 ) # Test that context manager behaves properly snake_case , snake_case , snake_case : Optional[int] = get_training_setup(__A ) for iteration, batch in enumerate(__A ): snake_case , snake_case : Optional[Any] = batch.values() # Gather the distributed inputs and targs for the base model snake_case , snake_case : List[str] = accelerator.gather((ddp_input, ddp_target) ) snake_case , snake_case : Union[str, Any] = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A , __A ) # Do "gradient accumulation" (noop) with accelerator.accumulate(__A ): step_model(__A , __A , __A , __A ) # 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(__A ) - 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 ) snake_case : Tuple = ddp_input[torch.randperm(len(__A ) )] GradientState._reset_state() def lowercase ( __A : Any=False , __A : str=False ) -> str: '''simple docstring''' snake_case : Any = Accelerator( split_batches=__A , dispatch_batches=__A , gradient_accumulation_steps=2 ) # Test that context manager behaves properly snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case : int = get_training_setup(__A , __A ) for iteration, batch in enumerate(__A ): snake_case , snake_case : Optional[Any] = batch.values() # Gather the distributed inputs and targs for the base model snake_case , snake_case : List[Any] = accelerator.gather((ddp_input, ddp_target) ) snake_case , snake_case : str = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(__A , __A , __A , __A , __A ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(__A )): 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(__A ): step_model(__A , __A , __A , __A ) 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""" snake_case : List[str] = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(__A )) if accelerator.num_processes > 1: check_model_parameters(__A , __A , __A , __A ) # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) GradientState._reset_state() def lowercase ( ) -> List[Any]: '''simple docstring''' snake_case : List[str] = Accelerator() snake_case : Dict = RegressionDataset(length=80 ) snake_case : Tuple = DataLoader(__A , batch_size=16 ) snake_case : Tuple = RegressionDataset(length=96 ) snake_case : Optional[Any] = DataLoader(__A , batch_size=16 ) snake_case , snake_case : Any = accelerator.prepare(__A , __A ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(__A ): assert id(accelerator.gradient_state.active_dataloader ) == id(__A ) if iteration < len(__A ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(__A ): assert id(accelerator.gradient_state.active_dataloader ) == id(__A ) if batch_num < len(__A ) - 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 lowercase ( ) -> List[str]: '''simple docstring''' snake_case : str = Accelerator() snake_case : Dict = 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(__A ) 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(__A ) 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(__A , __A ) # 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(__A , __A ) def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' main() if __name__ == "__main__": main()
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process __lowercase : Any = logging.getLogger(__name__) __lowercase : Any = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) __lowercase : Dict = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _A : '''simple docstring''' __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={ '''help''': ( '''The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.''' ) } , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(snake_case )} , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) __lowerCamelCase : bool = field( default=snake_case , metadata={'''help''': '''Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'''} , ) __lowerCamelCase : str = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) __lowerCamelCase : bool = field( default=snake_case , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) def snake_case_ ( self ): '''simple docstring''' if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( """--config_overrides can't be used in combination with --config_name or --model_name_or_path""" ) @dataclass class _A : '''simple docstring''' __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''The name of the dataset to use (via the datasets library).'''} ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) __lowerCamelCase : Optional[str] = field(default=snake_case , metadata={'''help''': '''The input training data file (a text file).'''} ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''An optional input train ref data file for whole word masking in Chinese.'''} , ) __lowerCamelCase : Optional[str] = field( default=snake_case , metadata={'''help''': '''An optional input validation ref data file for whole word masking in Chinese.'''} , ) __lowerCamelCase : bool = field( default=snake_case , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) __lowerCamelCase : Optional[int] = field( default=5 , metadata={ '''help''': '''The percentage of the train set used as validation set in case there\'s no validation split''' } , ) __lowerCamelCase : Optional[int] = field( default=snake_case , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated. Default to the max input length of the model.''' ) } , ) __lowerCamelCase : Optional[int] = field( default=snake_case , metadata={'''help''': '''The number of processes to use for the preprocessing.'''} , ) __lowerCamelCase : float = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) __lowerCamelCase : bool = field( default=snake_case , metadata={ '''help''': ( '''Whether to pad all samples to `max_seq_length`. ''' '''If False, will pad the samples dynamically when batching to the maximum length in the batch.''' ) } , ) def snake_case_ ( self ): '''simple docstring''' if self.train_file is not None: snake_case : Tuple = self.train_file.split(""".""" )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: snake_case : Optional[Any] = self.validation_file.split(""".""" )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def lowercase ( __A : Union[str, Any] , __A : List[Any] ) -> str: '''simple docstring''' with open(__A , """r""" , encoding="""utf-8""" ) as f: snake_case : int = [json.loads(__A ) for line in f.read().splitlines() if (len(__A ) > 0 and not line.isspace())] assert len(__A ) == len(__A ) snake_case : Optional[Any] = {c: dataset[c] for c in dataset.column_names} snake_case : Dict = refs return Dataset.from_dict(__A ) def lowercase ( ) -> str: '''simple docstring''' snake_case : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. snake_case , snake_case , snake_case : Any = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: snake_case , snake_case , snake_case : str = parser.parse_args_into_dataclasses() # Detecting last checkpoint. snake_case : Optional[int] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: snake_case : Tuple = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , __A ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. snake_case : Dict = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): snake_case : Union[str, Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""train[:{data_args.validation_split_percentage}%]""" , ) snake_case : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""train[{data_args.validation_split_percentage}%:]""" , ) else: snake_case : int = {} if data_args.train_file is not None: snake_case : Optional[int] = data_args.train_file if data_args.validation_file is not None: snake_case : Dict = data_args.validation_file snake_case : str = data_args.train_file.split(""".""" )[-1] if extension == "txt": snake_case : str = """text""" snake_case : List[str] = load_dataset(__A , data_files=__A ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case : Optional[Any] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: snake_case : Tuple = AutoConfig.from_pretrained(model_args.config_name , **__A ) elif model_args.model_name_or_path: snake_case : Tuple = AutoConfig.from_pretrained(model_args.model_name_or_path , **__A ) else: snake_case : Union[str, Any] = CONFIG_MAPPING[model_args.model_type]() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) snake_case : int = { """cache_dir""": model_args.cache_dir, """use_fast""": model_args.use_fast_tokenizer, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.tokenizer_name: snake_case : Optional[Any] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__A ) elif model_args.model_name_or_path: snake_case : Union[str, Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__A ) else: raise ValueError( """You are instantiating a new tokenizer from scratch. This is not supported by this script.""" """You can do it from another script, save it, and load it from here, using --tokenizer_name.""" ) if model_args.model_name_or_path: snake_case : Optional[Any] = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__A , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) snake_case : Any = AutoModelForMaskedLM.from_config(__A ) model.resize_token_embeddings(len(__A ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: snake_case : str = datasets["""train"""].column_names else: snake_case : Dict = datasets["""validation"""].column_names snake_case : List[str] = """text""" if """text""" in column_names else column_names[0] snake_case : int = """max_length""" if data_args.pad_to_max_length else False def tokenize_function(__A : Tuple ): # Remove empty lines snake_case : int = [line for line in examples["""text"""] if len(__A ) > 0 and not line.isspace()] return tokenizer(examples["""text"""] , padding=__A , truncation=__A , max_length=data_args.max_seq_length ) snake_case : Tuple = datasets.map( __A , batched=__A , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: snake_case : List[str] = add_chinese_references(tokenized_datasets["""train"""] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: snake_case : Optional[int] = add_chinese_references( tokenized_datasets["""validation"""] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer snake_case : int = data_args.train_ref_file or data_args.validation_ref_file if has_ref: snake_case : List[Any] = False # Data collator # This one will take care of randomly masking the tokens. snake_case : Optional[int] = DataCollatorForWholeWordMask(tokenizer=__A , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer snake_case : Optional[Any] = Trainer( model=__A , args=__A , train_dataset=tokenized_datasets["""train"""] if training_args.do_train else None , eval_dataset=tokenized_datasets["""validation"""] if training_args.do_eval else None , tokenizer=__A , data_collator=__A , ) # Training if training_args.do_train: if last_checkpoint is not None: snake_case : Any = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): snake_case : Optional[Any] = model_args.model_name_or_path else: snake_case : Any = None snake_case : Dict = trainer.train(resume_from_checkpoint=__A ) trainer.save_model() # Saves the tokenizer too for easy upload snake_case : Optional[Any] = os.path.join(training_args.output_dir , """train_results.txt""" ) if trainer.is_world_process_zero(): with open(__A , """w""" ) as writer: logger.info("""***** Train results *****""" ) for key, value in sorted(train_result.metrics.items() ): logger.info(f""" {key} = {value}""" ) writer.write(f"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , """trainer_state.json""" ) ) # Evaluation snake_case : List[str] = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) snake_case : Union[str, Any] = trainer.evaluate() snake_case : List[Any] = math.exp(eval_output["""eval_loss"""] ) snake_case : Any = perplexity snake_case : Any = os.path.join(training_args.output_dir , """eval_results_mlm_wwm.txt""" ) if trainer.is_world_process_zero(): with open(__A , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key, value in sorted(results.items() ): logger.info(f""" {key} = {value}""" ) writer.write(f"""{key} = {value}\n""" ) return results def lowercase ( __A : int ) -> Optional[Any]: '''simple docstring''' main() if __name__ == "__main__": main()
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger __lowercase : Union[str, Any] = '''<<<<<<< This should probably be modified because it mentions: ''' __lowercase : Tuple = '''======= >>>>>>> ''' __lowercase : Any = [ '''TextEncoderConfig''', '''ByteTextEncoder''', '''SubwordTextEncoder''', '''encoder_config''', '''maybe_build_from_corpus''', '''manual_dir''', ] __lowercase : List[Any] = [ # (pattern, replacement) # Order is important here for some replacements (r'''tfds\.core''', r'''datasets'''), (r'''tf\.io\.gfile\.GFile''', r'''open'''), (r'''tf\.([\w\d]+)''', r'''datasets.Value(\'\1\')'''), (r'''tfds\.features\.Text\(\)''', r'''datasets.Value(\'string\')'''), (r'''tfds\.features\.Text\(''', r'''datasets.Value(\'string\'),'''), (r'''features\s*=\s*tfds.features.FeaturesDict\(''', r'''features=datasets.Features('''), (r'''tfds\.features\.FeaturesDict\(''', r'''dict('''), (r'''The TensorFlow Datasets Authors''', r'''The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'''), (r'''tfds\.''', r'''datasets.'''), (r'''dl_manager\.manual_dir''', r'''self.config.data_dir'''), (r'''self\.builder_config''', r'''self.config'''), ] def lowercase ( __A : Namespace ) -> int: '''simple docstring''' return ConvertCommand(args.tfds_path , args.datasets_directory ) class _A ( snake_case ): '''simple docstring''' @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = parser.add_parser( """convert""" ,help="""Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.""" ,) train_parser.add_argument( """--tfds_path""" ,type=SCREAMING_SNAKE_CASE_ ,required=SCREAMING_SNAKE_CASE_ ,help="""Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.""" ,) train_parser.add_argument( """--datasets_directory""" ,type=SCREAMING_SNAKE_CASE_ ,required=SCREAMING_SNAKE_CASE_ ,help="""Path to the HuggingFace Datasets folder.""" ) train_parser.set_defaults(func=SCREAMING_SNAKE_CASE_ ) def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,*SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = get_logger("""datasets-cli/converting""" ) snake_case : List[str] = tfds_path snake_case : List[Any] = datasets_directory def snake_case_ ( self ): '''simple docstring''' if os.path.isdir(self._tfds_path ): snake_case : Tuple = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): snake_case : Dict = os.path.dirname(self._tfds_path ) else: raise ValueError("""--tfds_path is neither a directory nor a file. Please check path.""" ) snake_case : Tuple = os.path.abspath(self._datasets_directory ) self._logger.info(F"""Converting datasets from {abs_tfds_path} to {abs_datasets_path}""" ) snake_case : Union[str, Any] = [] snake_case : Optional[int] = [] snake_case : List[str] = {} if os.path.isdir(self._tfds_path ): snake_case : int = os.listdir(SCREAMING_SNAKE_CASE_ ) else: snake_case : List[Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(F"""Looking at file {f_name}""" ) snake_case : List[str] = os.path.join(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if not os.path.isfile(SCREAMING_SNAKE_CASE_ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("""Skipping file""" ) continue with open(SCREAMING_SNAKE_CASE_ ,encoding="""utf-8""" ) as f: snake_case : str = f.readlines() snake_case : List[str] = [] snake_case : List[str] = False snake_case : Union[str, Any] = False snake_case : Optional[int] = [] for line in lines: snake_case : Union[str, Any] = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: snake_case : str = """import datasets\n""" elif "import tensorflow" in out_line: # order is important here snake_case : Dict = """""" continue elif "from absl import logging" in out_line: snake_case : Any = """from datasets import logging\n""" elif "getLogger" in out_line: snake_case : List[str] = out_line.replace("""getLogger""" ,"""get_logger""" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): snake_case : int = True snake_case : List[str] = list(filter(lambda SCREAMING_SNAKE_CASE_ : e in out_line ,SCREAMING_SNAKE_CASE_ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(SCREAMING_SNAKE_CASE_ ) + """\n""" ) out_lines.append(SCREAMING_SNAKE_CASE_ ) out_lines.append(SCREAMING_SNAKE_CASE_ ) continue else: for pattern, replacement in TO_CONVERT: snake_case : List[str] = re.sub(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: snake_case : str = re.match(R"""from\stensorflow_datasets.*import\s([^\.\r\n]+)""" ,SCREAMING_SNAKE_CASE_ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(""",""" ) ) snake_case : List[str] = """from . import """ + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(F"""Error converting {out_line.strip()}""" ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: snake_case : str = True out_lines.append(SCREAMING_SNAKE_CASE_ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset snake_case : List[Any] = f_name.replace(""".py""" ,"""""" ) snake_case : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : str = os.path.join(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) os.makedirs(SCREAMING_SNAKE_CASE_ ,exist_ok=SCREAMING_SNAKE_CASE_ ) self._logger.info(F"""Adding directory {output_dir}""" ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(SCREAMING_SNAKE_CASE_ ) if needs_manual_update: with_manual_update.append(SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ ,"""w""" ,encoding="""utf-8""" ) as f: f.writelines(SCREAMING_SNAKE_CASE_ ) self._logger.info(F"""Converted in {output_file}""" ) for utils_file in utils_files: try: snake_case : int = os.path.basename(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = imports_to_builder_map[f_name.replace(""".py""" ,"""""" )] self._logger.info(F"""Moving {dest_folder} to {utils_file}""" ) shutil.copy(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) except KeyError: self._logger.error(F"""Cannot find destination folder for {utils_file}. Please copy manually.""" ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( F"""You need to manually update file {file_path} to remove configurations using 'TextEncoderConfig'.""" )
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
def lowercase ( __A : Optional[int] , __A : Optional[Any] , __A : Tuple=False ) -> List[str]: '''simple docstring''' if isinstance(__A , __A ) and isinstance(__A , __A ): snake_case : List[Any] = len(set_a.intersection(__A ) ) if alternative_union: snake_case : Any = len(__A ) + len(__A ) else: snake_case : List[str] = len(set_a.union(__A ) ) return intersection / union if isinstance(__A , (list, tuple) ) and isinstance(__A , (list, tuple) ): snake_case : int = [element for element in set_a if element in set_b] if alternative_union: snake_case : Any = len(__A ) + len(__A ) return len(__A ) / union else: snake_case : Any = set_a + [element for element in set_b if element not in set_a] return len(__A ) / len(__A ) return len(__A ) / len(__A ) return None if __name__ == "__main__": __lowercase : str = {'''a''', '''b''', '''c''', '''d''', '''e'''} __lowercase : List[str] = {'''c''', '''d''', '''e''', '''f''', '''h''', '''i'''} print(jaccard_similarity(set_a, set_b))
36
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : str = logging.get_logger(__name__) __lowercase : Union[str, Any] = { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''gpt_neox_japanese''' def __init__( self ,SCREAMING_SNAKE_CASE_=32000 ,SCREAMING_SNAKE_CASE_=2560 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=1.00 ,SCREAMING_SNAKE_CASE_=10000 ,SCREAMING_SNAKE_CASE_=2048 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=31996 ,SCREAMING_SNAKE_CASE_=31999 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.0 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = vocab_size snake_case : Optional[int] = max_position_embeddings snake_case : List[str] = hidden_size snake_case : Union[str, Any] = num_hidden_layers snake_case : Optional[Any] = num_attention_heads snake_case : Tuple = intermediate_multiple_size snake_case : Optional[int] = hidden_act snake_case : List[str] = rotary_pct snake_case : str = rotary_emb_base snake_case : Any = initializer_range snake_case : str = layer_norm_eps snake_case : Optional[Any] = use_cache snake_case : Dict = attention_dropout snake_case : List[str] = hidden_dropout
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
from ..utils import DummyObject, requires_backends class _A ( metaclass=snake_case ): '''simple docstring''' __lowerCamelCase : int = ['''note_seq'''] def __init__( self ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' requires_backends(self ,["""note_seq"""] ) @classmethod def snake_case_ ( cls ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' requires_backends(cls ,["""note_seq"""] ) @classmethod def snake_case_ ( cls ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' requires_backends(cls ,["""note_seq"""] )
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __lowercase : List[str] = {'''configuration_vit_mae''': ['''VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMAEConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[str] = [ '''VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMAEForPreTraining''', '''ViTMAELayer''', '''ViTMAEModel''', '''ViTMAEPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[str] = [ '''TFViTMAEForPreTraining''', '''TFViTMAEModel''', '''TFViTMAEPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_mae import VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMAEConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_mae import ( VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMAEForPreTraining, ViTMAELayer, ViTMAEModel, ViTMAEPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit_mae import TFViTMAEForPreTraining, TFViTMAEModel, TFViTMAEPreTrainedModel else: import sys __lowercase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" 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 - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking 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=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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 ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging __lowercase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__() self.register_modules( vae=SCREAMING_SNAKE_CASE_ ,text_encoder=SCREAMING_SNAKE_CASE_ ,tokenizer=SCREAMING_SNAKE_CASE_ ,unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ,safety_checker=SCREAMING_SNAKE_CASE_ ,feature_extractor=SCREAMING_SNAKE_CASE_ ,) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory snake_case : List[str] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' self.enable_attention_slicing(SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 512 ,SCREAMING_SNAKE_CASE_ = 512 ,SCREAMING_SNAKE_CASE_ = 50 ,SCREAMING_SNAKE_CASE_ = 7.5 ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = 1 ,SCREAMING_SNAKE_CASE_ = 0.0 ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = "pil" ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = 1 ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : List[Any] = 1 elif isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = len(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(SCREAMING_SNAKE_CASE_ )}""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) or callback_steps <= 0) ): raise ValueError( F"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" F""" {type(SCREAMING_SNAKE_CASE_ )}.""" ) # get prompt text embeddings snake_case : Any = self.tokenizer( SCREAMING_SNAKE_CASE_ ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,return_tensors="""pt""" ,) snake_case : int = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: snake_case : str = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" F""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) snake_case : Optional[int] = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: snake_case : List[Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method snake_case , snake_case , snake_case : Optional[int] = text_embeddings.shape snake_case : Any = text_embeddings.repeat(1 ,SCREAMING_SNAKE_CASE_ ,1 ) snake_case : List[str] = text_embeddings.view(bs_embed * num_images_per_prompt ,SCREAMING_SNAKE_CASE_ ,-1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. snake_case : int = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: snake_case : List[str] if negative_prompt is None: snake_case : int = [""""""] elif type(SCREAMING_SNAKE_CASE_ ) is not type(SCREAMING_SNAKE_CASE_ ): raise TypeError( F"""`negative_prompt` should be the same type to `prompt`, but got {type(SCREAMING_SNAKE_CASE_ )} !=""" F""" {type(SCREAMING_SNAKE_CASE_ )}.""" ) elif isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Dict = [negative_prompt] elif batch_size != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( F"""`negative_prompt`: {negative_prompt} has batch size {len(SCREAMING_SNAKE_CASE_ )}, but `prompt`:""" F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" """ the batch size of `prompt`.""" ) else: snake_case : Optional[Any] = negative_prompt snake_case : List[str] = text_input_ids.shape[-1] snake_case : List[str] = self.tokenizer( SCREAMING_SNAKE_CASE_ ,padding="""max_length""" ,max_length=SCREAMING_SNAKE_CASE_ ,truncation=SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ,) snake_case : str = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method snake_case : Optional[Any] = uncond_embeddings.shape[1] snake_case : Union[str, Any] = uncond_embeddings.repeat(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,1 ) snake_case : Any = uncond_embeddings.view(batch_size * num_images_per_prompt ,SCREAMING_SNAKE_CASE_ ,-1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes snake_case : List[str] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. snake_case : Union[str, Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) snake_case : Tuple = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) snake_case : Optional[int] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps snake_case : Optional[int] = torch.randn( SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,device="""cpu""" ,dtype=SCREAMING_SNAKE_CASE_ ).to(self.device ) snake_case : Any = torch.randn(SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,device="""cpu""" ,dtype=SCREAMING_SNAKE_CASE_ ).to( self.device ) else: snake_case : List[Any] = torch.randn( SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,device=self.device ,dtype=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = torch.randn(SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,device=self.device ,dtype=SCREAMING_SNAKE_CASE_ ) else: if latents_reference.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) snake_case : str = latents_reference.to(self.device ) snake_case : Optional[int] = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images snake_case : Optional[Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 snake_case : List[Any] = (latents_shape[2] - latents_shape_reference[2]) // 2 snake_case : List[Any] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx snake_case : Dict = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy snake_case : List[str] = 0 if dx < 0 else dx snake_case : Any = 0 if dy < 0 else dy snake_case : Dict = max(-dx ,0 ) snake_case : Optional[Any] = max(-dy ,0 ) # import pdb # pdb.set_trace() snake_case : Optional[Any] = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand snake_case : Union[str, Any] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler snake_case : List[str] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] snake_case : List[str] = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) snake_case : Optional[Any] = {} if accepts_eta: snake_case : int = eta for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE_ ) ): # expand the latents if we are doing classifier free guidance snake_case : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents snake_case : List[str] = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # predict the noise residual snake_case : str = self.unet(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,encoder_hidden_states=SCREAMING_SNAKE_CASE_ ).sample # perform guidance if do_classifier_free_guidance: snake_case , snake_case : str = noise_pred.chunk(2 ) snake_case : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 snake_case : int = self.scheduler.step(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = 1 / 0.1_82_15 * latents snake_case : Any = self.vae.decode(SCREAMING_SNAKE_CASE_ ).sample snake_case : str = (image / 2 + 0.5).clamp(0 ,1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 snake_case : Optional[int] = image.cpu().permute(0 ,2 ,3 ,1 ).float().numpy() if self.safety_checker is not None: snake_case : Tuple = self.feature_extractor(self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) ,return_tensors="""pt""" ).to( self.device ) snake_case , snake_case : Optional[Any] = self.safety_checker( images=SCREAMING_SNAKE_CASE_ ,clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: snake_case : List[str] = None if output_type == "pil": snake_case : Tuple = self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=SCREAMING_SNAKE_CASE_ ,nsfw_content_detected=SCREAMING_SNAKE_CASE_ )
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
from __future__ import annotations import math import random from typing import Any class _A : '''simple docstring''' def __init__( self ): '''simple docstring''' snake_case : list[Any] = [] snake_case : int = 0 snake_case : int = 0 def snake_case_ ( self ): '''simple docstring''' return self.head == self.tail def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' self.data.append(SCREAMING_SNAKE_CASE_ ) snake_case : int = self.tail + 1 def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.data[self.head] snake_case : int = self.head + 1 return ret def snake_case_ ( self ): '''simple docstring''' return self.tail - self.head def snake_case_ ( self ): '''simple docstring''' print(self.data ) print("""**************""" ) print(self.data[self.head : self.tail] ) class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Union[str, Any] = data snake_case : MyNode | None = None snake_case : MyNode | None = None snake_case : int = 1 def snake_case_ ( self ): '''simple docstring''' return self.data def snake_case_ ( self ): '''simple docstring''' return self.left def snake_case_ ( self ): '''simple docstring''' return self.right def snake_case_ ( self ): '''simple docstring''' return self.height def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = data def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[Any] = node def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[Any] = node def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = height def lowercase ( __A : MyNode | None ) -> int: '''simple docstring''' if node is None: return 0 return node.get_height() def lowercase ( __A : int , __A : int ) -> int: '''simple docstring''' if a > b: return a return b def lowercase ( __A : MyNode ) -> MyNode: '''simple docstring''' print("""left rotation node:""" , node.get_data() ) snake_case : Optional[int] = node.get_left() assert ret is not None node.set_left(ret.get_right() ) ret.set_right(__A ) snake_case : List[str] = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(__A ) snake_case : Dict = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(__A ) return ret def lowercase ( __A : MyNode ) -> MyNode: '''simple docstring''' print("""right rotation node:""" , node.get_data() ) snake_case : Tuple = node.get_right() assert ret is not None node.set_right(ret.get_left() ) ret.set_left(__A ) snake_case : str = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(__A ) snake_case : str = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(__A ) return ret def lowercase ( __A : MyNode ) -> MyNode: '''simple docstring''' snake_case : Optional[Any] = node.get_left() assert left_child is not None node.set_left(left_rotation(__A ) ) return right_rotation(__A ) def lowercase ( __A : MyNode ) -> MyNode: '''simple docstring''' snake_case : Dict = node.get_right() assert right_child is not None node.set_right(right_rotation(__A ) ) return left_rotation(__A ) def lowercase ( __A : MyNode | None , __A : Any ) -> MyNode | None: '''simple docstring''' if node is None: return MyNode(__A ) if data < node.get_data(): node.set_left(insert_node(node.get_left() , __A ) ) if ( get_height(node.get_left() ) - get_height(node.get_right() ) == 2 ): # an unbalance detected snake_case : Any = node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child snake_case : Optional[Any] = right_rotation(__A ) else: snake_case : List[str] = lr_rotation(__A ) else: node.set_right(insert_node(node.get_right() , __A ) ) if get_height(node.get_right() ) - get_height(node.get_left() ) == 2: snake_case : Tuple = node.get_right() assert right_child is not None if data < right_child.get_data(): snake_case : int = rl_rotation(__A ) else: snake_case : Tuple = left_rotation(__A ) snake_case : str = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(__A ) return node def lowercase ( __A : MyNode ) -> Any: '''simple docstring''' while True: snake_case : Dict = root.get_right() if right_child is None: break snake_case : int = right_child return root.get_data() def lowercase ( __A : MyNode ) -> Any: '''simple docstring''' while True: snake_case : Dict = root.get_left() if left_child is None: break snake_case : Optional[Any] = left_child return root.get_data() def lowercase ( __A : MyNode , __A : Any ) -> MyNode | None: '''simple docstring''' snake_case : Union[str, Any] = root.get_left() snake_case : Optional[Any] = root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: snake_case : List[Any] = get_left_most(__A ) root.set_data(__A ) root.set_right(del_node(__A , __A ) ) elif left_child is not None: snake_case : int = left_child elif right_child is not None: snake_case : Union[str, Any] = right_child else: return None elif root.get_data() > data: if left_child is None: print("""No such data""" ) return root else: root.set_left(del_node(__A , __A ) ) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(__A , __A ) ) if get_height(__A ) - get_height(__A ) == 2: assert right_child is not None if get_height(right_child.get_right() ) > get_height(right_child.get_left() ): snake_case : List[str] = left_rotation(__A ) else: snake_case : Optional[int] = rl_rotation(__A ) elif get_height(__A ) - get_height(__A ) == -2: assert left_child is not None if get_height(left_child.get_left() ) > get_height(left_child.get_right() ): snake_case : Any = right_rotation(__A ) else: snake_case : str = lr_rotation(__A ) snake_case : Dict = my_max(get_height(root.get_right() ) , get_height(root.get_left() ) ) + 1 root.set_height(__A ) return root class _A : '''simple docstring''' def __init__( self ): '''simple docstring''' snake_case : MyNode | None = None def snake_case_ ( self ): '''simple docstring''' return get_height(self.root ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' print("""insert:""" + str(SCREAMING_SNAKE_CASE_ ) ) snake_case : int = insert_node(self.root ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' print("""delete:""" + str(SCREAMING_SNAKE_CASE_ ) ) if self.root is None: print("""Tree is empty!""" ) return snake_case : List[Any] = del_node(self.root ,SCREAMING_SNAKE_CASE_ ) def __str__( self ,): # a level traversale, gives a more intuitive look on the tree '''simple docstring''' snake_case : Optional[Any] = """""" snake_case : Union[str, Any] = MyQueue() q.push(self.root ) snake_case : Optional[int] = self.get_height() if layer == 0: return output snake_case : List[str] = 0 while not q.is_empty(): snake_case : Dict = q.pop() snake_case : Dict = """ """ * int(math.pow(2 ,layer - 1 ) ) output += space if node is None: output += "*" q.push(SCREAMING_SNAKE_CASE_ ) q.push(SCREAMING_SNAKE_CASE_ ) else: output += str(node.get_data() ) q.push(node.get_left() ) q.push(node.get_right() ) output += space snake_case : Tuple = cnt + 1 for i in range(100 ): if cnt == math.pow(2 ,SCREAMING_SNAKE_CASE_ ) - 1: snake_case : int = layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def lowercase ( ) -> None: '''simple docstring''' import doctest doctest.testmod() if __name__ == "__main__": _test() __lowercase : str = AVLtree() __lowercase : Union[str, Any] = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
def lowercase ( __A : list ) -> list: '''simple docstring''' if len(__A ) <= 1: return lst snake_case : List[Any] = 1 while i < len(__A ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case , snake_case : Tuple = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case : int = 1 return lst if __name__ == "__main__": __lowercase : Dict = input('''Enter numbers separated by a comma:\n''').strip() __lowercase : Dict = [int(item) for item in user_input.split(''',''')] print(gnome_sort(unsorted))
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __lowercase : str = False class _A ( unittest.TestCase ): '''simple docstring''' pass @slow @require_torch_gpu class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = VersatileDiffusionImageVariationPipeline.from_pretrained("""shi-labs/versatile-diffusion""" ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) snake_case : int = torch.manual_seed(0 ) snake_case : List[Any] = pipe( image=SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=7.5 ,num_inference_steps=50 ,output_type="""numpy""" ,).images snake_case : Optional[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) snake_case : List[str] = np.array([0.04_41, 0.04_69, 0.05_07, 0.05_75, 0.06_32, 0.06_50, 0.08_65, 0.09_09, 0.09_45] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowercase : Dict = logging.get_logger(__name__) __lowercase : Tuple = { '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''', # See all Nat models at https://huggingface.co/models?filter=nat } class _A ( snake_case , snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = '''nat''' __lowerCamelCase : List[Any] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=[3, 4, 6, 5] ,SCREAMING_SNAKE_CASE_=[2, 4, 8, 16] ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=3.0 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = patch_size snake_case : Union[str, Any] = num_channels snake_case : Dict = embed_dim snake_case : List[str] = depths snake_case : int = len(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = num_heads snake_case : str = kernel_size snake_case : Dict = mlp_ratio snake_case : Any = qkv_bias snake_case : str = hidden_dropout_prob snake_case : str = attention_probs_dropout_prob snake_case : str = drop_path_rate snake_case : Any = hidden_act snake_case : Tuple = layer_norm_eps snake_case : List[Any] = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model snake_case : List[Any] = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE_ ) - 1) ) snake_case : Any = layer_scale_init_value snake_case : int = ["""stem"""] + [F"""stage{idx}""" for idx in range(1 ,len(SCREAMING_SNAKE_CASE_ ) + 1 )] snake_case , snake_case : Union[str, Any] = get_aligned_output_features_output_indices( out_features=SCREAMING_SNAKE_CASE_ ,out_indices=SCREAMING_SNAKE_CASE_ ,stage_names=self.stage_names )
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
import os import pytest from transformers.dynamic_module_utils import get_imports __lowercase : int = ''' import os ''' __lowercase : Optional[Any] = ''' def foo(): import os return False ''' __lowercase : Optional[int] = ''' def foo(): def bar(): if True: import os return False return bar() ''' __lowercase : List[str] = ''' import os try: import bar except ImportError: raise ValueError() ''' __lowercase : Any = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' __lowercase : str = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' __lowercase : Optional[int] = ''' import os try: import bar except ImportError as e: raise ValueError() ''' __lowercase : Optional[int] = ''' import os try: import bar except: raise ValueError() ''' __lowercase : List[Any] = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' __lowercase : Optional[Any] = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' __lowercase : Tuple = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize("""case""" , __A ) def lowercase ( __A : int , __A : int ) -> List[Any]: '''simple docstring''' snake_case : List[Any] = os.path.join(__A , """test_file.py""" ) with open(__A , """w""" ) as _tmp_file: _tmp_file.write(__A ) snake_case : Dict = get_imports(__A ) assert parsed_imports == ["os"]
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def lowercase ( __A : Union[str, Any] ) -> Any: '''simple docstring''' if "cls_token" in name: snake_case : List[str] = name.replace("""cls_token""" , """vit.embeddings.cls_token""" ) if "mask_token" in name: snake_case : Dict = name.replace("""mask_token""" , """decoder.mask_token""" ) if "decoder_pos_embed" in name: snake_case : List[str] = name.replace("""decoder_pos_embed""" , """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: snake_case : Optional[Any] = name.replace("""pos_embed""" , """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: snake_case : Any = name.replace("""patch_embed.proj""" , """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: snake_case : Optional[int] = name.replace("""patch_embed.norm""" , """vit.embeddings.norm""" ) if "decoder_blocks" in name: snake_case : Optional[Any] = name.replace("""decoder_blocks""" , """decoder.decoder_layers""" ) if "blocks" in name: snake_case : int = name.replace("""blocks""" , """vit.encoder.layer""" ) if "attn.proj" in name: snake_case : Tuple = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: snake_case : Tuple = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: snake_case : Tuple = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: snake_case : List[str] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: snake_case : Dict = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: snake_case : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if "decoder_embed" in name: snake_case : Union[str, Any] = name.replace("""decoder_embed""" , """decoder.decoder_embed""" ) if "decoder_norm" in name: snake_case : Tuple = name.replace("""decoder_norm""" , """decoder.decoder_norm""" ) if "decoder_pred" in name: snake_case : Optional[Any] = name.replace("""decoder_pred""" , """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: snake_case : List[Any] = name.replace("""norm.weight""" , """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: snake_case : Optional[Any] = name.replace("""norm.bias""" , """vit.layernorm.bias""" ) return name def lowercase ( __A : Tuple , __A : Optional[int] ) -> Tuple: '''simple docstring''' for key in orig_state_dict.copy().keys(): snake_case : Any = orig_state_dict.pop(__A ) if "qkv" in key: snake_case : List[Any] = key.split(""".""" ) snake_case : int = int(key_split[1] ) if "decoder_blocks" in key: snake_case : int = config.decoder_hidden_size snake_case : Union[str, Any] = """decoder.decoder_layers.""" if "weight" in key: snake_case : Optional[Any] = val[:dim, :] snake_case : Tuple = val[dim : dim * 2, :] snake_case : Optional[int] = val[-dim:, :] elif "bias" in key: snake_case : Union[str, Any] = val[:dim] snake_case : int = val[dim : dim * 2] snake_case : Optional[Any] = val[-dim:] else: snake_case : List[str] = config.hidden_size snake_case : List[str] = """vit.encoder.layer.""" if "weight" in key: snake_case : Any = val[:dim, :] snake_case : int = val[dim : dim * 2, :] snake_case : Union[str, Any] = val[-dim:, :] elif "bias" in key: snake_case : Optional[Any] = val[:dim] snake_case : int = val[dim : dim * 2] snake_case : Optional[int] = val[-dim:] else: snake_case : Optional[Any] = val return orig_state_dict def lowercase ( __A : Tuple , __A : str ) -> List[Any]: '''simple docstring''' snake_case : Optional[int] = ViTMAEConfig() if "large" in checkpoint_url: snake_case : List[str] = 1024 snake_case : Optional[int] = 4096 snake_case : Optional[int] = 24 snake_case : Tuple = 16 elif "huge" in checkpoint_url: snake_case : Dict = 14 snake_case : int = 1280 snake_case : Dict = 5120 snake_case : List[str] = 32 snake_case : Optional[Any] = 16 snake_case : str = ViTMAEForPreTraining(__A ) snake_case : Optional[int] = torch.hub.load_state_dict_from_url(__A , map_location="""cpu""" )["""model"""] snake_case : Any = ViTMAEImageProcessor(size=config.image_size ) snake_case : Tuple = convert_state_dict(__A , __A ) model.load_state_dict(__A ) model.eval() snake_case : Tuple = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" snake_case : Union[str, Any] = Image.open(requests.get(__A , stream=__A ).raw ) snake_case : Dict = ViTMAEImageProcessor(size=config.image_size ) snake_case : str = image_processor(images=__A , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) snake_case : List[str] = model(**__A ) snake_case : str = outputs.logits if "large" in checkpoint_url: snake_case : str = torch.tensor( [[-0.7_309, -0.7_128, -1.0_169], [-1.0_161, -0.9_058, -1.1_878], [-1.0_478, -0.9_411, -1.1_911]] ) elif "huge" in checkpoint_url: snake_case : List[Any] = torch.tensor( [[-1.1_599, -0.9_199, -1.2_221], [-1.1_952, -0.9_269, -1.2_307], [-1.2_143, -0.9_337, -1.2_262]] ) else: snake_case : Optional[int] = torch.tensor( [[-0.9_192, -0.8_481, -1.1_259], [-1.1_349, -1.0_034, -1.2_599], [-1.1_757, -1.0_429, -1.2_726]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , __A , atol=1E-4 ) print(f"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__A ) if __name__ == "__main__": __lowercase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) __lowercase : Optional[Any] = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
def lowercase ( __A : float , __A : float , __A : float , __A : float , __A : float , ) -> float: '''simple docstring''' snake_case : Any = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("""All input parameters must be positive""" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("""Relative densities cannot be greater than one""" ) else: snake_case : List[Any] = 1 - (matter_density + radiation_density + dark_energy) snake_case : Tuple = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) snake_case : Tuple = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation __lowercase : Tuple = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1E-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Any = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) snake_case : Optional[int] = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) snake_case : Any = UNetaDConditionModel( sample_size=32 ,layers_per_block=1 ,block_out_channels=[32, 64] ,down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] ,mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" ,up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] ,in_channels=3 ,out_channels=6 ,cross_attention_dim=32 ,encoder_hid_dim=32 ,attention_head_dim=8 ,addition_embed_type="""text""" ,addition_embed_type_num_heads=2 ,cross_attention_norm="""group_norm""" ,resnet_time_scale_shift="""scale_shift""" ,act_fn="""gelu""" ,) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) snake_case : Dict = DDPMScheduler( num_train_timesteps=1000 ,beta_schedule="""squaredcos_cap_v2""" ,beta_start=0.00_01 ,beta_end=0.02 ,thresholding=SCREAMING_SNAKE_CASE_ ,dynamic_thresholding_ratio=0.95 ,sample_max_value=1.0 ,prediction_type="""epsilon""" ,variance_type="""learned_range""" ,) torch.manual_seed(0 ) snake_case : Tuple = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Optional[int] = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) snake_case : Union[str, Any] = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) snake_case : int = UNetaDConditionModel( sample_size=32 ,layers_per_block=[1, 2] ,block_out_channels=[32, 64] ,down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] ,mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" ,up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] ,in_channels=6 ,out_channels=6 ,cross_attention_dim=32 ,encoder_hid_dim=32 ,attention_head_dim=8 ,addition_embed_type="""text""" ,addition_embed_type_num_heads=2 ,cross_attention_norm="""group_norm""" ,resnet_time_scale_shift="""scale_shift""" ,act_fn="""gelu""" ,class_embed_type="""timestep""" ,mid_block_scale_factor=1.4_14 ,time_embedding_act_fn="""gelu""" ,time_embedding_dim=32 ,) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) snake_case : List[Any] = DDPMScheduler( num_train_timesteps=1000 ,beta_schedule="""squaredcos_cap_v2""" ,beta_start=0.00_01 ,beta_end=0.02 ,thresholding=SCREAMING_SNAKE_CASE_ ,dynamic_thresholding_ratio=0.95 ,sample_max_value=1.0 ,prediction_type="""epsilon""" ,variance_type="""learned_range""" ,) torch.manual_seed(0 ) snake_case : List[Any] = DDPMScheduler( num_train_timesteps=1000 ,beta_schedule="""squaredcos_cap_v2""" ,beta_start=0.00_01 ,beta_end=0.02 ,) torch.manual_seed(0 ) snake_case : int = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.get_dummy_components() snake_case : Any = self.pipeline_class(**SCREAMING_SNAKE_CASE_ ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : int = inputs["""prompt"""] snake_case : Dict = inputs["""generator"""] snake_case : Optional[int] = inputs["""num_inference_steps"""] snake_case : Any = inputs["""output_type"""] if "image" in inputs: snake_case : Any = inputs["""image"""] else: snake_case : Tuple = None if "mask_image" in inputs: snake_case : Any = inputs["""mask_image"""] else: snake_case : List[Any] = None if "original_image" in inputs: snake_case : str = inputs["""original_image"""] else: snake_case : int = None snake_case , snake_case : List[str] = pipe.encode_prompt(SCREAMING_SNAKE_CASE_ ) # inputs with prompt converted to embeddings snake_case : Union[str, Any] = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: snake_case : Any = image if mask_image is not None: snake_case : List[str] = mask_image if original_image is not None: snake_case : List[Any] = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = pipe(**SCREAMING_SNAKE_CASE_ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : str = self.pipeline_class.from_pretrained(SCREAMING_SNAKE_CASE_ ) pipe_loaded.to(SCREAMING_SNAKE_CASE_ ) pipe_loaded.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) is None ,F"""`{optional_component}` did not stay set to None after loading.""" ,) snake_case : Dict = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = inputs["""generator"""] snake_case : Tuple = inputs["""num_inference_steps"""] snake_case : Optional[Any] = inputs["""output_type"""] # inputs with prompt converted to embeddings snake_case : List[Any] = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: snake_case : Dict = image if mask_image is not None: snake_case : List[str] = mask_image if original_image is not None: snake_case : List[Any] = original_image snake_case : List[str] = pipe_loaded(**SCREAMING_SNAKE_CASE_ )[0] snake_case : Optional[int] = np.abs(to_np(SCREAMING_SNAKE_CASE_ ) - to_np(SCREAMING_SNAKE_CASE_ ) ).max() self.assertLess(SCREAMING_SNAKE_CASE_ ,1E-4 ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.get_dummy_components() snake_case : Optional[Any] = self.pipeline_class(**SCREAMING_SNAKE_CASE_ ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : int = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : int = pipe(**SCREAMING_SNAKE_CASE_ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = self.pipeline_class.from_pretrained(SCREAMING_SNAKE_CASE_ ) pipe_loaded.to(SCREAMING_SNAKE_CASE_ ) pipe_loaded.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests snake_case : List[str] = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = pipe_loaded(**SCREAMING_SNAKE_CASE_ )[0] snake_case : Optional[Any] = np.abs(to_np(SCREAMING_SNAKE_CASE_ ) - to_np(SCREAMING_SNAKE_CASE_ ) ).max() self.assertLess(SCREAMING_SNAKE_CASE_ ,1E-4 )
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class _A ( snake_case , snake_case ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = 1 @register_to_config def __init__( self ,SCREAMING_SNAKE_CASE_=2000 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=20 ,SCREAMING_SNAKE_CASE_=1E-3 ): '''simple docstring''' snake_case : Tuple = None snake_case : List[Any] = None snake_case : List[str] = None def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : int = torch.linspace(1 ,self.config.sampling_eps ,SCREAMING_SNAKE_CASE_ ,device=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score snake_case : List[str] = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) snake_case : Any = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) snake_case : Tuple = std.flatten() while len(std.shape ) < len(score.shape ): snake_case : Tuple = std.unsqueeze(-1 ) snake_case : Union[str, Any] = -score / std # compute snake_case : List[str] = -1.0 / len(self.timesteps ) snake_case : Tuple = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) snake_case : Any = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): snake_case : Optional[Any] = beta_t.unsqueeze(-1 ) snake_case : Optional[int] = -0.5 * beta_t * x snake_case : str = torch.sqrt(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = drift - diffusion**2 * score snake_case : int = x + drift * dt # add noise snake_case : Tuple = randn_tensor(x.shape ,layout=x.layout ,generator=SCREAMING_SNAKE_CASE_ ,device=x.device ,dtype=x.dtype ) snake_case : str = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_lxmert import LxmertTokenizer __lowercase : Dict = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} __lowercase : Dict = { '''vocab_file''': { '''unc-nlp/lxmert-base-uncased''': '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt''', }, '''tokenizer_file''': { '''unc-nlp/lxmert-base-uncased''': ( '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json''' ), }, } __lowercase : List[str] = { '''unc-nlp/lxmert-base-uncased''': 512, } __lowercase : List[Any] = { '''unc-nlp/lxmert-base-uncased''': {'''do_lower_case''': True}, } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = VOCAB_FILES_NAMES __lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : List[str] = PRETRAINED_INIT_CONFIGURATION __lowerCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : str = LxmertTokenizer def __init__( self ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="[UNK]" ,SCREAMING_SNAKE_CASE_="[SEP]" ,SCREAMING_SNAKE_CASE_="[PAD]" ,SCREAMING_SNAKE_CASE_="[CLS]" ,SCREAMING_SNAKE_CASE_="[MASK]" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__( SCREAMING_SNAKE_CASE_ ,tokenizer_file=SCREAMING_SNAKE_CASE_ ,do_lower_case=SCREAMING_SNAKE_CASE_ ,unk_token=SCREAMING_SNAKE_CASE_ ,sep_token=SCREAMING_SNAKE_CASE_ ,pad_token=SCREAMING_SNAKE_CASE_ ,cls_token=SCREAMING_SNAKE_CASE_ ,mask_token=SCREAMING_SNAKE_CASE_ ,tokenize_chinese_chars=SCREAMING_SNAKE_CASE_ ,strip_accents=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) snake_case : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" ,SCREAMING_SNAKE_CASE_ ) != do_lower_case or normalizer_state.get("""strip_accents""" ,SCREAMING_SNAKE_CASE_ ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" ,SCREAMING_SNAKE_CASE_ ) != tokenize_chinese_chars ): snake_case : Union[str, Any] = getattr(SCREAMING_SNAKE_CASE_ ,normalizer_state.pop("""type""" ) ) snake_case : List[Any] = do_lower_case snake_case : List[Any] = strip_accents snake_case : str = tokenize_chinese_chars snake_case : int = normalizer_class(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = do_lower_case def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' snake_case : List[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Optional[Any] = [self.sep_token_id] snake_case : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = self._tokenizer.model.save(SCREAMING_SNAKE_CASE_ ,name=SCREAMING_SNAKE_CASE_ ) return tuple(SCREAMING_SNAKE_CASE_ )
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _A ( unittest.TestCase ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=18 ,SCREAMING_SNAKE_CASE_=30 ,SCREAMING_SNAKE_CASE_=400 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=True ,): '''simple docstring''' snake_case : List[str] = size if size is not None else {"""height""": 18, """width""": 18} snake_case : int = parent snake_case : str = batch_size snake_case : Optional[int] = num_channels snake_case : Optional[Any] = image_size snake_case : Union[str, Any] = min_resolution snake_case : Optional[int] = max_resolution snake_case : int = do_resize snake_case : Optional[int] = size snake_case : Tuple = apply_ocr def snake_case_ ( self ): '''simple docstring''' return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class _A ( snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = LayoutLMvaImageProcessingTester(self ) @property def snake_case_ ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,"""do_resize""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,"""size""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,"""apply_ocr""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 18, """width""": 18} ) snake_case : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{"""height""": 42, """width""": 42} ) def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' # Initialize image_processing snake_case : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case : Tuple = prepare_image_inputs(self.image_processor_tester ,equal_resolution=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,Image.Image ) # Test not batched input snake_case : Tuple = image_processing(image_inputs[0] ,return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) self.assertIsInstance(encoding.words ,SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(encoding.boxes ,SCREAMING_SNAKE_CASE_ ) # Test batched snake_case : Union[str, Any] = image_processing(SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def snake_case_ ( self ): '''simple docstring''' # Initialize image_processing snake_case : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=SCREAMING_SNAKE_CASE_ ,numpify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,np.ndarray ) # Test not batched input snake_case : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case : List[Any] = image_processing(SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def snake_case_ ( self ): '''simple docstring''' # Initialize image_processing snake_case : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=SCREAMING_SNAKE_CASE_ ,torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,torch.Tensor ) # Test not batched input snake_case : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case : int = image_processing(SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def snake_case_ ( self ): '''simple docstring''' # with apply_OCR = True snake_case : Union[str, Any] = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case : int = load_dataset("""hf-internal-testing/fixtures_docvqa""" ,split="""test""" ) snake_case : Any = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) snake_case : Optional[int] = image_processing(SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case : Optional[Any] = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 snake_case : List[Any] = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,SCREAMING_SNAKE_CASE_ ) self.assertListEqual(encoding.boxes ,SCREAMING_SNAKE_CASE_ ) # with apply_OCR = False snake_case : Union[str, Any] = LayoutLMvaImageProcessor(apply_ocr=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = image_processing(SCREAMING_SNAKE_CASE_ ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
from __future__ import annotations def lowercase ( __A : list[list[int]] ) -> bool: '''simple docstring''' snake_case : Dict = len(__A ) # We need to create solution object to save path. snake_case : Union[str, Any] = [[0 for _ in range(__A )] for _ in range(__A )] snake_case : str = run_maze(__A , 0 , 0 , __A ) if solved: print("""\n""".join(str(__A ) for row in solutions ) ) else: print("""No solution exists!""" ) return solved def lowercase ( __A : list[list[int]] , __A : int , __A : int , __A : list[list[int]] ) -> bool: '''simple docstring''' snake_case : Any = len(__A ) # Final check point. if i == j == (size - 1): snake_case : str = 1 return True snake_case : str = (not i < 0) and (not j < 0) # Check lower bounds snake_case : Union[str, Any] = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. snake_case : Optional[int] = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited snake_case : List[str] = 1 # check for directions if ( run_maze(__A , i + 1 , __A , __A ) or run_maze(__A , __A , j + 1 , __A ) or run_maze(__A , i - 1 , __A , __A ) or run_maze(__A , __A , j - 1 , __A ) ): return True snake_case : Optional[int] = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _A : '''simple docstring''' @staticmethod def snake_case_ ( *SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass @is_pipeline_test @require_vision @require_timm @require_torch class _A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Tuple = MODEL_FOR_OBJECT_DETECTION_MAPPING def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_ ,image_processor=SCREAMING_SNAKE_CASE_ ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ,threshold=0.0 ) self.assertGreater(len(SCREAMING_SNAKE_CASE_ ) ,0 ) for detected_object in outputs: self.assertEqual( SCREAMING_SNAKE_CASE_ ,{ """score""": ANY(SCREAMING_SNAKE_CASE_ ), """label""": ANY(SCREAMING_SNAKE_CASE_ ), """box""": {"""xmin""": ANY(SCREAMING_SNAKE_CASE_ ), """ymin""": ANY(SCREAMING_SNAKE_CASE_ ), """xmax""": ANY(SCREAMING_SNAKE_CASE_ ), """ymax""": ANY(SCREAMING_SNAKE_CASE_ )}, } ,) import datasets snake_case : Any = datasets.load_dataset("""hf-internal-testing/fixtures_image_utils""" ,"""image""" ,split="""test""" ) snake_case : Any = [ Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ), """http://images.cocodataset.org/val2017/000000039769.jpg""", # RGBA dataset[0]["""file"""], # LA dataset[1]["""file"""], # L dataset[2]["""file"""], ] snake_case : Union[str, Any] = object_detector(SCREAMING_SNAKE_CASE_ ,threshold=0.0 ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(SCREAMING_SNAKE_CASE_ ) ) for outputs in batch_outputs: self.assertGreater(len(SCREAMING_SNAKE_CASE_ ) ,0 ) for detected_object in outputs: self.assertEqual( SCREAMING_SNAKE_CASE_ ,{ """score""": ANY(SCREAMING_SNAKE_CASE_ ), """label""": ANY(SCREAMING_SNAKE_CASE_ ), """box""": {"""xmin""": ANY(SCREAMING_SNAKE_CASE_ ), """ymin""": ANY(SCREAMING_SNAKE_CASE_ ), """xmax""": ANY(SCREAMING_SNAKE_CASE_ ), """ymax""": ANY(SCREAMING_SNAKE_CASE_ )}, } ,) @require_tf @unittest.skip("""Object detection not implemented in TF""" ) def snake_case_ ( self ): '''simple docstring''' pass @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = """hf-internal-testing/tiny-detr-mobilenetsv3""" snake_case : Optional[int] = AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : str = ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_ ,feature_extractor=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ,threshold=0.0 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ] ,) snake_case : Optional[Any] = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ,threshold=0.0 ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ [ {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ], [ {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}}, ], ] ,) @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = """facebook/detr-resnet-50""" snake_case : Union[str, Any] = AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Any = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_ ,feature_extractor=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) snake_case : str = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ [ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], [ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], ] ,) @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : str = """facebook/detr-resnet-50""" snake_case : str = pipeline("""object-detection""" ,model=SCREAMING_SNAKE_CASE_ ) snake_case : Any = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) snake_case : List[Any] = object_detector( [ """http://images.cocodataset.org/val2017/000000039769.jpg""", """http://images.cocodataset.org/val2017/000000039769.jpg""", ] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ [ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], [ {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}}, {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}}, {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}}, {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ], ] ,) @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = 0.99_85 snake_case : List[Any] = """facebook/detr-resnet-50""" snake_case : Any = pipeline("""object-detection""" ,model=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg""" ,threshold=SCREAMING_SNAKE_CASE_ ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}}, {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}}, ] ,) @require_torch @require_pytesseract @slow def snake_case_ ( self ): '''simple docstring''' snake_case : int = """Narsil/layoutlmv3-finetuned-funsd""" snake_case : Dict = 0.99_93 snake_case : Any = pipeline("""object-detection""" ,model=SCREAMING_SNAKE_CASE_ ,threshold=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = object_detector( """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png""" ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.99_93, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}}, {"""score""": 0.99_93, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}}, ] ,)
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
from collections import defaultdict def lowercase ( __A : int ) -> int: '''simple docstring''' snake_case : Union[str, Any] = 1 snake_case : str = True for v in tree[start]: if v not in visited: ret += dfs(__A ) if ret % 2 == 0: cuts.append(__A ) return ret def lowercase ( ) -> Union[str, Any]: '''simple docstring''' dfs(1 ) if __name__ == "__main__": __lowercase , __lowercase : Union[str, Any] = 10, 9 __lowercase : Dict = defaultdict(list) __lowercase : dict[int, bool] = {} __lowercase : list[int] = [] __lowercase : Optional[Any] = 0 __lowercase : Optional[Any] = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
from __future__ import annotations __lowercase : Tuple = 1.6021E-19 # units = C def lowercase ( __A : float , __A : float , __A : float , ) -> tuple[str, float]: '''simple docstring''' if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError("""You cannot supply more or less than 2 values""" ) elif conductivity < 0: raise ValueError("""Conductivity cannot be negative""" ) elif electron_conc < 0: raise ValueError("""Electron concentration cannot be negative""" ) elif mobility < 0: raise ValueError("""mobility cannot be negative""" ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
36
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
def lowercase ( __A : int = 10 ) -> str: '''simple docstring''' if not isinstance(__A , __A ) or n < 0: raise ValueError("""Invalid input""" ) snake_case : Dict = 10**n snake_case : Optional[int] = 2_8433 * (pow(2 , 783_0457 , __A )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f'''{solution(10) = }''')
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import os import numpy import onnx def lowercase ( __A : str , __A : List[Any] ) -> Union[str, Any]: '''simple docstring''' snake_case : Any = a.name snake_case : int = b.name snake_case : Tuple = """""" snake_case : Any = """""" snake_case : Optional[int] = a == b snake_case : Any = name_a snake_case : str = name_b return res def lowercase ( __A : Optional[int] , __A : List[Any] , __A : List[Any] ) -> int: '''simple docstring''' for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(__A , __A ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , __A , __A ) _graph_replace_input_with(node_proto.attribute[1].g , __A , __A ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , __A , __A ) def lowercase ( __A : Tuple , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for n in graph_proto.node: _node_replace_input_with(__A , __A , __A ) def lowercase ( __A : Dict , __A : Any , __A : Union[str, Any] ) -> Dict: '''simple docstring''' snake_case : Dict = list(model.graph.initializer ) snake_case : Optional[Any] = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i snake_case : Optional[int] = inits[i].name snake_case : str = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , __A , __A ) def lowercase ( __A : Tuple ) -> List[Any]: '''simple docstring''' snake_case : List[Any] = os.path.dirname(__A ) snake_case : Union[str, Any] = os.path.basename(__A ) snake_case : Dict = onnx.load(os.path.join(__A , __A ) ) snake_case : Optional[Any] = list(model.graph.initializer ) snake_case : Optional[Any] = set() snake_case : Optional[int] = {} snake_case : Optional[int] = [] snake_case : List[str] = 0 for i in range(len(__A ) ): if i in dup_set: continue for j in range(i + 1 , len(__A ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(__A ) dup_set.add(__A ) snake_case : Optional[Any] = inits[j].data_type snake_case : Any = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , __A ) total_reduced_size += mem_size snake_case : Tuple = inits[i].name snake_case : Optional[Any] = inits[j].name if name_i in dup_map: dup_map[name_i].append(__A ) else: snake_case : int = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1024 / 1024 / 1024 , """GB""" ) snake_case : Union[str, Any] = sorted(__A ) _remove_dup_initializers_from_model(__A , __A , __A ) snake_case : List[str] = """optimized_""" + model_file_name snake_case : List[Any] = os.path.join(__A , __A ) onnx.save(__A , __A ) return new_model
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
def lowercase ( __A : int = 100 ) -> int: '''simple docstring''' snake_case : Dict = set() snake_case : Optional[Any] = 0 snake_case : List[str] = n + 1 # maximum limit for a in range(2 , __A ): for b in range(2 , __A ): snake_case : List[Any] = a**b # calculates the current power collect_powers.add(__A ) # adds the result to the set return len(__A ) if __name__ == "__main__": print('''Number of terms ''', solution(int(str(input()).strip())))
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" 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 - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking 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=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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 ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
__lowercase : Optional[Any] = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] __lowercase : Union[str, Any] = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] __lowercase : Any = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] __lowercase : Union[str, Any] = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] __lowercase : Optional[int] = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] __lowercase : Union[str, Any] = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] __lowercase : List[Any] = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] __lowercase : Optional[int] = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
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, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __lowercase : Optional[Any] = logging.get_logger(__name__) def lowercase ( __A : List[str] ) -> List[List[ImageInput]]: '''simple docstring''' if isinstance(__A , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(__A , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(__A ): return [[videos]] raise ValueError(f"""Could not make batched video from {videos}""" ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} snake_case : Optional[int] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[int] = do_resize snake_case : Optional[Any] = size snake_case : Optional[Any] = do_center_crop snake_case : int = crop_size snake_case : str = resample snake_case : Optional[int] = do_rescale snake_case : List[Any] = rescale_factor snake_case : Tuple = do_normalize snake_case : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN snake_case : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : int = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" in size: snake_case : List[str] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) elif "height" in size and "width" in size: snake_case : Optional[int] = (size["""height"""], size["""width"""]) else: raise ValueError(F"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Dict = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""Size must have 'height' and 'width' as keys. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return normalize(SCREAMING_SNAKE_CASE_ ,mean=SCREAMING_SNAKE_CASE_ ,std=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,): '''simple docstring''' if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_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.""" ) # All transformations expect numpy arrays. snake_case : int = to_numpy_array(SCREAMING_SNAKE_CASE_ ) if do_resize: snake_case : Any = self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) if do_center_crop: snake_case : List[Any] = self.center_crop(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) if do_rescale: snake_case : int = self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) if do_normalize: snake_case : Tuple = self.normalize(image=SCREAMING_SNAKE_CASE_ ,mean=SCREAMING_SNAKE_CASE_ ,std=SCREAMING_SNAKE_CASE_ ) snake_case : int = to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) return image def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : int = do_resize if do_resize is not None else self.do_resize snake_case : Union[str, Any] = resample if resample is not None else self.resample snake_case : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : List[Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : List[Any] = do_normalize if do_normalize is not None else self.do_normalize snake_case : Any = image_mean if image_mean is not None else self.image_mean snake_case : List[Any] = image_std if image_std is not None else self.image_std snake_case : Dict = size if size is not None else self.size snake_case : List[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Any = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) snake_case : List[str] = make_batched(SCREAMING_SNAKE_CASE_ ) snake_case : Any = [ [ self._preprocess_image( image=SCREAMING_SNAKE_CASE_ ,do_resize=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,do_center_crop=SCREAMING_SNAKE_CASE_ ,crop_size=SCREAMING_SNAKE_CASE_ ,do_rescale=SCREAMING_SNAKE_CASE_ ,rescale_factor=SCREAMING_SNAKE_CASE_ ,do_normalize=SCREAMING_SNAKE_CASE_ ,image_mean=SCREAMING_SNAKE_CASE_ ,image_std=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,) for img in video ] for video in videos ] snake_case : Tuple = {"""pixel_values""": videos} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ )
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() __lowercase : List[Any] = logging.get_logger(__name__) def lowercase ( __A : Any ) -> Dict: '''simple docstring''' snake_case : int = DPTConfig() if "large" in checkpoint_url: snake_case : Optional[Any] = 1024 snake_case : List[str] = 4096 snake_case : Optional[int] = 24 snake_case : Dict = 16 snake_case : Optional[Any] = [5, 11, 17, 23] snake_case : int = [256, 512, 1024, 1024] snake_case : int = (1, 384, 384) if "ade" in checkpoint_url: snake_case : str = True snake_case : Optional[int] = 150 snake_case : List[str] = """huggingface/label-files""" snake_case : Optional[int] = """ade20k-id2label.json""" snake_case : Optional[int] = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type="""dataset""" ) ) , """r""" ) ) snake_case : Tuple = {int(__A ): v for k, v in idalabel.items()} snake_case : Any = idalabel snake_case : Any = {v: k for k, v in idalabel.items()} snake_case : Dict = [1, 150, 480, 480] return config, expected_shape def lowercase ( __A : int ) -> int: '''simple docstring''' snake_case : List[Any] = ["""pretrained.model.head.weight""", """pretrained.model.head.bias"""] for k in ignore_keys: state_dict.pop(__A , __A ) def lowercase ( __A : Tuple ) -> str: '''simple docstring''' if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): snake_case : List[str] = name.replace("""pretrained.model""" , """dpt.encoder""" ) if "pretrained.model" in name: snake_case : int = name.replace("""pretrained.model""" , """dpt.embeddings""" ) if "patch_embed" in name: snake_case : Tuple = name.replace("""patch_embed""" , """patch_embeddings""" ) if "pos_embed" in name: snake_case : List[str] = name.replace("""pos_embed""" , """position_embeddings""" ) if "attn.proj" in name: snake_case : int = name.replace("""attn.proj""" , """attention.output.dense""" ) if "proj" in name and "project" not in name: snake_case : int = name.replace("""proj""" , """projection""" ) if "blocks" in name: snake_case : Union[str, Any] = name.replace("""blocks""" , """layer""" ) if "mlp.fc1" in name: snake_case : Optional[int] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: snake_case : List[Any] = name.replace("""mlp.fc2""" , """output.dense""" ) if "norm1" in name: snake_case : List[Any] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: snake_case : List[str] = name.replace("""norm2""" , """layernorm_after""" ) if "scratch.output_conv" in name: snake_case : str = name.replace("""scratch.output_conv""" , """head""" ) if "scratch" in name: snake_case : Dict = name.replace("""scratch""" , """neck""" ) if "layer1_rn" in name: snake_case : Optional[int] = name.replace("""layer1_rn""" , """convs.0""" ) if "layer2_rn" in name: snake_case : Tuple = name.replace("""layer2_rn""" , """convs.1""" ) if "layer3_rn" in name: snake_case : Any = name.replace("""layer3_rn""" , """convs.2""" ) if "layer4_rn" in name: snake_case : Optional[Any] = name.replace("""layer4_rn""" , """convs.3""" ) if "refinenet" in name: snake_case : int = int(name[len("""neck.refinenet""" ) : len("""neck.refinenet""" ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 snake_case : Tuple = name.replace(f"""refinenet{layer_idx}""" , f"""fusion_stage.layers.{abs(layer_idx-4 )}""" ) if "out_conv" in name: snake_case : Tuple = name.replace("""out_conv""" , """projection""" ) if "resConfUnit1" in name: snake_case : Optional[Any] = name.replace("""resConfUnit1""" , """residual_layer1""" ) if "resConfUnit2" in name: snake_case : Optional[Any] = name.replace("""resConfUnit2""" , """residual_layer2""" ) if "conv1" in name: snake_case : Dict = name.replace("""conv1""" , """convolution1""" ) if "conv2" in name: snake_case : List[Any] = name.replace("""conv2""" , """convolution2""" ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: snake_case : List[Any] = name.replace("""pretrained.act_postprocess1.0.project.0""" , """neck.reassemble_stage.readout_projects.0.0""" ) if "pretrained.act_postprocess2.0.project.0" in name: snake_case : List[str] = name.replace("""pretrained.act_postprocess2.0.project.0""" , """neck.reassemble_stage.readout_projects.1.0""" ) if "pretrained.act_postprocess3.0.project.0" in name: snake_case : Optional[Any] = name.replace("""pretrained.act_postprocess3.0.project.0""" , """neck.reassemble_stage.readout_projects.2.0""" ) if "pretrained.act_postprocess4.0.project.0" in name: snake_case : List[str] = name.replace("""pretrained.act_postprocess4.0.project.0""" , """neck.reassemble_stage.readout_projects.3.0""" ) # resize blocks if "pretrained.act_postprocess1.3" in name: snake_case : Any = name.replace("""pretrained.act_postprocess1.3""" , """neck.reassemble_stage.layers.0.projection""" ) if "pretrained.act_postprocess1.4" in name: snake_case : Tuple = name.replace("""pretrained.act_postprocess1.4""" , """neck.reassemble_stage.layers.0.resize""" ) if "pretrained.act_postprocess2.3" in name: snake_case : Optional[int] = name.replace("""pretrained.act_postprocess2.3""" , """neck.reassemble_stage.layers.1.projection""" ) if "pretrained.act_postprocess2.4" in name: snake_case : Any = name.replace("""pretrained.act_postprocess2.4""" , """neck.reassemble_stage.layers.1.resize""" ) if "pretrained.act_postprocess3.3" in name: snake_case : List[Any] = name.replace("""pretrained.act_postprocess3.3""" , """neck.reassemble_stage.layers.2.projection""" ) if "pretrained.act_postprocess4.3" in name: snake_case : List[str] = name.replace("""pretrained.act_postprocess4.3""" , """neck.reassemble_stage.layers.3.projection""" ) if "pretrained.act_postprocess4.4" in name: snake_case : str = name.replace("""pretrained.act_postprocess4.4""" , """neck.reassemble_stage.layers.3.resize""" ) if "pretrained" in name: snake_case : Any = name.replace("""pretrained""" , """dpt""" ) if "bn" in name: snake_case : Optional[Any] = name.replace("""bn""" , """batch_norm""" ) if "head" in name: snake_case : Optional[int] = name.replace("""head""" , """head.head""" ) if "encoder.norm" in name: snake_case : Optional[int] = name.replace("""encoder.norm""" , """layernorm""" ) if "auxlayer" in name: snake_case : Union[str, Any] = name.replace("""auxlayer""" , """auxiliary_head.head""" ) return name def lowercase ( __A : Dict , __A : Tuple ) -> Dict: '''simple docstring''' for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case : Any = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.weight""" ) snake_case : List[str] = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case : Optional[int] = in_proj_weight[: config.hidden_size, :] snake_case : Optional[Any] = in_proj_bias[: config.hidden_size] snake_case : Union[str, Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case : str = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case : List[str] = in_proj_weight[ -config.hidden_size :, : ] snake_case : Tuple = in_proj_bias[-config.hidden_size :] def lowercase ( ) -> Any: '''simple docstring''' snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : List[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Tuple , __A : Optional[int] , __A : Optional[Any] , __A : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' snake_case , snake_case : Union[str, Any] = get_dpt_config(__A ) # load original state_dict from URL snake_case : Union[str, Any] = torch.hub.load_state_dict_from_url(__A , map_location="""cpu""" ) # remove certain keys remove_ignore_keys_(__A ) # rename keys for key in state_dict.copy().keys(): snake_case : Optional[Any] = state_dict.pop(__A ) snake_case : List[str] = val # read in qkv matrices read_in_q_k_v(__A , __A ) # load HuggingFace model snake_case : int = DPTForSemanticSegmentation(__A ) if """ade""" in checkpoint_url else DPTForDepthEstimation(__A ) model.load_state_dict(__A ) model.eval() # Check outputs on an image snake_case : int = 480 if """ade""" in checkpoint_url else 384 snake_case : Optional[int] = DPTImageProcessor(size=__A ) snake_case : List[Any] = prepare_img() snake_case : Optional[Any] = image_processor(__A , return_tensors="""pt""" ) # forward pass snake_case : Any = model(**__A ).logits if """ade""" in checkpoint_url else model(**__A ).predicted_depth # Assert logits snake_case : int = torch.tensor([[6.3_199, 6.3_629, 6.4_148], [6.3_850, 6.3_615, 6.4_166], [6.3_519, 6.3_176, 6.3_575]] ) if "ade" in checkpoint_url: snake_case : List[str] = torch.tensor([[4.0_480, 4.2_420, 4.4_360], [4.3_124, 4.5_693, 4.8_261], [4.5_768, 4.8_965, 5.2_163]] ) assert outputs.shape == torch.Size(__A ) assert ( torch.allclose(outputs[0, 0, :3, :3] , __A , atol=1E-4 ) if "ade" in checkpoint_url else torch.allclose(outputs[0, :3, :3] , __A ) ) Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__A ) if push_to_hub: print("""Pushing model to hub...""" ) model.push_to_hub( repo_path_or_name=Path(__A , __A ) , organization="""nielsr""" , commit_message="""Add model""" , use_temp_dir=__A , ) image_processor.push_to_hub( repo_path_or_name=Path(__A , __A ) , organization="""nielsr""" , commit_message="""Add image processor""" , use_temp_dir=__A , ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt''', type=str, help='''URL of the original DPT checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', ) parser.add_argument( '''--model_name''', default='''dpt-large''', type=str, help='''Name of the model, in case you\'re pushing to the hub.''', ) __lowercase : List[Any] = parser.parse_args() convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
from typing import Dict, List, Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowercase : int = logging.get_logger(__name__) __lowercase : List[Any] = { '''nielsr/canine-s''': 2_048, } # Unicode defines 1,114,112 total “codepoints” __lowercase : List[str] = 1_114_112 # Below: Constants defining canonical codepoints for special, pseudo-characters. # Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py __lowercase : int = 0 __lowercase : Union[str, Any] = 0Xe000 __lowercase : Tuple = 0Xe001 __lowercase : List[Any] = 0Xe002 __lowercase : Optional[Any] = 0Xe003 __lowercase : Union[str, Any] = 0Xe004 # Maps special codepoints to human-readable names. __lowercase : Dict[int, str] = { # Special symbols are represented using codepoints values that are valid, # but designated as "Private Use", meaning that they will never be assigned # characters by the Unicode Consortium, and are thus safe for use here. # # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly # excluded and should fail with a hard error. CLS: "[CLS]", SEP: "[SEP]", BOS: "[BOS]", MASK: "[MASK]", PAD: "[PAD]", RESERVED: "[RESERVED]", } # Maps special codepoint human-readable names to their codepoint values. __lowercase : Dict[str, int] = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=chr(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=2048 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Optional[Any] = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else bos_token snake_case : Tuple = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else eos_token snake_case : Optional[Any] = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else sep_token snake_case : str = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else cls_token snake_case : Tuple = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it snake_case : List[Any] = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( bos_token=SCREAMING_SNAKE_CASE_ ,eos_token=SCREAMING_SNAKE_CASE_ ,sep_token=SCREAMING_SNAKE_CASE_ ,cls_token=SCREAMING_SNAKE_CASE_ ,pad_token=SCREAMING_SNAKE_CASE_ ,mask_token=SCREAMING_SNAKE_CASE_ ,add_prefix_space=SCREAMING_SNAKE_CASE_ ,model_max_length=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,) # Creates a mapping for looking up the IDs of special symbols. snake_case : Dict[str, int] = {} for codepoint, name in SPECIAL_CODEPOINTS.items(): snake_case : Optional[Any] = codepoint # Creates a mapping for looking up the string forms of special symbol IDs. snake_case : Dict[int, str] = { codepoint: name for name, codepoint in self._special_codepoints.items() } snake_case : Tuple = UNICODE_VOCAB_SIZE snake_case : Union[str, Any] = len(self._special_codepoints ) @property def snake_case_ ( self ): '''simple docstring''' return self._unicode_vocab_size def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return list(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' try: return ord(SCREAMING_SNAKE_CASE_ ) except TypeError: raise ValueError(F"""invalid token: '{token}'""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' try: if index in SPECIAL_CODEPOINTS: return SPECIAL_CODEPOINTS[index] return chr(SCREAMING_SNAKE_CASE_ ) except TypeError: raise ValueError(F"""invalid id: {index}""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return "".join(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : List[str] = [self.sep_token_id] snake_case : Tuple = [self.cls_token_id] snake_case : List[Any] = cls + token_ids_a + sep if token_ids_a is not None: result += token_ids_a + sep return result def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ ,token_ids_a=SCREAMING_SNAKE_CASE_ ,already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] if token_ids_a is not None: result += ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return result def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Optional[int] = [self.sep_token_id] snake_case : Optional[Any] = [self.cls_token_id] snake_case : Union[str, Any] = len(cls + token_ids_a + sep ) * [0] if token_ids_a is not None: result += len(token_ids_a + sep ) * [1] return result def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return ()
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
def lowercase ( __A : str ) -> str: '''simple docstring''' snake_case : Optional[int] = 0 # if input_string is "aba" than new_input_string become "a|b|a" snake_case : Optional[int] = """""" snake_case : Tuple = """""" # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__A ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring snake_case , snake_case : Any = 0, 0 # length[i] shows the length of palindromic substring with center i snake_case : Union[str, Any] = [1 for i in range(len(__A ) )] # for each character in new_string find corresponding palindromic string snake_case : Tuple = 0 for j in range(len(__A ) ): snake_case : List[str] = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__A ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 snake_case : List[str] = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: snake_case : str = j - k + 1 # noqa: E741 snake_case : int = j + k - 1 # update max_length and start position if max_length < length[j]: snake_case : str = length[j] snake_case : Optional[Any] = j # create that string snake_case : int = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler __lowercase : List[str] = 16 __lowercase : Union[str, Any] = 32 def lowercase ( __A : Accelerator , __A : int = 16 , __A : str = "bert-base-cased" ) -> Optional[Any]: '''simple docstring''' snake_case : str = AutoTokenizer.from_pretrained(__A ) snake_case : Optional[int] = load_dataset("""glue""" , """mrpc""" ) def tokenize_function(__A : Optional[int] ): # max_length=None => use the model max length (it's actually the default) snake_case : Optional[int] = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=__A , max_length=__A ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case : str = datasets.map( __A , batched=__A , remove_columns=["""idx""", """sentence1""", """sentence2"""] , load_from_cache_file=__A ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case : Dict = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(__A : str ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__A , padding="""max_length""" , max_length=128 , return_tensors="""pt""" ) return tokenizer.pad(__A , padding="""longest""" , return_tensors="""pt""" ) # Instantiate dataloaders. snake_case : str = DataLoader( tokenized_datasets["""train"""] , shuffle=__A , collate_fn=__A , batch_size=__A ) snake_case : Any = DataLoader( tokenized_datasets["""validation"""] , shuffle=__A , collate_fn=__A , batch_size=__A ) return train_dataloader, eval_dataloader def lowercase ( __A : int , __A : Union[str, Any] , __A : Any , __A : Tuple ) -> str: '''simple docstring''' model.eval() snake_case : Optional[Any] = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): snake_case : Tuple = model(**__A ) snake_case : Any = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times snake_case , snake_case : str = accelerator.gather( (predictions, batch["""labels"""]) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(__A ) - 1: snake_case : str = predictions[: len(eval_dataloader.dataset ) - samples_seen] snake_case : Dict = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=__A , references=__A , ) snake_case : Union[str, Any] = metric.compute() return eval_metric["accuracy"] def lowercase ( __A : Optional[Any] , __A : int ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case : Any = config["""lr"""] snake_case : Dict = int(config["""num_epochs"""] ) snake_case : Union[str, Any] = int(config["""seed"""] ) snake_case : Union[str, Any] = int(config["""batch_size"""] ) snake_case : List[Any] = args.model_name_or_path set_seed(__A ) snake_case , snake_case : str = get_dataloaders(__A , __A , __A ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained(__A , return_dict=__A ) # Instantiate optimizer snake_case : str = ( AdamW if accelerator.state.deepspeed_plugin is None or """optimizer""" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) snake_case : Dict = optimizer_cls(params=model.parameters() , lr=__A ) if accelerator.state.deepspeed_plugin is not None: snake_case : Union[str, Any] = accelerator.state.deepspeed_plugin.deepspeed_config[ """gradient_accumulation_steps""" ] else: snake_case : Dict = 1 snake_case : Tuple = (len(__A ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): snake_case : Optional[int] = get_linear_schedule_with_warmup( optimizer=__A , num_warmup_steps=0 , num_training_steps=__A , ) else: snake_case : Dict = DummyScheduler(__A , total_num_steps=__A , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case , snake_case , snake_case , snake_case , snake_case : Tuple = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over snake_case : Optional[int] = 0 # We also need to keep track of the stating epoch so files are named properly snake_case : str = 0 snake_case : Dict = evaluate.load("""glue""" , """mrpc""" ) snake_case : int = num_epochs if args.partial_train_epoch is not None: snake_case : Dict = args.partial_train_epoch if args.resume_from_checkpoint: accelerator.load_state(args.resume_from_checkpoint ) snake_case : List[str] = args.resume_from_checkpoint.split("""epoch_""" )[1] snake_case : List[str] = """""" for char in epoch_string: if char.isdigit(): state_epoch_num += char else: break snake_case : Optional[Any] = int(__A ) + 1 snake_case : Optional[int] = evaluation_loop(__A , __A , __A , __A ) accelerator.print("""resumed checkpoint performance:""" , __A ) accelerator.print("""resumed checkpoint's scheduler's lr:""" , lr_scheduler.get_lr()[0] ) accelerator.print("""resumed optimizers's lr:""" , optimizer.param_groups[0]["""lr"""] ) with open(os.path.join(args.output_dir , f"""state_{starting_epoch-1}.json""" ) , """r""" ) as f: snake_case : List[str] = json.load(__A ) assert resumed_state["accuracy"] == accuracy, "Accuracy mismatch, loading from checkpoint failed" assert ( resumed_state["lr"] == lr_scheduler.get_lr()[0] ), "Scheduler learning rate mismatch, loading from checkpoint failed" assert ( resumed_state["optimizer_lr"] == optimizer.param_groups[0]["lr"] ), "Optimizer learning rate mismatch, loading from checkpoint failed" assert resumed_state["epoch"] == starting_epoch - 1, "Epoch mismatch, loading from checkpoint failed" return # Now we train the model snake_case : int = {} for epoch in range(__A , __A ): model.train() for step, batch in enumerate(__A ): snake_case : Dict = model(**__A ) snake_case : Union[str, Any] = outputs.loss snake_case : int = loss / gradient_accumulation_steps accelerator.backward(__A ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 snake_case : Any = f"""epoch_{epoch}""" snake_case : Optional[int] = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) snake_case : Optional[int] = evaluation_loop(__A , __A , __A , __A ) snake_case : str = accuracy snake_case : Optional[int] = lr_scheduler.get_lr()[0] snake_case : str = optimizer.param_groups[0]["""lr"""] snake_case : Tuple = epoch snake_case : Optional[int] = overall_step accelerator.print(f"""epoch {epoch}:""" , __A ) accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , f"""state_{epoch}.json""" ) , """w""" ) as f: json.dump(__A , __A ) def lowercase ( ) -> Dict: '''simple docstring''' snake_case : int = argparse.ArgumentParser(description="""Simple example of training script tracking peak GPU memory usage.""" ) parser.add_argument( """--model_name_or_path""" , type=__A , default="""bert-base-cased""" , help="""Path to pretrained model or model identifier from huggingface.co/models.""" , required=__A , ) parser.add_argument( """--output_dir""" , type=__A , default=""".""" , help="""Optional save directory where all checkpoint folders will be stored. Default is the current working directory.""" , ) parser.add_argument( """--resume_from_checkpoint""" , type=__A , default=__A , help="""If the training should continue from a checkpoint folder.""" , ) parser.add_argument( """--partial_train_epoch""" , type=__A , default=__A , help="""If passed, the training will stop after this number of epochs.""" , ) parser.add_argument( """--num_epochs""" , type=__A , default=2 , help="""Number of train epochs.""" , ) snake_case : int = parser.parse_args() snake_case : List[Any] = {"""lr""": 2E-5, """num_epochs""": args.num_epochs, """seed""": 42, """batch_size""": 16} training_function(__A , __A ) if __name__ == "__main__": main()
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class _A ( yaml.SafeLoader ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = [self.constructed_objects[key_node] for key_node, _ in node.value] snake_case : Optional[Any] = [tuple(SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else key for key in keys] snake_case : Optional[int] = Counter(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(F"""Got duplicate yaml keys: {duplicate_keys}""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' snake_case : Union[str, Any] = super().construct_mapping(SCREAMING_SNAKE_CASE_ ,deep=SCREAMING_SNAKE_CASE_ ) self._check_no_duplicates_on_constructed_node(SCREAMING_SNAKE_CASE_ ) return mapping def lowercase ( __A : str ) -> Tuple[Optional[str], str]: '''simple docstring''' snake_case : Any = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: snake_case : Tuple = full_content[1:].index("""---""" ) + 1 snake_case : str = """\n""".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(__A ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Any = {'''train_eval_index'''} # train-eval-index in the YAML metadata @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE_ ,encoding="""utf-8""" ) as readme_file: snake_case , snake_case : int = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(SCREAMING_SNAKE_CASE_ ) else: return cls() def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if path.exists(): with open(SCREAMING_SNAKE_CASE_ ,encoding="""utf-8""" ) as readme_file: snake_case : Union[str, Any] = readme_file.read() else: snake_case : Dict = None snake_case : int = self._to_readme(SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ ,"""w""" ,encoding="""utf-8""" ) as readme_file: readme_file.write(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' if readme_content is not None: snake_case , snake_case : List[str] = _split_yaml_from_readme(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = """---\n""" + self.to_yaml_string() + """---\n""" + content else: snake_case : Optional[int] = """---\n""" + self.to_yaml_string() + """---\n""" return full_content @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[Any] = yaml.load(SCREAMING_SNAKE_CASE_ ,Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields snake_case : Dict = { (key.replace("""-""" ,"""_""" ) if key.replace("""-""" ,"""_""" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''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=SCREAMING_SNAKE_CASE_ ,allow_unicode=SCREAMING_SNAKE_CASE_ ,encoding="""utf-8""" ,).decode("""utf-8""" ) __lowercase : Union[str, Any] = { '''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 __lowercase : List[str] = ArgumentParser(usage='''Validate the yaml metadata block of a README.md file.''') ap.add_argument('''readme_filepath''') __lowercase : Any = ap.parse_args() __lowercase : List[Any] = Path(args.readme_filepath) __lowercase : str = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
from __future__ import annotations def lowercase ( __A : str , __A : list[str] | None = None ) -> list[list[str]]: '''simple docstring''' snake_case : Optional[int] = word_bank or [] # create a table snake_case : int = len(__A ) + 1 snake_case : list[list[list[str]]] = [] for _ in range(__A ): table.append([] ) # seed value snake_case : Optional[Any] = [[]] # because empty string has empty combination # iterate through the indices for i in range(__A ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(__A )] == word: snake_case : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(__A )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(__A )]: combination.reverse() return table[len(__A )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
def lowercase ( __A : list ) -> list: '''simple docstring''' snake_case : Optional[Any] = False while is_sorted is False: # Until all the indices are traversed keep looping snake_case : Tuple = True for i in range(0 , len(__A ) - 1 , 2 ): # iterating over all even indices if input_list[i] > input_list[i + 1]: snake_case , snake_case : List[Any] = input_list[i + 1], input_list[i] # swapping if elements not in order snake_case : Tuple = False for i in range(1 , len(__A ) - 1 , 2 ): # iterating over all odd indices if input_list[i] > input_list[i + 1]: snake_case , snake_case : Union[str, Any] = input_list[i + 1], input_list[i] # swapping if elements not in order snake_case : Union[str, Any] = False return input_list if __name__ == "__main__": print('''Enter list to be sorted''') __lowercase : int = [int(x) for x in input().split()] # inputing elements of the list in one line __lowercase : List[str] = odd_even_sort(input_list) print('''The sorted list is''') print(sorted_list)
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class _A ( snake_case , snake_case , snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : int = StableDiffusionInstructPixaPixPipeline __lowerCamelCase : Optional[int] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''cross_attention_kwargs'''} __lowerCamelCase : Any = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase : Any = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase : int = IMAGE_TO_IMAGE_IMAGE_PARAMS def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=8 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=32 ,) snake_case : str = PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE_ ) torch.manual_seed(0 ) snake_case : Optional[int] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,) torch.manual_seed(0 ) snake_case : List[str] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1000 ,) snake_case : List[Any] = CLIPTextModel(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case : Optional[Any] = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=0 ): '''simple docstring''' snake_case : int = floats_tensor((1, 3, 32, 32) ,rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = image.cpu().permute(0 ,2 ,3 ,1 )[0] snake_case : Any = Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE_ ) ).convert("""RGB""" ) if str(SCREAMING_SNAKE_CASE_ ).startswith("""mps""" ): snake_case : Optional[int] = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) else: snake_case : Optional[Any] = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """image_guidance_scale""": 1, """output_type""": """numpy""", } return inputs def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : Dict = self.get_dummy_components() snake_case : List[Any] = StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = sd_pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : int = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case : Optional[int] = np.array([0.75_26, 0.37_50, 0.45_47, 0.61_17, 0.58_66, 0.50_16, 0.43_27, 0.56_42, 0.48_15] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : Any = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : Tuple = self.get_dummy_components() snake_case : List[Any] = StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = """french fries""" snake_case : Tuple = sd_pipe(**SCREAMING_SNAKE_CASE_ ,negative_prompt=SCREAMING_SNAKE_CASE_ ) snake_case : int = output.images snake_case : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case : List[str] = np.array([0.75_11, 0.36_42, 0.45_53, 0.62_36, 0.57_97, 0.50_13, 0.43_43, 0.56_11, 0.48_31] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : int = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : str = self.get_dummy_components() snake_case : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = [inputs["""prompt"""]] * 2 snake_case : Optional[Any] = np.array(inputs["""image"""] ).astype(np.floataa ) / 2_55.0 snake_case : Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ).to(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = image / 2 + 0.5 snake_case : Any = image.permute(0 ,3 ,1 ,2 ) snake_case : Optional[int] = image.repeat(2 ,1 ,1 ,1 ) snake_case : Tuple = sd_pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : int = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case : Optional[int] = np.array([0.58_12, 0.57_48, 0.52_22, 0.59_08, 0.56_95, 0.71_74, 0.68_04, 0.55_23, 0.55_79] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : Union[str, Any] = self.get_dummy_components() snake_case : Optional[int] = EulerAncestralDiscreteScheduler( beta_start=0.0_00_85 ,beta_end=0.0_12 ,beta_schedule="""scaled_linear""" ) snake_case : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_ ) snake_case : Any = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = sd_pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : Optional[int] = image[0, -3:, -3:, -1] snake_case : Union[str, Any] = [round(SCREAMING_SNAKE_CASE_ ,4 ) for x in image_slice.flatten().tolist()] print(""",""".join([str(SCREAMING_SNAKE_CASE_ ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case : Union[str, Any] = np.array([0.74_17, 0.38_42, 0.47_32, 0.57_76, 0.58_91, 0.51_39, 0.40_52, 0.56_73, 0.49_86] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.get_dummy_components() snake_case : int = StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE_ ,do_normalize=SCREAMING_SNAKE_CASE_ ) snake_case : int = pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : int = pipe(**self.get_dummy_inputs_by_type(SCREAMING_SNAKE_CASE_ ,input_image_type="""pt""" ) )[0] snake_case : List[str] = components["""vae"""] snake_case : Optional[int] = self.get_dummy_inputs_by_type(SCREAMING_SNAKE_CASE_ ,input_image_type="""pt""" ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case : List[Any] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case : Any = pipe(**SCREAMING_SNAKE_CASE_ )[0] snake_case : Tuple = np.abs(out - out_latents_inputs ).max() self.assertLess(SCREAMING_SNAKE_CASE_ ,1E-4 ,"""passing latents as image input generate different result from passing image""" ) @slow @require_torch_gpu class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ ( self ,SCREAMING_SNAKE_CASE_=0 ): '''simple docstring''' snake_case : List[str] = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : int = load_image( """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg""" ) snake_case : Union[str, Any] = { """prompt""": """turn him into a cyborg""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 7.5, """image_guidance_scale""": 1.0, """output_type""": """numpy""", } return inputs def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing() snake_case : Optional[int] = self.get_inputs() snake_case : List[Any] = pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : str = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case : Dict = np.array([0.59_02, 0.60_15, 0.60_27, 0.59_83, 0.60_92, 0.60_61, 0.57_65, 0.57_85, 0.55_55] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing() snake_case : Union[str, Any] = self.get_inputs() snake_case : Tuple = pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : Any = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case : int = np.array([0.65_78, 0.68_17, 0.69_72, 0.67_61, 0.68_56, 0.69_16, 0.64_28, 0.65_16, 0.63_01] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) snake_case : int = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing() snake_case : Optional[Any] = self.get_inputs() snake_case : Union[str, Any] = pipe(**SCREAMING_SNAKE_CASE_ ).images snake_case : Optional[int] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case : Any = np.array([0.38_28, 0.38_34, 0.38_18, 0.37_92, 0.38_65, 0.37_52, 0.37_92, 0.38_47, 0.37_53] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = 0 def callback_fn(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) -> None: snake_case : str = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case : List[str] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case : Union[str, Any] = latents[0, -3:, -3:, -1] snake_case : Dict = np.array([-0.24_63, -0.46_44, -0.97_56, 1.51_76, 1.44_14, 0.78_66, 0.98_97, 0.85_21, 0.79_83] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case : Optional[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case : Optional[int] = latents[0, -3:, -3:, -1] snake_case : List[str] = np.array([-0.26_44, -0.46_26, -0.96_53, 1.51_76, 1.45_51, 0.76_86, 0.98_05, 0.84_52, 0.81_15] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case : List[Any] = False snake_case : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" ,safety_checker=SCREAMING_SNAKE_CASE_ ,torch_dtype=torch.floataa ) snake_case : List[str] = pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing() snake_case : List[Any] = self.get_inputs() pipe(**SCREAMING_SNAKE_CASE_ ,callback=SCREAMING_SNAKE_CASE_ ,callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def snake_case_ ( self ): '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" ,safety_checker=SCREAMING_SNAKE_CASE_ ,torch_dtype=torch.floataa ) snake_case : Tuple = pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case : Dict = self.get_inputs() snake_case : int = pipe(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case : Any = inputs["""image"""].resize((504, 504) ) snake_case : Union[str, Any] = """timbrooks/instruct-pix2pix""" snake_case : Optional[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( SCREAMING_SNAKE_CASE_ ,safety_checker=SCREAMING_SNAKE_CASE_ ,) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) pipe.enable_attention_slicing() snake_case : Union[str, Any] = pipe(**SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = output.images[0] snake_case : str = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case : Optional[Any] = np.array([0.27_26, 0.25_29, 0.26_64, 0.26_55, 0.26_41, 0.26_42, 0.25_91, 0.26_49, 0.25_90] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''''' __lowerCamelCase : Union[str, Any] = '''hf-legacy''' # "hf://"" is reserved for hffs def __init__( self ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(self ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = repo_info snake_case : Dict = token snake_case : Any = None def snake_case_ ( self ): '''simple docstring''' if self.dir_cache is None: snake_case : str = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes snake_case : Union[str, Any] = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(SCREAMING_SNAKE_CASE_ ): {"""name""": str(SCREAMING_SNAKE_CASE_ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "rb" ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' if not isinstance(self.repo_info ,SCREAMING_SNAKE_CASE_ ): raise NotImplementedError(F"""Open is only implemented for dataset repositories, but got {self.repo_info}""" ) snake_case : Tuple = hf_hub_url(self.repo_info.id ,SCREAMING_SNAKE_CASE_ ,revision=self.repo_info.sha ) return fsspec.open( SCREAMING_SNAKE_CASE_ ,mode=SCREAMING_SNAKE_CASE_ ,headers=get_authentication_headers_for_url(SCREAMING_SNAKE_CASE_ ,use_auth_token=self.token ) ,client_kwargs={"""trust_env""": True} ,).open() def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' self._get_dirs() snake_case : List[Any] = self._strip_protocol(SCREAMING_SNAKE_CASE_ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' self._get_dirs() snake_case : List[str] = PurePosixPath(path.strip("""/""" ) ) snake_case : Optional[int] = {} for p, f in self.dir_cache.items(): snake_case : List[str] = PurePosixPath(p.strip("""/""" ) ) snake_case : int = p.parent if root == path: snake_case : Any = f snake_case : Optional[Any] = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _A : '''simple docstring''' @staticmethod def snake_case_ ( *SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass @is_pipeline_test @require_torch @require_vision class _A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[Any] = pipeline("""visual-question-answering""" ,model="""hf-internal-testing/tiny-vilt-random-vqa""" ) snake_case : str = [ { """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ), """question""": """How many cats are there?""", }, { """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""", """question""": """How many cats are there?""", }, ] return vqa_pipeline, examples def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[int] = vqa_pipeline(SCREAMING_SNAKE_CASE_ ,top_k=1 ) self.assertEqual( SCREAMING_SNAKE_CASE_ ,[ [{"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}], [{"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}], ] ,) @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = pipeline("""visual-question-answering""" ,model="""hf-internal-testing/tiny-vilt-random-vqa""" ) snake_case : str = """./tests/fixtures/tests_samples/COCO/000000039769.png""" snake_case : Any = """How many cats are there?""" snake_case : Any = vqa_pipeline(image=SCREAMING_SNAKE_CASE_ ,question="""How many cats are there?""" ,top_k=2 ) self.assertEqual( SCREAMING_SNAKE_CASE_ ,[{"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}] ) snake_case : Dict = vqa_pipeline({"""image""": image, """question""": question} ,top_k=2 ) self.assertEqual( SCREAMING_SNAKE_CASE_ ,[{"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}, {"""score""": ANY(SCREAMING_SNAKE_CASE_ ), """answer""": ANY(SCREAMING_SNAKE_CASE_ )}] ) @slow @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : int = pipeline("""visual-question-answering""" ,model="""dandelin/vilt-b32-finetuned-vqa""" ) snake_case : Union[str, Any] = """./tests/fixtures/tests_samples/COCO/000000039769.png""" snake_case : Union[str, Any] = """How many cats are there?""" snake_case : str = vqa_pipeline(image=SCREAMING_SNAKE_CASE_ ,question=SCREAMING_SNAKE_CASE_ ,top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}] ) snake_case : Union[str, Any] = vqa_pipeline({"""image""": image, """question""": question} ,top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}] ) snake_case : List[str] = vqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] ,top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]] * 2 ,) @require_tf @unittest.skip("""Visual question answering not implemented in TF""" ) def snake_case_ ( self ): '''simple docstring''' pass
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys __lowercase : Optional[int] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
from __future__ import annotations import time __lowercase : Optional[Any] = list[tuple[int, int]] __lowercase : Tuple = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] __lowercase : Optional[Any] = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = pos_x snake_case : List[str] = pos_y snake_case : Optional[int] = (pos_y, pos_x) snake_case : List[str] = goal_x snake_case : str = goal_y snake_case : int = parent class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Union[str, Any] = Node(start[1] ,start[0] ,goal[1] ,goal[0] ,SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = Node(goal[1] ,goal[0] ,goal[1] ,goal[0] ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = [self.start] snake_case : int = False def snake_case_ ( self ): '''simple docstring''' while self.node_queue: snake_case : Any = self.node_queue.pop(0 ) if current_node.pos == self.target.pos: snake_case : List[Any] = True return self.retrace_path(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = self.get_successors(SCREAMING_SNAKE_CASE_ ) for node in successors: self.node_queue.append(SCREAMING_SNAKE_CASE_ ) if not self.reached: return [self.start.pos] return None def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = [] for action in delta: snake_case : Optional[Any] = parent.pos_x + action[1] snake_case : Optional[Any] = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(SCREAMING_SNAKE_CASE_ ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,self.target.pos_y ,self.target.pos_x ,SCREAMING_SNAKE_CASE_ ) ) return successors def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[Any] = node snake_case : List[str] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) snake_case : Any = current_node.parent path.reverse() return path class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = BreadthFirstSearch(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Any = BreadthFirstSearch(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : int = False def snake_case_ ( self ): '''simple docstring''' while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: snake_case : List[str] = self.fwd_bfs.node_queue.pop(0 ) snake_case : Union[str, Any] = self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: snake_case : Optional[int] = True return self.retrace_bidirectional_path( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = current_bwd_node snake_case : Any = current_fwd_node snake_case : Optional[Any] = { self.fwd_bfs: self.fwd_bfs.get_successors(SCREAMING_SNAKE_CASE_ ), self.bwd_bfs: self.bwd_bfs.get_successors(SCREAMING_SNAKE_CASE_ ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(SCREAMING_SNAKE_CASE_ ) if not self.reached: return [self.fwd_bfs.start.pos] return None def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = self.fwd_bfs.retrace_path(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = self.bwd_bfs.retrace_path(SCREAMING_SNAKE_CASE_ ) bwd_path.pop() bwd_path.reverse() snake_case : Optional[Any] = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() __lowercase : List[Any] = (0, 0) __lowercase : Optional[int] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) __lowercase : Optional[int] = time.time() __lowercase : Optional[int] = BreadthFirstSearch(init, goal) __lowercase : Any = bfs.search() __lowercase : List[str] = time.time() - start_bfs_time print('''Unidirectional BFS computation time : ''', bfs_time) __lowercase : List[str] = time.time() __lowercase : Dict = BidirectionalBreadthFirstSearch(init, goal) __lowercase : Union[str, Any] = bd_bfs.search() __lowercase : Optional[Any] = time.time() - start_bd_bfs_time print('''Bidirectional BFS computation time : ''', bd_bfs_time)
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = set_counts snake_case : Union[str, Any] = max(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = len(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = [1] * num_sets snake_case : Optional[Any] = list(range(SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = self.get_parent(SCREAMING_SNAKE_CASE_ ) snake_case : Any = self.get_parent(SCREAMING_SNAKE_CASE_ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] snake_case : Any = 0 snake_case : List[str] = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 snake_case : str = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] snake_case : List[Any] = 0 snake_case : List[str] = src_parent snake_case : Any = self.set_counts[src_parent] snake_case : List[str] = max(self.max_set ,SCREAMING_SNAKE_CASE_ ) return True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.parents[disj_set] == disj_set: return disj_set snake_case : str = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _A : '''simple docstring''' @staticmethod def snake_case_ ( *SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass @is_pipeline_test @require_vision @require_torch class _A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : str = pipeline( """zero-shot-object-detection""" ,model="""hf-internal-testing/tiny-random-owlvit-object-detection""" ) snake_case : List[Any] = [ { """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""", """candidate_labels""": ["""cat""", """remote""", """couch"""], } ] return object_detector, examples def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = object_detector(examples[0] ,threshold=0.0 ) snake_case : Dict = len(SCREAMING_SNAKE_CASE_ ) self.assertGreater(SCREAMING_SNAKE_CASE_ ,0 ) self.assertEqual( SCREAMING_SNAKE_CASE_ ,[ { """score""": ANY(SCREAMING_SNAKE_CASE_ ), """label""": ANY(SCREAMING_SNAKE_CASE_ ), """box""": {"""xmin""": ANY(SCREAMING_SNAKE_CASE_ ), """ymin""": ANY(SCREAMING_SNAKE_CASE_ ), """xmax""": ANY(SCREAMING_SNAKE_CASE_ ), """ymax""": ANY(SCREAMING_SNAKE_CASE_ )}, } for i in range(SCREAMING_SNAKE_CASE_ ) ] ,) @require_tf @unittest.skip("""Zero Shot Object Detection not implemented in TF""" ) def snake_case_ ( self ): '''simple docstring''' pass @require_torch def snake_case_ ( self ): '''simple docstring''' snake_case : str = pipeline( """zero-shot-object-detection""" ,model="""hf-internal-testing/tiny-random-owlvit-object-detection""" ) snake_case : Union[str, Any] = object_detector( """./tests/fixtures/tests_samples/COCO/000000039769.png""" ,candidate_labels=["""cat""", """remote""", """couch"""] ,threshold=0.64 ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}}, {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}}, {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}}, ] ,) snake_case : Optional[int] = object_detector( [ { """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""", """candidate_labels""": ["""cat""", """remote""", """couch"""], } ] ,threshold=0.64 ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ [ {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}}, {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}}, {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}}, {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}}, {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}}, ] ] ,) @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = pipeline("""zero-shot-object-detection""" ) snake_case : Dict = object_detector( """http://images.cocodataset.org/val2017/000000039769.jpg""" ,candidate_labels=["""cat""", """remote""", """couch"""] ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}}, {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}}, {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}}, {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}}, {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}}, ] ,) snake_case : int = object_detector( [ { """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""", """candidate_labels""": ["""cat""", """remote""", """couch"""], }, { """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""", """candidate_labels""": ["""cat""", """remote""", """couch"""], }, ] ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ [ {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}}, {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}}, {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}}, {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}}, {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}}, ], [ {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}}, {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}}, {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}}, {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}}, {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}}, ], ] ,) @require_tf @unittest.skip("""Zero Shot Object Detection not implemented in TF""" ) def snake_case_ ( self ): '''simple docstring''' pass @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : int = 0.2 snake_case : Dict = pipeline("""zero-shot-object-detection""" ) snake_case : int = object_detector( """http://images.cocodataset.org/val2017/000000039769.jpg""" ,candidate_labels=["""cat""", """remote""", """couch"""] ,threshold=SCREAMING_SNAKE_CASE_ ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}}, {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}}, {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}}, ] ,) @require_torch @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = 2 snake_case : Dict = pipeline("""zero-shot-object-detection""" ) snake_case : str = object_detector( """http://images.cocodataset.org/val2017/000000039769.jpg""" ,candidate_labels=["""cat""", """remote""", """couch"""] ,top_k=SCREAMING_SNAKE_CASE_ ,) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ,decimals=4 ) ,[ {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}}, {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}}, ] ,)
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
import argparse import os import torch from transformers.utils import WEIGHTS_NAME __lowercase : str = ['''small''', '''medium''', '''large'''] __lowercase : Any = '''lm_head.decoder.weight''' __lowercase : str = '''lm_head.weight''' def lowercase ( __A : str , __A : str ) -> List[Any]: '''simple docstring''' snake_case : Optional[int] = torch.load(__A ) snake_case : Union[str, Any] = d.pop(__A ) os.makedirs(__A , exist_ok=__A ) torch.save(__A , os.path.join(__A , __A ) ) if __name__ == "__main__": __lowercase : Any = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) __lowercase : Any = parser.parse_args() for MODEL in DIALOGPT_MODELS: __lowercase : List[Any] = os.path.join(args.dialogpt_path, f'''{MODEL}_ft.pkl''') __lowercase : List[str] = f'''./DialoGPT-{MODEL}''' convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
36
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = ['''image_processor''', '''tokenizer'''] __lowerCamelCase : int = '''CLIPImageProcessor''' __lowerCamelCase : Optional[Any] = ('''XLMRobertaTokenizer''', '''XLMRobertaTokenizerFast''') def __init__( self ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[int] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" ,SCREAMING_SNAKE_CASE_ ,) snake_case : List[Any] = kwargs.pop("""feature_extractor""" ) snake_case : str = 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__(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def __call__( self ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: snake_case : List[Any] = self.tokenizer(SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if images is not None: snake_case : int = self.image_processor(SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) if text is not None and images is not None: snake_case : Dict = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_ ) ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) @property def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = self.tokenizer.model_input_names snake_case : List[str] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowercase : Optional[int] = logging.get_logger(__name__) __lowercase : Optional[Any] = '''▁''' __lowercase : Union[str, Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} __lowercase : Optional[Any] = { '''vocab_file''': { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model''' ), } } __lowercase : Optional[Any] = { '''xlm-roberta-base''': 512, '''xlm-roberta-large''': 512, '''xlm-roberta-large-finetuned-conll02-dutch''': 512, '''xlm-roberta-large-finetuned-conll02-spanish''': 512, '''xlm-roberta-large-finetuned-conll03-english''': 512, '''xlm-roberta-large-finetuned-conll03-german''': 512, } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[Any] = ['''input_ids''', '''attention_mask'''] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_="<s>" ,SCREAMING_SNAKE_CASE_="</s>" ,SCREAMING_SNAKE_CASE_="</s>" ,SCREAMING_SNAKE_CASE_="<s>" ,SCREAMING_SNAKE_CASE_="<unk>" ,SCREAMING_SNAKE_CASE_="<pad>" ,SCREAMING_SNAKE_CASE_="<mask>" ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' # Mask token behave like a normal word, i.e. include the space before it snake_case : Tuple = AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else mask_token snake_case : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=SCREAMING_SNAKE_CASE_ ,eos_token=SCREAMING_SNAKE_CASE_ ,unk_token=SCREAMING_SNAKE_CASE_ ,sep_token=SCREAMING_SNAKE_CASE_ ,cls_token=SCREAMING_SNAKE_CASE_ ,pad_token=SCREAMING_SNAKE_CASE_ ,mask_token=SCREAMING_SNAKE_CASE_ ,sp_model_kwargs=self.sp_model_kwargs ,**SCREAMING_SNAKE_CASE_ ,) snake_case : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(SCREAMING_SNAKE_CASE_ ) ) snake_case : List[Any] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token snake_case : str = {"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case : str = 1 snake_case : List[Any] = len(self.sp_model ) + self.fairseq_offset snake_case : List[str] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ): '''simple docstring''' snake_case : Union[str, Any] = self.__dict__.copy() snake_case : List[Any] = None snake_case : int = self.sp_model.serialized_model_proto() return state def __setstate__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = d # for backward compatibility if not hasattr(self ,"""sp_model_kwargs""" ): snake_case : Optional[Any] = {} snake_case : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case : Any = [self.cls_token_id] snake_case : Union[str, Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ ,token_ids_a=SCREAMING_SNAKE_CASE_ ,already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Union[str, Any] = [self.sep_token_id] snake_case : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def snake_case_ ( self ): '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + 1 # Add the <mask> token def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.sp_model.encode(SCREAMING_SNAKE_CASE_ ,out_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case : str = self.sp_model.PieceToId(SCREAMING_SNAKE_CASE_ ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = """""".join(SCREAMING_SNAKE_CASE_ ).replace(SCREAMING_SNAKE_CASE_ ,""" """ ).strip() return out_string def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case : Optional[Any] = os.path.join( SCREAMING_SNAKE_CASE_ ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,SCREAMING_SNAKE_CASE_ ) elif not os.path.isfile(self.vocab_file ): with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as fi: snake_case : List[str] = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowercase : Union[str, Any] = { '''configuration_graphormer''': ['''GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GraphormerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Tuple = [ '''GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GraphormerForGraphClassification''', '''GraphormerModel''', '''GraphormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys __lowercase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" 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 - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking 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=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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 ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : Dict = logging.get_logger(__name__) __lowercase : Dict = { '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''', # See all SEW models at https://huggingface.co/models?filter=sew } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''sew''' def __init__( self ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=768 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=3072 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_="group" ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,SCREAMING_SNAKE_CASE_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,SCREAMING_SNAKE_CASE_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.05 ,SCREAMING_SNAKE_CASE_=10 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=10 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_="mean" ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=256 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=2 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ,pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = hidden_size snake_case : List[Any] = feat_extract_norm snake_case : List[str] = feat_extract_activation snake_case : int = list(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = list(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = list(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = conv_bias snake_case : Any = num_conv_pos_embeddings snake_case : List[str] = num_conv_pos_embedding_groups snake_case : Union[str, Any] = len(self.conv_dim ) snake_case : Optional[Any] = num_hidden_layers snake_case : List[str] = intermediate_size snake_case : List[Any] = squeeze_factor snake_case : Dict = hidden_act snake_case : Tuple = num_attention_heads snake_case : int = hidden_dropout snake_case : Tuple = attention_dropout snake_case : Tuple = activation_dropout snake_case : List[str] = feat_proj_dropout snake_case : Tuple = final_dropout snake_case : Tuple = layerdrop snake_case : Any = layer_norm_eps snake_case : Union[str, Any] = initializer_range snake_case : Optional[int] = 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 snake_case : int = apply_spec_augment snake_case : Any = mask_time_prob snake_case : int = mask_time_length snake_case : Any = mask_time_min_masks snake_case : List[Any] = mask_feature_prob snake_case : Dict = mask_feature_length snake_case : Any = mask_feature_min_masks # ctc loss snake_case : List[str] = ctc_loss_reduction snake_case : Union[str, Any] = ctc_zero_infinity # sequence classification snake_case : int = use_weighted_layer_sum snake_case : List[str] = classifier_proj_size @property def snake_case_ ( self ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
def lowercase ( __A : int = 200_0000 ) -> int: '''simple docstring''' snake_case : List[str] = [0 for i in range(n + 1 )] snake_case : Optional[Any] = 1 snake_case : Tuple = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , __A ): snake_case : Optional[int] = 1 snake_case : List[str] = 0 for i in range(__A ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'''{solution() = }''')
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
def lowercase ( __A : Any ) -> str: # noqa: E741 '''simple docstring''' snake_case : Any = len(__A ) snake_case : Dict = 0 snake_case : Optional[Any] = [0] * n snake_case : str = [False] * n snake_case : Tuple = [False] * n def dfs(__A : Dict , __A : Optional[Any] , __A : int , __A : List[str] ): if parent == root: out_edge_count += 1 snake_case : List[str] = True snake_case : List[str] = at for to in l[at]: if to == parent: pass elif not visited[to]: snake_case : List[str] = dfs(__A , __A , __A , __A ) snake_case : Optional[Any] = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: snake_case : List[str] = True # AP found via cycle if at == low[to]: snake_case : Any = True else: snake_case : Optional[Any] = min(low[at] , __A ) return out_edge_count for i in range(__A ): if not visited[i]: snake_case : List[Any] = 0 snake_case : Union[str, Any] = dfs(__A , __A , -1 , __A ) snake_case : Optional[int] = out_edge_count > 1 for x in range(len(__A ) ): if is_art[x] is True: print(__A ) # Adjacency list of graph __lowercase : Dict = { 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)
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
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(): __lowercase : str = '''pt''' elif is_tf_available(): __lowercase : str = '''tf''' else: __lowercase : int = '''jax''' class _A ( snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Tuple = ByTaTokenizer __lowerCamelCase : Union[str, Any] = False def snake_case_ ( self ): '''simple docstring''' super().setUp() snake_case : List[Any] = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def snake_case_ ( self ): '''simple docstring''' return ByTaTokenizer.from_pretrained("""google/byt5-small""" ) def snake_case_ ( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.tokenizer_class.from_pretrained(self.tmpdirname ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=20 ,SCREAMING_SNAKE_CASE_=5 ): '''simple docstring''' # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. snake_case : str = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): try: snake_case : Optional[Any] = tokenizer.decode([i] ,clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) except UnicodeDecodeError: pass toks.append((i, tok) ) snake_case : List[str] = list(filter(lambda SCREAMING_SNAKE_CASE_ : re.match(R"""^[ a-zA-Z]+$""" ,t[1] ) ,SCREAMING_SNAKE_CASE_ ) ) snake_case : Tuple = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: snake_case : List[str] = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: snake_case : Tuple = toks + toks # toks_str = [t[1] for t in toks] snake_case : Optional[Any] = [t[0] for t in toks] # Ensure consistency snake_case : Optional[Any] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ,clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: snake_case : List[Any] = ( tokenizer.decode([toks_ids[0]] ,clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + """ """ + tokenizer.decode(toks_ids[1:] ,clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: snake_case : Tuple = """ """ + output_txt snake_case : List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.ta_base_tokenizer snake_case : Any = tokenizer(["""hi</s>""", """I went to the gym</s>""", """</s>"""] ) snake_case : Tuple = tokenizer(["""hi""", """I went to the gym""", """"""] ) self.assertListEqual(batch_with_eos_added["""input_ids"""] ,batch_without_eos_added["""input_ids"""] ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.ta_base_tokenizer snake_case : Union[str, Any] = """Unicode €.""" snake_case : Any = tokenizer(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = [88, 113, 108, 102, 114, 103, 104, 35, 229, 133, 175, 49, 1] self.assertEqual(encoded["""input_ids"""] ,SCREAMING_SNAKE_CASE_ ) # decoding snake_case : Optional[Any] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,"""Unicode €.</s>""" ) snake_case : str = tokenizer("""e è é ê ë""" ) snake_case : Dict = [104, 35, 198, 171, 35, 198, 172, 35, 198, 173, 35, 198, 174, 1] self.assertEqual(encoded["""input_ids"""] ,SCREAMING_SNAKE_CASE_ ) # decoding snake_case : Dict = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,"""e è é ê ë</s>""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) ,"""e è é ê ë</s>""" ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.ta_base_tokenizer snake_case : List[str] = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off snake_case : Tuple = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 1, 0] # fmt: on snake_case : Optional[Any] = tokenizer(SCREAMING_SNAKE_CASE_ ,padding=SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if FRAMEWORK != "jax": snake_case : int = list(batch.input_ids.numpy()[0] ) else: snake_case : Any = list(batch.input_ids.tolist()[0] ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual((2, 37) ,batch.input_ids.shape ) self.assertEqual((2, 37) ,batch.attention_mask.shape ) def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = self.ta_base_tokenizer snake_case : Optional[Any] = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] snake_case : Dict = tokenizer(SCREAMING_SNAKE_CASE_ ,padding=SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" ,SCREAMING_SNAKE_CASE_ ) self.assertIn("""attention_mask""" ,SCREAMING_SNAKE_CASE_ ) self.assertNotIn("""decoder_input_ids""" ,SCREAMING_SNAKE_CASE_ ) self.assertNotIn("""decoder_attention_mask""" ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = self.ta_base_tokenizer snake_case : Dict = [ """Summary of the text.""", """Another summary.""", ] snake_case : Optional[Any] = tokenizer( text_target=SCREAMING_SNAKE_CASE_ ,max_length=32 ,padding="""max_length""" ,truncation=SCREAMING_SNAKE_CASE_ ,return_tensors=SCREAMING_SNAKE_CASE_ ) self.assertEqual(32 ,targets["""input_ids"""].shape[1] ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = self.ta_base_tokenizer snake_case : Dict = ["""A long paragraph for summarization. </s>"""] snake_case : int = ["""Summary of the text. </s>"""] # fmt: off snake_case : Optional[int] = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 35, 1] snake_case : int = [86, 120, 112, 112, 100, 117, 124, 35, 114, 105, 35, 119, 107, 104, 35, 119, 104, 123, 119, 49, 35, 1] # fmt: on snake_case : Dict = tokenizer(SCREAMING_SNAKE_CASE_ ,text_target=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,batch["""input_ids"""][0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,batch["""labels"""][0] ) def snake_case_ ( self ): '''simple docstring''' # safety check on max_len default value so we are sure the test works snake_case : List[str] = 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 snake_case : str = 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 snake_case : Union[str, Any] = tempfile.mkdtemp() snake_case : Optional[Any] = """ He is very happy, UNwant\u00E9d,running""" snake_case : Any = tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : int = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : int = after_tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) snake_case : str = 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 snake_case : Any = tempfile.mkdtemp() snake_case : Optional[Any] = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) snake_case : List[Any] = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) snake_case : Optional[int] = tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = after_tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertIn("""new_additional_special_token""" ,after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length ,42 ) snake_case : Dict = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ ,model_max_length=43 ) self.assertEqual(tokenizer.model_max_length ,43 ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = [] 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(SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ ,"""special_tokens_map.json""" ) ,encoding="""utf-8""" ) as json_file: snake_case : int = json.load(SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ ,"""tokenizer_config.json""" ) ,encoding="""utf-8""" ) as json_file: snake_case : int = json.load(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = [F"""<extra_id_{i}>""" for i in range(125 )] snake_case : List[Any] = added_tokens_extra_ids + [ """an_additional_special_token""" ] snake_case : Optional[int] = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(SCREAMING_SNAKE_CASE_ ,"""special_tokens_map.json""" ) ,"""w""" ,encoding="""utf-8""" ) as outfile: json.dump(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ ,"""tokenizer_config.json""" ) ,"""w""" ,encoding="""utf-8""" ) as outfile: json.dump(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # 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 snake_case : List[Any] = tokenizer_class.from_pretrained( SCREAMING_SNAKE_CASE_ ,) 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 snake_case : Any = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" ,lstrip=SCREAMING_SNAKE_CASE_ )] snake_case : int = tokenizer_class.from_pretrained( SCREAMING_SNAKE_CASE_ ,additional_special_tokens=SCREAMING_SNAKE_CASE_ ,) 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 snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = [] 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(SCREAMING_SNAKE_CASE_ ) snake_case : Any = tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertTrue(tokenizer.decode([255] ) == """""" ) def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens snake_case : Any = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ ,do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): snake_case : Tuple = ["""t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """x""", """t""", """</s>"""] snake_case : Union[str, Any] = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): snake_case : List[str] = [ """bos_token""", """eos_token""", """unk_token""", """sep_token""", """pad_token""", """cls_token""", """mask_token""", ] snake_case : Dict = 0 snake_case : Optional[int] = tokenizer.convert_ids_to_tokens( SCREAMING_SNAKE_CASE_ ,skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for attr in attributes_list: setattr(SCREAMING_SNAKE_CASE_ ,attr + """_id""" ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ ,attr + """_id""" ) ,SCREAMING_SNAKE_CASE_ ) setattr(SCREAMING_SNAKE_CASE_ ,attr + """_id""" ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ ,attr + """_id""" ) ,SCREAMING_SNAKE_CASE_ ) setattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens_ids""" ,[] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens""" ) ,[] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens_ids""" ) ,[] ) setattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens_ids""" ,[token_id_to_test_setters] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens""" ) ,[token_to_test_setters] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ ,"""additional_special_tokens_ids""" ) ,[token_id_to_test_setters] )
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
from transformers import BertTokenizerFast from .custom_tokenization import CustomTokenizer class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = CustomTokenizer pass
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=99 ,SCREAMING_SNAKE_CASE_=36 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=37 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=6 ,SCREAMING_SNAKE_CASE_=6 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=1000 ,): '''simple docstring''' snake_case : int = parent snake_case : Optional[Any] = batch_size snake_case : List[Any] = num_channels snake_case : Optional[Any] = image_size snake_case : List[Any] = patch_size snake_case : List[str] = is_training snake_case : Union[str, Any] = use_input_mask snake_case : Tuple = use_token_type_ids snake_case : str = use_labels snake_case : int = vocab_size snake_case : Optional[Any] = hidden_size snake_case : int = num_hidden_layers snake_case : Dict = num_attention_heads snake_case : Any = intermediate_size snake_case : int = hidden_act snake_case : Any = hidden_dropout_prob snake_case : int = attention_probs_dropout_prob snake_case : List[str] = max_position_embeddings snake_case : List[str] = type_vocab_size snake_case : List[Any] = type_sequence_label_size snake_case : Tuple = initializer_range snake_case : Optional[Any] = coordinate_size snake_case : Optional[Any] = shape_size snake_case : Dict = num_labels snake_case : Tuple = num_choices snake_case : str = scope snake_case : Optional[Any] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) snake_case : Dict = text_seq_length snake_case : Optional[Any] = (image_size // patch_size) ** 2 + 1 snake_case : Union[str, Any] = self.text_seq_length + self.image_seq_length def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = ids_tensor([self.batch_size, self.text_seq_length] ,self.vocab_size ) snake_case : Tuple = ids_tensor([self.batch_size, self.text_seq_length, 4] ,self.range_bbox ) snake_case : Optional[int] = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: snake_case : Dict = bbox[i, j, 3] snake_case : Dict = bbox[i, j, 1] snake_case : Optional[int] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: snake_case : Dict = bbox[i, j, 2] snake_case : Optional[Any] = bbox[i, j, 0] snake_case : Optional[int] = tmp_coordinate snake_case : Optional[Any] = tf.constant(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case : List[str] = None if self.use_input_mask: snake_case : str = random_attention_mask([self.batch_size, self.text_seq_length] ) snake_case : Optional[int] = None if self.use_token_type_ids: snake_case : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] ,self.type_vocab_size ) snake_case : Tuple = None snake_case : Union[str, Any] = None if self.use_labels: snake_case : List[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case : Any = ids_tensor([self.batch_size, self.text_seq_length] ,self.num_labels ) snake_case : Any = LayoutLMvaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,initializer_range=self.initializer_range ,coordinate_size=self.coordinate_size ,shape_size=self.shape_size ,input_size=self.image_size ,patch_size=self.patch_size ,) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : str = TFLayoutLMvaModel(config=SCREAMING_SNAKE_CASE_ ) # text + image snake_case : Optional[int] = model(SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = model( SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ ,token_type_ids=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ,) snake_case : int = model(SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) # text only snake_case : str = model(SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.text_seq_length, self.hidden_size) ) # image only snake_case : Optional[int] = model({"""pixel_values""": pixel_values} ,training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.image_seq_length, self.hidden_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = self.num_labels snake_case : Optional[int] = TFLayoutLMvaForSequenceClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : int = model( SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ ,token_type_ids=SCREAMING_SNAKE_CASE_ ,labels=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = self.num_labels snake_case : Optional[int] = TFLayoutLMvaForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = model( SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ ,token_type_ids=SCREAMING_SNAKE_CASE_ ,labels=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.text_seq_length, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[int] = 2 snake_case : Dict = TFLayoutLMvaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = model( SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ ,token_type_ids=SCREAMING_SNAKE_CASE_ ,start_positions=SCREAMING_SNAKE_CASE_ ,end_positions=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ,) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = self.prepare_config_and_inputs() ((snake_case) , (snake_case) , (snake_case) , (snake_case) , (snake_case) , (snake_case) , (snake_case) , (snake_case)) : Dict = config_and_inputs snake_case : Union[str, Any] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class _A ( snake_case , snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) __lowerCamelCase : List[str] = ( {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel} if is_tf_available() else {} ) __lowerCamelCase : Optional[Any] = False __lowerCamelCase : List[Any] = False __lowerCamelCase : Any = False def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' snake_case : str = copy.deepcopy(SCREAMING_SNAKE_CASE_ ) if model_class in get_values(SCREAMING_SNAKE_CASE_ ): snake_case : Any = { k: tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ ,1 ) ,(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(SCREAMING_SNAKE_CASE_ ,tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(SCREAMING_SNAKE_CASE_ ): snake_case : Optional[int] = tf.ones(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(SCREAMING_SNAKE_CASE_ ): snake_case : Dict = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) snake_case : Tuple = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(SCREAMING_SNAKE_CASE_ ): snake_case : List[Any] = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(SCREAMING_SNAKE_CASE_ ): snake_case : Dict = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) ,dtype=tf.intaa ) return inputs_dict def snake_case_ ( self ): '''simple docstring''' snake_case : str = TFLayoutLMvaModelTester(self ) snake_case : str = ConfigTester(self ,config_class=SCREAMING_SNAKE_CASE_ ,hidden_size=37 ) def snake_case_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case : List[str] = model_class(SCREAMING_SNAKE_CASE_ ) if getattr(SCREAMING_SNAKE_CASE_ ,"""hf_compute_loss""" ,SCREAMING_SNAKE_CASE_ ): # The number of elements in the loss should be the same as the number of elements in the label snake_case : Optional[Any] = self._prepare_for_class(inputs_dict.copy() ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() ,reverse=SCREAMING_SNAKE_CASE_ )[0] ] snake_case : List[Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs snake_case : Tuple = self._prepare_for_class(inputs_dict.copy() ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = prepared_for_class.pop("""input_ids""" ) snake_case : Dict = model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions snake_case : Union[str, Any] = self._prepare_for_class(inputs_dict.copy() ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : Any = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: snake_case : Union[str, Any] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: snake_case : Dict = -100 snake_case : Optional[Any] = tf.convert_to_tensor(SCREAMING_SNAKE_CASE_ ) snake_case : int = model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict snake_case : str = self._prepare_for_class(inputs_dict.copy() ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) snake_case : Dict = model(SCREAMING_SNAKE_CASE_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple snake_case : Any = self._prepare_for_class(inputs_dict.copy() ,SCREAMING_SNAKE_CASE_ ,return_labels=SCREAMING_SNAKE_CASE_ ) # Get keys that were added with the _prepare_for_class function snake_case : Optional[Any] = prepared_for_class.keys() - inputs_dict.keys() snake_case : Optional[int] = inspect.signature(model.call ).parameters snake_case : Tuple = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple snake_case : Optional[Any] = {0: """input_ids"""} for label_key in label_keys: snake_case : Dict = signature_names.index(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = label_key snake_case : List[str] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple snake_case : Dict = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: snake_case : Optional[Any] = prepared_for_class[value] snake_case : Optional[int] = tuple(SCREAMING_SNAKE_CASE_ ) # Send to model snake_case : Union[str, Any] = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def snake_case_ ( self ): '''simple docstring''' ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case : int = type self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) @slow def snake_case_ ( self ): '''simple docstring''' for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case : str = TFLayoutLMvaModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def lowercase ( ) -> Dict: '''simple docstring''' snake_case : List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class _A ( unittest.TestCase ): '''simple docstring''' @cached_property def snake_case_ ( self ): '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=SCREAMING_SNAKE_CASE_ ) if is_vision_available() else None @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Any = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) snake_case : int = self.default_image_processor snake_case : Optional[Any] = prepare_img() snake_case : str = image_processor(images=SCREAMING_SNAKE_CASE_ ,return_tensors="""tf""" ).pixel_values snake_case : Optional[int] = tf.constant([[1, 2]] ) snake_case : List[str] = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) ,axis=0 ) # forward pass snake_case : List[str] = model(input_ids=SCREAMING_SNAKE_CASE_ ,bbox=SCREAMING_SNAKE_CASE_ ,pixel_values=SCREAMING_SNAKE_CASE_ ,training=SCREAMING_SNAKE_CASE_ ) # verify the logits snake_case : List[Any] = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape ,SCREAMING_SNAKE_CASE_ ) snake_case : str = tf.constant( [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] ,SCREAMING_SNAKE_CASE_ ,atol=1E-4 ) )
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowercase : str = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[Any] = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys __lowercase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Tuple = FlaxStableDiffusionPipeline.from_pretrained( """stabilityai/stable-diffusion-2""" ,revision="""bf16""" ,dtype=jnp.bfloataa ,) snake_case : int = """A painting of a squirrel eating a burger""" snake_case : Dict = jax.device_count() snake_case : Optional[Any] = num_samples * [prompt] snake_case : Any = sd_pipe.prepare_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = replicate(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = shard(SCREAMING_SNAKE_CASE_ ) snake_case : Any = jax.random.PRNGKey(0 ) snake_case : List[Any] = jax.random.split(SCREAMING_SNAKE_CASE_ ,jax.device_count() ) snake_case : Dict = sd_pipe(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_inference_steps=25 ,jit=SCREAMING_SNAKE_CASE_ )[0] assert images.shape == (jax.device_count(), 1, 768, 768, 3) snake_case : Optional[int] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) snake_case : Union[str, Any] = images[0, 253:256, 253:256, -1] snake_case : List[Any] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) snake_case : Tuple = jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12] ) print(F"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = """stabilityai/stable-diffusion-2""" snake_case , snake_case : str = FlaxDPMSolverMultistepScheduler.from_pretrained(SCREAMING_SNAKE_CASE_ ,subfolder="""scheduler""" ) snake_case , snake_case : List[Any] = FlaxStableDiffusionPipeline.from_pretrained( SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ,revision="""bf16""" ,dtype=jnp.bfloataa ,) snake_case : int = scheduler_params snake_case : int = """A painting of a squirrel eating a burger""" snake_case : int = jax.device_count() snake_case : List[str] = num_samples * [prompt] snake_case : Union[str, Any] = sd_pipe.prepare_inputs(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = replicate(SCREAMING_SNAKE_CASE_ ) snake_case : Any = shard(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = jax.random.PRNGKey(0 ) snake_case : List[Any] = jax.random.split(SCREAMING_SNAKE_CASE_ ,jax.device_count() ) snake_case : Tuple = sd_pipe(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,num_inference_steps=25 ,jit=SCREAMING_SNAKE_CASE_ )[0] assert images.shape == (jax.device_count(), 1, 768, 768, 3) snake_case : List[Any] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) snake_case : str = images[0, 253:256, 253:256, -1] snake_case : List[Any] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) snake_case : Union[str, Any] = jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97] ) print(F"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __lowercase : Optional[int] = logging.get_logger(__name__) def lowercase ( __A : Union[str, Any] ) -> Tuple: '''simple docstring''' snake_case : Optional[Any] = torch.load(__A , map_location="""cpu""" ) if "model" in sd.keys(): snake_case : str = torch.load(__A , map_location="""cpu""" )["""model"""] # pop unnecessary weights snake_case : Optional[Any] = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(__A ) snake_case : Union[str, Any] = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: snake_case : Dict = sd.pop(__A ) snake_case : List[str] = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: snake_case : Dict = sd[key] # We split QKV in separate Q,K,V snake_case : str = key.replace(""".qkv_proj.""" , """.q_proj.""" ) snake_case : List[str] = key.replace(""".qkv_proj.""" , """.k_proj.""" ) snake_case : Union[str, Any] = key.replace(""".qkv_proj.""" , """.v_proj.""" ) snake_case : Optional[Any] = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 snake_case , snake_case , snake_case : Union[str, Any] = torch.split(__A , depth // 3 , dim=0 ) snake_case : Optional[int] = q snake_case : Optional[Any] = k snake_case : Any = v del sd[key] return sd @torch.no_grad() def lowercase ( __A : Tuple , __A : Dict , __A : List[str]=None ) -> Tuple: '''simple docstring''' snake_case : Optional[int] = load_checkpoint(__A ) if config is not None: snake_case : List[Any] = OPTConfig.from_pretrained(__A ) else: snake_case : Union[str, Any] = OPTConfig() snake_case : str = OPTModel(__A ).half().eval() model.load_state_dict(__A ) # Check results Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--fairseq_path''', type=str, help=( '''path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:''' ''' https://huggingface.co/models?other=opt_metasq''' ), ) parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--hf_config''', default=None, type=str, help='''Define HF config.''') __lowercase : Union[str, Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
import unittest import numpy as np from transformers import BertConfig, 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.bert.modeling_flax_bert import ( FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, ) class _A ( unittest.TestCase ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=13 ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=99 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=5 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=37 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=4 ,): '''simple docstring''' snake_case : Tuple = parent snake_case : Union[str, Any] = batch_size snake_case : Union[str, Any] = seq_length snake_case : List[str] = is_training snake_case : Optional[int] = use_attention_mask snake_case : List[Any] = use_token_type_ids snake_case : Tuple = use_labels snake_case : List[str] = vocab_size snake_case : Optional[Any] = hidden_size snake_case : str = num_hidden_layers snake_case : Optional[Any] = num_attention_heads snake_case : List[str] = intermediate_size snake_case : Tuple = hidden_act snake_case : Optional[Any] = hidden_dropout_prob snake_case : Union[str, Any] = attention_probs_dropout_prob snake_case : List[str] = max_position_embeddings snake_case : List[Any] = type_vocab_size snake_case : Tuple = type_sequence_label_size snake_case : int = initializer_range snake_case : List[Any] = num_choices def snake_case_ ( self ): '''simple docstring''' snake_case : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case : List[str] = None if self.use_attention_mask: snake_case : str = random_attention_mask([self.batch_size, self.seq_length] ) snake_case : Dict = None if self.use_token_type_ids: snake_case : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) snake_case : List[str] = BertConfig( 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=SCREAMING_SNAKE_CASE_ ,initializer_range=self.initializer_range ,) return config, input_ids, token_type_ids, attention_mask def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.prepare_config_and_inputs() snake_case , snake_case , snake_case , snake_case : List[Any] = config_and_inputs snake_case : List[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.prepare_config_and_inputs() snake_case , snake_case , snake_case , snake_case : List[Any] = config_and_inputs snake_case : Tuple = True snake_case : Optional[Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) snake_case : int = ids_tensor([self.batch_size, self.seq_length] ,vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class _A ( snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[str] = True __lowerCamelCase : Optional[Any] = ( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = FlaxBertModelTester(self ) @slow def snake_case_ ( self ): '''simple docstring''' # Only check this for base model, not necessary for all model classes. # This will also help speed-up tests. snake_case : str = FlaxBertModel.from_pretrained("""bert-base-cased""" ) snake_case : Tuple = model(np.ones((1, 1) ) ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ )
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
from scipy.stats import spearmanr import datasets __lowercase : Any = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' __lowercase : Optional[Any] = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' __lowercase : Dict = r'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""float""" ), """references""": datasets.Value("""float""" ), } ) ,reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html"""] ,) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' snake_case : Optional[Any] = spearmanr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": __lowercase : Tuple = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') __lowercase : int = f'''https://www.google.com/search?q={query}&num=100''' __lowercase : int = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: __lowercase : Tuple = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: __lowercase : Union[str, Any] = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
def lowercase ( __A : str , __A : str ) -> int: '''simple docstring''' if len(__A ) != len(__A ): raise ValueError("""String lengths must match!""" ) snake_case : List[Any] = 0 for chara, chara in zip(__A , __A ): if chara != chara: count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
from math import sqrt def lowercase ( __A : int = 100_0000 ) -> int: '''simple docstring''' snake_case : int = 0 snake_case : int = 0 snake_case : int while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 , 2 * max_cuboid_size + 1 ): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2 ).is_integer(): num_cuboids += ( min(__A , sum_shortest_sides // 2 ) - max(1 , sum_shortest_sides - max_cuboid_size ) + 1 ) return max_cuboid_size if __name__ == "__main__": print(f'''{solution() = }''')
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class _A ( ctypes.Structure ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = [('''size''', ctypes.c_int), ('''visible''', ctypes.c_byte)] def lowercase ( ) -> Optional[Any]: '''simple docstring''' if os.name == "nt": snake_case : int = CursorInfo() snake_case : Union[str, Any] = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) snake_case : Union[str, Any] = False ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write("""\033[?25l""" ) sys.stdout.flush() def lowercase ( ) -> Tuple: '''simple docstring''' if os.name == "nt": snake_case : Optional[Any] = CursorInfo() snake_case : Dict = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__A , ctypes.byref(__A ) ) snake_case : Optional[int] = True ctypes.windll.kernelaa.SetConsoleCursorInfo(__A , ctypes.byref(__A ) ) elif os.name == "posix": sys.stdout.write("""\033[?25h""" ) sys.stdout.flush() @contextmanager def lowercase ( ) -> Union[str, Any]: '''simple docstring''' try: hide_cursor() yield finally: show_cursor()
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1