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 import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCamelCase_ : def __init__( self , __lowerCAmelCase , __lowerCAmelCase=1_3 , __lowerCAmelCase=3_0 , __lowerCAmelCase=2 , __lowerCAmelCase=3 , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=3_2 , __lowerCAmelCase=2 , __lowerCAmelCase=4 , __lowerCAmelCase=3_7 , __lowerCAmelCase="gelu" , __lowerCAmelCase=0.1 , __lowerCAmelCase=0.1 , __lowerCAmelCase=1_0 , __lowerCAmelCase=0.02 , __lowerCAmelCase=3 , __lowerCAmelCase=0.6 , __lowerCAmelCase=None , ): """simple docstring""" __magic_name__ :Tuple = parent __magic_name__ :List[str] = batch_size __magic_name__ :Any = image_size __magic_name__ :List[str] = patch_size __magic_name__ :Optional[int] = num_channels __magic_name__ :int = is_training __magic_name__ :Tuple = use_labels __magic_name__ :List[Any] = hidden_size __magic_name__ :Dict = num_hidden_layers __magic_name__ :Union[str, Any] = num_attention_heads __magic_name__ :Any = intermediate_size __magic_name__ :List[Any] = hidden_act __magic_name__ :Tuple = hidden_dropout_prob __magic_name__ :str = attention_probs_dropout_prob __magic_name__ :int = type_sequence_label_size __magic_name__ :Dict = initializer_range __magic_name__ :Optional[Any] = mask_ratio __magic_name__ :Any = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) __magic_name__ :int = (image_size // patch_size) ** 2 __magic_name__ :List[Any] = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def A ( self ): """simple docstring""" __magic_name__ :List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __magic_name__ :List[str] = None if self.use_labels: __magic_name__ :Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __magic_name__ :Dict = self.get_config() return config, pixel_values, labels def A ( self ): """simple docstring""" return ViTMAEConfig( 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 , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_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=__lowerCAmelCase , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" __magic_name__ :Union[str, Any] = TFViTMAEModel(config=__lowerCAmelCase ) __magic_name__ :Union[str, Any] = model(__lowerCAmelCase , training=__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" __magic_name__ :Any = TFViTMAEForPreTraining(__lowerCAmelCase ) __magic_name__ :List[str] = model(__lowerCAmelCase , training=__lowerCAmelCase ) # expected sequence length = num_patches __magic_name__ :List[str] = (self.image_size // self.patch_size) ** 2 __magic_name__ :str = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images __magic_name__ :Optional[Any] = 1 __magic_name__ :Union[str, Any] = TFViTMAEForPreTraining(__lowerCAmelCase ) __magic_name__ :str = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __magic_name__ :Union[str, Any] = model(__lowerCAmelCase , training=__lowerCAmelCase ) __magic_name__ :List[str] = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def A ( self ): """simple docstring""" __magic_name__ :List[str] = self.prepare_config_and_inputs() ((__magic_name__) , (__magic_name__) , (__magic_name__)) :Tuple = config_and_inputs __magic_name__ :Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowerCamelCase_ ( lowerCamelCase , lowerCamelCase , unittest.TestCase ): a__ = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () a__ = {'''feature-extraction''': TFViTMAEModel} if is_tf_available() else {} a__ = False a__ = False a__ = False a__ = False def A ( self ): """simple docstring""" __magic_name__ :List[str] = TFViTMAEModelTester(self ) __magic_name__ :Optional[Any] = ConfigTester(self , config_class=__lowerCAmelCase , has_text_modality=__lowerCAmelCase , hidden_size=3_7 ) def A ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def A ( self ): """simple docstring""" pass def A ( self ): """simple docstring""" __magic_name__ , __magic_name__ :Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __magic_name__ :Dict = model_class(__lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) __magic_name__ :Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__lowerCAmelCase , tf.keras.layers.Layer ) ) def A ( self ): """simple docstring""" __magic_name__ , __magic_name__ :Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __magic_name__ :Optional[int] = model_class(__lowerCAmelCase ) __magic_name__ :Dict = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __magic_name__ :Optional[Any] = [*signature.parameters.keys()] __magic_name__ :Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*__lowerCAmelCase ) def A ( self ): """simple docstring""" # make the mask reproducible np.random.seed(2 ) __magic_name__ , __magic_name__ :List[Any] = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ :Any = int((config.image_size // config.patch_size) ** 2 ) __magic_name__ :str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: __magic_name__ :int = model_class(__lowerCAmelCase ) __magic_name__ :str = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :str = model(__lowerCAmelCase , noise=__lowerCAmelCase ) __magic_name__ :str = copy.deepcopy(self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) ) __magic_name__ :Optional[Any] = model(**__lowerCAmelCase , noise=__lowerCAmelCase ) __magic_name__ :Any = outputs_dict[0].numpy() __magic_name__ :Any = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def A ( self ): """simple docstring""" # make the mask reproducible np.random.seed(2 ) __magic_name__ , __magic_name__ :Any = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ :str = int((config.image_size // config.patch_size) ** 2 ) __magic_name__ :Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(__lowerCAmelCase ): __magic_name__ :Dict = {} for k, v in inputs_dict.items(): if tf.is_tensor(__lowerCAmelCase ): __magic_name__ :Optional[int] = v.numpy() else: __magic_name__ :Union[str, Any] = np.array(__lowerCAmelCase ) return inputs_np_dict for model_class in self.all_model_classes: __magic_name__ :Union[str, Any] = model_class(__lowerCAmelCase ) __magic_name__ :Optional[Any] = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Any = prepare_numpy_arrays(__lowerCAmelCase ) __magic_name__ :Dict = model(__lowerCAmelCase , noise=__lowerCAmelCase ) __magic_name__ :Dict = model(**__lowerCAmelCase , noise=__lowerCAmelCase ) self.assert_outputs_same(__lowerCAmelCase , __lowerCAmelCase ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" # make masks reproducible np.random.seed(2 ) __magic_name__ :Tuple = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) __magic_name__ :Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) __magic_name__ :List[str] = tf.constant(__lowerCAmelCase ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument __magic_name__ :List[Any] = tf_noise super().check_pt_tf_models(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) def A ( self ): """simple docstring""" # make mask reproducible np.random.seed(2 ) __magic_name__ , __magic_name__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ :int = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(__lowerCAmelCase ) if module_member_name.endswith('''MainLayer''' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('''MainLayer''' )] == model_class.__name__[: -len('''Model''' )] for module_member in (getattr(__lowerCAmelCase , __lowerCAmelCase ),) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(__lowerCAmelCase , '''_keras_serializable''' , __lowerCAmelCase ) } __magic_name__ :Dict = int((config.image_size // config.patch_size) ** 2 ) __magic_name__ :Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) __magic_name__ :Dict = tf.convert_to_tensor(__lowerCAmelCase ) inputs_dict.update({'''noise''': noise} ) for main_layer_class in tf_main_layer_classes: __magic_name__ :str = main_layer_class(__lowerCAmelCase ) __magic_name__ :Optional[Any] = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } __magic_name__ :List[str] = tf.keras.Model(__lowerCAmelCase , outputs=main_layer(__lowerCAmelCase ) ) __magic_name__ :Tuple = model(__lowerCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: __magic_name__ :List[Any] = os.path.join(__lowerCAmelCase , '''keras_model.h5''' ) model.save(__lowerCAmelCase ) __magic_name__ :Dict = tf.keras.models.load_model( __lowerCAmelCase , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(__lowerCAmelCase , tf.keras.Model ) __magic_name__ :int = model(__lowerCAmelCase ) self.assert_outputs_same(__lowerCAmelCase , __lowerCAmelCase ) @slow def A ( self ): """simple docstring""" # make mask reproducible np.random.seed(2 ) __magic_name__ , __magic_name__ :List[str] = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ :Dict = int((config.image_size // config.patch_size) ** 2 ) __magic_name__ :List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: __magic_name__ :Tuple = model_class(__lowerCAmelCase ) __magic_name__ :str = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :List[Any] = model(__lowerCAmelCase , noise=__lowerCAmelCase ) if model_class.__name__ == "TFViTMAEModel": __magic_name__ :Dict = outputs.last_hidden_state.numpy() __magic_name__ :Any = 0 else: __magic_name__ :Dict = outputs.logits.numpy() __magic_name__ :Union[str, Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__lowerCAmelCase , saved_model=__lowerCAmelCase ) __magic_name__ :List[str] = model_class.from_pretrained(__lowerCAmelCase ) __magic_name__ :int = model(__lowerCAmelCase , noise=__lowerCAmelCase ) if model_class.__name__ == "TFViTMAEModel": __magic_name__ :Optional[int] = after_outputs['''last_hidden_state'''].numpy() __magic_name__ :str = 0 else: __magic_name__ :str = after_outputs['''logits'''].numpy() __magic_name__ :Optional[Any] = 0 __magic_name__ :int = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(__lowerCAmelCase , 1E-5 ) def A ( self ): """simple docstring""" # make mask reproducible np.random.seed(2 ) __magic_name__ , __magic_name__ :Dict = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ :List[str] = int((config.image_size // config.patch_size) ** 2 ) __magic_name__ :List[str] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: __magic_name__ :Union[str, Any] = model_class(__lowerCAmelCase ) __magic_name__ :Union[str, Any] = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Tuple = model(__lowerCAmelCase , noise=__lowerCAmelCase ) __magic_name__ :Tuple = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(__lowerCAmelCase ) __magic_name__ :List[Any] = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config __magic_name__ :Union[str, Any] = model_class.from_config(model.config ) __magic_name__ :Dict = new_model(__lowerCAmelCase ) # Build model new_model.set_weights(model.get_weights() ) __magic_name__ :Any = new_model(__lowerCAmelCase , noise=__lowerCAmelCase ) self.assert_outputs_same(__lowerCAmelCase , __lowerCAmelCase ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def A ( self ): """simple docstring""" pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def A ( self ): """simple docstring""" pass @slow def A ( self ): """simple docstring""" __magic_name__ :Any = TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(__lowerCAmelCase ) def __lowercase ( ): """simple docstring""" __magic_name__ :Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class lowerCamelCase_ ( unittest.TestCase ): @cached_property def A ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def A ( self ): """simple docstring""" # make random mask reproducible across the PT and TF model np.random.seed(2 ) __magic_name__ :str = TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ) __magic_name__ :Tuple = self.default_image_processor __magic_name__ :Dict = prepare_img() __magic_name__ :Dict = image_processor(images=__lowerCAmelCase , return_tensors='''tf''' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) __magic_name__ :Tuple = ViTMAEConfig() __magic_name__ :Union[str, Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) __magic_name__ :List[str] = np.random.uniform(size=(1, num_patches) ) # forward pass __magic_name__ :str = model(**__lowerCAmelCase , noise=__lowerCAmelCase ) # verify the logits __magic_name__ :List[Any] = tf.convert_to_tensor([1, 1_9_6, 7_6_8] ) self.assertEqual(outputs.logits.shape , __lowerCAmelCase ) __magic_name__ :int = tf.convert_to_tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , __lowerCAmelCase , atol=1E-4 )
0
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = CLIPTokenizer _lowercase = CLIPTokenizerFast _lowercase = True _lowercase = {} _lowercase = False def _UpperCamelCase( self : List[Any] ): super().setUp() # fmt: off a__ : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on a__ : Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a__ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] a__ : Optional[Any] = {"unk_token": "<unk>"} a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def _UpperCamelCase( self : Dict , **lowerCamelCase__ : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] , **lowerCamelCase__ : Optional[int] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : int = "lower newer" a__ : Optional[int] = "lower newer" return input_text, output_text def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a__ : int = "lower newer" a__ : List[str] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] a__ : Union[str, Any] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = tokens + [tokenizer.unk_token] a__ : Union[str, Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) @require_ftfy def _UpperCamelCase( self : Optional[Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : List[str] = self.tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : Any = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : int = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." a__ : Optional[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : Dict = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways a__ : Optional[Any] = "xa\u0303y" + " " + "x\xe3y" a__ : Optional[int] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of space type a__ : str = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: a__ : Any = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of line break type a__ : Union[str, Any] = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: a__ : List[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : str = "hello" # `hello` is a token in the vocabulary of `pretrained_name` a__ : Tuple = f'''{text_of_1_token} {text_of_1_token}''' a__ : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Union[str, Any] = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCamelCase__ ) + 1, len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) a__ : Optional[Any] = f''' {text}''' a__ : str = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Dict = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCamelCase__ ) + 1, 1 + len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) def _UpperCamelCase( self : int ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCamelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _UpperCamelCase( self : int ): super().test_tokenization_python_rust_equals() def _UpperCamelCase( self : str ): # CLIP always lower cases letters pass
37
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/config.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/config.json''' # See all FNet models at https://huggingface.co/models?filter=fnet } class __lowerCamelCase (_a ): _lowercase = """fnet""" def __init__( self: Optional[Any],A_: str=3_2000,A_: Optional[Any]=768,A_: str=12,A_: List[str]=3072,A_: Union[str, Any]="gelu_new",A_: Optional[int]=0.1,A_: List[str]=512,A_: Optional[Any]=4,A_: Optional[int]=0.0_2,A_: Optional[Any]=1E-12,A_: int=False,A_: Any=512,A_: Optional[Any]=3,A_: List[Any]=1,A_: Tuple=2,**A_: Any,): '''simple docstring''' super().__init__(pad_token_id=A_,bos_token_id=A_,eos_token_id=A_,**A_ ) __UpperCamelCase = vocab_size __UpperCamelCase = max_position_embeddings __UpperCamelCase = hidden_size __UpperCamelCase = num_hidden_layers __UpperCamelCase = intermediate_size __UpperCamelCase = hidden_act __UpperCamelCase = hidden_dropout_prob __UpperCamelCase = initializer_range __UpperCamelCase = type_vocab_size __UpperCamelCase = layer_norm_eps __UpperCamelCase = use_tpu_fourier_optimizations __UpperCamelCase = tpu_short_seq_length
1
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase : Dict = """<<<<<<< This should probably be modified because it mentions: """ UpperCamelCase : List[Any] = """======= >>>>>>> """ UpperCamelCase : Optional[Any] = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] UpperCamelCase : 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 UpperCamelCase_ ( __a ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class A__ ( A__ ): """simple docstring""" @staticmethod def _UpperCamelCase( lowerCamelCase__ : ArgumentParser ): a__ : List[str] = parser.add_parser( "convert" , help="Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset." , ) train_parser.add_argument( "--tfds_path" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to a TensorFlow Datasets folder to convert or a single tfds file to convert." , ) train_parser.add_argument( "--datasets_directory" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to the HuggingFace Datasets folder." ) train_parser.set_defaults(func=lowerCamelCase__ ) def __init__( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str , *lowerCamelCase__ : Tuple ): a__ : str = get_logger("datasets-cli/converting" ) a__ : Optional[Any] = tfds_path a__ : Optional[int] = datasets_directory def _UpperCamelCase( self : int ): if os.path.isdir(self._tfds_path ): a__ : List[str] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a__ : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("--tfds_path is neither a directory nor a file. Please check path." ) a__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a__ : Tuple = [] a__ : str = [] a__ : List[Any] = {} if os.path.isdir(self._tfds_path ): a__ : List[str] = os.listdir(lowerCamelCase__ ) else: a__ : Union[str, Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'''Looking at file {f_name}''' ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Dict = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) if not os.path.isfile(lowerCamelCase__ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("Skipping file" ) continue with open(lowerCamelCase__ , encoding="utf-8" ) as f: a__ : List[Any] = f.readlines() a__ : Union[str, Any] = [] a__ : Union[str, Any] = False a__ : Union[str, Any] = False a__ : Dict = [] for line in lines: a__ : Optional[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: a__ : List[Any] = "import datasets\n" elif "import tensorflow" in out_line: # order is important here a__ : List[str] = "" continue elif "from absl import logging" in out_line: a__ : Dict = "from datasets import logging\n" elif "getLogger" in out_line: a__ : List[Any] = out_line.replace("getLogger" , "get_logger" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a__ : List[str] = True a__ : Dict = list(filter(lambda lowerCamelCase__ : e in out_line , lowerCamelCase__ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(lowerCamelCase__ ) + "\n" ) out_lines.append(lowerCamelCase__ ) out_lines.append(lowerCamelCase__ ) continue else: for pattern, replacement in TO_CONVERT: a__ : Tuple = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a__ : Optional[int] = re.match(r"from\stensorflow_datasets.*import\s([^\.\r\n]+)" , lowerCamelCase__ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split("," ) ) a__ : Optional[Any] = "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: a__ : Optional[int] = True out_lines.append(lowerCamelCase__ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a__ : Dict = f_name.replace(".py" , "" ) a__ : Optional[int] = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) 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(lowerCamelCase__ ) if needs_manual_update: with_manual_update.append(lowerCamelCase__ ) with open(lowerCamelCase__ , "w" , encoding="utf-8" ) as f: f.writelines(lowerCamelCase__ ) self._logger.info(f'''Converted in {output_file}''' ) for utils_file in utils_files: try: a__ : Any = os.path.basename(lowerCamelCase__ ) a__ : Optional[int] = imports_to_builder_map[f_name.replace(".py" , "" )] self._logger.info(f'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(lowerCamelCase__ , lowerCamelCase__ ) 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\'.''' )
37
0
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers UpperCAmelCase_ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py') def SCREAMING_SNAKE_CASE_ ( _snake_case :str , _snake_case :Union[str, Any]=None ) -> Any: require_version(deps[pkg] , _snake_case )
2
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class A__ ( A__ ): """simple docstring""" _lowercase = '' _lowercase = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _lowercase = None # compression type in fsspec. ex: "gzip" _lowercase = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , lowerCamelCase__ : str = "" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , **lowerCamelCase__ : List[str] ): super().__init__(self , **lowerCamelCase__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode a__ : str = fsspec.open( lowerCamelCase__ , mode="rb" , protocol=lowerCamelCase__ , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) a__ : Optional[int] = os.path.basename(self.file.path.split("::" )[0] ) a__ : int = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) a__ : List[Any] = None @classmethod def _UpperCamelCase( cls : int , lowerCamelCase__ : int ): # compressed file paths are always relative to the archive root return super()._strip_protocol(lowerCamelCase__ ).lstrip("/" ) def _UpperCamelCase( self : Dict ): if self.dir_cache is None: a__ : Dict = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} a__ : int = {f["name"]: f} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : str ): return self.file.open().read() def _UpperCamelCase( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : int=None , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[str]=None , **lowerCamelCase__ : Optional[Any] , ): a__ : Optional[int] = self._strip_protocol(lowerCamelCase__ ) if mode != "rb": raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class A__ ( A__ ): """simple docstring""" _lowercase = 'bz2' _lowercase = 'bz2' _lowercase = '.bz2' class A__ ( A__ ): """simple docstring""" _lowercase = 'gzip' _lowercase = 'gzip' _lowercase = '.gz' class A__ ( A__ ): """simple docstring""" _lowercase = 'lz4' _lowercase = 'lz4' _lowercase = '.lz4' class A__ ( A__ ): """simple docstring""" _lowercase = 'xz' _lowercase = 'xz' _lowercase = '.xz' class A__ ( A__ ): """simple docstring""" _lowercase = 'zstd' _lowercase = 'zstd' _lowercase = '.zst' def __init__( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , lowerCamelCase__ : int = DEFAULT_BLOCK_SIZE , **lowerCamelCase__ : Tuple , ): super().__init__( fo=lowerCamelCase__ , mode=lowerCamelCase__ , target_protocol=lowerCamelCase__ , target_options=lowerCamelCase__ , block_size=lowerCamelCase__ , **lowerCamelCase__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 a__ : Any = self.file.__enter__ class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : str ): a__ : List[Any] = file_ def __enter__( self : str ): self._file.__enter__() return self def __exit__( self : int , *lowerCamelCase__ : List[str] , **lowerCamelCase__ : int ): self._file.__exit__(*lowerCamelCase__ , **lowerCamelCase__ ) def __iter__( self : List[str] ): return iter(self._file ) def _UpperCamelCase( self : Any ): return next(self._file ) def __getattr__( self : Optional[Any] , lowerCamelCase__ : Tuple ): return getattr(self._file , lowerCamelCase__ ) def fixed_enter(*lowerCamelCase__ : List[str] , **lowerCamelCase__ : str ): return WrappedFile(_enter(*lowerCamelCase__ , **lowerCamelCase__ ) ) a__ : Any = fixed_enter
37
0
'''simple docstring''' def A_( A : int , A : Any): UpperCamelCase = '' for i in table: res += inp[i - 1] return res def A_( A : Union[str, Any]): return data[1:] + data[0] def A_( A : List[Any] , A : int): UpperCamelCase = '' for i in range(len(A)): if a[i] == b[i]: res += "0" else: res += "1" return res def A_( A : List[str] , A : Optional[int]): UpperCamelCase = int('0b' + data[0] + data[-1] , 2) UpperCamelCase = int('0b' + data[1:3] , 2) return bin(s[row][col])[2:] def A_( A : int , A : Dict , A : Union[str, Any] , A : Tuple , A : Tuple): UpperCamelCase = message[:4] UpperCamelCase = message[4:] UpperCamelCase = apply_table(A , A) UpperCamelCase = xor(A , A) UpperCamelCase = apply_sbox(A , temp[:4]) # noqa: E741 UpperCamelCase = apply_sbox(A , temp[4:]) UpperCamelCase = '0' * (2 - len(A)) + l # noqa: E741 UpperCamelCase = '0' * (2 - len(A)) + r UpperCamelCase = apply_table(l + r , A) UpperCamelCase = xor(A , A) return temp + right if __name__ == "__main__": lowerCAmelCase : Dict = input('Enter 10 bit key: ') lowerCAmelCase : Tuple = input('Enter 8 bit message: ') lowerCAmelCase : Any = [6, 3, 7, 4, 8, 5, 10, 9] lowerCAmelCase : Any = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] lowerCAmelCase : Optional[int] = [2, 4, 3, 1] lowerCAmelCase : List[str] = [2, 6, 3, 1, 4, 8, 5, 7] lowerCAmelCase : Tuple = [4, 1, 3, 5, 7, 2, 8, 6] lowerCAmelCase : Union[str, Any] = [4, 1, 2, 3, 2, 3, 4, 1] lowerCAmelCase : Tuple = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] lowerCAmelCase : Tuple = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation lowerCAmelCase : Optional[Any] = apply_table(key, paa_table) lowerCAmelCase : int = temp[:5] lowerCAmelCase : str = temp[5:] lowerCAmelCase : List[Any] = left_shift(left) lowerCAmelCase : List[str] = left_shift(right) lowerCAmelCase : Tuple = apply_table(left + right, pa_table) lowerCAmelCase : Optional[int] = left_shift(left) lowerCAmelCase : List[str] = left_shift(right) lowerCAmelCase : int = left_shift(left) lowerCAmelCase : str = left_shift(right) lowerCAmelCase : int = apply_table(left + right, pa_table) # encryption lowerCAmelCase : Union[str, Any] = apply_table(message, IP) lowerCAmelCase : Dict = function(expansion, sa, sa, keya, temp) lowerCAmelCase : int = temp[4:] + temp[:4] lowerCAmelCase : str = function(expansion, sa, sa, keya, temp) lowerCAmelCase : Dict = apply_table(temp, IP_inv) print('Cipher text is:', CT) # decryption lowerCAmelCase : Tuple = apply_table(CT, IP) lowerCAmelCase : Optional[int] = function(expansion, sa, sa, keya, temp) lowerCAmelCase : List[Any] = temp[4:] + temp[:4] lowerCAmelCase : Dict = function(expansion, sa, sa, keya, temp) lowerCAmelCase : Dict = apply_table(temp, IP_inv) print('Plain text after decypting is:', PT)
3
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Union[str, Any] = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") a__ : Union[str, Any] = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__a ): os.makedirs(__a ) a__ : Any = model.state_dict() def to_tf_var_name(__a ): for patt, repl in iter(__a ): a__ : Tuple = name.replace(__a , __a ) return f'''bert/{name}''' def create_tf_var(__a , __a , __a ): a__ : Tuple = tf.dtypes.as_dtype(tensor.dtype ) a__ : Dict = tf.get_variable(dtype=__a , shape=tensor.shape , name=__a , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__a ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: a__ : int = to_tf_var_name(__a ) a__ : Union[str, Any] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): a__ : int = torch_tensor.T a__ : Optional[Any] = create_tf_var(tensor=__a , name=__a , session=__a ) tf.keras.backend.set_value(__a , __a ) a__ : int = session.run(__a ) print(f'''Successfully created {tf_name}: {np.allclose(__a , __a )}''' ) a__ : Any = tf.train.Saver(tf.trainable_variables() ) saver.save(__a , os.path.join(__a , model_name.replace("-" , "_" ) + ".ckpt" ) ) def UpperCamelCase_ ( __a=None ) -> int: a__ : Dict = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__a , required=__a , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__a , default=__a , required=__a , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__a , required=__a , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__a , required=__a , help="Directory in which to save tensorflow model" ) a__ : Optional[Any] = parser.parse_args(__a ) a__ : Tuple = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__a , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
37
0
"""simple docstring""" def _SCREAMING_SNAKE_CASE (): return [list(range(1000 - i , -1000 - i , -1 ) ) for i in range(1000 )] __UpperCamelCase : Union[str, Any] = generate_large_matrix() __UpperCamelCase : Tuple = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : list[list[int]] ): assert all(row == sorted(_UpperCAmelCase , reverse=_UpperCAmelCase ) for row in grid ) assert all(list(_UpperCAmelCase ) == sorted(_UpperCAmelCase , reverse=_UpperCAmelCase ) for col in zip(*_UpperCAmelCase ) ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : list[int] ): lowerCAmelCase = 0 lowerCAmelCase = len(_UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: lowerCAmelCase = (left + right) // 2 lowerCAmelCase = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: lowerCAmelCase = mid + 1 else: lowerCAmelCase = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(_UpperCAmelCase ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : list[list[int]] ): lowerCAmelCase = 0 lowerCAmelCase = len(grid[0] ) for i in range(len(_UpperCAmelCase ) ): lowerCAmelCase = find_negative_index(grid[i][:bound] ) total += bound return (len(_UpperCAmelCase ) * len(grid[0] )) - total def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : list[list[int]] ): return len([number for row in grid for number in row if number < 0] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : list[list[int]] ): lowerCAmelCase = 0 for row in grid: for i, number in enumerate(_UpperCAmelCase ): if number < 0: total += len(_UpperCAmelCase ) - i break return total def _SCREAMING_SNAKE_CASE (): from timeit import timeit print('Running benchmarks' ) lowerCAmelCase = ( 'from __main__ import count_negatives_binary_search, ' 'count_negatives_brute_force, count_negatives_brute_force_with_break, grid' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): lowerCAmelCase = timeit(F'{func}(grid=grid)' , setup=_UpperCAmelCase , number=500 ) print(F'{func}() took {time:0.4f} seconds' ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
4
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=2 , lowerCamelCase__ : Any=24 , lowerCamelCase__ : Optional[Any]=16 , lowerCamelCase__ : int=True , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[Any]=32 , lowerCamelCase__ : List[str]=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Optional[Any]=37 , lowerCamelCase__ : Any="gelu" , lowerCamelCase__ : Union[str, Any]=0.1 , lowerCamelCase__ : Optional[int]=0.1 , lowerCamelCase__ : str=10 , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : str=None , lowerCamelCase__ : List[str]=2 , lowerCamelCase__ : Optional[Any]=2 , ): a__ : str = parent a__ : Any = batch_size a__ : Dict = patch_size a__ : List[Any] = max_length a__ : str = num_mel_bins a__ : Optional[Any] = is_training a__ : Optional[int] = use_labels a__ : List[Any] = hidden_size a__ : str = num_hidden_layers a__ : Any = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : List[str] = hidden_act a__ : str = hidden_dropout_prob a__ : Tuple = attention_probs_dropout_prob a__ : List[Any] = type_sequence_label_size a__ : Any = initializer_range a__ : str = scope a__ : List[str] = frequency_stride a__ : Union[str, Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) a__ : List[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 a__ : List[str] = (self.max_length - self.patch_size) // self.time_stride + 1 a__ : Tuple = frequency_out_dimension * time_out_dimension a__ : List[str] = num_patches + 2 def _UpperCamelCase( self : List[str] ): a__ : Any = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) a__ : List[Any] = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : List[str] = self.get_config() return config, input_values, labels def _UpperCamelCase( self : Optional[int] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] ): a__ : List[Any] = ASTModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Dict = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : str ): a__ : Dict = self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ) : Optional[int] = config_and_inputs a__ : List[Any] = {"input_values": input_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) _lowercase = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def _UpperCamelCase( self : str ): a__ : str = ASTModelTester(self ) a__ : Any = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[str] ): self.config_tester.run_common_tests() @unittest.skip(reason="AST does not use inputs_embeds" ) def _UpperCamelCase( self : List[str] ): pass def _UpperCamelCase( self : Optional[int] ): a__, a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Any = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : Tuple ): a__, a__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Dict = model_class(lowerCamelCase__ ) a__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Optional[Any] = ["input_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Union[str, Any] = ASTModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : Optional[int] = hf_hub_download( repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset" ) a__, a__ : List[str] = torchaudio.load(__a ) return audio, sampling_rate @require_torch @require_torchaudio class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _UpperCamelCase( self : List[str] ): return ( ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ) if is_torchaudio_available() else None ) @slow def _UpperCamelCase( self : Optional[int] ): a__ : int = self.default_feature_extractor a__ : Optional[Any] = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ).to(lowerCamelCase__ ) a__ : Any = self.default_feature_extractor a__, a__ : Dict = prepare_audio() a__ : str = audio.squeeze().numpy() a__ : Any = feature_extractor(lowerCamelCase__ , sampling_rate=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(**lowerCamelCase__ ) # verify the logits a__ : Union[str, Any] = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) a__ : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) )
37
0
'''simple docstring''' def A (): _lowerCAmelCase = [] _lowerCAmelCase = 1 while len(__lowerCamelCase ) < 1e6: constant.append(str(__lowerCamelCase ) ) i += 1 _lowerCAmelCase = """""".join(__lowerCamelCase ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[99999] ) * int(constant[999999] ) ) if __name__ == "__main__": print(solution())
5
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast 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 UpperCamelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = XGLMTokenizer _lowercase = XGLMTokenizerFast _lowercase = True _lowercase = True def _UpperCamelCase( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase( self : List[Any] ): a__ : int = "<pad>" a__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(lowerCamelCase__ ) , 1_008 ) def _UpperCamelCase( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def _UpperCamelCase( self : Optional[int] ): a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) a__ : List[str] = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowerCamelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) a__ : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a__ : List[str] = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) a__ : Dict = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _UpperCamelCase( self : Dict ): return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def _UpperCamelCase( self : Union[str, Any] ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCamelCase__ , f.name ) a__ : Any = XGLMTokenizer(f.name , keep_accents=lowerCamelCase__ ) a__ : List[str] = pickle.dumps(lowerCamelCase__ ) pickle.loads(lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): if not self.test_rust_tokenizer: return a__ : Any = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : Tuple = "I was born in 92000, and this is falsé." a__ : List[str] = tokenizer.tokenize(lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : Optional[int] = tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = self.get_rust_tokenizer() a__ : Tuple = tokenizer.encode(lowerCamelCase__ ) a__ : Optional[Any] = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = "Hello World!" a__ : List[str] = [2, 31_227, 4_447, 35] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Union[str, Any] ): a__ : Optional[int] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off a__ : Union[str, Any] = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : List[Any] ): # fmt: off a__ : Optional[int] = { "input_ids": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], "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, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name="facebook/xglm-564M" , padding=lowerCamelCase__ , )
37
0
def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: list ): if any(not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or x < 0 for x in sequence ): raise TypeError("""Sequence must be list of non-negative integers""" ) for _ in range(len(UpperCamelCase__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(UpperCamelCase__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
6
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCamelCase_ ( ) -> int: a__ : int = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg" a__ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ).convert("RGB" ) return image def UpperCamelCase_ ( __a ) -> Optional[Any]: a__ : Any = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.embeddings.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.embeddings.layernorm.bias") ) # fmt: on return rename_keys def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Union[str, Any] = dct.pop(__a ) a__ : List[str] = val def UpperCamelCase_ ( __a , __a ) -> Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__ : Any = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) a__ : Tuple = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict a__ : str = torch.cat((q_bias, torch.zeros_like(__a , requires_grad=__a ), v_bias) ) a__ : int = qkv_bias def UpperCamelCase_ ( __a ) -> Dict: a__ : Tuple = 364 if "coco" in model_name else 224 a__ : int = InstructBlipVisionConfig(image_size=__a ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: a__ : Tuple = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__ : Dict = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: a__ : List[Any] = LlamaConfig.from_pretrained("decapoda-research/llama-7b-hf" , vocab_size=32_001 ).to_dict() elif "vicuna-13b" in model_name: a__ : Optional[int] = LlamaConfig.from_pretrained("decapoda-research/llama-13b-hf" , vocab_size=32_001 ).to_dict() else: raise ValueError("Model name not supported" ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 a__ : Optional[Any] = InstructBlipQFormerConfig(vocab_size=30_523 ).to_dict() a__ : Any = InstructBlipConfig(vision_config=__a , text_config=__a , qformer_config=__a ) return config, image_size @torch.no_grad() def UpperCamelCase_ ( __a , __a=None , __a=False ) -> int: a__ : Tuple = AutoTokenizer.from_pretrained("bert-base-uncased" , truncation_side="left" ) qformer_tokenizer.add_special_tokens({"bos_token": "[DEC]"} ) if "t5" in model_name: a__ : List[Any] = TaTokenizerFast.from_pretrained("google/flan-t5-xl" , truncation_side="left" ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) a__ : Union[str, Any] = LlamaTokenizerFast.from_pretrained( "huggyllama/llama-7b" , truncation_side="left" , bos_token="</s>" , unk_token="</s>" ) tokenizer.add_special_tokens({"pad_token": "[PAD]"} ) a__, a__ : List[str] = get_blipa_config(__a ) a__ : Any = InstructBlipForConditionalGeneration(__a ).eval() a__ : Dict = { "instructblip-vicuna-7b": ("blip2_vicuna_instruct", "vicuna7b"), "instructblip-vicuna-13b": ("blip2_vicuna_instruct", "vicuna13b"), "instructblip-flan-t5-xl": ("blip2_t5_instruct", "flant5xl"), "instructblip-flan-t5-xxl": ("blip2_t5_instruct", "flant5xxl"), } a__, a__ : Dict = model_name_to_original[model_name] # load original model print("Loading original model..." ) a__ : Optional[Any] = "cuda:1" if torch.cuda.is_available() else "cpu" a__ : List[Any] = "cuda:2" if torch.cuda.is_available() else "cpu" a__, a__, a__ : Tuple = load_model_and_preprocess( name=__a , model_type=__a , is_eval=__a , device=__a ) original_model.eval() print("Done!" ) # update state dict keys a__ : Dict = original_model.state_dict() a__ : Optional[int] = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a , __a , __a ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__ : Optional[int] = state_dict.pop(__a ) if key.startswith("Qformer.bert" ): a__ : List[Any] = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: a__ : Any = key.replace("self" , "attention" ) if "llm_proj" in key: a__ : Dict = key.replace("llm_proj" , "language_projection" ) if "t5_proj" in key: a__ : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("llm_model" ): a__ : List[str] = key.replace("llm_model" , "language_model" ) if key.startswith("t5" ): a__ : str = key.replace("t5" , "language" ) a__ : Dict = val # read in qv biases read_in_q_v_bias(__a , __a ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(__a , strict=__a ) a__ : Union[str, Any] = load_demo_image() a__ : int = "What is unusual about this image?" # create processor a__ : Any = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__a , image_std=__a ) a__ : Tuple = InstructBlipProcessor( image_processor=__a , tokenizer=__a , qformer_tokenizer=__a , ) a__ : Tuple = processor(images=__a , text=__a , return_tensors="pt" ).to(__a ) # make sure processor creates exact same pixel values a__ : Optional[int] = vis_processors["eval"](__a ).unsqueeze(0 ).to(__a ) a__ : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , __a ) original_model.to(__a ) hf_model.to(__a ) with torch.no_grad(): if "vicuna" in model_name: a__ : str = original_model({"image": original_pixel_values, "text_input": [prompt]} ).logits a__ : List[str] = hf_model(**__a ).logits else: a__ : List[Any] = original_model( {"image": original_pixel_values, "text_input": [prompt], "text_output": ["\n"]} ).logits a__ : str = tokenizer("\n" , return_tensors="pt" ).input_ids.to(__a ) a__ : Dict = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) a__ : Any = hf_model(**__a , labels=__a ).logits print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape a__ : Tuple = 1e-4 if "vicuna" in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , __a , atol=__a ) print("Looks ok!" ) print("Generating with original model..." ) a__ : Tuple = original_model.generate({"image": original_pixel_values, "prompt": prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print("Generating with HF model..." ) a__ : int = hf_model.generate( **__a , do_sample=__a , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? a__ : int = 2 print("Original generation:" , __a ) a__ : str = processor.batch_decode(__a , skip_special_tokens=__a ) a__ : str = [text.strip() for text in output_text] print("HF generation:" , __a ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() UpperCamelCase : Optional[int] = [ """instructblip-vicuna-7b""", """instructblip-vicuna-13b""", """instructblip-flan-t5-xl""", """instructblip-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""instructblip-flan-t5-xl""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase : Dict = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
37
0
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class lowercase_ : '''simple docstring''' UpperCAmelCase : List[str] UpperCAmelCase : Optional[str] = None # Automatically constructed UpperCAmelCase : ClassVar[str] = "dict" UpperCAmelCase : ClassVar[Any] = None UpperCAmelCase : str = field(default='''Translation''' , init=__lowerCAmelCase , repr=__lowerCAmelCase ) def __call__( self : List[Any] ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def lowerCAmelCase_ ( self : int ): from .features import Value return {k: Value('string' ) for k in sorted(self.languages )} @dataclass class lowercase_ : '''simple docstring''' UpperCAmelCase : Optional[List] = None UpperCAmelCase : Optional[int] = None UpperCAmelCase : Optional[str] = None # Automatically constructed UpperCAmelCase : ClassVar[str] = "dict" UpperCAmelCase : ClassVar[Any] = None UpperCAmelCase : str = field(default='''TranslationVariableLanguages''' , init=__lowerCAmelCase , repr=__lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): _A = sorted(set(self.languages ) ) if self.languages else None _A = len(self.languages ) if self.languages else None def __call__( self : Tuple ): return pa.struct({'language': pa.list_(pa.string() ), 'translation': pa.list_(pa.string() )} ) def lowerCAmelCase_ ( self : str , _UpperCAmelCase : Union[str, Any] ): _A = set(self.languages ) if self.languages and set(_UpperCAmelCase ) - lang_set: raise ValueError( F'''Some languages in example ({", ".join(sorted(set(_UpperCAmelCase ) - lang_set ) )}) are not in valid set ({", ".join(_UpperCAmelCase )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. _A = [] for lang, text in translation_dict.items(): if isinstance(_UpperCAmelCase , _UpperCAmelCase ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. _A , _A = zip(*sorted(_UpperCAmelCase ) ) return {"language": languages, "translation": translations} def lowerCAmelCase_ ( self : Optional[int] ): from .features import Sequence, Value return { "language": Sequence(Value('string' ) ), "translation": Sequence(Value('string' ) ), }
7
def UpperCamelCase_ ( __a , __a ) -> Tuple: a__ : Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 a__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. a__ : Any = min(__a , __a ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
37
0
'''simple docstring''' import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py lowercase__ : List[str] = '''src/diffusers''' lowercase__ : Dict = '''.''' # This is to make sure the diffusers module imported is the one in the repo. lowercase__ : str = importlib.util.spec_from_file_location( '''diffusers''', os.path.join(DIFFUSERS_PATH, '''__init__.py'''), submodule_search_locations=[DIFFUSERS_PATH], ) lowercase__ : List[Any] = spec.loader.load_module() def _lowerCAmelCase ( __snake_case : List[Any] , __snake_case : Union[str, Any] ) -> Tuple: return line.startswith(__snake_case ) or len(__snake_case ) <= 1 or re.search(r'^\s*\)(\s*->.*:|:)\s*$' , __snake_case ) is not None def _lowerCAmelCase ( __snake_case : Dict ) -> List[str]: __A : Tuple = object_name.split('.' ) __A : str = 0 # First let's find the module where our object lives. __A : int = parts[i] while i < len(__snake_case ) and not os.path.isfile(os.path.join(__snake_case , f'{module}.py' ) ): i += 1 if i < len(__snake_case ): __A : Tuple = os.path.join(__snake_case , parts[i] ) if i >= len(__snake_case ): raise ValueError(f'`object_name` should begin with the name of a module of diffusers but got {object_name}.' ) with open(os.path.join(__snake_case , f'{module}.py' ) , 'r' , encoding='utf-8' , newline='\n' ) as f: __A : Optional[int] = f.readlines() # Now let's find the class / func in the code! __A : Tuple = '' __A : Dict = 0 for name in parts[i + 1 :]: while ( line_index < len(__snake_case ) and re.search(rf'^{indent}(class|def)\s+{name}(\(|\:)' , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(__snake_case ): raise ValueError(f' {object_name} does not match any function or class in {module}.' ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). __A : int = line_index while line_index < len(__snake_case ) and _should_continue(lines[line_index] , __snake_case ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __A : List[str] = lines[start_index:line_index] return "".join(__snake_case ) lowercase__ : Optional[int] = re.compile(r'''^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)''') lowercase__ : Optional[Any] = re.compile(r'''^\s*(\S+)->(\S+)(\s+.*|$)''') lowercase__ : Optional[int] = re.compile(r'''<FILL\s+[^>]*>''') def _lowerCAmelCase ( __snake_case : str ) -> Union[str, Any]: __A : Dict = code.split('\n' ) __A : Optional[Any] = 0 while idx < len(__snake_case ) and len(lines[idx] ) == 0: idx += 1 if idx < len(__snake_case ): return re.search(r'^(\s*)\S' , lines[idx] ).groups()[0] return "" def _lowerCAmelCase ( __snake_case : Optional[Any] ) -> List[Any]: __A : Union[str, Any] = len(get_indent(__snake_case ) ) > 0 if has_indent: __A : List[Any] = f'class Bla:\n{code}' __A : Any = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=1_19 , preview=__snake_case ) __A : Tuple = black.format_str(__snake_case , mode=__snake_case ) __A ,__A : Tuple = style_docstrings_in_code(__snake_case ) return result[len('class Bla:\n' ) :] if has_indent else result def _lowerCAmelCase ( __snake_case : int , __snake_case : int=False ) -> Union[str, Any]: with open(__snake_case , 'r' , encoding='utf-8' , newline='\n' ) as f: __A : Union[str, Any] = f.readlines() __A : int = [] __A : int = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(__snake_case ): __A : Union[str, Any] = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. __A ,__A ,__A : Dict = search.groups() __A : Any = find_code_in_diffusers(__snake_case ) __A : Dict = get_indent(__snake_case ) __A : List[Any] = line_index + 1 if indent == theoretical_indent else line_index + 2 __A : Optional[int] = theoretical_indent __A : Optional[Any] = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. __A : Any = True while line_index < len(__snake_case ) and should_continue: line_index += 1 if line_index >= len(__snake_case ): break __A : Tuple = lines[line_index] __A : Tuple = _should_continue(__snake_case , __snake_case ) and re.search(f'^{indent}# End copy' , __snake_case ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __A : Union[str, Any] = lines[start_index:line_index] __A : Optional[Any] = ''.join(__snake_case ) # Remove any nested `Copied from` comments to avoid circular copies __A : Union[str, Any] = [line for line in theoretical_code.split('\n' ) if _re_copy_warning.search(__snake_case ) is None] __A : str = '\n'.join(__snake_case ) # Before comparing, use the `replace_pattern` on the original code. if len(__snake_case ) > 0: __A : str = replace_pattern.replace('with' , '' ).split(',' ) __A : Optional[int] = [_re_replace_pattern.search(__snake_case ) for p in patterns] for pattern in patterns: if pattern is None: continue __A ,__A ,__A : Tuple = pattern.groups() __A : Optional[int] = re.sub(__snake_case , __snake_case , __snake_case ) if option.strip() == "all-casing": __A : Optional[int] = re.sub(obja.lower() , obja.lower() , __snake_case ) __A : str = re.sub(obja.upper() , obja.upper() , __snake_case ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line __A : Any = blackify(lines[start_index - 1] + theoretical_code ) __A : List[str] = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: __A : str = lines[:start_index] + [theoretical_code] + lines[line_index:] __A : int = start_index + 1 if overwrite and len(__snake_case ) > 0: # Warn the user a file has been modified. print(f'Detected changes, rewriting {filename}.' ) with open(__snake_case , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(__snake_case ) return diffs def _lowerCAmelCase ( __snake_case : bool = False ) -> int: __A : List[Any] = glob.glob(os.path.join(__snake_case , '**/*.py' ) , recursive=__snake_case ) __A : Optional[Any] = [] for filename in all_files: __A : List[str] = is_copy_consistent(__snake_case , __snake_case ) diffs += [f'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs] if not overwrite and len(__snake_case ) > 0: __A : Dict = '\n'.join(__snake_case ) raise Exception( 'Found the following copy inconsistencies:\n' + diff + '\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.' ) if __name__ == "__main__": lowercase__ : int = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') lowercase__ : Tuple = parser.parse_args() check_copies(args.fix_and_overwrite)
8
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Optional[Any] = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } UpperCamelCase : Dict = { """allenai/led-base-16384""": 1_6384, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = LEDTokenizer _lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[str]=None , lowerCamelCase__ : Any=None , lowerCamelCase__ : int="replace" , lowerCamelCase__ : Union[str, Any]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Tuple="</s>" , lowerCamelCase__ : Optional[int]="<s>" , lowerCamelCase__ : str="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Any="<mask>" , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : int=True , **lowerCamelCase__ : Union[str, Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : List[str] = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : Optional[Any] = add_prefix_space a__ : List[str] = pre_tok_class(**lowerCamelCase__ ) a__ : Optional[int] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ : Any = "post_processor" a__ : str = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Optional[Any] = tuple(state["sep"] ) if "cls" in state: a__ : Optional[Any] = tuple(state["cls"] ) a__ : Optional[int] = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Dict = add_prefix_space a__ : int = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : List[Any] = trim_offsets a__ : List[str] = True if changes_to_apply: a__ : int = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : int = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Union[str, Any] ): a__ : Any = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : Union[str, Any] = value def _UpperCamelCase( self : Any , *lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : List[str] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Any , *lowerCamelCase__ : Dict , **lowerCamelCase__ : Optional[Any] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : List[str] = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[Any]=None ): a__ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : List[str] = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0] def _UpperCamelCase( self : Dict , lowerCamelCase__ : Union[Dict[str, EncodedInput], BatchEncoding] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[bool] = None , ): a__ : str = super()._pad( encoded_inputs=lowerCamelCase__ , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) # Load from model defaults if return_attention_mask is None: a__ : Optional[int] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ : Tuple = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ : Dict = len(encoded_inputs["global_attention_mask"] ) != len(lowerCamelCase__ ) if needs_to_be_padded: a__ : Union[str, Any] = len(lowerCamelCase__ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ : List[Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a__ : Any = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
37
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = b.T A__ = np.sum(np.square(__UpperCamelCase ) , axis=1 ) A__ = np.sum(np.square(__UpperCamelCase ) , axis=0 ) A__ = np.matmul(__UpperCamelCase , __UpperCamelCase ) A__ = aa[:, None] - 2 * ab + ba[None, :] return d def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = x.reshape(-1 , 3 ) A__ = squared_euclidean_distance(__UpperCamelCase , __UpperCamelCase ) return np.argmin(__UpperCamelCase , axis=1 ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Dict = ["pixel_values"] def __init__( self : List[str] , _snake_case : Optional[Union[List[List[int]], np.ndarray]] = None , _snake_case : bool = True , _snake_case : Dict[str, int] = None , _snake_case : PILImageResampling = PILImageResampling.BILINEAR , _snake_case : bool = True , _snake_case : bool = True , **_snake_case : Dict , ): """simple docstring""" super().__init__(**_snake_case ) A__ = size if size is not None else {'height': 2_56, 'width': 2_56} A__ = get_size_dict(_snake_case ) A__ = np.array(_snake_case ) if clusters is not None else None A__ = do_resize A__ = size A__ = resample A__ = do_normalize A__ = do_color_quantize def _a ( self : int , _snake_case : np.ndarray , _snake_case : Dict[str, int] , _snake_case : PILImageResampling = PILImageResampling.BILINEAR , _snake_case : Optional[Union[str, ChannelDimension]] = None , **_snake_case : str , ): """simple docstring""" A__ = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(F'''Size dictionary must contain both height and width keys. Got {size.keys()}''' ) return resize( _snake_case , size=(size['height'], size['width']) , resample=_snake_case , data_format=_snake_case , **_snake_case ) def _a ( self : Any , _snake_case : np.ndarray , _snake_case : Optional[Union[str, ChannelDimension]] = None , ): """simple docstring""" A__ = rescale(image=_snake_case , scale=1 / 127.5 , data_format=_snake_case ) A__ = image - 1 return image def _a ( self : List[Any] , _snake_case : ImageInput , _snake_case : bool = None , _snake_case : Dict[str, int] = None , _snake_case : PILImageResampling = None , _snake_case : bool = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[List[List[int]], np.ndarray]] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST , **_snake_case : int , ): """simple docstring""" A__ = do_resize if do_resize is not None else self.do_resize A__ = size if size is not None else self.size A__ = get_size_dict(_snake_case ) A__ = resample if resample is not None else self.resample A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = do_color_quantize if do_color_quantize is not None else self.do_color_quantize A__ = clusters if clusters is not None else self.clusters A__ = np.array(_snake_case ) A__ = make_list_of_images(_snake_case ) if not valid_images(_snake_case ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_color_quantize and clusters is None: raise ValueError('Clusters must be specified if do_color_quantize is True.' ) # All transformations expect numpy arrays. A__ = [to_numpy_array(_snake_case ) for image in images] if do_resize: A__ = [self.resize(image=_snake_case , size=_snake_case , resample=_snake_case ) for image in images] if do_normalize: A__ = [self.normalize(image=_snake_case ) for image in images] if do_color_quantize: A__ = [to_channel_dimension_format(_snake_case , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) A__ = np.array(_snake_case ) A__ = color_quantize(_snake_case , _snake_case ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) A__ = images.shape[0] A__ = images.reshape(_snake_case , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. A__ = list(_snake_case ) else: A__ = [to_channel_dimension_format(_snake_case , _snake_case ) for image in images] A__ = {'input_ids': images} return BatchFeature(data=_snake_case , tensor_type=_snake_case )
9
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Any = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Union[str, Any] = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : List[str] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = ['input_ids', 'attention_mask'] _lowercase = RobertaTokenizer def __init__( self : List[str] , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]="replace" , lowerCamelCase__ : List[str]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Any="</s>" , lowerCamelCase__ : Any="<s>" , lowerCamelCase__ : int="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Tuple="<mask>" , lowerCamelCase__ : Any=False , lowerCamelCase__ : Dict=True , **lowerCamelCase__ : Optional[Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Any = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : int = add_prefix_space a__ : Tuple = pre_tok_class(**lowerCamelCase__ ) a__ : str = add_prefix_space a__ : Tuple = "post_processor" a__ : Dict = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Tuple = tuple(state["sep"] ) if "cls" in state: a__ : str = tuple(state["cls"] ) a__ : str = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : str = add_prefix_space a__ : Any = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : int = trim_offsets a__ : Dict = True if changes_to_apply: a__ : Union[str, Any] = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : str = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : Tuple ): a__ : List[Any] = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : List[str] = value def _UpperCamelCase( self : Union[str, Any] , *lowerCamelCase__ : int , **lowerCamelCase__ : int ): a__ : Optional[int] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Tuple , *lowerCamelCase__ : Dict , **lowerCamelCase__ : List[str] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : int = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int]=None ): a__ : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Dict , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : Tuple = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0]
37
0
def _snake_case ( __snake_case ): _UpperCamelCase = 0 for ch in input_str: _UpperCamelCase = ord(__snake_case ) _UpperCamelCase = pow(2 , __snake_case ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
10
from statistics import mean, stdev def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : List[str] = min(__a ) a__ : str = max(__a ) # normalize data return [round((x - x_min) / (x_max - x_min) , __a ) for x in data] def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : str = mean(__a ) a__ : List[str] = stdev(__a ) # standardize data return [round((x - mu) / (sigma) , __a ) for x in data]
37
0
'''simple docstring''' import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> Optional[int]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> Tuple: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> Tuple: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def a__ (self ) -> Optional[int]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = True _a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) _a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = True __lowerCamelCase : int = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxRobertaModelTester(self ) @slow def a__ (self ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''roberta-base''' , from_pt=A ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A )
11
def UpperCamelCase_ ( __a = 50 ) -> int: a__ : Tuple = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
37
0
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _snake_case ( UpperCAmelCase_ ): def __init__( self , *SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , **SCREAMING_SNAKE_CASE_): '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_) lowercase__ : Optional[Any] = eval_examples lowercase__ : Any = post_process_function def lowercase__ ( self , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_ = "eval"): '''simple docstring''' lowercase__ : List[str] = self.eval_dataset if eval_dataset is None else eval_dataset lowercase__ : Union[str, Any] = self.get_eval_dataloader(SCREAMING_SNAKE_CASE_) lowercase__ : Tuple = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. lowercase__ : Optional[Any] = self.compute_metrics lowercase__ : Dict = None lowercase__ : List[Any] = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop lowercase__ : Tuple = time.time() try: lowercase__ : str = eval_loop( SCREAMING_SNAKE_CASE_ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE_ , metric_key_prefix=SCREAMING_SNAKE_CASE_ , ) finally: lowercase__ : Optional[Any] = compute_metrics lowercase__ : Tuple = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size) , )) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default lowercase__ : int = self.post_process_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , output.predictions) lowercase__ : List[str] = self.compute_metrics(SCREAMING_SNAKE_CASE_) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(f'{metric_key_prefix}_'): lowercase__ : List[str] = metrics.pop(SCREAMING_SNAKE_CASE_) metrics.update(output.metrics) else: lowercase__ : Tuple = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(SCREAMING_SNAKE_CASE_) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report()) lowercase__ : Tuple = self.callback_handler.on_evaluate(self.args , self.state , self.control , SCREAMING_SNAKE_CASE_) return metrics def lowercase__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_ = "test"): '''simple docstring''' lowercase__ : List[Any] = self.get_test_dataloader(SCREAMING_SNAKE_CASE_) # Temporarily disable metric computation, we will do it in the loop here. lowercase__ : List[str] = self.compute_metrics lowercase__ : Any = None lowercase__ : str = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop lowercase__ : List[str] = time.time() try: lowercase__ : Optional[Any] = eval_loop( SCREAMING_SNAKE_CASE_ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE_ , metric_key_prefix=SCREAMING_SNAKE_CASE_ , ) finally: lowercase__ : Optional[int] = compute_metrics lowercase__ : Tuple = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size) , )) if self.post_process_function is None or self.compute_metrics is None: return output lowercase__ : Dict = self.post_process_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , output.predictions , """predict""") lowercase__ : Any = self.compute_metrics(SCREAMING_SNAKE_CASE_) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(f'{metric_key_prefix}_'): lowercase__ : List[str] = metrics.pop(SCREAMING_SNAKE_CASE_) metrics.update(output.metrics) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=SCREAMING_SNAKE_CASE_)
12
class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] ): a__ : str = name a__ : Optional[int] = value a__ : Dict = weight def __repr__( self : Union[str, Any] ): return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def _UpperCamelCase( self : Dict ): return self.value def _UpperCamelCase( self : Optional[Any] ): return self.name def _UpperCamelCase( self : Optional[Any] ): return self.weight def _UpperCamelCase( self : Optional[int] ): return self.value / self.weight def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = [] for i in range(len(__a ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def UpperCamelCase_ ( __a , __a , __a ) -> Union[str, Any]: a__ : List[str] = sorted(__a , key=__a , reverse=__a ) a__ : List[Any] = [] a__, a__ : Union[str, Any] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def UpperCamelCase_ ( ) -> Union[str, Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
37
0
'''simple docstring''' # XXX: we want transformers master here - in the absense of conftest manipulating sys.path: # hack it in for now: import sys from pathlib import Path A__ : List[str] = Path(__file__).resolve().parents[3] / """src""" sys.path.insert(1, str(git_repo_path)) import dataclasses # noqa import io # noqa import itertools # noqa import json # noqa import os # noqa import unittest # noqa from copy import deepcopy # noqa from parameterized import parameterized # noqa from transformers import TrainingArguments, is_torch_available # noqa from transformers.deepspeed import is_deepspeed_available # noqa from transformers.file_utils import WEIGHTS_NAME # noqa from transformers.testing_utils import ( # noqa CaptureLogger, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, mockenv_context, require_deepspeed, require_torch_gpu, require_torch_multi_gpu, slow, ) from transformers.trainer_utils import set_seed # noqa set_seed(42) A__ : List[str] = {"""base""": """patrickvonplaten/wav2vec2_tiny_random""", """robust""": """patrickvonplaten/wav2vec2_tiny_random_robust"""} A__ : Tuple = """zero2""" A__ : str = """zero3""" A__ : int = [ZEROa, ZEROa] def UpperCAmelCase__ ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : str ) -> int: # customize the test name generator function as we want both params to appear in the sub-test # name, as by default it shows only the first param __lowerCamelCase : Dict = parameterized.to_safe_name('_'.join(str(UpperCAmelCase_ ) for x in param.args ) ) return F'{func.__name__}_{param_based_name}' # Cartesian-product of zero stages with models to test A__ : Union[str, Any] = list(itertools.product(stages, models.keys())) @slow @require_deepspeed @require_torch_gpu class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" @parameterized.expand(SCREAMING_SNAKE_CASE_ , name_func=SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: self.run_and_check( stage=SCREAMING_SNAKE_CASE_ , model=SCREAMING_SNAKE_CASE_ , distributed=SCREAMING_SNAKE_CASE_ , fpaa=SCREAMING_SNAKE_CASE_ , ) @require_torch_multi_gpu @parameterized.expand(SCREAMING_SNAKE_CASE_ , name_func=SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: self.run_and_check( stage=SCREAMING_SNAKE_CASE_ , model=SCREAMING_SNAKE_CASE_ , distributed=SCREAMING_SNAKE_CASE_ , fpaa=SCREAMING_SNAKE_CASE_ , ) @parameterized.expand(SCREAMING_SNAKE_CASE_ , name_func=SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: self.run_and_check( stage=SCREAMING_SNAKE_CASE_ , model=SCREAMING_SNAKE_CASE_ , distributed=SCREAMING_SNAKE_CASE_ , fpaa=SCREAMING_SNAKE_CASE_ , ) @require_torch_multi_gpu @parameterized.expand(SCREAMING_SNAKE_CASE_ , name_func=SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: self.run_and_check( stage=SCREAMING_SNAKE_CASE_ , model=SCREAMING_SNAKE_CASE_ , distributed=SCREAMING_SNAKE_CASE_ , fpaa=SCREAMING_SNAKE_CASE_ , ) def lowercase_ ( self , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: # XXX: run_asr is premature and doesn't save any results # so all we check for now is that the process didn't fail pass def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 10 , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = True , ) -> str: __lowerCamelCase : List[Any] = models[model] __lowerCamelCase : Union[str, Any] = self.run_trainer( stage=SCREAMING_SNAKE_CASE_ , model_name=SCREAMING_SNAKE_CASE_ , eval_steps=SCREAMING_SNAKE_CASE_ , num_train_epochs=1 , distributed=SCREAMING_SNAKE_CASE_ , fpaa=SCREAMING_SNAKE_CASE_ , ) self.do_checks(SCREAMING_SNAKE_CASE_ ) return output_dir def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 10 , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = True , ) -> Optional[Any]: __lowerCamelCase : Optional[int] = self.get_auto_remove_tmp_dir('./xxx' , after=SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : int = f'\n --model_name_or_path {model_name}\n --dataset_name hf-internal-testing/librispeech_asr_dummy\n --dataset_config_name clean\n --train_split_name validation\n --validation_split_name validation\n --output_dir {output_dir}\n --num_train_epochs {str(SCREAMING_SNAKE_CASE_ )}\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 2\n --evaluation_strategy steps\n --learning_rate 5e-4\n --warmup_steps 8\n --orthography timit\n --preprocessing_num_workers 1\n --group_by_length\n --freeze_feature_extractor\n --report_to none\n --save_steps 0\n --eval_steps {eval_steps}\n --report_to none\n '.split() if fpaa: args.extend(['--fp16'] ) # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true, # hence the separate config files __lowerCamelCase : str = f'--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json'.split() __lowerCamelCase : Any = [f'{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py'] __lowerCamelCase : Optional[int] = self.get_launcher(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Union[str, Any] = launcher + script + args + ds_args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=self.get_env() ) return output_dir def lowercase_ ( self , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: # 1. explicitly set --num_nodes=1 just in case these tests end up run on a multi-node setup # - it won't be able to handle that # 2. for now testing with just 2 gpus max (since some quality tests may give different # results with mode gpus because we use very little data) __lowerCamelCase : List[Any] = min(2 , get_gpu_count() ) if distributed else 1 return f'deepspeed --num_nodes 1 --num_gpus {num_gpus}'.split()
13
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class A__ ( A__ ): """simple docstring""" def __init__( self : Dict , lowerCamelCase__ : Union[str, "sqlalchemy.sql.Selectable"] , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[Features] = None , lowerCamelCase__ : str = None , lowerCamelCase__ : bool = False , **lowerCamelCase__ : Optional[int] , ): super().__init__(features=lowerCamelCase__ , cache_dir=lowerCamelCase__ , keep_in_memory=lowerCamelCase__ , **lowerCamelCase__ ) a__ : str = Sql( cache_dir=lowerCamelCase__ , features=lowerCamelCase__ , sql=lowerCamelCase__ , con=lowerCamelCase__ , **lowerCamelCase__ , ) def _UpperCamelCase( self : Tuple ): a__ : Optional[Any] = None a__ : Dict = None a__ : Union[str, Any] = None a__ : Union[str, Any] = None self.builder.download_and_prepare( download_config=lowerCamelCase__ , download_mode=lowerCamelCase__ , verification_mode=lowerCamelCase__ , base_path=lowerCamelCase__ , ) # Build dataset for splits a__ : List[str] = self.builder.as_dataset( split="train" , verification_mode=lowerCamelCase__ , in_memory=self.keep_in_memory ) return dataset class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : Dataset , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[int] = None , **lowerCamelCase__ : Optional[Any] , ): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) a__ : Any = dataset a__ : str = name a__ : Tuple = con a__ : List[Any] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a__ : Any = num_proc a__ : Tuple = to_sql_kwargs def _UpperCamelCase( self : List[Any] ): a__ : Any = self.to_sql_kwargs.pop("sql" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("con" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("index" , lowerCamelCase__ ) a__ : int = self._write(index=lowerCamelCase__ , **self.to_sql_kwargs ) return written def _UpperCamelCase( self : Any , lowerCamelCase__ : List[str] ): a__, a__, a__ : Union[str, Any] = args a__ : Any = {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs a__ : Tuple = query_table( table=self.dataset.data , key=slice(lowerCamelCase__ , offset + self.batch_size ) , indices=self.dataset._indices , ) a__ : str = batch.to_pandas() a__ : List[Any] = df.to_sql(self.name , self.con , index=lowerCamelCase__ , **lowerCamelCase__ ) return num_rows or len(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] , lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : str = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: a__, a__ : List[str] = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , lowerCamelCase__ , lowerCamelCase__ )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += num_rows return written
37
0
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def __UpperCAmelCase ( __a : Any ) -> str: """simple docstring""" _a : List[str] = fname.split(os.path.sep )[-1] return re.search(R'''^(.*)_\d+\.jpg$''' ,__a ).groups()[0] class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , _a , _a=None , _a=None ) -> Dict: _a : Optional[int] = file_names _a : List[str] = image_transform _a : List[Any] = label_to_id def __len__( self ) -> Any: return len(self.file_names ) def __getitem__( self , _a ) -> Tuple: _a : Union[str, Any] = self.file_names[idx] _a : Tuple = PIL.Image.open(_a ) _a : Dict = raw_image.convert('''RGB''' ) if self.image_transform is not None: _a : Tuple = self.image_transform(_a ) _a : Dict = extract_label(_a ) if self.label_to_id is not None: _a : Any = self.label_to_id[label] return {"image": image, "label": label} def __UpperCAmelCase ( __a : str ,__a : int ) -> Union[str, Any]: """simple docstring""" if args.with_tracking: _a : Tuple = Accelerator( cpu=args.cpu ,mixed_precision=args.mixed_precision ,log_with='''all''' ,project_dir=args.project_dir ) else: _a : int = Accelerator(cpu=args.cpu ,mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _a : List[Any] = config['''lr'''] _a : Optional[Any] = int(config['''num_epochs'''] ) _a : List[Any] = int(config['''seed'''] ) _a : Optional[Any] = int(config['''batch_size'''] ) _a : str = config['''image_size'''] if not isinstance(__a ,(list, tuple) ): _a : Optional[int] = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps ,'''isdigit''' ): if args.checkpointing_steps == "epoch": _a : List[str] = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): _a : int = int(args.checkpointing_steps ) else: raise ValueError( F"""Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed.""" ) else: _a : Optional[int] = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: _a : List[str] = os.path.split(__a )[-1].split('''.''' )[0] accelerator.init_trackers(__a ,__a ) # Grab all the image filenames _a : Dict = [os.path.join(args.data_dir ,__a ) for fname in os.listdir(args.data_dir ) if fname.endswith('''.jpg''' )] # Build the label correspondences _a : Dict = [extract_label(__a ) for fname in file_names] _a : str = list(set(__a ) ) id_to_label.sort() _a : List[str] = {lbl: i for i, lbl in enumerate(__a )} # Set the seed before splitting the data. np.random.seed(__a ) torch.manual_seed(__a ) torch.cuda.manual_seed_all(__a ) # Split our filenames between train and validation _a : str = np.random.permutation(len(__a ) ) _a : List[Any] = int(0.8 * len(__a ) ) _a : Dict = random_perm[:cut] _a : Any = random_perm[cut:] # For training we use a simple RandomResizedCrop _a : Optional[int] = Compose([RandomResizedCrop(__a ,scale=(0.5, 1.0) ), ToTensor()] ) _a : Any = PetsDataset( [file_names[i] for i in train_split] ,image_transform=__a ,label_to_id=__a ) # For evaluation, we use a deterministic Resize _a : Dict = Compose([Resize(__a ), ToTensor()] ) _a : Optional[Any] = PetsDataset([file_names[i] for i in eval_split] ,image_transform=__a ,label_to_id=__a ) # Instantiate dataloaders. _a : Tuple = DataLoader(__a ,shuffle=__a ,batch_size=__a ,num_workers=4 ) _a : Optional[Any] = DataLoader(__a ,shuffle=__a ,batch_size=__a ,num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _a : str = create_model('''resnet50d''' ,pretrained=__a ,num_classes=len(__a ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). _a : int = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): _a : Optional[int] = False for param in model.get_classifier().parameters(): _a : Any = True # We normalize the batches of images to be a bit faster. _a : int = torch.tensor(model.default_cfg['''mean'''] )[None, :, None, None].to(accelerator.device ) _a : Dict = torch.tensor(model.default_cfg['''std'''] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer _a : Union[str, Any] = torch.optim.Adam(params=model.parameters() ,lr=lr / 25 ) # Instantiate learning rate scheduler _a : Optional[int] = OneCycleLR(optimizer=__a ,max_lr=__a ,epochs=__a ,steps_per_epoch=len(__a ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _a , _a , _a , _a , _a : Any = accelerator.prepare( __a ,__a ,__a ,__a ,__a ) # We need to keep track of how many total steps we have iterated over _a : Optional[int] = 0 # We also need to keep track of the starting epoch so files are named properly _a : List[str] = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"""Resumed from checkpoint: {args.resume_from_checkpoint}""" ) accelerator.load_state(args.resume_from_checkpoint ) _a : Tuple = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint _a : Optional[Any] = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) _a : List[Any] = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` _a : Optional[int] = os.path.splitext(__a )[0] if "epoch" in training_difference: _a : List[Any] = int(training_difference.replace('''epoch_''' ,'''''' ) ) + 1 _a : str = None else: _a : str = int(training_difference.replace('''step_''' ,'''''' ) ) _a : Any = resume_step // len(__a ) resume_step -= starting_epoch * len(__a ) # Now we train the model for epoch in range(__a ,__a ): model.train() if args.with_tracking: _a : List[Any] = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step _a : List[Any] = accelerator.skip_first_batches(__a ,__a ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader _a : str = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. _a : Optional[Any] = {k: v.to(accelerator.device ) for k, v in batch.items()} _a : Dict = (batch['''image'''] - mean) / std _a : Optional[int] = model(__a ) _a : Union[str, Any] = torch.nn.functional.cross_entropy(__a ,batch['''label'''] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__a ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__a ,__a ): _a : List[str] = F"""step_{overall_step}""" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: _a : Dict = os.path.join(args.output_dir ,__a ) accelerator.save_state(__a ) model.eval() _a : Union[str, Any] = 0 _a : List[str] = 0 for step, batch in enumerate(__a ): # We could avoid this line since we set the accelerator with `device_placement=True`. _a : str = {k: v.to(accelerator.device ) for k, v in batch.items()} _a : Tuple = (batch['''image'''] - mean) / std with torch.no_grad(): _a : int = model(__a ) _a : Tuple = outputs.argmax(dim=-1 ) _a , _a : Optional[int] = accelerator.gather_for_metrics((predictions, batch['''label''']) ) _a : Union[str, Any] = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() _a : Any = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}: {100 * eval_metric:.2f}""" ) if args.with_tracking: accelerator.log( { '''accuracy''': 100 * eval_metric, '''train_loss''': total_loss.item() / len(__a ), '''epoch''': epoch, } ,step=__a ,) if checkpointing_steps == "epoch": _a : str = F"""epoch_{epoch}""" if args.output_dir is not None: _a : Optional[int] = os.path.join(args.output_dir ,__a ) accelerator.save_state(__a ) if args.with_tracking: accelerator.end_training() def __UpperCAmelCase ( ) -> List[Any]: """simple docstring""" _a : List[Any] = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument('''--data_dir''' ,required=__a ,help='''The data folder on disk.''' ) parser.add_argument('''--fp16''' ,action='''store_true''' ,help='''If passed, will use FP16 training.''' ) parser.add_argument( '''--mixed_precision''' ,type=__a ,default=__a ,choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] ,help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' ,) parser.add_argument('''--cpu''' ,action='''store_true''' ,help='''If passed, will train on the CPU.''' ) parser.add_argument( '''--checkpointing_steps''' ,type=__a ,default=__a ,help='''Whether the various states should be saved at the end of every n steps, or \'epoch\' for each epoch.''' ,) 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( '''--with_tracking''' ,action='''store_true''' ,help='''Whether to load in all available experiment trackers from the environment and use them for logging.''' ,) parser.add_argument( '''--project_dir''' ,type=__a ,default='''logs''' ,help='''Location on where to store experiment tracking logs` and relevent project information''' ,) _a : str = parser.parse_args() _a : Optional[Any] = {'''lr''': 3E-2, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 64, '''image_size''': 224} training_function(__a ,__a ) if __name__ == "__main__": main()
14
import math from datetime import datetime, timedelta def UpperCamelCase_ ( __a ) -> datetime: a__ : Union[str, Any] = year % 19 a__ : List[str] = year % 4 a__ : str = year % 7 a__ : Any = math.floor(year / 100 ) a__ : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) a__ : Optional[int] = leap_day_inhibits / 4 a__ : Union[str, Any] = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 a__ : Dict = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 a__ : Any = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon a__ : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 18 ) else: return datetime(__a , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): UpperCamelCase : Tuple = """will be""" if year > datetime.now().year else """was""" print(f"""Easter in {year} {tense} {gauss_easter(year)}""")
37
0
import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml A : int = logging.get_logger(__name__) def UpperCamelCase ( __magic_name__ : bool , __magic_name__ : bool ) -> Optional[int]: """simple docstring""" def run_func(__magic_name__ : Dict ): @wraps(__magic_name__ ) def run_in_eager_mode(*__magic_name__ : str , **__magic_name__ : Union[str, Any] ): return func(*__magic_name__ , **__magic_name__ ) @wraps(__magic_name__ ) @tf.function(experimental_compile=__magic_name__ ) def run_in_graph_mode(*__magic_name__ : Optional[Any] , **__magic_name__ : Tuple ): return func(*__magic_name__ , **__magic_name__ ) if do_eager_mode is True: if use_xla is not False: raise ValueError( """Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.""" ) return run_in_eager_mode else: return run_in_graph_mode return run_func def UpperCamelCase ( __magic_name__ : int , __magic_name__ : int , __magic_name__ : int ) -> ["tf.Tensor"]: """simple docstring""" lowercase__ = random.Random() lowercase__ = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(__magic_name__ , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class A ( UpperCAmelCase__ ): '''simple docstring''' A__ = 42 A__ = 42 A__ = "TensorFlow" @property def lowerCamelCase__ (self : Dict ) -> List[Any]: """simple docstring""" return tf.__version__ def lowerCamelCase__ (self : int , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> float: """simple docstring""" lowercase__ = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowercase__ = self._prepare_inference_func(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return self._measure_speed(_inference ) def lowerCamelCase__ (self : Optional[Any] , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> float: """simple docstring""" lowercase__ = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowercase__ = self._prepare_train_func(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return self._measure_speed(_train ) def lowerCamelCase__ (self : Any , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> [Memory, Optional[MemorySummary]]: """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _UpperCAmelCase ) lowercase__ = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowercase__ = self._prepare_inference_func(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return self._measure_memory(_inference ) def lowerCamelCase__ (self : Dict , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> [Memory, Optional[MemorySummary]]: """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _UpperCAmelCase ) lowercase__ = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowercase__ = self._prepare_train_func(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return self._measure_memory(_train ) def lowerCamelCase__ (self : str , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> Callable[[], None]: """simple docstring""" lowercase__ = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError("""Mixed precision is currently not supported.""" ) lowercase__ = ( hasattr(_UpperCAmelCase , """architectures""" ) and isinstance(config.architectures , _UpperCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowercase__ = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowercase__ = __import__("""transformers""" , fromlist=[model_class] ) lowercase__ = getattr(_UpperCAmelCase , _UpperCAmelCase ) lowercase__ = model_cls(_UpperCAmelCase ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' """ set `--only_pretrain_model` or `args.only_pretrain_model=True`.""" ) else: lowercase__ = TF_MODEL_MAPPING[config.__class__](_UpperCAmelCase ) # encoder-decoder has vocab size saved differently lowercase__ = config.vocab_size if hasattr(_UpperCAmelCase , """vocab_size""" ) else config.encoder.vocab_size lowercase__ = random_input_ids(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase , training=_UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(_UpperCAmelCase , training=_UpperCAmelCase ) lowercase__ = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def lowerCamelCase__ (self : int , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> Callable[[], None]: """simple docstring""" lowercase__ = self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError("""Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.""" ) if self.args.fpaa: raise NotImplementedError("""Mixed precision is currently not supported.""" ) lowercase__ = ( hasattr(_UpperCAmelCase , """architectures""" ) and isinstance(config.architectures , _UpperCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowercase__ = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowercase__ = __import__("""transformers""" , fromlist=[model_class] ) lowercase__ = getattr(_UpperCAmelCase , _UpperCAmelCase ) lowercase__ = model_cls(_UpperCAmelCase ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' """ set `--only_pretrain_model` or `args.only_pretrain_model=True`.""" ) else: lowercase__ = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_UpperCAmelCase ) # encoder-decoder has vocab size saved differently lowercase__ = config.vocab_size if hasattr(_UpperCAmelCase , """vocab_size""" ) else config.encoder.vocab_size lowercase__ = random_input_ids(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): lowercase__ = model(_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase , labels=_UpperCAmelCase , training=_UpperCAmelCase )[0] lowercase__ = tf.gradients(_UpperCAmelCase , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): lowercase__ = model(_UpperCAmelCase , labels=_UpperCAmelCase , training=_UpperCAmelCase )[0] lowercase__ = tf.gradients(_UpperCAmelCase , model.trainable_variables ) return gradients lowercase__ = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def lowerCamelCase__ (self : Optional[Any] , _UpperCAmelCase : Dict ) -> float: """simple docstring""" with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info("""Do inference on TPU. Running model 5 times to stabilize compilation""" ) timeit.repeat(_UpperCAmelCase , repeat=1 , number=5 ) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average lowercase__ = timeit.repeat( _UpperCAmelCase , repeat=self.args.repeat , number=10 , ) return min(_UpperCAmelCase ) / 10.0 except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) def lowerCamelCase__ (self : Dict , _UpperCAmelCase : Callable[[], None] ) -> [Memory, MemorySummary]: """simple docstring""" logger.info( """Note that TensorFlow allocates more memory than """ """it might need to speed up computation. """ """The memory reported here corresponds to the memory """ """reported by `nvidia-smi`, which can vary depending """ """on total available memory on the GPU that is used.""" ) with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( """`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory""" """ consumption line by line.""" ) lowercase__ = start_memory_tracing("""transformers""" ) if self.args.is_tpu: # tpu raise NotImplementedError( """Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking""" """ with `args.memory=False`""" ) elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( """py3nvml not installed, we won't log GPU memory usage. """ """Install py3nvml (pip install py3nvml) to log information about GPU.""" ) lowercase__ = """N/A""" else: logger.info( """Measuring total GPU usage on GPU device. Make sure to not have additional processes""" """ running on the same GPU.""" ) # init nvml nvml.nvmlInit() func() lowercase__ = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) lowercase__ = nvml.nvmlDeviceGetMemoryInfo(_UpperCAmelCase ) lowercase__ = meminfo.used lowercase__ = Memory(_UpperCAmelCase ) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( """When enabling line by line tracing, the max peak memory for CPU is inaccurate in""" """ TensorFlow.""" ) lowercase__ = None else: lowercase__ = measure_peak_memory_cpu(_UpperCAmelCase ) lowercase__ = Memory(_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else memory_bytes if self.args.trace_memory_line_by_line: lowercase__ = stop_memory_tracing(_UpperCAmelCase ) if memory is None: lowercase__ = summary.total else: lowercase__ = None return memory, summary except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) return "N/A", None
15
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def UpperCamelCase_ ( __a ) -> Union[str, Any]: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class A__ ( nn.Module ): """simple docstring""" def __init__( self : List[str] , lowerCamelCase__ : nn.Module , lowerCamelCase__ : int ): super().__init__() a__ : int = module a__ : Any = nn.Sequential( nn.Linear(module.in_features , lowerCamelCase__ , bias=lowerCamelCase__ ) , nn.Linear(lowerCamelCase__ , module.out_features , bias=lowerCamelCase__ ) , ) a__ : Tuple = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=lowerCamelCase__ ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Optional[int] , *lowerCamelCase__ : int , **lowerCamelCase__ : Dict ): return self.module(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) + self.adapter(lowerCamelCase__ ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" _lowercase = 'bigscience/bloom-1b7' # Constant values _lowercase = 2.1_09_65_95_52_69_25_74 _lowercase = 'Hello my name is' _lowercase = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) _lowercase = 1_0 def _UpperCamelCase( self : Dict ): # Models and tokenizer a__ : List[str] = AutoTokenizer.from_pretrained(self.model_name ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Union[str, Any] ): super().setUp() # Models and tokenizer a__ : List[Any] = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map="auto" ) a__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : List[Any] ): a__ : str = self.model_abit.config self.assertTrue(hasattr(lowerCamelCase__ , "quantization_config" ) ) a__ : Optional[Any] = config.to_dict() a__ : int = config.to_diff_dict() a__ : List[str] = config.to_json_string() def _UpperCamelCase( self : int ): from bitsandbytes.nn import Paramsabit a__ : List[Any] = self.model_fpaa.get_memory_footprint() a__ : str = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) a__ : Optional[Any] = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def _UpperCamelCase( self : Tuple ): from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(lowerCamelCase__ , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def _UpperCamelCase( self : str ): a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Tuple = self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[Any] = BitsAndBytesConfig() a__ : Optional[int] = True a__ : int = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , device_map="auto" ) a__ : str = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : int = model_abit_from_config.generate( input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : Dict ): with self.assertRaises(lowerCamelCase__ ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] ): a__ : int = BitsAndBytesConfig() with self.assertRaises(lowerCamelCase__ ): a__ : Dict = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , load_in_abit=lowerCamelCase__ , device_map="auto" , bnb_abit_quant_type="nf4" , ) def _UpperCamelCase( self : int ): with self.assertRaises(lowerCamelCase__ ): # Tries with `str` self.model_abit.to("cpu" ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.to(torch.device("cuda:0" ) ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.float() with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything a__ : int = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Any = self.model_fpaa.to(torch.floataa ) a__ : List[Any] = self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.to("cpu" ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.half() # Check this does not throw an error a__ : Dict = self.model_fpaa.float() def _UpperCamelCase( self : Dict ): a__ : List[str] = AutoModelForSeqaSeqLM.from_pretrained("t5-small" , load_in_abit=lowerCamelCase__ , device_map="auto" ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" @classmethod def _UpperCamelCase( cls : str ): a__ : Dict = "t5-small" a__ : List[Any] = "google/flan-t5-small" # flan-t5 uses dense-act instead of dense-relu-dense a__ : int = AutoTokenizer.from_pretrained(cls.model_name ) a__ : str = "Translate in German: Hello, my dog is cute" def _UpperCamelCase( self : Optional[int] ): gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Optional[int] ): from transformers import TaForConditionalGeneration a__ : List[Any] = TaForConditionalGeneration._keep_in_fpaa_modules a__ : Optional[Any] = None # test with `t5-small` a__ : Any = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : Dict = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Any = model.generate(**lowerCamelCase__ ) a__ : Union[str, Any] = modules def _UpperCamelCase( self : List[Any] ): import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` a__ : List[str] = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) a__ : Union[str, Any] = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : int = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Any = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Optional[int] = model.generate(**lowerCamelCase__ ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : List[str] ): super().setUp() # model_name a__ : Union[str, Any] = "bigscience/bloom-560m" a__ : Union[str, Any] = "t5-small" # Different types of model a__ : int = AutoModel.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Sequence classification model a__ : Dict = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # CausalLM model a__ : str = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Seq2seq model a__ : Dict = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Union[str, Any] ): from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): super().setUp() def _UpperCamelCase( self : int ): del self.pipe gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Tuple ): a__ : int = pipeline( "text-generation" , model=self.model_name , model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass a__ : Tuple = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]["generated_text"] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Tuple ): super().setUp() def _UpperCamelCase( self : List[Any] ): a__ : str = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="balanced" ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model a__ : List[Any] = self.tokenizer(self.input_text , return_tensors="pt" ) # Second real batch a__ : List[Any] = model_parallel.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): a__ : Any = "facebook/opt-350m" super().setUp() def _UpperCamelCase( self : int ): if version.parse(importlib.metadata.version("bitsandbytes" ) ) < version.parse("0.37.0" ): return # Step 1: freeze all parameters a__ : Tuple = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): a__ : Any = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability a__ : Tuple = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(lowerCamelCase__ ) ): a__ : Dict = LoRALayer(module.q_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.k_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch a__ : Dict = self.tokenizer("Test batch " , return_tensors="pt" ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): a__ : Optional[Any] = model.forward(**lowerCamelCase__ ) out.logits.norm().backward() for module in model.modules(): if isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(lowerCamelCase__ , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class A__ ( A__ ): """simple docstring""" _lowercase = 'gpt2-xl' _lowercase = 3.31_91_85_48_54_15_21_87
37
0
from jiwer import compute_measures import datasets __A : Optional[int] = '\\n@inproceedings{inproceedings,\n author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n year = {2004},\n month = {01},\n pages = {},\n title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n' __A : Tuple = '\\nWord error rate (WER) is a common metric of the performance of an automatic speech recognition system.\n\nThe general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.\n\nThis problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.\n\nWord error rate can then be computed as:\n\nWER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct words,\nN is the number of words in the reference (N=S+D+C).\n\nThis value indicates the average number of errors per reference word. The lower the value, the better the\nperformance of the ASR system with a WER of 0 being a perfect score.\n' __A : Tuple = '\nCompute WER score of transcribed segments against references.\n\nArgs:\n references: List of references for each speech input.\n predictions: List of transcriptions to score.\n concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.\n\nReturns:\n (float): the word error rate\n\nExamples:\n\n >>> predictions = ["this is the prediction", "there is an other sample"]\n >>> references = ["this is the reference", "there is another one"]\n >>> wer = datasets.load_metric("wer")\n >>> wer_score = wer.compute(predictions=predictions, references=references)\n >>> print(wer_score)\n 0.5\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _SCREAMING_SNAKE_CASE ( datasets.Metric ): '''simple docstring''' def _snake_case ( self : Any ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/jitsi/jiwer/"] , reference_urls=[ "https://en.wikipedia.org/wiki/Word_error_rate", ] , ) def _snake_case ( self : Union[str, Any] , __lowerCamelCase : int=None , __lowerCamelCase : int=None , __lowerCamelCase : Optional[int]=False ): if concatenate_texts: return compute_measures(__lowerCamelCase , __lowerCamelCase )["wer"] else: SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 for prediction, reference in zip(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = compute_measures(__lowerCamelCase , __lowerCamelCase ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
16
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Optional[int]=100 , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[int]=30 , lowerCamelCase__ : Union[str, Any]=2 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Tuple=True , lowerCamelCase__ : int=32 , lowerCamelCase__ : Union[str, Any]=4 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Union[str, Any]=37 , lowerCamelCase__ : List[Any]="gelu" , lowerCamelCase__ : List[Any]=0.1 , lowerCamelCase__ : int=0.1 , lowerCamelCase__ : Union[str, Any]=10 , lowerCamelCase__ : str=0.02 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]=[0, 1, 2, 3] , ): a__ : Dict = parent a__ : Dict = 100 a__ : Optional[int] = batch_size a__ : Union[str, Any] = image_size a__ : Any = patch_size a__ : Optional[Any] = num_channels a__ : int = is_training a__ : List[str] = use_labels a__ : Optional[Any] = hidden_size a__ : List[Any] = num_hidden_layers a__ : str = num_attention_heads a__ : str = intermediate_size a__ : int = hidden_act a__ : List[Any] = hidden_dropout_prob a__ : Dict = attention_probs_dropout_prob a__ : Union[str, Any] = type_sequence_label_size a__ : Optional[Any] = initializer_range a__ : List[str] = scope a__ : int = out_indices a__ : List[str] = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a__ : Optional[int] = (image_size // patch_size) ** 2 a__ : Union[str, Any] = num_patches + 1 def _UpperCamelCase( self : int ): a__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a__ : Optional[Any] = None a__ : Tuple = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) a__ : Optional[int] = self.get_config() return config, pixel_values, labels, pixel_labels def _UpperCamelCase( self : Tuple ): return BeitConfig( vocab_size=self.vocab_size , 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=lowerCamelCase__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any ): a__ : str = BeitModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : List[str] , lowerCamelCase__ : Tuple ): a__ : int = BeitForMaskedImageModeling(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _UpperCamelCase( self : str , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Any ): a__ : List[str] = self.type_sequence_label_size a__ : Optional[Any] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a__ : Optional[Any] = 1 a__ : List[str] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a__ : Union[str, Any] = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _UpperCamelCase( self : Any , lowerCamelCase__ : str , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): a__ : int = self.num_labels a__ : List[str] = BeitForSemanticSegmentation(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Tuple = model(lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def _UpperCamelCase( self : Optional[int] ): a__ : Any = self.prepare_config_and_inputs() a__, a__, a__, a__ : Union[str, Any] = config_and_inputs a__ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) _lowercase = ( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Any ): a__ : int = BeitModelTester(self ) a__ : Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def _UpperCamelCase( self : str ): pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _UpperCamelCase( self : Dict ): pass def _UpperCamelCase( self : Optional[Any] ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : List[str] = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : str ): a__, a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : int = model_class(lowerCamelCase__ ) a__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def _UpperCamelCase( self : int ): a__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] ): a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): if not self.model_tester.is_training: return a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : str = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling]: continue a__ : List[str] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.train() a__ : Any = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : Tuple = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : Tuple ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return a__ : List[Any] = False a__ : List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue a__ : Optional[Any] = model_class(lowerCamelCase__ ) model.gradient_checkpointing_enable() model.to(lowerCamelCase__ ) model.train() a__ : Union[str, Any] = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : int = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : List[str] ): a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : Dict = _config_zero_init(lowerCamelCase__ ) for model_class in self.all_model_classes: a__ : str = model_class(config=lowerCamelCase__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _UpperCamelCase( self : Optional[int] ): for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Tuple = BeitModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : List[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 _UpperCamelCase( self : Optional[int] ): return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def _UpperCamelCase( self : str ): a__ : int = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(lowerCamelCase__ ) a__ : Optional[Any] = self.default_image_processor a__ : Dict = prepare_img() a__ : Optional[int] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).pixel_values.to(lowerCamelCase__ ) # prepare bool_masked_pos a__ : Optional[Any] = torch.ones((1, 196) , dtype=torch.bool ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(pixel_values=lowerCamelCase__ , bool_masked_pos=lowerCamelCase__ ) a__ : Tuple = outputs.logits # verify the logits a__ : List[str] = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[int] = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , lowerCamelCase__ , atol=1E-2 ) ) @slow def _UpperCamelCase( self : Dict ): a__ : str = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(lowerCamelCase__ ) a__ : int = self.default_image_processor a__ : List[Any] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Union[str, Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Union[str, Any] = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Tuple = 281 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : Any ): a__ : Dict = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( lowerCamelCase__ ) a__ : str = self.default_image_processor a__ : List[str] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Dict = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Optional[int] = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[Any] = torch.tensor([1.6881, -0.2787, 0.5901] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Optional[Any] = 2_396 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): a__ : Optional[Any] = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : Tuple = model.to(lowerCamelCase__ ) a__ : List[Any] = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Tuple = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : Union[str, Any] = Image.open(ds[0]["file"] ) a__ : List[Any] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Optional[Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Tuple = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: a__ : Dict = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=lowerCamelCase__ , ) else: a__ : Dict = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=lowerCamelCase__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , lowerCamelCase__ , atol=1E-4 ) ) @slow def _UpperCamelCase( self : Tuple ): a__ : str = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : List[Any] = model.to(lowerCamelCase__ ) a__ : int = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Optional[int] = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : str = Image.open(ds[0]["file"] ) a__ : str = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : List[Any] = model(**lowerCamelCase__ ) a__ : Any = outputs.logits.detach().cpu() a__ : List[Any] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ , target_sizes=[(500, 300)] ) a__ : Optional[int] = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ ) a__ : List[str] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ ) a__ : Any = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ )
37
0
import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def __SCREAMING_SNAKE_CASE ( a__ : Optional[Any] ,a__ : Union[str, Any] ,a__ : Optional[int] ) -> List[Any]: # Initialise PyTorch model __A : Dict = MobileBertConfig.from_json_file(a__ ) print(f"""Building PyTorch model from configuration: {config}""" ) __A : Tuple = MobileBertForPreTraining(a__ ) # Load weights from tf checkpoint __A : Dict = load_tf_weights_in_mobilebert(a__ ,a__ ,a__ ) # Save pytorch-model print(f"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() ,a__ ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--mobilebert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained MobileBERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) UpperCAmelCase_ : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
17
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) def UpperCamelCase_ ( __a ) -> Union[str, Any]: a__ : Tuple = R"\w+[.]\d+" a__ : List[Any] = re.findall(__a , __a ) for pat in pats: a__ : Union[str, Any] = key.replace(__a , "_".join(pat.split("." ) ) ) return key def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : List[str] = pt_tuple_key[:-1] + ("scale",) if ( any("norm" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): a__ : Any = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: a__ : Optional[Any] = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: a__ : Union[str, Any] = pt_tuple_key[:-1] + ("embedding",) return renamed_pt_tuple_key, pt_tensor # conv layer a__ : List[str] = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: a__ : str = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer a__ : Tuple = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight": a__ : Tuple = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight a__ : Optional[Any] = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias a__ : Union[str, Any] = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCamelCase_ ( __a , __a , __a=42 ) -> str: # Step 1: Convert pytorch tensor to numpy a__ : Optional[int] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params a__ : Tuple = flax_model.init_weights(PRNGKey(__a ) ) a__ : Optional[Any] = flatten_dict(__a ) a__ : Union[str, Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): a__ : Optional[int] = rename_key(__a ) a__ : Optional[int] = tuple(renamed_pt_key.split("." ) ) # Correctly rename weight parameters a__, a__ : Union[str, Any] = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ''' f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) # also add unexpected weight so that warning is thrown a__ : str = jnp.asarray(__a ) return unflatten_dict(__a )
37
0
'''simple docstring''' from __future__ import annotations def __a(SCREAMING_SNAKE_CASE_ : list[int | float] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): '''simple docstring''' if len(SCREAMING_SNAKE_CASE_ ) == 0: raise ValueError("find_max() arg is an empty sequence" ) if ( left >= len(SCREAMING_SNAKE_CASE_ ) or left < -len(SCREAMING_SNAKE_CASE_ ) or right >= len(SCREAMING_SNAKE_CASE_ ) or right < -len(SCREAMING_SNAKE_CASE_ ) ): raise IndexError("list index out of range" ) if left == right: return nums[left] _lowerCAmelCase = (left + right) >> 1 # the middle _lowerCAmelCase = find_max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # find max in range[left, mid] _lowerCAmelCase = find_max(SCREAMING_SNAKE_CASE_ , mid + 1 , SCREAMING_SNAKE_CASE_ ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
18
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def UpperCamelCase_ ( ) -> int: a__ : Any = HfArgumentParser(__a ) a__ : Any = parser.parse_args_into_dataclasses()[0] a__ : Optional[int] = TensorFlowBenchmark(args=__a ) try: a__ : Optional[int] = parser.parse_args_into_dataclasses()[0] except ValueError as e: a__ : Tuple = "Arg --no_{0} is no longer used, please use --no-{0} instead." a__ : List[Any] = " ".join(str(__a ).split(" " )[:-1] ) a__ : str = "" a__ : List[Any] = eval(str(__a ).split(" " )[-1] ) a__ : List[str] = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(__a ) if len(__a ) > 0: a__ : Tuple = full_error_msg + begin_error_msg + str(__a ) raise ValueError(__a ) benchmark.run() if __name__ == "__main__": main()
37
0
"""simple docstring""" import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer _a = ["""gpt2"""] _a = """gpt2""" if is_tf_available(): class _UpperCAmelCase( tf.Module ): def __init__( self , __a) -> Optional[Any]: '''simple docstring''' super().__init__() _UpperCamelCase = tokenizer _UpperCamelCase = AutoConfig.from_pretrained(__a) _UpperCamelCase = TFGPTaLMHeadModel.from_config(__a) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text'''),)) def UpperCAmelCase ( self , __a) -> List[str]: '''simple docstring''' _UpperCamelCase = self.tokenizer(__a) _UpperCamelCase = tokenized['''input_ids'''].to_tensor() _UpperCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) _UpperCamelCase = self.model(input_ids=__a , attention_mask=__a)['''logits'''] return outputs @require_tf @require_keras_nlp class _UpperCAmelCase( unittest.TestCase ): def UpperCAmelCase ( self) -> Optional[int]: '''simple docstring''' super().setUp() _UpperCamelCase = [GPTaTokenizer.from_pretrained(__a) for checkpoint in (TOKENIZER_CHECKPOINTS)] _UpperCamelCase = [TFGPTaTokenizer.from_pretrained(__a) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers) == len(self.tf_tokenizers) _UpperCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] _UpperCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1])) def UpperCAmelCase ( self) -> Dict: '''simple docstring''' for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers): for test_inputs in self.test_sentences: _UpperCamelCase = tokenizer([test_inputs] , return_tensors='''tf''') _UpperCamelCase = tf_tokenizer([test_inputs]) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors _UpperCamelCase = python_outputs[key].numpy() _UpperCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape)) self.assertTrue(tf.reduce_all(tf.cast(__a , tf.intaa) == tf_outputs_values)) @slow def UpperCAmelCase ( self) -> Optional[Any]: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: _UpperCamelCase = tf.function(__a) for test_inputs in self.test_sentences: _UpperCamelCase = tf.constant(__a) _UpperCamelCase = compiled_tokenizer(__a) _UpperCamelCase = tf_tokenizer(__a) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key])) @slow def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: _UpperCamelCase = ModelToSave(tokenizer=__a) _UpperCamelCase = tf.convert_to_tensor([self.test_sentences[0]]) _UpperCamelCase = model.serving(__a) # Build model with some sample inputs with TemporaryDirectory() as tempdir: _UpperCamelCase = Path(__a) / '''saved.model''' tf.saved_model.save(__a , __a , signatures={'''serving_default''': model.serving}) _UpperCamelCase = tf.saved_model.load(__a) _UpperCamelCase = loaded_model.signatures['''serving_default'''](__a)['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output)) @slow def UpperCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: _UpperCamelCase = tf.convert_to_tensor([self.test_sentences[0]]) _UpperCamelCase = tf_tokenizer(__a) # Build model with some sample inputs _UpperCamelCase = tf_tokenizer.get_config() _UpperCamelCase = TFGPTaTokenizer.from_config(__a) _UpperCamelCase = model_from_config(__a) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key])) @slow def UpperCAmelCase ( self) -> Optional[int]: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: # for the test to run _UpperCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: _UpperCamelCase = tf.convert_to_tensor([self.test_sentences[0]]) _UpperCamelCase = tf_tokenizer(__a , max_length=__a) _UpperCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
19
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip UpperCamelCase : Optional[int] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def UpperCamelCase_ ( __a ) -> Any: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def UpperCamelCase_ ( __a , __a , __a ) -> Any: return max(metric_fn(__a , __a ) for gt in ground_truths ) def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [] if args.gold_data_mode == "qa": a__ : Any = pd.read_csv(__a , sep="\t" , header=__a ) for answer_list in data[1]: a__ : Union[str, Any] = ast.literal_eval(__a ) answers.append(__a ) else: a__ : List[str] = [line.strip() for line in open(__a , "r" ).readlines()] a__ : List[str] = [[reference] for reference in references] a__ : List[str] = 0 for prediction, ground_truths in zip(__a , __a ): total += 1 em += metric_max_over_ground_truths(__a , __a , __a ) fa += metric_max_over_ground_truths(__a , __a , __a ) a__ : Dict = 100.0 * em / total a__ : Optional[Any] = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = args.k a__ : str = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = 0 for hypo, reference in zip(__a , __a ): a__ : Any = set(hypo.split("\t" )[:k] ) a__ : Union[str, Any] = set(reference.split("\t" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k a__ : Union[str, Any] = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: def strip_title(__a ): if title.startswith("\"" ): a__ : Optional[Any] = title[1:] if title.endswith("\"" ): a__ : Union[str, Any] = title[:-1] return title a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a , )["input_ids"].to(args.device ) a__ : Optional[int] = rag_model.rag.question_encoder(__a ) a__ : Union[str, Any] = question_enc_outputs[0] a__ : Optional[int] = rag_model.retriever( __a , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="pt" , ) a__ : List[Any] = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) a__ : int = [] for docs in all_docs: a__ : Optional[int] = [strip_title(__a ) for title in docs["title"]] provenance_strings.append("\t".join(__a ) ) return provenance_strings def UpperCamelCase_ ( __a , __a , __a ) -> Dict: with torch.no_grad(): a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a ) a__ : Any = inputs_dict.input_ids.to(args.device ) a__ : Dict = inputs_dict.attention_mask.to(args.device ) a__ : Optional[int] = rag_model.generate( # rag_model overwrites generate __a , attention_mask=__a , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__a , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) a__ : int = rag_model.retriever.generator_tokenizer.batch_decode(__a , skip_special_tokens=__a ) if args.print_predictions: for q, a in zip(__a , __a ): logger.info("Q: {} - A: {}".format(__a , __a ) ) return answers def UpperCamelCase_ ( ) -> List[str]: a__ : int = argparse.ArgumentParser() parser.add_argument( "--model_type" , choices=["rag_sequence", "rag_token", "bart"] , type=__a , help=( "RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the" " model_name_or_path" ) , ) parser.add_argument( "--index_name" , default=__a , choices=["exact", "compressed", "legacy"] , type=__a , help="RAG model retriever type" , ) parser.add_argument( "--index_path" , default=__a , type=__a , help="Path to the retrieval index" , ) parser.add_argument("--n_docs" , default=5 , type=__a , help="Number of retrieved docs" ) parser.add_argument( "--model_name_or_path" , default=__a , type=__a , required=__a , help="Path to pretrained checkpoints or model identifier from huggingface.co/models" , ) parser.add_argument( "--eval_mode" , choices=["e2e", "retrieval"] , default="e2e" , type=__a , help=( "Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates" " precision@k." ) , ) parser.add_argument("--k" , default=1 , type=__a , help="k for the precision@k calculation" ) parser.add_argument( "--evaluation_set" , default=__a , type=__a , required=__a , help="Path to a file containing evaluation samples" , ) parser.add_argument( "--gold_data_path" , default=__a , type=__a , required=__a , help="Path to a tab-separated file with gold samples" , ) parser.add_argument( "--gold_data_mode" , default="qa" , type=__a , choices=["qa", "ans"] , help=( "Format of the gold data file" "qa - a single line in the following format: question [tab] answer_list" "ans - a single line of the gold file contains the expected answer string" ) , ) parser.add_argument( "--predictions_path" , type=__a , default="predictions.txt" , help="Name of the predictions file, to be stored in the checkpoints directory" , ) parser.add_argument( "--eval_all_checkpoints" , action="store_true" , help="Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number" , ) parser.add_argument( "--eval_batch_size" , default=8 , type=__a , help="Batch size per GPU/CPU for evaluation." , ) parser.add_argument( "--recalculate" , help="Recalculate predictions even if the prediction file exists" , action="store_true" , ) parser.add_argument( "--num_beams" , default=4 , type=__a , help="Number of beams to be used when generating answers" , ) parser.add_argument("--min_length" , default=1 , type=__a , help="Min length of the generated answers" ) parser.add_argument("--max_length" , default=50 , type=__a , help="Max length of the generated answers" ) parser.add_argument( "--print_predictions" , action="store_true" , help="If True, prints predictions while evaluating." , ) parser.add_argument( "--print_docs" , action="store_true" , help="If True, prints docs retried while generating." , ) a__ : int = parser.parse_args() a__ : Dict = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) return args def UpperCamelCase_ ( __a ) -> Optional[int]: a__ : Tuple = {} if args.model_type is None: a__ : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("rag" ): a__ : int = RagTokenForGeneration if args.model_type == "rag_token" else RagSequenceForGeneration a__ : Tuple = args.n_docs if args.index_name is not None: a__ : Any = args.index_name if args.index_path is not None: a__ : int = args.index_path else: a__ : Optional[Any] = BartForConditionalGeneration a__ : Tuple = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("Evaluate the following checkpoints: %s" , __a ) a__ : Any = get_scores if args.eval_mode == "e2e" else get_precision_at_k a__ : Union[str, Any] = evaluate_batch_eae if args.eval_mode == "e2e" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("Calculating metrics based on an existing predictions file: {}".format(args.predictions_path ) ) score_fn(__a , args.predictions_path , args.gold_data_path ) continue logger.info("***** Running evaluation for {} *****".format(__a ) ) logger.info(" Batch size = %d" , args.eval_batch_size ) logger.info(" Predictions will be stored under {}".format(args.predictions_path ) ) if args.model_type.startswith("rag" ): a__ : str = RagRetriever.from_pretrained(__a , **__a ) a__ : Optional[int] = model_class.from_pretrained(__a , retriever=__a , **__a ) model.retriever.init_retrieval() else: a__ : Dict = model_class.from_pretrained(__a , **__a ) model.to(args.device ) with open(args.evaluation_set , "r" ) as eval_file, open(args.predictions_path , "w" ) as preds_file: a__ : List[Any] = [] for line in tqdm(__a ): questions.append(line.strip() ) if len(__a ) == args.eval_batch_size: a__ : Union[str, Any] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) + "\n" ) preds_file.flush() a__ : Any = [] if len(__a ) > 0: a__ : List[str] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) ) preds_file.flush() score_fn(__a , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": UpperCamelCase : List[Any] = get_args() main(args)
37
0
import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup _lowerCAmelCase: Any = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582' } def _lowercase( __a : str = "dhaka" , __a : int = 5 ): a__ =min(__a , 50 ) # Prevent abuse! a__ ={ 'q': query, 'tbm': 'isch', 'hl': 'en', 'ijn': '0', } a__ =requests.get('https://www.google.com/search' , params=__a , headers=__a ) a__ =BeautifulSoup(html.text , 'html.parser' ) a__ =''.join( re.findall(r'AF_initDataCallback\(([^<]+)\);' , str(soup.select('script' ) ) ) ) a__ =json.dumps(__a ) a__ =json.loads(__a ) a__ =re.findall( r'\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\",' , __a , ) if not matched_google_image_data: return 0 a__ =re.sub( r'\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]' , '' , str(__a ) , ) a__ =re.findall( r'(?:\'|,),\[\"(https:|http.*?)\",\d+,\d+\]' , __a , ) for index, fixed_full_res_image in enumerate(__a ): if index >= max_images: return index a__ =bytes(__a , 'ascii' ).decode( 'unicode-escape' ) a__ =bytes(__a , 'ascii' ).decode( 'unicode-escape' ) a__ =urllib.request.build_opener() a__ =[ ( 'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582', ) ] urllib.request.install_opener(__a ) a__ =f"""query_{query.replace(' ' , '_' )}""" if not os.path.exists(__a ): os.makedirs(__a ) urllib.request.urlretrieve( # noqa: S310 __a , f"""{path_name}/original_size_img_{index}.jpg""" ) return index if __name__ == "__main__": try: _lowerCAmelCase: Optional[Any] = download_images_from_google_query(sys.argv[1]) print(F"""{image_count} images were downloaded to disk.""") except IndexError: print('Please provide a search term.') raise
20
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a = None , ) -> str: a__ : int = {} if train_file is not None: a__ : int = [train_file] if eval_file is not None: a__ : Union[str, Any] = [eval_file] if test_file is not None: a__ : str = [test_file] a__ : Optional[Any] = datasets.load_dataset("csv" , data_files=__a ) a__ : List[Any] = list(ds[list(files.keys() )[0]].features.keys() ) a__ : str = features_name.pop(__a ) a__ : Dict = list(set(ds[list(files.keys() )[0]][label_name] ) ) a__ : str = {label: i for i, label in enumerate(__a )} a__ : Tuple = tokenizer.model_input_names a__ : List[str] = {} if len(__a ) == 1: for k in files.keys(): a__ : Optional[Any] = ds[k].map( lambda __a : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=__a , max_length=__a , padding="max_length" ) , batched=__a , ) elif len(__a ) == 2: for k in files.keys(): a__ : Dict = ds[k].map( lambda __a : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=__a , max_length=__a , padding="max_length" , ) , batched=__a , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: a__ : str = {k: v for k, v in ex.items() if k in input_names} a__ : str = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: a__ : Tuple = {k: v for k, v in ex.items() if k in input_names} a__ : List[Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: a__ : List[Any] = {k: v for k, v in ex.items() if k in input_names} a__ : Optional[int] = labelaid[ex[label_name]] yield (d, label) a__ : Optional[Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: a__ : Optional[int] = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: a__ : Optional[Any] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: a__ : Tuple = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid UpperCamelCase : Optional[Any] = logging.getLogger(__name__) @dataclass class A__ : """simple docstring""" _lowercase = field(metadata={'help': 'Which column contains the label'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the training file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the development file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the test file'} ) _lowercase = field( default=1_2_8 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowercase = field( default=A__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) @dataclass class A__ : """simple docstring""" _lowercase = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowercase = field(default=A__ , metadata={'help': 'Set this flag to use fast tokenization.'} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. _lowercase = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) def UpperCamelCase_ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. a__ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) a__, a__, a__ : str = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.info( f'''n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, ''' f'''16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a__ : Union[str, Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) a__, a__, a__, a__ : Optional[Any] = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=__a , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) a__ : Optional[int] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(__a ) , labelaid=__a , idalabel={id: label for label, id in labelaid.items()} , finetuning_task="text-classification" , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): a__ : Any = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool(".bin" in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , ) def compute_metrics(__a ) -> Dict: a__ : Union[str, Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer a__ : Dict = TFTrainer( model=__a , args=__a , train_dataset=__a , eval_dataset=__a , compute_metrics=__a , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation a__ : Optional[Any] = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) a__ : Dict = trainer.evaluate() a__ : int = os.path.join(training_args.output_dir , "eval_results.txt" ) with open(__a , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(f''' {key} = {value}''' ) writer.write(f'''{key} = {value}\n''' ) results.update(__a ) return results if __name__ == "__main__": main()
37
0
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 UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : Any = { "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 ( UpperCamelCase__ ): UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , __snake_case :Optional[int]=5_02_65 , __snake_case :Any=5_12 , __snake_case :Tuple=8 , __snake_case :Optional[Any]=20_48 , __snake_case :List[Any]=16 , __snake_case :Any=8 , __snake_case :Union[str, Any]=20_48 , __snake_case :Any=16 , __snake_case :List[str]=0.0 , __snake_case :Dict=0.0 , __snake_case :str=True , __snake_case :Optional[int]=True , __snake_case :Optional[int]="gelu" , __snake_case :Dict=5_12 , __snake_case :Optional[Any]=0.1 , __snake_case :Tuple=0.0 , __snake_case :Optional[Any]=0.0 , __snake_case :Optional[int]=0.02 , __snake_case :Optional[int]=1 , __snake_case :str=False , __snake_case :List[Any]=0 , __snake_case :int=1 , __snake_case :List[Any]=2 , __snake_case :Optional[Any]=2 , **__snake_case :str , ): '''simple docstring''' __magic_name__ : int =vocab_size __magic_name__ : Optional[Any] =max_position_embeddings __magic_name__ : Optional[Any] =d_model __magic_name__ : str =encoder_ffn_dim __magic_name__ : Tuple =encoder_layers __magic_name__ : List[str] =encoder_attention_heads __magic_name__ : Union[str, Any] =decoder_ffn_dim __magic_name__ : int =decoder_layers __magic_name__ : Tuple =decoder_attention_heads __magic_name__ : Tuple =dropout __magic_name__ : List[str] =attention_dropout __magic_name__ : int =activation_dropout __magic_name__ : Union[str, Any] =activation_function __magic_name__ : Any =init_std __magic_name__ : Any =encoder_layerdrop __magic_name__ : Optional[int] =decoder_layerdrop __magic_name__ : int =use_cache __magic_name__ : Dict =encoder_layers __magic_name__ : Tuple =scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case , is_encoder_decoder=__snake_case , decoder_start_token_id=__snake_case , forced_eos_token_id=__snake_case , **__snake_case , ) class __A ( UpperCamelCase__ ): @property def A__ ( self :Any ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __magic_name__ : List[Any] =OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}), ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}), ] ) if self.use_past: __magic_name__ : Tuple ={0: """batch"""} __magic_name__ : Tuple ={0: """batch""", 1: """past_decoder_sequence + sequence"""} else: __magic_name__ : Optional[Any] ={0: """batch""", 1: """decoder_sequence"""} __magic_name__ : Union[str, Any] ={0: """batch""", 1: """decoder_sequence"""} if self.use_past: self.fill_with_past_key_values_(__snake_case , direction="""inputs""" ) elif self.task == "causal-lm": # TODO: figure this case out. __magic_name__ : Optional[Any] =OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}), ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}), ] ) if self.use_past: __magic_name__ , __magic_name__ : List[Any] =self.num_layers for i in range(__snake_case ): __magic_name__ : Optional[int] ={0: """batch""", 2: """past_sequence + sequence"""} __magic_name__ : str ={0: """batch""", 2: """past_sequence + sequence"""} else: __magic_name__ : Dict =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 A__ ( self :Any ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __magic_name__ : List[Any] =super().outputs else: __magic_name__ : Optional[Any] =super(__snake_case , self ).outputs if self.use_past: __magic_name__ , __magic_name__ : int =self.num_layers for i in range(__snake_case ): __magic_name__ : Optional[int] ={0: """batch""", 2: """past_sequence + sequence"""} __magic_name__ : str ={0: """batch""", 2: """past_sequence + sequence"""} return common_outputs def A__ ( self :List[Any] , __snake_case :PreTrainedTokenizer , __snake_case :int = -1 , __snake_case :int = -1 , __snake_case :bool = False , __snake_case :Optional[TensorType] = None , ): '''simple docstring''' __magic_name__ : str =self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Generate decoder inputs __magic_name__ : List[str] =seq_length if not self.use_past else 1 __magic_name__ : Any =self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) __magic_name__ : Any ={f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} __magic_name__ : List[str] =dict(**__snake_case , **__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 __magic_name__ , __magic_name__ : int =common_inputs["""input_ids"""].shape __magic_name__ : Any =common_inputs["""decoder_input_ids"""].shape[1] __magic_name__ , __magic_name__ : Optional[int] =self.num_attention_heads __magic_name__ : int =( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) __magic_name__ : List[Any] =decoder_seq_length + 3 __magic_name__ : List[Any] =( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) __magic_name__ : Any =torch.cat( [common_inputs["""decoder_attention_mask"""], torch.ones(__snake_case , __snake_case )] , dim=1 ) __magic_name__ : Optional[Any] =[] # If the number of encoder and decoder layers are present in the model configuration, both are considered __magic_name__ , __magic_name__ : Dict =self.num_layers __magic_name__ : List[Any] =min(__snake_case , __snake_case ) __magic_name__ : List[Any] =max(__snake_case , __snake_case ) - min_num_layers __magic_name__ : Optional[Any] ="""encoder""" if num_encoder_layers > num_decoder_layers else """decoder""" for _ in range(__snake_case ): common_inputs["past_key_values"].append( ( torch.zeros(__snake_case ), torch.zeros(__snake_case ), torch.zeros(__snake_case ), torch.zeros(__snake_case ), ) ) # TODO: test this. __magic_name__ : int =encoder_shape if remaining_side_name == """encoder""" else decoder_shape for _ in range(__snake_case , __snake_case ): common_inputs["past_key_values"].append((torch.zeros(__snake_case ), torch.zeros(__snake_case )) ) return common_inputs def A__ ( self :List[str] , __snake_case :PreTrainedTokenizer , __snake_case :int = -1 , __snake_case :int = -1 , __snake_case :bool = False , __snake_case :Optional[TensorType] = None , ): '''simple docstring''' __magic_name__ : Optional[Any] =self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __snake_case , __snake_case , __snake_case , __snake_case , __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 __magic_name__ , __magic_name__ : int =common_inputs["""input_ids"""].shape # Not using the same length for past_key_values __magic_name__ : int =seqlen + 2 __magic_name__ , __magic_name__ : Tuple =self.num_layers __magic_name__ , __magic_name__ : List[str] =self.num_attention_heads __magic_name__ : List[Any] =( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) __magic_name__ : Union[str, Any] =common_inputs["""attention_mask"""].dtype __magic_name__ : int =torch.cat( [common_inputs["""attention_mask"""], torch.ones(__snake_case , __snake_case , dtype=__snake_case )] , dim=1 ) __magic_name__ : Dict =[ (torch.zeros(__snake_case ), torch.zeros(__snake_case )) for _ in range(__snake_case ) ] return common_inputs def A__ ( self :Any , __snake_case :PreTrainedTokenizer , __snake_case :int = -1 , __snake_case :int = -1 , __snake_case :bool = False , __snake_case :Optional[TensorType] = None , ): '''simple docstring''' __magic_name__ : Tuple =compute_effective_axis_dimension( __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 __magic_name__ : Any =tokenizer.num_special_tokens_to_add(__snake_case ) __magic_name__ : Optional[Any] =compute_effective_axis_dimension( __snake_case , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__snake_case ) # Generate dummy inputs according to compute batch and sequence __magic_name__ : List[str] =[""" """.join([tokenizer.unk_token] ) * seq_length] * batch_size __magic_name__ : int =dict(tokenizer(__snake_case , return_tensors=__snake_case ) ) return common_inputs def A__ ( self :Optional[Any] , __snake_case :PreTrainedTokenizer , __snake_case :int = -1 , __snake_case :int = -1 , __snake_case :bool = False , __snake_case :Optional[TensorType] = None , ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __magic_name__ : Any =self._generate_dummy_inputs_for_default_and_seqaseq_lm( __snake_case , batch_size=__snake_case , seq_length=__snake_case , is_pair=__snake_case , framework=__snake_case ) elif self.task == "causal-lm": __magic_name__ : List[Any] =self._generate_dummy_inputs_for_causal_lm( __snake_case , batch_size=__snake_case , seq_length=__snake_case , is_pair=__snake_case , framework=__snake_case ) else: __magic_name__ : int =self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __snake_case , batch_size=__snake_case , seq_length=__snake_case , is_pair=__snake_case , framework=__snake_case ) return common_inputs def A__ ( self :List[str] , __snake_case :Any , __snake_case :Dict , __snake_case :Any , __snake_case :Any ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: __magic_name__ : Union[str, Any] =super()._flatten_past_key_values_(__snake_case , __snake_case , __snake_case , __snake_case ) else: __magic_name__ : Optional[Any] =super(__snake_case , self )._flatten_past_key_values_( __snake_case , __snake_case , __snake_case , __snake_case )
21
import argparse import collections import json import os import re import string import sys import numpy as np UpperCamelCase : List[str] = re.compile(r"""\b(a|an|the)\b""", re.UNICODE) UpperCamelCase : Union[str, Any] = None def UpperCamelCase_ ( ) -> List[str]: a__ : List[Any] = 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 UpperCamelCase_ ( __a ) -> str: a__ : Optional[Any] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : Dict = bool(qa["answers"]["text"] ) return qid_to_has_ans def UpperCamelCase_ ( __a ) -> List[Any]: def remove_articles(__a ): return ARTICLES_REGEX.sub(" " , __a ) def white_space_fix(__a ): return " ".join(text.split() ) def remove_punc(__a ): a__ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def UpperCamelCase_ ( __a ) -> Dict: if not s: return [] return normalize_answer(__a ).split() def UpperCamelCase_ ( __a , __a ) -> str: return int(normalize_answer(__a ) == normalize_answer(__a ) ) def UpperCamelCase_ ( __a , __a ) -> Dict: a__ : int = get_tokens(__a ) a__ : Optional[Any] = get_tokens(__a ) a__ : Any = collections.Counter(__a ) & collections.Counter(__a ) a__ : Dict = 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 a__ : Tuple = 1.0 * num_same / len(__a ) a__ : str = 1.0 * num_same / len(__a ) a__ : str = (2 * precision * recall) / (precision + recall) return fa def UpperCamelCase_ ( __a , __a ) -> int: a__ : List[str] = {} a__ : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : List[Any] = qa["id"] a__ : Dict = [t for t in qa["answers"]["text"] if normalize_answer(__a )] if not gold_answers: # For unanswerable questions, only correct answer is empty string a__ : Tuple = [""] if qid not in preds: print(f'''Missing prediction for {qid}''' ) continue a__ : Tuple = preds[qid] # Take max over all gold answers a__ : Optional[int] = max(compute_exact(__a , __a ) for a in gold_answers ) a__ : str = max(compute_fa(__a , __a ) for a in gold_answers ) return exact_scores, fa_scores def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: a__ : Optional[Any] = {} for qid, s in scores.items(): a__ : Dict = na_probs[qid] > na_prob_thresh if pred_na: a__ : Dict = float(not qid_to_has_ans[qid] ) else: a__ : Optional[Any] = s return new_scores def UpperCamelCase_ ( __a , __a , __a=None ) -> Tuple: if not qid_list: a__ : Union[str, Any] = len(__a ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: a__ : int = 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 UpperCamelCase_ ( __a , __a , __a ) -> List[str]: for k in new_eval: a__ : Optional[Any] = new_eval[k] def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: 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 UpperCamelCase_ ( __a , __a , __a , __a , __a=None , __a=None ) -> Dict: a__ : Optional[Any] = sorted(__a , key=lambda __a : na_probs[k] ) a__ : Any = 0.0 a__ : Optional[int] = 1.0 a__ : Optional[int] = 0.0 a__ : Any = [1.0] a__ : Tuple = [0.0] a__ : List[str] = 0.0 for i, qid in enumerate(__a ): if qid_to_has_ans[qid]: true_pos += scores[qid] a__ : Any = true_pos / float(i + 1 ) a__ : int = 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 UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> str: if out_image_dir and not os.path.exists(__a ): os.makedirs(__a ) a__ : Optional[int] = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return a__ : Optional[int] = 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" , ) a__ : Optional[Any] = make_precision_recall_eval( __a , __a , __a , __a , out_image=os.path.join(__a , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) a__ : str = {k: float(__a ) for k, v in qid_to_has_ans.items()} a__ : Optional[Any] = 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> str: if not qid_list: return a__ : Optional[Any] = [na_probs[k] for k in qid_list] a__ : 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[Any]: a__ : str = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) a__ : Optional[Any] = num_no_ans a__ : Dict = cur_score a__ : Any = 0.0 a__ : Optional[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]: a__ : Optional[int] = scores[qid] else: if preds[qid]: a__ : str = -1 else: a__ : Union[str, Any] = 0 cur_score += diff if cur_score > best_score: a__ : Any = cur_score a__ : Dict = na_probs[qid] return 100.0 * best_score / len(__a ), best_thresh def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> Any: a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__ : Any = best_exact a__ : Any = exact_thresh a__ : List[Any] = best_fa a__ : Optional[int] = fa_thresh def UpperCamelCase_ ( ) -> Tuple: with open(OPTS.data_file ) as f: a__ : List[Any] = json.load(__a ) a__ : Any = dataset_json["data"] with open(OPTS.pred_file ) as f: a__ : int = json.load(__a ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: a__ : List[str] = json.load(__a ) else: a__ : Optional[int] = {k: 0.0 for k in preds} a__ : Optional[Any] = make_qid_to_has_ans(__a ) # maps qid to True/False a__ : List[Any] = [k for k, v in qid_to_has_ans.items() if v] a__ : Union[str, Any] = [k for k, v in qid_to_has_ans.items() if not v] a__, a__ : str = get_raw_scores(__a , __a ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : Tuple = make_eval_dict(__a , __a ) if has_ans_qids: a__ : str = make_eval_dict(__a , __a , qid_list=__a ) merge_eval(__a , __a , "HasAns" ) if no_ans_qids: a__ : List[Any] = 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__": UpperCamelCase : Any = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("""Agg""") import matplotlib.pyplot as plt main()
37
0
'''simple docstring''' import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available _snake_case : Optional[int] = logging.getLogger(__name__) @dataclass class A : lowercase_ = 42 lowercase_ = 42 lowercase_ = 42 @dataclass class A : lowercase_ = 42 lowercase_ = 42 lowercase_ = None lowercase_ = None class A ( _a ): lowercase_ = 'train' lowercase_ = 'dev' lowercase_ = 'test' class A : @staticmethod def __lowerCAmelCase ( lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Union[Split, str] ) -> List[InputExample]: """simple docstring""" raise NotImplementedError @staticmethod def __lowerCAmelCase ( lowerCAmelCase_ : str ) -> List[str]: """simple docstring""" raise NotImplementedError @staticmethod def __lowerCAmelCase ( lowerCAmelCase_ : List[InputExample] , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : int , lowerCAmelCase_ : PreTrainedTokenizer , lowerCAmelCase_ : str=False , lowerCAmelCase_ : Dict="[CLS]" , lowerCAmelCase_ : Optional[int]=1 , lowerCAmelCase_ : Union[str, Any]="[SEP]" , lowerCAmelCase_ : Optional[int]=False , lowerCAmelCase_ : str=False , lowerCAmelCase_ : Optional[Any]=0 , lowerCAmelCase_ : Tuple=0 , lowerCAmelCase_ : Dict=-1_00 , lowerCAmelCase_ : List[str]=0 , lowerCAmelCase_ : str=True , ) -> List[InputFeatures]: """simple docstring""" _a = {label: i for i, label in enumerate(lowerCAmelCase_ )} _a = [] for ex_index, example in enumerate(lowerCAmelCase_ ): if ex_index % 1_00_00 == 0: logger.info('''Writing example %d of %d''' , lowerCAmelCase_ , len(lowerCAmelCase_ ) ) _a = [] _a = [] for word, label in zip(example.words , example.labels ): _a = tokenizer.tokenize(lowerCAmelCase_ ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(lowerCAmelCase_ ) > 0: tokens.extend(lowerCAmelCase_ ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(lowerCAmelCase_ ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. _a = tokenizer.num_special_tokens_to_add() if len(lowerCAmelCase_ ) > max_seq_length - special_tokens_count: _a = tokens[: (max_seq_length - special_tokens_count)] _a = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] _a = [sequence_a_segment_id] * len(lowerCAmelCase_ ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: _a = [cls_token] + tokens _a = [pad_token_label_id] + label_ids _a = [cls_token_segment_id] + segment_ids _a = tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. _a = [1 if mask_padding_with_zero else 0] * len(lowerCAmelCase_ ) # Zero-pad up to the sequence length. _a = max_seq_length - len(lowerCAmelCase_ ) if pad_on_left: _a = ([pad_token] * padding_length) + input_ids _a = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask _a = ([pad_token_segment_id] * padding_length) + segment_ids _a = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(lowerCAmelCase_ ) == max_seq_length assert len(lowerCAmelCase_ ) == max_seq_length assert len(lowerCAmelCase_ ) == max_seq_length assert len(lowerCAmelCase_ ) == max_seq_length if ex_index < 5: logger.info('''*** Example ***''' ) logger.info('''guid: %s''' , example.guid ) logger.info('''tokens: %s''' , ''' '''.join([str(lowerCAmelCase_ ) for x in tokens] ) ) logger.info('''input_ids: %s''' , ''' '''.join([str(lowerCAmelCase_ ) for x in input_ids] ) ) logger.info('''input_mask: %s''' , ''' '''.join([str(lowerCAmelCase_ ) for x in input_mask] ) ) logger.info('''segment_ids: %s''' , ''' '''.join([str(lowerCAmelCase_ ) for x in segment_ids] ) ) logger.info('''label_ids: %s''' , ''' '''.join([str(lowerCAmelCase_ ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: _a = None features.append( InputFeatures( input_ids=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ , token_type_ids=lowerCAmelCase_ , label_ids=lowerCAmelCase_ ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class A ( _a ): lowercase_ = 42 lowercase_ = nn.CrossEntropyLoss().ignore_index def __init__( self : Dict , lowerCAmelCase_ : TokenClassificationTask , lowerCAmelCase_ : str , lowerCAmelCase_ : PreTrainedTokenizer , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[int] = None , lowerCAmelCase_ : Optional[Any]=False , lowerCAmelCase_ : Split = Split.train , ) -> List[str]: """simple docstring""" _a = os.path.join( lowerCAmelCase_ , '''cached_{}_{}_{}'''.format(mode.value , tokenizer.__class__.__name__ , str(lowerCAmelCase_ ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _a = cached_features_file + '''.lock''' with FileLock(lowerCAmelCase_ ): if os.path.exists(lowerCAmelCase_ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) _a = torch.load(lowerCAmelCase_ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) _a = token_classification_task.read_examples_from_file(lowerCAmelCase_ , lowerCAmelCase_ ) # TODO clean up all this to leverage built-in features of tokenizers _a = token_classification_task.convert_examples_to_features( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=lowerCAmelCase_ , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(F'Saving features into cached file {cached_features_file}' ) torch.save(self.features , lowerCAmelCase_ ) def __len__( self : Any ) -> int: """simple docstring""" return len(self.features ) def __getitem__( self : Dict , lowerCAmelCase_ : Optional[int] ) -> InputFeatures: """simple docstring""" return self.features[i] if is_tf_available(): import tensorflow as tf class A : lowercase_ = 42 lowercase_ = -100 def __init__( self : Optional[Any] , lowerCAmelCase_ : TokenClassificationTask , lowerCAmelCase_ : str , lowerCAmelCase_ : PreTrainedTokenizer , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[int] = None , lowerCAmelCase_ : List[str]=False , lowerCAmelCase_ : Split = Split.train , ) -> str: """simple docstring""" _a = token_classification_task.read_examples_from_file(lowerCAmelCase_ , lowerCAmelCase_ ) # TODO clean up all this to leverage built-in features of tokenizers _a = token_classification_task.convert_examples_to_features( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=lowerCAmelCase_ , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: _a = tf.data.Dataset.from_generator( lowerCAmelCase_ , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa}, tf.intaa) , ( {'''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: _a = tf.data.Dataset.from_generator( lowerCAmelCase_ , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa, '''token_type_ids''': tf.intaa}, tf.intaa) , ( { '''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] ), '''token_type_ids''': tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def __lowerCAmelCase ( self : List[Any] ) -> List[Any]: """simple docstring""" _a = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Tuple ) -> Tuple: """simple docstring""" return len(self.features ) def __getitem__( self : Union[str, Any] , lowerCAmelCase_ : Tuple ) -> InputFeatures: """simple docstring""" return self.features[i]
22
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = CLIPTokenizer _lowercase = CLIPTokenizerFast _lowercase = True _lowercase = {} _lowercase = False def _UpperCamelCase( self : List[Any] ): super().setUp() # fmt: off a__ : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on a__ : Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a__ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] a__ : Optional[Any] = {"unk_token": "<unk>"} a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def _UpperCamelCase( self : Dict , **lowerCamelCase__ : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] , **lowerCamelCase__ : Optional[int] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : int = "lower newer" a__ : Optional[int] = "lower newer" return input_text, output_text def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a__ : int = "lower newer" a__ : List[str] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] a__ : Union[str, Any] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = tokens + [tokenizer.unk_token] a__ : Union[str, Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) @require_ftfy def _UpperCamelCase( self : Optional[Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : List[str] = self.tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : Any = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : int = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." a__ : Optional[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : Dict = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways a__ : Optional[Any] = "xa\u0303y" + " " + "x\xe3y" a__ : Optional[int] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of space type a__ : str = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: a__ : Any = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of line break type a__ : Union[str, Any] = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: a__ : List[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : str = "hello" # `hello` is a token in the vocabulary of `pretrained_name` a__ : Tuple = f'''{text_of_1_token} {text_of_1_token}''' a__ : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Union[str, Any] = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCamelCase__ ) + 1, len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) a__ : Optional[Any] = f''' {text}''' a__ : str = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Dict = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCamelCase__ ) + 1, 1 + len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) def _UpperCamelCase( self : int ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCamelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _UpperCamelCase( self : int ): super().test_tokenization_python_rust_equals() def _UpperCamelCase( self : str ): # CLIP always lower cases letters pass
37
0
import unittest from transformers import DonutProcessor snake_case__ : Union[str, Any] = """naver-clova-ix/donut-base""" class _a ( unittest.TestCase ): """simple docstring""" def _UpperCAmelCase ( self ) -> Any: UpperCamelCase_ = DonutProcessor.from_pretrained(_UpperCAmelCase ) def _UpperCAmelCase ( self ) -> List[Any]: UpperCamelCase_ = { 'name': 'John Doe', 'age': '99', 'city': 'Atlanta', 'state': 'GA', 'zip': '30301', 'phone': '123-4567', 'nicknames': [{'nickname': 'Johnny'}, {'nickname': 'JD'}], } UpperCamelCase_ = ( '<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>' '<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>' '<s_nicknames><s_nickname>Johnny</s_nickname>' '<sep/><s_nickname>JD</s_nickname></s_nicknames>' ) UpperCamelCase_ = self.processor.tokenajson(_UpperCAmelCase ) self.assertDictEqual(_UpperCAmelCase , _UpperCAmelCase )
23
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase : Dict = """<<<<<<< This should probably be modified because it mentions: """ UpperCamelCase : List[Any] = """======= >>>>>>> """ UpperCamelCase : Optional[Any] = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] UpperCamelCase : 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 UpperCamelCase_ ( __a ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class A__ ( A__ ): """simple docstring""" @staticmethod def _UpperCamelCase( lowerCamelCase__ : ArgumentParser ): a__ : List[str] = parser.add_parser( "convert" , help="Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset." , ) train_parser.add_argument( "--tfds_path" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to a TensorFlow Datasets folder to convert or a single tfds file to convert." , ) train_parser.add_argument( "--datasets_directory" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to the HuggingFace Datasets folder." ) train_parser.set_defaults(func=lowerCamelCase__ ) def __init__( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str , *lowerCamelCase__ : Tuple ): a__ : str = get_logger("datasets-cli/converting" ) a__ : Optional[Any] = tfds_path a__ : Optional[int] = datasets_directory def _UpperCamelCase( self : int ): if os.path.isdir(self._tfds_path ): a__ : List[str] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a__ : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("--tfds_path is neither a directory nor a file. Please check path." ) a__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a__ : Tuple = [] a__ : str = [] a__ : List[Any] = {} if os.path.isdir(self._tfds_path ): a__ : List[str] = os.listdir(lowerCamelCase__ ) else: a__ : Union[str, Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'''Looking at file {f_name}''' ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Dict = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) if not os.path.isfile(lowerCamelCase__ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("Skipping file" ) continue with open(lowerCamelCase__ , encoding="utf-8" ) as f: a__ : List[Any] = f.readlines() a__ : Union[str, Any] = [] a__ : Union[str, Any] = False a__ : Union[str, Any] = False a__ : Dict = [] for line in lines: a__ : Optional[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: a__ : List[Any] = "import datasets\n" elif "import tensorflow" in out_line: # order is important here a__ : List[str] = "" continue elif "from absl import logging" in out_line: a__ : Dict = "from datasets import logging\n" elif "getLogger" in out_line: a__ : List[Any] = out_line.replace("getLogger" , "get_logger" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a__ : List[str] = True a__ : Dict = list(filter(lambda lowerCamelCase__ : e in out_line , lowerCamelCase__ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(lowerCamelCase__ ) + "\n" ) out_lines.append(lowerCamelCase__ ) out_lines.append(lowerCamelCase__ ) continue else: for pattern, replacement in TO_CONVERT: a__ : Tuple = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a__ : Optional[int] = re.match(r"from\stensorflow_datasets.*import\s([^\.\r\n]+)" , lowerCamelCase__ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split("," ) ) a__ : Optional[Any] = "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: a__ : Optional[int] = True out_lines.append(lowerCamelCase__ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a__ : Dict = f_name.replace(".py" , "" ) a__ : Optional[int] = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) 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(lowerCamelCase__ ) if needs_manual_update: with_manual_update.append(lowerCamelCase__ ) with open(lowerCamelCase__ , "w" , encoding="utf-8" ) as f: f.writelines(lowerCamelCase__ ) self._logger.info(f'''Converted in {output_file}''' ) for utils_file in utils_files: try: a__ : Any = os.path.basename(lowerCamelCase__ ) a__ : Optional[int] = imports_to_builder_map[f_name.replace(".py" , "" )] self._logger.info(f'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(lowerCamelCase__ , lowerCamelCase__ ) 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\'.''' )
37
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase): __lowercase : List[str] = StableDiffusionSAGPipeline __lowercase : Union[str, Any] = TEXT_TO_IMAGE_PARAMS __lowercase : Dict = TEXT_TO_IMAGE_BATCH_PARAMS __lowercase : Dict = TEXT_TO_IMAGE_IMAGE_PARAMS __lowercase : List[str] = TEXT_TO_IMAGE_IMAGE_PARAMS __lowercase : Optional[int] = False def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' torch.manual_seed(0 ) __snake_case = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __snake_case = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_one=__SCREAMING_SNAKE_CASE , ) torch.manual_seed(0 ) __snake_case = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __snake_case = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) __snake_case = CLIPTextModel(__SCREAMING_SNAKE_CASE ) __snake_case = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __snake_case = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ) -> int: '''simple docstring''' if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): __snake_case = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: __snake_case = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) __snake_case = { '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase ( self ) -> Any: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class lowerCAmelCase ( unittest.TestCase): def lowerCAmelCase ( self ) -> str: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' __snake_case = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) __snake_case = sag_pipe.to(__SCREAMING_SNAKE_CASE ) sag_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __snake_case = '''.''' __snake_case = torch.manual_seed(0 ) __snake_case = sag_pipe( [prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) __snake_case = output.images __snake_case = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __snake_case = np.array([0.1_568, 0.1_738, 0.1_695, 0.1_693, 0.1_507, 0.1_705, 0.1_547, 0.1_751, 0.1_949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) __snake_case = sag_pipe.to(__SCREAMING_SNAKE_CASE ) sag_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __snake_case = '''.''' __snake_case = torch.manual_seed(0 ) __snake_case = sag_pipe( [prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) __snake_case = output.images __snake_case = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __snake_case = np.array([0.3_459, 0.2_876, 0.2_537, 0.3_002, 0.2_671, 0.2_160, 0.3_026, 0.2_262, 0.2_371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) __snake_case = sag_pipe.to(__SCREAMING_SNAKE_CASE ) sag_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __snake_case = '''.''' __snake_case = torch.manual_seed(0 ) __snake_case = sag_pipe( [prompt] , width=768 , height=512 , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) __snake_case = output.images assert image.shape == (1, 512, 768, 3)
24
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class A__ ( A__ ): """simple docstring""" _lowercase = '' _lowercase = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _lowercase = None # compression type in fsspec. ex: "gzip" _lowercase = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , lowerCamelCase__ : str = "" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , **lowerCamelCase__ : List[str] ): super().__init__(self , **lowerCamelCase__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode a__ : str = fsspec.open( lowerCamelCase__ , mode="rb" , protocol=lowerCamelCase__ , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) a__ : Optional[int] = os.path.basename(self.file.path.split("::" )[0] ) a__ : int = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) a__ : List[Any] = None @classmethod def _UpperCamelCase( cls : int , lowerCamelCase__ : int ): # compressed file paths are always relative to the archive root return super()._strip_protocol(lowerCamelCase__ ).lstrip("/" ) def _UpperCamelCase( self : Dict ): if self.dir_cache is None: a__ : Dict = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} a__ : int = {f["name"]: f} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : str ): return self.file.open().read() def _UpperCamelCase( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : int=None , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[str]=None , **lowerCamelCase__ : Optional[Any] , ): a__ : Optional[int] = self._strip_protocol(lowerCamelCase__ ) if mode != "rb": raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class A__ ( A__ ): """simple docstring""" _lowercase = 'bz2' _lowercase = 'bz2' _lowercase = '.bz2' class A__ ( A__ ): """simple docstring""" _lowercase = 'gzip' _lowercase = 'gzip' _lowercase = '.gz' class A__ ( A__ ): """simple docstring""" _lowercase = 'lz4' _lowercase = 'lz4' _lowercase = '.lz4' class A__ ( A__ ): """simple docstring""" _lowercase = 'xz' _lowercase = 'xz' _lowercase = '.xz' class A__ ( A__ ): """simple docstring""" _lowercase = 'zstd' _lowercase = 'zstd' _lowercase = '.zst' def __init__( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , lowerCamelCase__ : int = DEFAULT_BLOCK_SIZE , **lowerCamelCase__ : Tuple , ): super().__init__( fo=lowerCamelCase__ , mode=lowerCamelCase__ , target_protocol=lowerCamelCase__ , target_options=lowerCamelCase__ , block_size=lowerCamelCase__ , **lowerCamelCase__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 a__ : Any = self.file.__enter__ class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : str ): a__ : List[Any] = file_ def __enter__( self : str ): self._file.__enter__() return self def __exit__( self : int , *lowerCamelCase__ : List[str] , **lowerCamelCase__ : int ): self._file.__exit__(*lowerCamelCase__ , **lowerCamelCase__ ) def __iter__( self : List[str] ): return iter(self._file ) def _UpperCamelCase( self : Any ): return next(self._file ) def __getattr__( self : Optional[Any] , lowerCamelCase__ : Tuple ): return getattr(self._file , lowerCamelCase__ ) def fixed_enter(*lowerCamelCase__ : List[str] , **lowerCamelCase__ : str ): return WrappedFile(_enter(*lowerCamelCase__ , **lowerCamelCase__ ) ) a__ : Any = fixed_enter
37
0
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging a_ = logging.get_logger(__name__) a_ = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} # See all BART models at https://huggingface.co/models?filter=bart a_ = { 'vocab_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json', }, 'merges_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt', }, } a_ = { 'facebook/bart-base': 1024, 'facebook/bart-large': 1024, 'facebook/bart-large-mnli': 1024, 'facebook/bart-large-cnn': 1024, 'facebook/bart-large-xsum': 1024, 'yjernite/bart_eli5': 1024, } @lru_cache() def lowerCamelCase__ ( ): SCREAMING_SNAKE_CASE : int = ( list(range(ord("!") , ord("~") + 1)) + list(range(ord("¡") , ord("¬") + 1)) + list(range(ord("®") , ord("ÿ") + 1)) ) SCREAMING_SNAKE_CASE : Union[str, Any] = bs[:] SCREAMING_SNAKE_CASE : List[str] = 0 for b in range(2**8): if b not in bs: bs.append(_a) cs.append(2**8 + n) n += 1 SCREAMING_SNAKE_CASE : Any = [chr(_a) for n in cs] return dict(zip(_a , _a)) def lowerCamelCase__ ( _a): SCREAMING_SNAKE_CASE : str = set() SCREAMING_SNAKE_CASE : str = word[0] for char in word[1:]: pairs.add((prev_char, char)) SCREAMING_SNAKE_CASE : Any = char return pairs class _UpperCamelCase ( __A ): '''simple docstring''' lowerCamelCase__ =VOCAB_FILES_NAMES lowerCamelCase__ =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ =['input_ids', 'attention_mask'] def __init__( self : List[str] , a : int , a : str , a : str="replace" , a : Tuple="<s>" , a : int="</s>" , a : Any="</s>" , a : Any="<s>" , a : List[str]="<unk>" , a : List[Any]="<pad>" , a : List[str]="<mask>" , a : Union[str, Any]=False , **a : Any , ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else bos_token SCREAMING_SNAKE_CASE : Tuple = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else eos_token SCREAMING_SNAKE_CASE : Union[str, Any] = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else sep_token SCREAMING_SNAKE_CASE : Optional[Any] = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else cls_token SCREAMING_SNAKE_CASE : str = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else unk_token SCREAMING_SNAKE_CASE : Optional[Any] = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE : Union[str, Any] = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( errors=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , add_prefix_space=a , **a , ) with open(a , encoding="utf-8" ) as vocab_handle: SCREAMING_SNAKE_CASE : int = json.load(a ) SCREAMING_SNAKE_CASE : str = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE : Optional[Any] = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE : Optional[int] = bytes_to_unicode() SCREAMING_SNAKE_CASE : str = {v: k for k, v in self.byte_encoder.items()} with open(a , encoding="utf-8" ) as merges_handle: SCREAMING_SNAKE_CASE : Tuple = merges_handle.read().split("\n" )[1:-1] SCREAMING_SNAKE_CASE : Optional[Any] = [tuple(merge.split() ) for merge in bpe_merges] SCREAMING_SNAKE_CASE : Union[str, Any] = dict(zip(a , range(len(a ) ) ) ) SCREAMING_SNAKE_CASE : Optional[int] = {} SCREAMING_SNAKE_CASE : Dict = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE : Tuple = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property def __UpperCamelCase ( self : int ) -> List[Any]: """simple docstring""" return len(self.encoder ) def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def __UpperCamelCase ( self : Dict , a : Optional[Any] ) -> Optional[Any]: """simple docstring""" if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE : int = tuple(a ) SCREAMING_SNAKE_CASE : Optional[Any] = get_pairs(a ) if not pairs: return token while True: SCREAMING_SNAKE_CASE : Optional[Any] = min(a , key=lambda a : self.bpe_ranks.get(a , float("inf" ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : Optional[int] = bigram SCREAMING_SNAKE_CASE : Optional[Any] = [] SCREAMING_SNAKE_CASE : Any = 0 while i < len(a ): try: SCREAMING_SNAKE_CASE : List[Any] = word.index(a , a ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE : Tuple = j if word[i] == first and i < len(a ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE : Optional[Any] = tuple(a ) SCREAMING_SNAKE_CASE : Tuple = new_word if len(a ) == 1: break else: SCREAMING_SNAKE_CASE : str = get_pairs(a ) SCREAMING_SNAKE_CASE : str = " ".join(a ) SCREAMING_SNAKE_CASE : List[str] = word return word def __UpperCamelCase ( self : Any , a : Tuple ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = [] for token in re.findall(self.pat , a ): SCREAMING_SNAKE_CASE : int = "".join( self.byte_encoder[b] for b in token.encode("utf-8" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(a ).split(" " ) ) return bpe_tokens def __UpperCamelCase ( self : Tuple , a : Any ) -> str: """simple docstring""" return self.encoder.get(a , self.encoder.get(self.unk_token ) ) def __UpperCamelCase ( self : List[str] , a : List[str] ) -> Union[str, Any]: """simple docstring""" return self.decoder.get(a ) def __UpperCamelCase ( self : List[str] , a : List[Any] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE : Dict = "".join(a ) SCREAMING_SNAKE_CASE : Union[str, Any] = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def __UpperCamelCase ( self : Union[str, Any] , a : str , a : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(a ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE : Tuple = os.path.join( a , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE : List[Any] = os.path.join( a , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(a , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=a , ensure_ascii=a ) + "\n" ) SCREAMING_SNAKE_CASE : List[Any] = 0 with open(a , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda a : kv[1] ): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!" ) SCREAMING_SNAKE_CASE : Any = token_index writer.write(" ".join(a ) + "\n" ) index += 1 return vocab_file, merge_file def __UpperCamelCase ( self : List[str] , a : List[int] , a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE : Any = [self.cls_token_id] SCREAMING_SNAKE_CASE : Union[str, Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __UpperCamelCase ( self : Tuple , a : List[int] , a : Optional[List[int]] = None , a : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a , token_ids_a=a , already_has_special_tokens=a ) if token_ids_a is None: return [1] + ([0] * len(a )) + [1] return [1] + ([0] * len(a )) + [1, 1] + ([0] * len(a )) + [1] def __UpperCamelCase ( self : List[Any] , a : List[int] , a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = [self.sep_token_id] SCREAMING_SNAKE_CASE : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase ( self : int , a : Tuple , a : List[str]=False , **a : List[Any] ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(a ) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE : Tuple = " " + text return (text, kwargs)
25
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Union[str, Any] = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") a__ : Union[str, Any] = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__a ): os.makedirs(__a ) a__ : Any = model.state_dict() def to_tf_var_name(__a ): for patt, repl in iter(__a ): a__ : Tuple = name.replace(__a , __a ) return f'''bert/{name}''' def create_tf_var(__a , __a , __a ): a__ : Tuple = tf.dtypes.as_dtype(tensor.dtype ) a__ : Dict = tf.get_variable(dtype=__a , shape=tensor.shape , name=__a , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__a ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: a__ : int = to_tf_var_name(__a ) a__ : Union[str, Any] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): a__ : int = torch_tensor.T a__ : Optional[Any] = create_tf_var(tensor=__a , name=__a , session=__a ) tf.keras.backend.set_value(__a , __a ) a__ : int = session.run(__a ) print(f'''Successfully created {tf_name}: {np.allclose(__a , __a )}''' ) a__ : Any = tf.train.Saver(tf.trainable_variables() ) saver.save(__a , os.path.join(__a , model_name.replace("-" , "_" ) + ".ckpt" ) ) def UpperCamelCase_ ( __a=None ) -> int: a__ : Dict = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__a , required=__a , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__a , default=__a , required=__a , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__a , required=__a , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__a , required=__a , help="Directory in which to save tensorflow model" ) a__ : Optional[Any] = parser.parse_args(__a ) a__ : Tuple = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__a , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
37
0
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase = logging.get_logger() @dataclass class _A : lowercase__: nn.Module lowercase__: List[nn.Module] = field(default_factory=__lowercase ) lowercase__: list = field(default_factory=__lowercase ) def lowercase__ ( self : Optional[Any] , __magic_name__ : Optional[int] , __magic_name__ : Tensor , __magic_name__ : Tensor ) -> List[Any]: """simple docstring""" __snake_case : Tuple = len(list(m.modules() ) ) == 1 or isinstance(__magic_name__ , nn.Convad ) or isinstance(__magic_name__ , nn.BatchNormad ) if has_not_submodules: self.traced.append(__magic_name__ ) def __call__( self : Optional[int] , __magic_name__ : Tensor ) -> Any: """simple docstring""" for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(__magic_name__ ) [x.remove() for x in self.handles] return self @property def lowercase__ ( self : List[Any] ) -> str: """simple docstring""" return list(filter(lambda __magic_name__ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class _A : lowercase__: nn.Module lowercase__: nn.Module lowercase__: int = 1 lowercase__: List = field(default_factory=__lowercase ) lowercase__: List = field(default_factory=__lowercase ) lowercase__: bool = True def __call__( self : Dict , __magic_name__ : Tensor ) -> List[str]: """simple docstring""" __snake_case : Any = Tracker(self.dest )(__magic_name__ ).parametrized __snake_case : Dict = Tracker(self.src )(__magic_name__ ).parametrized __snake_case : List[str] = list(filter(lambda __magic_name__ : type(__magic_name__ ) not in self.src_skip , __magic_name__ ) ) __snake_case : List[str] = list(filter(lambda __magic_name__ : type(__magic_name__ ) not in self.dest_skip , __magic_name__ ) ) if len(__magic_name__ ) != len(__magic_name__ ) and self.raise_if_mismatch: raise Exception( f'''Numbers of operations are different. Source module has {len(__magic_name__ )} operations while''' f''' destination module has {len(__magic_name__ )}.''' ) for dest_m, src_m in zip(__magic_name__ , __magic_name__ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f'''Transfered from={src_m} to={dest_m}''' ) class _A ( nn.Module ): def __init__( self : Dict , __magic_name__ : nn.Module ) -> Any: """simple docstring""" super().__init__() __snake_case : List[Tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("""conv1""", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("""block""" ), f'''Unexpected layer name {k}''' __snake_case : Optional[int] = len(__magic_name__ ) + 1 feature_blocks.append((f'''res{block_index}''', v) ) __snake_case : str = nn.ModuleDict(__magic_name__ ) def lowercase__ ( self : int , __magic_name__ : Tensor ) -> Tuple: """simple docstring""" return get_trunk_forward_outputs( __magic_name__ , out_feat_keys=__magic_name__ , feature_blocks=self._feature_blocks , ) class _A ( __lowercase ): def lowercase__ ( self : Any , __magic_name__ : str ) -> str: """simple docstring""" __snake_case : List[Any] = x.split("""-""" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self : int , __magic_name__ : str ) -> Callable[[], Tuple[nn.Module, Dict]]: """simple docstring""" if x not in self: __snake_case : Tuple = self.convert_name_to_timm(__magic_name__ ) __snake_case : List[str] = partial(lambda: (timm.create_model(__magic_name__ , pretrained=__magic_name__ ).eval(), None) ) else: __snake_case : Tuple = super().__getitem__(__magic_name__ ) return val class _A ( __lowercase ): def __getitem__( self : Optional[Any] , __magic_name__ : str ) -> Callable[[], nn.Module]: """simple docstring""" if "seer" in x and "in1k" not in x: __snake_case : Any = RegNetModel else: __snake_case : Tuple = RegNetForImageClassification return val def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" for from_key, to_key in keys: __snake_case : int = from_state_dict[from_key].clone() print(F'''Copied key={from_key} to={to_key}''' ) return to_state_dict def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = True , ) -> Union[str, Any]: """simple docstring""" print(F'''Converting {name}...''' ) with torch.no_grad(): __snake_case , __snake_case : str = from_model_func() __snake_case : Optional[Any] = our_model_func(_lowerCamelCase ).eval() __snake_case : Dict = ModuleTransfer(src=_lowerCamelCase , dest=_lowerCamelCase , raise_if_mismatch=_lowerCamelCase ) __snake_case : Union[str, Any] = torch.randn((1, 3, 224, 224) ) module_transfer(_lowerCamelCase ) if from_state_dict is not None: __snake_case : Optional[Any] = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: __snake_case : Any = [("""0.clf.0.weight""", """classifier.1.weight"""), ("""0.clf.0.bias""", """classifier.1.bias""")] __snake_case : List[str] = manually_copy_vissl_head(_lowerCamelCase , our_model.state_dict() , _lowerCamelCase ) our_model.load_state_dict(_lowerCamelCase ) __snake_case : str = our_model(_lowerCamelCase , output_hidden_states=_lowerCamelCase ) __snake_case : Any = ( our_outputs.logits if isinstance(_lowerCamelCase , _lowerCamelCase ) else our_outputs.last_hidden_state ) __snake_case : List[str] = from_model(_lowerCamelCase ) __snake_case : List[Any] = from_output[-1] if type(_lowerCamelCase ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: __snake_case : Union[str, Any] = our_outputs.hidden_states[-1] assert torch.allclose(_lowerCamelCase , _lowerCamelCase ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add model""" , use_temp_dir=_lowerCamelCase , ) __snake_case : Optional[int] = 224 if """seer""" not in name else 384 # we can use the convnext one __snake_case : List[Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" , size=_lowerCamelCase ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add image processor""" , use_temp_dir=_lowerCamelCase , ) print(F'''Pushed {name}''' ) def _a ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = True ) -> List[str]: """simple docstring""" __snake_case : int = """imagenet-1k-id2label.json""" __snake_case : int = 1000 __snake_case : Any = (1, num_labels) __snake_case : Union[str, Any] = """huggingface/label-files""" __snake_case : List[str] = num_labels __snake_case : int = json.load(open(cached_download(hf_hub_url(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) ) , """r""" ) ) __snake_case : Optional[int] = {int(_lowerCamelCase ): v for k, v in idalabel.items()} __snake_case : Optional[Any] = idalabel __snake_case : Any = {v: k for k, v in idalabel.items()} __snake_case : int = partial(_lowerCamelCase , num_labels=_lowerCamelCase , idalabel=_lowerCamelCase , labelaid=_lowerCamelCase ) __snake_case : Optional[Any] = { """regnet-x-002""": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 , layer_type="""x""" ), """regnet-x-004""": ImageNetPreTrainedConfig( depths=[1, 2, 7, 12] , hidden_sizes=[32, 64, 160, 384] , groups_width=16 , layer_type="""x""" ), """regnet-x-006""": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[48, 96, 240, 528] , groups_width=24 , layer_type="""x""" ), """regnet-x-008""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[64, 128, 288, 672] , groups_width=16 , layer_type="""x""" ), """regnet-x-016""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 2] , hidden_sizes=[72, 168, 408, 912] , groups_width=24 , layer_type="""x""" ), """regnet-x-032""": ImageNetPreTrainedConfig( depths=[2, 6, 15, 2] , hidden_sizes=[96, 192, 432, 1008] , groups_width=48 , layer_type="""x""" ), """regnet-x-040""": ImageNetPreTrainedConfig( depths=[2, 5, 14, 2] , hidden_sizes=[80, 240, 560, 1360] , groups_width=40 , layer_type="""x""" ), """regnet-x-064""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 392, 784, 1624] , groups_width=56 , layer_type="""x""" ), """regnet-x-080""": ImageNetPreTrainedConfig( depths=[2, 5, 15, 1] , hidden_sizes=[80, 240, 720, 1920] , groups_width=120 , layer_type="""x""" ), """regnet-x-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 , layer_type="""x""" ), """regnet-x-160""": ImageNetPreTrainedConfig( depths=[2, 6, 13, 1] , hidden_sizes=[256, 512, 896, 2048] , groups_width=128 , layer_type="""x""" ), """regnet-x-320""": ImageNetPreTrainedConfig( depths=[2, 7, 13, 1] , hidden_sizes=[336, 672, 1344, 2520] , groups_width=168 , layer_type="""x""" ), # y variant """regnet-y-002""": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 ), """regnet-y-004""": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[48, 104, 208, 440] , groups_width=8 ), """regnet-y-006""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[48, 112, 256, 608] , groups_width=16 ), """regnet-y-008""": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[64, 128, 320, 768] , groups_width=16 ), """regnet-y-016""": ImageNetPreTrainedConfig( depths=[2, 6, 17, 2] , hidden_sizes=[48, 120, 336, 888] , groups_width=24 ), """regnet-y-032""": ImageNetPreTrainedConfig( depths=[2, 5, 13, 1] , hidden_sizes=[72, 216, 576, 1512] , groups_width=24 ), """regnet-y-040""": ImageNetPreTrainedConfig( depths=[2, 6, 12, 2] , hidden_sizes=[128, 192, 512, 1088] , groups_width=64 ), """regnet-y-064""": ImageNetPreTrainedConfig( depths=[2, 7, 14, 2] , hidden_sizes=[144, 288, 576, 1296] , groups_width=72 ), """regnet-y-080""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 448, 896, 2016] , groups_width=56 ), """regnet-y-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 ), """regnet-y-160""": ImageNetPreTrainedConfig( depths=[2, 4, 11, 1] , hidden_sizes=[224, 448, 1232, 3024] , groups_width=112 ), """regnet-y-320""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 """regnet-y-320-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer""": RegNetConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer""": RegNetConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), # finetuned on imagenet """regnet-y-320-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer-in1k""": ImageNetPreTrainedConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), } __snake_case : List[str] = NameToOurModelFuncMap() __snake_case : List[str] = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(_lowerCamelCase , _lowerCamelCase ) -> Tuple[nn.Module, Dict]: __snake_case : List[str] = torch.hub.load_state_dict_from_url(_lowerCamelCase , model_dir=str(_lowerCamelCase ) , map_location="""cpu""" ) __snake_case : Optional[Any] = model_func() # check if we have a head, if yes add it __snake_case : str = files["""classy_state_dict"""]["""base_model"""]["""model"""] __snake_case : Any = model_state_dict["""trunk"""] model.load_state_dict(_lowerCamelCase ) return model.eval(), model_state_dict["heads"] # pretrained __snake_case : List[str] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __snake_case : List[str] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __snake_case : Tuple = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) __snake_case : str = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=6_20.83 , w_m=2.52 ) ) ) , ) # IN1K finetuned __snake_case : Dict = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __snake_case : List[Any] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __snake_case : Union[str, Any] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) __snake_case : str = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=6_20.83 , w_m=2.52 ) ) ) , ) if model_name: convert_weight_and_push( _lowerCamelCase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , _lowerCamelCase , _lowerCamelCase , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( _lowerCamelCase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ) return config, expected_shape if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default=None, type=str, help=( "The name of the model you wish to convert, it must be one of the supported regnet* architecture," " currently: regnetx-*, regnety-*. If `None`, all of them will the converted." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=Path, required=True, help="Path to the output PyTorch model directory.", ) parser.add_argument( "--push_to_hub", default=True, type=bool, required=False, help="If True, push model and image processor to the hub.", ) __UpperCamelCase = parser.parse_args() __UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
26
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=2 , lowerCamelCase__ : Any=24 , lowerCamelCase__ : Optional[Any]=16 , lowerCamelCase__ : int=True , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[Any]=32 , lowerCamelCase__ : List[str]=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Optional[Any]=37 , lowerCamelCase__ : Any="gelu" , lowerCamelCase__ : Union[str, Any]=0.1 , lowerCamelCase__ : Optional[int]=0.1 , lowerCamelCase__ : str=10 , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : str=None , lowerCamelCase__ : List[str]=2 , lowerCamelCase__ : Optional[Any]=2 , ): a__ : str = parent a__ : Any = batch_size a__ : Dict = patch_size a__ : List[Any] = max_length a__ : str = num_mel_bins a__ : Optional[Any] = is_training a__ : Optional[int] = use_labels a__ : List[Any] = hidden_size a__ : str = num_hidden_layers a__ : Any = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : List[str] = hidden_act a__ : str = hidden_dropout_prob a__ : Tuple = attention_probs_dropout_prob a__ : List[Any] = type_sequence_label_size a__ : Any = initializer_range a__ : str = scope a__ : List[str] = frequency_stride a__ : Union[str, Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) a__ : List[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 a__ : List[str] = (self.max_length - self.patch_size) // self.time_stride + 1 a__ : Tuple = frequency_out_dimension * time_out_dimension a__ : List[str] = num_patches + 2 def _UpperCamelCase( self : List[str] ): a__ : Any = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) a__ : List[Any] = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : List[str] = self.get_config() return config, input_values, labels def _UpperCamelCase( self : Optional[int] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] ): a__ : List[Any] = ASTModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Dict = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : str ): a__ : Dict = self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ) : Optional[int] = config_and_inputs a__ : List[Any] = {"input_values": input_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) _lowercase = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def _UpperCamelCase( self : str ): a__ : str = ASTModelTester(self ) a__ : Any = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[str] ): self.config_tester.run_common_tests() @unittest.skip(reason="AST does not use inputs_embeds" ) def _UpperCamelCase( self : List[str] ): pass def _UpperCamelCase( self : Optional[int] ): a__, a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Any = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : Tuple ): a__, a__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Dict = model_class(lowerCamelCase__ ) a__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Optional[Any] = ["input_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Union[str, Any] = ASTModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : Optional[int] = hf_hub_download( repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset" ) a__, a__ : List[str] = torchaudio.load(__a ) return audio, sampling_rate @require_torch @require_torchaudio class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _UpperCamelCase( self : List[str] ): return ( ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ) if is_torchaudio_available() else None ) @slow def _UpperCamelCase( self : Optional[int] ): a__ : int = self.default_feature_extractor a__ : Optional[Any] = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ).to(lowerCamelCase__ ) a__ : Any = self.default_feature_extractor a__, a__ : Dict = prepare_audio() a__ : str = audio.squeeze().numpy() a__ : Any = feature_extractor(lowerCamelCase__ , sampling_rate=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(**lowerCamelCase__ ) # verify the logits a__ : Union[str, Any] = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) a__ : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) )
37
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __A : List[str] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ["NllbTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["NllbTokenizerFast"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __A : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
27
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast 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 UpperCamelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = XGLMTokenizer _lowercase = XGLMTokenizerFast _lowercase = True _lowercase = True def _UpperCamelCase( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase( self : List[Any] ): a__ : int = "<pad>" a__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(lowerCamelCase__ ) , 1_008 ) def _UpperCamelCase( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def _UpperCamelCase( self : Optional[int] ): a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) a__ : List[str] = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowerCamelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) a__ : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a__ : List[str] = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) a__ : Dict = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _UpperCamelCase( self : Dict ): return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def _UpperCamelCase( self : Union[str, Any] ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCamelCase__ , f.name ) a__ : Any = XGLMTokenizer(f.name , keep_accents=lowerCamelCase__ ) a__ : List[str] = pickle.dumps(lowerCamelCase__ ) pickle.loads(lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): if not self.test_rust_tokenizer: return a__ : Any = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : Tuple = "I was born in 92000, and this is falsé." a__ : List[str] = tokenizer.tokenize(lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : Optional[int] = tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = self.get_rust_tokenizer() a__ : Tuple = tokenizer.encode(lowerCamelCase__ ) a__ : Optional[Any] = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = "Hello World!" a__ : List[str] = [2, 31_227, 4_447, 35] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Union[str, Any] ): a__ : Optional[int] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off a__ : Union[str, Any] = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : List[Any] ): # fmt: off a__ : Optional[int] = { "input_ids": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], "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, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name="facebook/xglm-564M" , padding=lowerCamelCase__ , )
37
0
'''simple docstring''' from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowercase__( __UpperCamelCase: Namespace ): """simple docstring""" return ConvertCommand( args.model_type ,args.tf_checkpoint ,args.pytorch_dump_output ,args.config ,args.finetuning_task_name ) UpperCamelCase_ = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def UpperCamelCase_ ( A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = parser.add_parser( 'convert', help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.', ) train_parser.add_argument('--model_type', type=A, required=A, help='Model\'s type.' ) train_parser.add_argument( '--tf_checkpoint', type=A, required=A, help='TensorFlow checkpoint path or folder.' ) train_parser.add_argument( '--pytorch_dump_output', type=A, required=A, help='Path to the PyTorch saved model output.' ) train_parser.add_argument('--config', type=A, default='', help='Configuration file path or folder.' ) train_parser.add_argument( '--finetuning_task_name', type=A, default=A, help='Optional fine-tuning task name if the TF model was a finetuned model.', ) train_parser.set_defaults(func=A ) def __init__( self, A, A, A, A, A, *A, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger('transformers-cli/converting' ) self._logger.info(F"Loading model {model_type}" ) SCREAMING_SNAKE_CASE : Any = model_type SCREAMING_SNAKE_CASE : Tuple = tf_checkpoint SCREAMING_SNAKE_CASE : Optional[Any] = pytorch_dump_output SCREAMING_SNAKE_CASE : Optional[int] = config SCREAMING_SNAKE_CASE : Optional[Any] = finetuning_task_name def UpperCamelCase_ ( self ): '''simple docstring''' if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(A ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) if "ckpt" in self._tf_checkpoint.lower(): SCREAMING_SNAKE_CASE : Dict = self._tf_checkpoint SCREAMING_SNAKE_CASE : List[str] = '' else: SCREAMING_SNAKE_CASE : List[Any] = self._tf_checkpoint SCREAMING_SNAKE_CASE : int = '' convert_transfo_xl_checkpoint_to_pytorch( A, self._config, self._pytorch_dump_output, A ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(A ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint, self._config, self._pytorch_dump_output, self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint, self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint, self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint, self._config, self._pytorch_dump_output ) else: raise ValueError( '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]' )
28
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCamelCase_ ( ) -> int: a__ : int = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg" a__ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ).convert("RGB" ) return image def UpperCamelCase_ ( __a ) -> Optional[Any]: a__ : Any = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.embeddings.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.embeddings.layernorm.bias") ) # fmt: on return rename_keys def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Union[str, Any] = dct.pop(__a ) a__ : List[str] = val def UpperCamelCase_ ( __a , __a ) -> Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__ : Any = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) a__ : Tuple = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict a__ : str = torch.cat((q_bias, torch.zeros_like(__a , requires_grad=__a ), v_bias) ) a__ : int = qkv_bias def UpperCamelCase_ ( __a ) -> Dict: a__ : Tuple = 364 if "coco" in model_name else 224 a__ : int = InstructBlipVisionConfig(image_size=__a ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: a__ : Tuple = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__ : Dict = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: a__ : List[Any] = LlamaConfig.from_pretrained("decapoda-research/llama-7b-hf" , vocab_size=32_001 ).to_dict() elif "vicuna-13b" in model_name: a__ : Optional[int] = LlamaConfig.from_pretrained("decapoda-research/llama-13b-hf" , vocab_size=32_001 ).to_dict() else: raise ValueError("Model name not supported" ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 a__ : Optional[Any] = InstructBlipQFormerConfig(vocab_size=30_523 ).to_dict() a__ : Any = InstructBlipConfig(vision_config=__a , text_config=__a , qformer_config=__a ) return config, image_size @torch.no_grad() def UpperCamelCase_ ( __a , __a=None , __a=False ) -> int: a__ : Tuple = AutoTokenizer.from_pretrained("bert-base-uncased" , truncation_side="left" ) qformer_tokenizer.add_special_tokens({"bos_token": "[DEC]"} ) if "t5" in model_name: a__ : List[Any] = TaTokenizerFast.from_pretrained("google/flan-t5-xl" , truncation_side="left" ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) a__ : Union[str, Any] = LlamaTokenizerFast.from_pretrained( "huggyllama/llama-7b" , truncation_side="left" , bos_token="</s>" , unk_token="</s>" ) tokenizer.add_special_tokens({"pad_token": "[PAD]"} ) a__, a__ : List[str] = get_blipa_config(__a ) a__ : Any = InstructBlipForConditionalGeneration(__a ).eval() a__ : Dict = { "instructblip-vicuna-7b": ("blip2_vicuna_instruct", "vicuna7b"), "instructblip-vicuna-13b": ("blip2_vicuna_instruct", "vicuna13b"), "instructblip-flan-t5-xl": ("blip2_t5_instruct", "flant5xl"), "instructblip-flan-t5-xxl": ("blip2_t5_instruct", "flant5xxl"), } a__, a__ : Dict = model_name_to_original[model_name] # load original model print("Loading original model..." ) a__ : Optional[Any] = "cuda:1" if torch.cuda.is_available() else "cpu" a__ : List[Any] = "cuda:2" if torch.cuda.is_available() else "cpu" a__, a__, a__ : Tuple = load_model_and_preprocess( name=__a , model_type=__a , is_eval=__a , device=__a ) original_model.eval() print("Done!" ) # update state dict keys a__ : Dict = original_model.state_dict() a__ : Optional[int] = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a , __a , __a ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__ : Optional[int] = state_dict.pop(__a ) if key.startswith("Qformer.bert" ): a__ : List[Any] = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: a__ : Any = key.replace("self" , "attention" ) if "llm_proj" in key: a__ : Dict = key.replace("llm_proj" , "language_projection" ) if "t5_proj" in key: a__ : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("llm_model" ): a__ : List[str] = key.replace("llm_model" , "language_model" ) if key.startswith("t5" ): a__ : str = key.replace("t5" , "language" ) a__ : Dict = val # read in qv biases read_in_q_v_bias(__a , __a ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(__a , strict=__a ) a__ : Union[str, Any] = load_demo_image() a__ : int = "What is unusual about this image?" # create processor a__ : Any = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__a , image_std=__a ) a__ : Tuple = InstructBlipProcessor( image_processor=__a , tokenizer=__a , qformer_tokenizer=__a , ) a__ : Tuple = processor(images=__a , text=__a , return_tensors="pt" ).to(__a ) # make sure processor creates exact same pixel values a__ : Optional[int] = vis_processors["eval"](__a ).unsqueeze(0 ).to(__a ) a__ : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , __a ) original_model.to(__a ) hf_model.to(__a ) with torch.no_grad(): if "vicuna" in model_name: a__ : str = original_model({"image": original_pixel_values, "text_input": [prompt]} ).logits a__ : List[str] = hf_model(**__a ).logits else: a__ : List[Any] = original_model( {"image": original_pixel_values, "text_input": [prompt], "text_output": ["\n"]} ).logits a__ : str = tokenizer("\n" , return_tensors="pt" ).input_ids.to(__a ) a__ : Dict = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) a__ : Any = hf_model(**__a , labels=__a ).logits print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape a__ : Tuple = 1e-4 if "vicuna" in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , __a , atol=__a ) print("Looks ok!" ) print("Generating with original model..." ) a__ : Tuple = original_model.generate({"image": original_pixel_values, "prompt": prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print("Generating with HF model..." ) a__ : int = hf_model.generate( **__a , do_sample=__a , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? a__ : int = 2 print("Original generation:" , __a ) a__ : str = processor.batch_decode(__a , skip_special_tokens=__a ) a__ : str = [text.strip() for text in output_text] print("HF generation:" , __a ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() UpperCamelCase : Optional[int] = [ """instructblip-vicuna-7b""", """instructblip-vicuna-13b""", """instructblip-flan-t5-xl""", """instructblip-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""instructblip-flan-t5-xl""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase : Dict = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
37
0
"""simple docstring""" import contextlib from multiprocessing import Pool, RLock from tqdm.auto import tqdm from ..utils import experimental, logging A_ = logging.get_logger(__name__) class __lowerCamelCase : a__: Tuple = None @experimental def lowercase ( lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ): if ParallelBackendConfig.backend_name is None: return _map_with_multiprocessing_pool( lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ) return _map_with_joblib(lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ): lowerCamelCase_ = num_proc if num_proc <= len(lowerCAmelCase__ ) else len(lowerCAmelCase__ ) lowerCamelCase_ = [] # We organize the splits ourselve (contiguous splits) for index in range(lowerCAmelCase__ ): lowerCamelCase_ = len(lowerCAmelCase__ ) // num_proc lowerCamelCase_ = len(lowerCAmelCase__ ) % num_proc lowerCamelCase_ = div * index + min(lowerCAmelCase__ ,lowerCAmelCase__ ) lowerCamelCase_ = start + div + (1 if index < mod else 0) split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc) ) if len(lowerCAmelCase__ ) != sum(len(i[1] ) for i in split_kwds ): raise ValueError( f"Error dividing inputs iterable among processes. " f"Total number of objects {len(lowerCAmelCase__ )}, " f"length: {sum(len(i[1] ) for i in split_kwds )}" ) logger.info( f"Spawning {num_proc} processes for {len(lowerCAmelCase__ )} objects in slices of {[len(i[1] ) for i in split_kwds]}" ) lowerCamelCase_ , lowerCamelCase_ = None, None if not disable_tqdm: lowerCamelCase_ , lowerCamelCase_ = (RLock(),), tqdm.set_lock with Pool(lowerCAmelCase__ ,initargs=lowerCAmelCase__ ,initializer=lowerCAmelCase__ ) as pool: lowerCamelCase_ = pool.map(lowerCAmelCase__ ,lowerCAmelCase__ ) logger.info(f"Finished {num_proc} processes" ) lowerCamelCase_ = [obj for proc_res in mapped for obj in proc_res] logger.info(f"Unpacked {len(lowerCAmelCase__ )} objects" ) return mapped def lowercase ( lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ): # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib, # and it requires monkey-patching joblib internal classes which is subject to change import joblib with joblib.parallel_backend(ParallelBackendConfig.backend_name ,n_jobs=lowerCAmelCase__ ): return joblib.Parallel()( joblib.delayed(lowerCAmelCase__ )((function, obj, types, None, True, None) ) for obj in iterable ) @experimental @contextlib.contextmanager def lowercase ( lowerCAmelCase__ ): lowerCamelCase_ = backend_name if backend_name == "spark": from joblibspark import register_spark register_spark() # TODO: call create_cache_and_write_probe if "download" in steps # TODO: raise NotImplementedError when Dataset.map etc is called try: yield finally: lowerCamelCase_ = None
29
def UpperCamelCase_ ( __a , __a ) -> Tuple: a__ : Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 a__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. a__ : Any = min(__a , __a ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
37
0
import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ImageGPTImageProcessor 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 ,) -> Dict: UpperCAmelCase_ : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 18} UpperCAmelCase_ : List[str] = parent UpperCAmelCase_ : int = batch_size UpperCAmelCase_ : Dict = num_channels UpperCAmelCase_ : Tuple = image_size UpperCAmelCase_ : List[str] = min_resolution UpperCAmelCase_ : Any = max_resolution UpperCAmelCase_ : Optional[int] = do_resize UpperCAmelCase_ : Any = size UpperCAmelCase_ : str = do_normalize def a__ ( self ) -> Optional[int]: return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.88_66_44_36_34_03_32_03, 0.66_18_82_93_69_54_49_83, 0.38_91_74_64_01_78_68_04], [-0.60_42_55_91_46_88_11_04, -0.0_22_95_00_88_60_52_84_69, 0.54_23_79_73_69_00_32_96], ] ), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class __a( _a , unittest.TestCase ): """simple docstring""" lowerCAmelCase = ImageGPTImageProcessor if is_vision_available() else None def a__ ( self ) -> int: UpperCAmelCase_ : Dict = ImageGPTImageProcessingTester(self ) @property def a__ ( self ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: UpperCAmelCase_ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''clusters''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_resize''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''size''' ) ) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,'''do_normalize''' ) ) def a__ ( self ) -> List[Any]: UpperCAmelCase_ : Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'''height''': 18, '''width''': 18} ) UpperCAmelCase_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'''height''': 42, '''width''': 42} ) def a__ ( self ) -> int: UpperCAmelCase_ : List[Any] = self.image_processing_class(**self.image_processor_dict ) UpperCAmelCase_ : Tuple = json.loads(image_processor.to_json_string() ) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE ,obj[key] ) ) else: self.assertEqual(obj[key] ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> Dict: UpperCAmelCase_ : Dict = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase_ : Any = os.path.join(_SCREAMING_SNAKE_CASE ,'''image_processor.json''' ) image_processor_first.to_json_file(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = self.image_processing_class.from_json_file(_SCREAMING_SNAKE_CASE ).to_dict() UpperCAmelCase_ : int = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE ,image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> Any: UpperCAmelCase_ : str = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = self.image_processing_class.from_pretrained(_SCREAMING_SNAKE_CASE ).to_dict() UpperCAmelCase_ : Dict = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE ,image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] ,_SCREAMING_SNAKE_CASE ) @unittest.skip('''ImageGPT requires clusters at initialization''' ) def a__ ( self ) -> str: pass def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : int = load_dataset('''hf-internal-testing/fixtures_image_utils''' , split='''test''' ) UpperCAmelCase_ : Optional[int] = Image.open(dataset[4]['''file'''] ) UpperCAmelCase_ : Any = Image.open(dataset[5]['''file'''] ) UpperCAmelCase_ : Union[str, Any] = [imagea, imagea] return images @require_vision @require_torch class __a( unittest.TestCase ): """simple docstring""" @slow def a__ ( self ) -> List[str]: UpperCAmelCase_ : int = ImageGPTImageProcessor.from_pretrained('''openai/imagegpt-small''' ) UpperCAmelCase_ : Any = prepare_images() # test non-batched UpperCAmelCase_ : Tuple = image_processing(images[0] ,return_tensors='''pt''' ) self.assertIsInstance(encoding.input_ids ,torch.LongTensor ) self.assertEqual(encoding.input_ids.shape ,(1, 1_024) ) UpperCAmelCase_ : str = [306, 191, 191] self.assertEqual(encoding.input_ids[0, :3].tolist() ,_SCREAMING_SNAKE_CASE ) # test batched UpperCAmelCase_ : List[str] = image_processing(_SCREAMING_SNAKE_CASE ,return_tensors='''pt''' ) self.assertIsInstance(encoding.input_ids ,torch.LongTensor ) self.assertEqual(encoding.input_ids.shape ,(2, 1_024) ) UpperCAmelCase_ : List[str] = [303, 13, 13] self.assertEqual(encoding.input_ids[1, -3:].tolist() ,_SCREAMING_SNAKE_CASE )
30
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Optional[Any] = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } UpperCamelCase : Dict = { """allenai/led-base-16384""": 1_6384, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = LEDTokenizer _lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[str]=None , lowerCamelCase__ : Any=None , lowerCamelCase__ : int="replace" , lowerCamelCase__ : Union[str, Any]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Tuple="</s>" , lowerCamelCase__ : Optional[int]="<s>" , lowerCamelCase__ : str="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Any="<mask>" , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : int=True , **lowerCamelCase__ : Union[str, Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : List[str] = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : Optional[Any] = add_prefix_space a__ : List[str] = pre_tok_class(**lowerCamelCase__ ) a__ : Optional[int] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ : Any = "post_processor" a__ : str = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Optional[Any] = tuple(state["sep"] ) if "cls" in state: a__ : Optional[Any] = tuple(state["cls"] ) a__ : Optional[int] = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Dict = add_prefix_space a__ : int = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : List[Any] = trim_offsets a__ : List[str] = True if changes_to_apply: a__ : int = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : int = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Union[str, Any] ): a__ : Any = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : Union[str, Any] = value def _UpperCamelCase( self : Any , *lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : List[str] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Any , *lowerCamelCase__ : Dict , **lowerCamelCase__ : Optional[Any] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : List[str] = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[Any]=None ): a__ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : List[str] = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0] def _UpperCamelCase( self : Dict , lowerCamelCase__ : Union[Dict[str, EncodedInput], BatchEncoding] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[bool] = None , ): a__ : str = super()._pad( encoded_inputs=lowerCamelCase__ , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) # Load from model defaults if return_attention_mask is None: a__ : Optional[int] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ : Tuple = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ : Dict = len(encoded_inputs["global_attention_mask"] ) != len(lowerCamelCase__ ) if needs_to_be_padded: a__ : Union[str, Any] = len(lowerCamelCase__ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ : List[Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a__ : Any = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
37
0
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class lowerCamelCase_ : '''simple docstring''' lowercase_ = MBartConfig lowercase_ = {} lowercase_ = "gelu" def __init__( self : List[Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]=13 , _lowerCAmelCase : List[str]=7 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : Optional[Any]=False , _lowerCAmelCase : Dict=99 , _lowerCAmelCase : str=32 , _lowerCAmelCase : int=2 , _lowerCAmelCase : Any=4 , _lowerCAmelCase : List[Any]=37 , _lowerCAmelCase : List[str]=0.1 , _lowerCAmelCase : Union[str, Any]=0.1 , _lowerCAmelCase : int=20 , _lowerCAmelCase : Optional[Any]=2 , _lowerCAmelCase : Optional[Any]=1 , _lowerCAmelCase : Dict=0 , ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = eos_token_id SCREAMING_SNAKE_CASE_ = pad_token_id SCREAMING_SNAKE_CASE_ = bos_token_id def lowerCAmelCase_ ( self : Tuple ): SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE_ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) SCREAMING_SNAKE_CASE_ = prepare_mbart_inputs_dict(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return config, inputs_dict def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Tuple , _lowerCAmelCase : int ): SCREAMING_SNAKE_CASE_ = TFMBartModel(config=_lowerCAmelCase ).get_decoder() SCREAMING_SNAKE_CASE_ = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE_ = input_ids[:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['attention_mask'][:1, :] SCREAMING_SNAKE_CASE_ = inputs_dict['head_mask'] SCREAMING_SNAKE_CASE_ = 1 # first forward pass SCREAMING_SNAKE_CASE_ = model(_lowerCAmelCase , attention_mask=_lowerCAmelCase , head_mask=_lowerCAmelCase , use_cache=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = outputs.to_tuple() SCREAMING_SNAKE_CASE_ = past_key_values[1] def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Any=None , __UpperCAmelCase : Optional[Any]=None , ) -> List[str]: if attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.cast(tf.math.not_equal(__UpperCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () lowercase_ = (TFMBartForConditionalGeneration,) if is_tf_available() else () lowercase_ = ( { "conversational": TFMBartForConditionalGeneration, "feature-extraction": TFMBartModel, "summarization": TFMBartForConditionalGeneration, "text2text-generation": TFMBartForConditionalGeneration, "translation": TFMBartForConditionalGeneration, } if is_tf_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : List[str] , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : str ): if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = TFMBartModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_lowerCAmelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' lowercase_ = [ " UN Chief Says There Is No Military Solution in Syria", ] lowercase_ = [ "Şeful ONU declară că nu există o soluţie militară în Siria", ] lowercase_ = "facebook/mbart-large-en-ro" @cached_property def lowerCAmelCase_ ( self : Tuple ): return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def lowerCAmelCase_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE_ = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def lowerCAmelCase_ ( self : int , **_lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.translate_src_text(**_lowerCAmelCase ) self.assertListEqual(self.expected_text , _lowerCAmelCase ) def lowerCAmelCase_ ( self : str , **_lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer(self.src_text , **_lowerCAmelCase , return_tensors='tf' ) SCREAMING_SNAKE_CASE_ = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode(_lowerCAmelCase , skip_special_tokens=_lowerCAmelCase ) return generated_words @slow def lowerCAmelCase_ ( self : Any ): self._assert_generated_batch_equal_expected()
31
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Any = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Union[str, Any] = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : List[str] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = ['input_ids', 'attention_mask'] _lowercase = RobertaTokenizer def __init__( self : List[str] , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]="replace" , lowerCamelCase__ : List[str]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Any="</s>" , lowerCamelCase__ : Any="<s>" , lowerCamelCase__ : int="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Tuple="<mask>" , lowerCamelCase__ : Any=False , lowerCamelCase__ : Dict=True , **lowerCamelCase__ : Optional[Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Any = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : int = add_prefix_space a__ : Tuple = pre_tok_class(**lowerCamelCase__ ) a__ : str = add_prefix_space a__ : Tuple = "post_processor" a__ : Dict = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Tuple = tuple(state["sep"] ) if "cls" in state: a__ : str = tuple(state["cls"] ) a__ : str = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : str = add_prefix_space a__ : Any = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : int = trim_offsets a__ : Dict = True if changes_to_apply: a__ : Union[str, Any] = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : str = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : Tuple ): a__ : List[Any] = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : List[str] = value def _UpperCamelCase( self : Union[str, Any] , *lowerCamelCase__ : int , **lowerCamelCase__ : int ): a__ : Optional[int] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Tuple , *lowerCamelCase__ : Dict , **lowerCamelCase__ : List[str] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : int = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int]=None ): a__ : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Dict , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : Tuple = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0]
37
0
def A__ ( SCREAMING_SNAKE_CASE_ : int = 10_00 ) -> int: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = 1, 1 _UpperCAmelCase = [] for i in range(1 , n + 1 ): _UpperCAmelCase = prev_numerator + 2 * prev_denominator _UpperCAmelCase = prev_numerator + prev_denominator if len(str(SCREAMING_SNAKE_CASE_ ) ) > len(str(SCREAMING_SNAKE_CASE_ ) ): result.append(SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = numerator _UpperCAmelCase = denominator return len(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": print(f'''{solution() = }''')
32
from statistics import mean, stdev def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : List[str] = min(__a ) a__ : str = max(__a ) # normalize data return [round((x - x_min) / (x_max - x_min) , __a ) for x in data] def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : str = mean(__a ) a__ : List[str] = stdev(__a ) # standardize data return [round((x - mu) / (sigma) , __a ) for x in data]
37
0
def SCREAMING_SNAKE_CASE ( __lowerCAmelCase ) -> str: snake_case__ , snake_case__ = [], [] while len(__lowerCAmelCase ) > 1: snake_case__ , snake_case__ = min(__lowerCAmelCase ), max(__lowerCAmelCase ) start.append(__lowerCAmelCase ) end.append(__lowerCAmelCase ) collection.remove(__lowerCAmelCase ) collection.remove(__lowerCAmelCase ) end.reverse() return start + collection + end if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = input("""Enter numbers separated by a comma:\n""").strip() lowerCamelCase__ : int = [int(item) for item in user_input.split(""",""")] print(*merge_sort(unsorted), sep=""",""")
33
def UpperCamelCase_ ( __a = 50 ) -> int: a__ : Tuple = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
37
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = { '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 snake_case_ ( lowerCamelCase_ ): """simple docstring""" A_ = '''mobilenet_v1''' def __init__( self , lowerCamelCase_=3 , lowerCamelCase_=2_2_4 , lowerCamelCase_=1.0 , lowerCamelCase_=8 , lowerCamelCase_="relu6" , lowerCamelCase_=True , lowerCamelCase_=0.999 , lowerCamelCase_=0.02 , lowerCamelCase_=0.001 , **lowerCamelCase_ , ) -> int: super().__init__(**lowerCamelCase_) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''') UpperCamelCase = num_channels UpperCamelCase = image_size UpperCamelCase = depth_multiplier UpperCamelCase = min_depth UpperCamelCase = hidden_act UpperCamelCase = tf_padding UpperCamelCase = classifier_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps class snake_case_ ( lowerCamelCase_ ): """simple docstring""" A_ = version.parse('''1.11''' ) @property def UpperCAmelCase__ ( self) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})]) @property def UpperCAmelCase__ ( self) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})]) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})]) @property def UpperCAmelCase__ ( self) -> float: return 1e-4
34
class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] ): a__ : str = name a__ : Optional[int] = value a__ : Dict = weight def __repr__( self : Union[str, Any] ): return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def _UpperCamelCase( self : Dict ): return self.value def _UpperCamelCase( self : Optional[Any] ): return self.name def _UpperCamelCase( self : Optional[Any] ): return self.weight def _UpperCamelCase( self : Optional[int] ): return self.value / self.weight def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = [] for i in range(len(__a ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def UpperCamelCase_ ( __a , __a , __a ) -> Union[str, Any]: a__ : List[str] = sorted(__a , key=__a , reverse=__a ) a__ : List[Any] = [] a__, a__ : Union[str, Any] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def UpperCamelCase_ ( ) -> Union[str, Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
37
0
import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy a_ :Optional[Any] = logging.getLogger(__name__) def a ( A__ , A__ , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = False , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = bnb_quantization_config.load_in_abit SCREAMING_SNAKE_CASE__ : List[str] = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( '''You have a version of `bitsandbytes` that is not compatible with 8bit quantization,''' ''' make sure you have the latest version of `bitsandbytes` installed.''' ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( '''You have a version of `bitsandbytes` that is not compatible with 4bit quantization,''' '''make sure you have the latest version of `bitsandbytes` installed.''' ) SCREAMING_SNAKE_CASE__ : int = [] # custom device map if isinstance(A__ , A__ ) and len(device_map.keys() ) > 1: SCREAMING_SNAKE_CASE__ : List[Any] = [key for key, value in device_map.items() if value in ['''disk''', '''cpu''']] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: SCREAMING_SNAKE_CASE__ : Optional[int] = get_keys_to_not_convert(A__ ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(A__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: SCREAMING_SNAKE_CASE__ : List[Any] = [] SCREAMING_SNAKE_CASE__ : List[str] = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(A__ ) # compatibility with peft SCREAMING_SNAKE_CASE__ : Union[str, Any] = load_in_abit SCREAMING_SNAKE_CASE__ : int = load_in_abit SCREAMING_SNAKE_CASE__ : int = get_parameter_device(A__ ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( '''It is not recommended to quantize a loaded model. ''' '''The model should be instantiated under the `init_empty_weights` context manager.''' ) SCREAMING_SNAKE_CASE__ : List[str] = replace_with_bnb_layers(A__ , A__ , modules_to_not_convert=A__ ) # convert param to the right dtype SCREAMING_SNAKE_CASE__ : int = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: SCREAMING_SNAKE_CASE__ : int = name.replace('''.weight''' , '''''' ).replace('''.bias''' , '''''' ) SCREAMING_SNAKE_CASE__ : List[str] = getattr(A__ , A__ , A__ ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(A__ ): param.to(A__ ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError('''No GPU found. A GPU is needed for quantization.''' ) logger.info( f"""The model device type is {model_device.type}. However, cuda is needed for quantization.""" '''We move the model to cuda.''' ) return model elif weights_location is None: raise RuntimeError( f"""`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} """ ) else: with init_empty_weights(): SCREAMING_SNAKE_CASE__ : List[Any] = replace_with_bnb_layers( A__ , A__ , modules_to_not_convert=A__ ) SCREAMING_SNAKE_CASE__ : List[Any] = get_quantized_model_device_map( A__ , A__ , A__ , max_memory=A__ , no_split_module_classes=A__ , ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): SCREAMING_SNAKE_CASE__ : Optional[int] = True SCREAMING_SNAKE_CASE__ : Any = any(x in list(device_map.values() ) for x in ['''cpu''', '''disk'''] ) load_checkpoint_in_model( A__ , A__ , A__ , dtype=bnb_quantization_config.torch_dtype , offload_folder=A__ , offload_state_dict=A__ , keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules , offload_abit_bnb=load_in_abit and offload , ) return dispatch_model(A__ , device_map=A__ , offload_dir=A__ ) def a ( A__ , A__ , A__=None , A__=None , A__=None ) -> Union[str, Any]: '''simple docstring''' if device_map is None: if torch.cuda.is_available(): SCREAMING_SNAKE_CASE__ : Dict = {'''''': torch.cuda.current_device()} else: raise RuntimeError('''No GPU found. A GPU is needed for quantization.''' ) logger.info('''The device_map was not initialized.''' '''Setting device_map to `{\'\':torch.cuda.current_device()}`.''' ) if isinstance(A__ , A__ ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( '''If passing a string for `device_map`, please choose \'auto\', \'balanced\', \'balanced_low_0\' or ''' '''\'sequential\'.''' ) SCREAMING_SNAKE_CASE__ : List[Any] = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) SCREAMING_SNAKE_CASE__ : List[str] = {} SCREAMING_SNAKE_CASE__ : Dict = special_dtypes SCREAMING_SNAKE_CASE__ : Union[str, Any] = no_split_module_classes SCREAMING_SNAKE_CASE__ : Union[str, Any] = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": SCREAMING_SNAKE_CASE__ : int = get_balanced_memory( A__ , low_zero=(device_map == '''balanced_low_0''') , max_memory=A__ , **A__ , ) SCREAMING_SNAKE_CASE__ : str = max_memory SCREAMING_SNAKE_CASE__ : Dict = infer_auto_device_map(A__ , **A__ ) if isinstance(A__ , A__ ): # check if don't have any quantized module on the cpu SCREAMING_SNAKE_CASE__ : Optional[Any] = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules SCREAMING_SNAKE_CASE__ : Optional[Any] = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( ''' Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in `torch_dtype`, you need to pass a custom `device_map` to `load_and_quantize_model`. Check https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk for more details. ''' ) else: logger.info( '''Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit''' ) del device_map_without_some_modules return device_map def a ( A__ , A__ , A__=None , A__=None ) -> str: '''simple docstring''' if modules_to_not_convert is None: SCREAMING_SNAKE_CASE__ : Optional[Any] = [] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : str = _replace_with_bnb_layers( A__ , A__ , A__ , A__ ) if not has_been_replaced: logger.warning( '''You are loading your model in 8bit or 4bit but no linear modules were found in your model.''' ''' this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers.''' ''' Please double check your model architecture, or submit an issue on github if you think this is''' ''' a bug.''' ) return model def a ( A__ , A__ , A__=None , A__=None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = False for name, module in model.named_children(): if current_key_name is None: SCREAMING_SNAKE_CASE__ : List[str] = [] current_key_name.append(A__ ) if isinstance(A__ , nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` SCREAMING_SNAKE_CASE__ : int = '''.'''.join(A__ ) SCREAMING_SNAKE_CASE__ : Dict = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: SCREAMING_SNAKE_CASE__ : Any = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: SCREAMING_SNAKE_CASE__ : Any = bnb.nn.LinearabitLt( module.in_features , module.out_features , module.bias is not None , has_fpaa_weights=A__ , threshold=bnb_quantization_config.llm_inta_threshold , ) elif bnb_quantization_config.load_in_abit: SCREAMING_SNAKE_CASE__ : List[str] = bnb.nn.Linearabit( module.in_features , module.out_features , module.bias is not None , bnb_quantization_config.bnb_abit_compute_dtype , compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant , quant_type=bnb_quantization_config.bnb_abit_quant_type , ) else: raise ValueError('''load_in_8bit and load_in_4bit can\'t be both False''' ) SCREAMING_SNAKE_CASE__ : Dict = module.weight.data if module.bias is not None: SCREAMING_SNAKE_CASE__ : Union[str, Any] = module.bias.data bnb_module.requires_grad_(A__ ) setattr(A__ , A__ , A__ ) SCREAMING_SNAKE_CASE__ : List[Any] = True if len(list(module.children() ) ) > 0: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = _replace_with_bnb_layers( A__ , A__ , A__ , A__ ) SCREAMING_SNAKE_CASE__ : int = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def a ( A__ ) -> str: '''simple docstring''' with init_empty_weights(): SCREAMING_SNAKE_CASE__ : Union[str, Any] = deepcopy(A__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` SCREAMING_SNAKE_CASE__ : Dict = find_tied_parameters(A__ ) # For compatibility with Accelerate < 0.18 if isinstance(A__ , A__ ): SCREAMING_SNAKE_CASE__ : str = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: SCREAMING_SNAKE_CASE__ : List[str] = sum(A__ , [] ) SCREAMING_SNAKE_CASE__ : str = len(A__ ) > 0 # Check if it is a base model SCREAMING_SNAKE_CASE__ : Dict = False if hasattr(A__ , '''base_model_prefix''' ): SCREAMING_SNAKE_CASE__ : Optional[int] = not hasattr(A__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head SCREAMING_SNAKE_CASE__ : List[str] = list(model.named_children() ) SCREAMING_SNAKE_CASE__ : Dict = [list_modules[-1][0]] # add last module together with tied weights SCREAMING_SNAKE_CASE__ : Any = set(A__ ) - set(A__ ) SCREAMING_SNAKE_CASE__ : List[Any] = list(set(A__ ) ) + list(A__ ) # remove ".weight" from the keys SCREAMING_SNAKE_CASE__ : int = ['''.weight''', '''.bias'''] SCREAMING_SNAKE_CASE__ : Union[str, Any] = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: SCREAMING_SNAKE_CASE__ : List[str] = name.replace(A__ , '''''' ) filtered_module_names.append(A__ ) return filtered_module_names def a ( A__ ) -> Optional[Any]: '''simple docstring''' for m in model.modules(): if isinstance(A__ , bnb.nn.Linearabit ): return True return False def a ( A__ ) -> Optional[Any]: '''simple docstring''' return next(parameter.parameters() ).device def a ( A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: '''simple docstring''' if fpaa_statistics is None: set_module_tensor_to_device(A__ , A__ , 0 , dtype=A__ , value=A__ ) SCREAMING_SNAKE_CASE__ : str = param_name SCREAMING_SNAKE_CASE__ : Dict = model if "." in tensor_name: SCREAMING_SNAKE_CASE__ : Optional[Any] = tensor_name.split('''.''' ) for split in splits[:-1]: SCREAMING_SNAKE_CASE__ : Union[str, Any] = getattr(A__ , A__ ) if new_module is None: raise ValueError(f"""{module} has no attribute {split}.""" ) SCREAMING_SNAKE_CASE__ : Tuple = new_module SCREAMING_SNAKE_CASE__ : Tuple = splits[-1] # offload weights SCREAMING_SNAKE_CASE__ : Union[str, Any] = False offload_weight(module._parameters[tensor_name] , A__ , A__ , index=A__ ) if hasattr(module._parameters[tensor_name] , '''SCB''' ): offload_weight( module._parameters[tensor_name].SCB , param_name.replace('''weight''' , '''SCB''' ) , A__ , index=A__ , ) else: offload_weight(A__ , A__ , A__ , index=A__ ) offload_weight(A__ , param_name.replace('''weight''' , '''SCB''' ) , A__ , index=A__ ) set_module_tensor_to_device(A__ , A__ , '''meta''' , dtype=A__ , value=torch.empty(*param.size() ) )
35
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class A__ ( A__ ): """simple docstring""" def __init__( self : Dict , lowerCamelCase__ : Union[str, "sqlalchemy.sql.Selectable"] , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[Features] = None , lowerCamelCase__ : str = None , lowerCamelCase__ : bool = False , **lowerCamelCase__ : Optional[int] , ): super().__init__(features=lowerCamelCase__ , cache_dir=lowerCamelCase__ , keep_in_memory=lowerCamelCase__ , **lowerCamelCase__ ) a__ : str = Sql( cache_dir=lowerCamelCase__ , features=lowerCamelCase__ , sql=lowerCamelCase__ , con=lowerCamelCase__ , **lowerCamelCase__ , ) def _UpperCamelCase( self : Tuple ): a__ : Optional[Any] = None a__ : Dict = None a__ : Union[str, Any] = None a__ : Union[str, Any] = None self.builder.download_and_prepare( download_config=lowerCamelCase__ , download_mode=lowerCamelCase__ , verification_mode=lowerCamelCase__ , base_path=lowerCamelCase__ , ) # Build dataset for splits a__ : List[str] = self.builder.as_dataset( split="train" , verification_mode=lowerCamelCase__ , in_memory=self.keep_in_memory ) return dataset class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : Dataset , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[int] = None , **lowerCamelCase__ : Optional[Any] , ): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) a__ : Any = dataset a__ : str = name a__ : Tuple = con a__ : List[Any] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a__ : Any = num_proc a__ : Tuple = to_sql_kwargs def _UpperCamelCase( self : List[Any] ): a__ : Any = self.to_sql_kwargs.pop("sql" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("con" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("index" , lowerCamelCase__ ) a__ : int = self._write(index=lowerCamelCase__ , **self.to_sql_kwargs ) return written def _UpperCamelCase( self : Any , lowerCamelCase__ : List[str] ): a__, a__, a__ : Union[str, Any] = args a__ : Any = {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs a__ : Tuple = query_table( table=self.dataset.data , key=slice(lowerCamelCase__ , offset + self.batch_size ) , indices=self.dataset._indices , ) a__ : str = batch.to_pandas() a__ : List[Any] = df.to_sql(self.name , self.con , index=lowerCamelCase__ , **lowerCamelCase__ ) return num_rows or len(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] , lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : str = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: a__, a__ : List[str] = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , lowerCamelCase__ , lowerCamelCase__ )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += num_rows return written
37
0
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
36
import math from datetime import datetime, timedelta def UpperCamelCase_ ( __a ) -> datetime: a__ : Union[str, Any] = year % 19 a__ : List[str] = year % 4 a__ : str = year % 7 a__ : Any = math.floor(year / 100 ) a__ : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) a__ : Optional[int] = leap_day_inhibits / 4 a__ : Union[str, Any] = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 a__ : Dict = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 a__ : Any = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon a__ : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 18 ) else: return datetime(__a , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): UpperCamelCase : Tuple = """will be""" if year > datetime.now().year else """was""" print(f"""Easter in {year} {tense} {gauss_easter(year)}""")
37
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) A_ : List[str] = { "configuration_convbert": ["CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvBertConfig", "ConvBertOnnxConfig"], "tokenization_convbert": ["ConvBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[str] = ["ConvBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "ConvBertForMaskedLM", "ConvBertForMultipleChoice", "ConvBertForQuestionAnswering", "ConvBertForSequenceClassification", "ConvBertForTokenClassification", "ConvBertLayer", "ConvBertModel", "ConvBertPreTrainedModel", "load_tf_weights_in_convbert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFConvBertForMaskedLM", "TFConvBertForMultipleChoice", "TFConvBertForQuestionAnswering", "TFConvBertForSequenceClassification", "TFConvBertForTokenClassification", "TFConvBertLayer", "TFConvBertModel", "TFConvBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys A_ : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
38
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def UpperCamelCase_ ( __a ) -> Union[str, Any]: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class A__ ( nn.Module ): """simple docstring""" def __init__( self : List[str] , lowerCamelCase__ : nn.Module , lowerCamelCase__ : int ): super().__init__() a__ : int = module a__ : Any = nn.Sequential( nn.Linear(module.in_features , lowerCamelCase__ , bias=lowerCamelCase__ ) , nn.Linear(lowerCamelCase__ , module.out_features , bias=lowerCamelCase__ ) , ) a__ : Tuple = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=lowerCamelCase__ ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Optional[int] , *lowerCamelCase__ : int , **lowerCamelCase__ : Dict ): return self.module(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) + self.adapter(lowerCamelCase__ ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" _lowercase = 'bigscience/bloom-1b7' # Constant values _lowercase = 2.1_09_65_95_52_69_25_74 _lowercase = 'Hello my name is' _lowercase = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) _lowercase = 1_0 def _UpperCamelCase( self : Dict ): # Models and tokenizer a__ : List[str] = AutoTokenizer.from_pretrained(self.model_name ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Union[str, Any] ): super().setUp() # Models and tokenizer a__ : List[Any] = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map="auto" ) a__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : List[Any] ): a__ : str = self.model_abit.config self.assertTrue(hasattr(lowerCamelCase__ , "quantization_config" ) ) a__ : Optional[Any] = config.to_dict() a__ : int = config.to_diff_dict() a__ : List[str] = config.to_json_string() def _UpperCamelCase( self : int ): from bitsandbytes.nn import Paramsabit a__ : List[Any] = self.model_fpaa.get_memory_footprint() a__ : str = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) a__ : Optional[Any] = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def _UpperCamelCase( self : Tuple ): from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(lowerCamelCase__ , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def _UpperCamelCase( self : str ): a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Tuple = self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[Any] = BitsAndBytesConfig() a__ : Optional[int] = True a__ : int = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , device_map="auto" ) a__ : str = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : int = model_abit_from_config.generate( input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : Dict ): with self.assertRaises(lowerCamelCase__ ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] ): a__ : int = BitsAndBytesConfig() with self.assertRaises(lowerCamelCase__ ): a__ : Dict = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , load_in_abit=lowerCamelCase__ , device_map="auto" , bnb_abit_quant_type="nf4" , ) def _UpperCamelCase( self : int ): with self.assertRaises(lowerCamelCase__ ): # Tries with `str` self.model_abit.to("cpu" ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.to(torch.device("cuda:0" ) ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.float() with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything a__ : int = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Any = self.model_fpaa.to(torch.floataa ) a__ : List[Any] = self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.to("cpu" ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.half() # Check this does not throw an error a__ : Dict = self.model_fpaa.float() def _UpperCamelCase( self : Dict ): a__ : List[str] = AutoModelForSeqaSeqLM.from_pretrained("t5-small" , load_in_abit=lowerCamelCase__ , device_map="auto" ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" @classmethod def _UpperCamelCase( cls : str ): a__ : Dict = "t5-small" a__ : List[Any] = "google/flan-t5-small" # flan-t5 uses dense-act instead of dense-relu-dense a__ : int = AutoTokenizer.from_pretrained(cls.model_name ) a__ : str = "Translate in German: Hello, my dog is cute" def _UpperCamelCase( self : Optional[int] ): gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Optional[int] ): from transformers import TaForConditionalGeneration a__ : List[Any] = TaForConditionalGeneration._keep_in_fpaa_modules a__ : Optional[Any] = None # test with `t5-small` a__ : Any = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : Dict = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Any = model.generate(**lowerCamelCase__ ) a__ : Union[str, Any] = modules def _UpperCamelCase( self : List[Any] ): import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` a__ : List[str] = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) a__ : Union[str, Any] = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : int = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Any = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Optional[int] = model.generate(**lowerCamelCase__ ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : List[str] ): super().setUp() # model_name a__ : Union[str, Any] = "bigscience/bloom-560m" a__ : Union[str, Any] = "t5-small" # Different types of model a__ : int = AutoModel.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Sequence classification model a__ : Dict = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # CausalLM model a__ : str = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Seq2seq model a__ : Dict = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Union[str, Any] ): from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): super().setUp() def _UpperCamelCase( self : int ): del self.pipe gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Tuple ): a__ : int = pipeline( "text-generation" , model=self.model_name , model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass a__ : Tuple = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]["generated_text"] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Tuple ): super().setUp() def _UpperCamelCase( self : List[Any] ): a__ : str = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="balanced" ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model a__ : List[Any] = self.tokenizer(self.input_text , return_tensors="pt" ) # Second real batch a__ : List[Any] = model_parallel.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): a__ : Any = "facebook/opt-350m" super().setUp() def _UpperCamelCase( self : int ): if version.parse(importlib.metadata.version("bitsandbytes" ) ) < version.parse("0.37.0" ): return # Step 1: freeze all parameters a__ : Tuple = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): a__ : Any = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability a__ : Tuple = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(lowerCamelCase__ ) ): a__ : Dict = LoRALayer(module.q_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.k_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch a__ : Dict = self.tokenizer("Test batch " , return_tensors="pt" ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): a__ : Optional[Any] = model.forward(**lowerCamelCase__ ) out.logits.norm().backward() for module in model.modules(): if isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(lowerCamelCase__ , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class A__ ( A__ ): """simple docstring""" _lowercase = 'gpt2-xl' _lowercase = 3.31_91_85_48_54_15_21_87
37
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase_ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Optional[int]=100 , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[int]=30 , lowerCamelCase__ : Union[str, Any]=2 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Tuple=True , lowerCamelCase__ : int=32 , lowerCamelCase__ : Union[str, Any]=4 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Union[str, Any]=37 , lowerCamelCase__ : List[Any]="gelu" , lowerCamelCase__ : List[Any]=0.1 , lowerCamelCase__ : int=0.1 , lowerCamelCase__ : Union[str, Any]=10 , lowerCamelCase__ : str=0.02 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]=[0, 1, 2, 3] , ): a__ : Dict = parent a__ : Dict = 100 a__ : Optional[int] = batch_size a__ : Union[str, Any] = image_size a__ : Any = patch_size a__ : Optional[Any] = num_channels a__ : int = is_training a__ : List[str] = use_labels a__ : Optional[Any] = hidden_size a__ : List[Any] = num_hidden_layers a__ : str = num_attention_heads a__ : str = intermediate_size a__ : int = hidden_act a__ : List[Any] = hidden_dropout_prob a__ : Dict = attention_probs_dropout_prob a__ : Union[str, Any] = type_sequence_label_size a__ : Optional[Any] = initializer_range a__ : List[str] = scope a__ : int = out_indices a__ : List[str] = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a__ : Optional[int] = (image_size // patch_size) ** 2 a__ : Union[str, Any] = num_patches + 1 def _UpperCamelCase( self : int ): a__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a__ : Optional[Any] = None a__ : Tuple = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) a__ : Optional[int] = self.get_config() return config, pixel_values, labels, pixel_labels def _UpperCamelCase( self : Tuple ): return BeitConfig( vocab_size=self.vocab_size , 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=lowerCamelCase__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any ): a__ : str = BeitModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : List[str] , lowerCamelCase__ : Tuple ): a__ : int = BeitForMaskedImageModeling(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _UpperCamelCase( self : str , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Any ): a__ : List[str] = self.type_sequence_label_size a__ : Optional[Any] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a__ : Optional[Any] = 1 a__ : List[str] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a__ : Union[str, Any] = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _UpperCamelCase( self : Any , lowerCamelCase__ : str , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): a__ : int = self.num_labels a__ : List[str] = BeitForSemanticSegmentation(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Tuple = model(lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def _UpperCamelCase( self : Optional[int] ): a__ : Any = self.prepare_config_and_inputs() a__, a__, a__, a__ : Union[str, Any] = config_and_inputs a__ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) _lowercase = ( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Any ): a__ : int = BeitModelTester(self ) a__ : Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def _UpperCamelCase( self : str ): pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _UpperCamelCase( self : Dict ): pass def _UpperCamelCase( self : Optional[Any] ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : List[str] = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : str ): a__, a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : int = model_class(lowerCamelCase__ ) a__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def _UpperCamelCase( self : int ): a__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] ): a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): if not self.model_tester.is_training: return a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : str = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling]: continue a__ : List[str] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.train() a__ : Any = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : Tuple = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : Tuple ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return a__ : List[Any] = False a__ : List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue a__ : Optional[Any] = model_class(lowerCamelCase__ ) model.gradient_checkpointing_enable() model.to(lowerCamelCase__ ) model.train() a__ : Union[str, Any] = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : int = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : List[str] ): a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : Dict = _config_zero_init(lowerCamelCase__ ) for model_class in self.all_model_classes: a__ : str = model_class(config=lowerCamelCase__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _UpperCamelCase( self : Optional[int] ): for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Tuple = BeitModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : List[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 _UpperCamelCase( self : Optional[int] ): return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def _UpperCamelCase( self : str ): a__ : int = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(lowerCamelCase__ ) a__ : Optional[Any] = self.default_image_processor a__ : Dict = prepare_img() a__ : Optional[int] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).pixel_values.to(lowerCamelCase__ ) # prepare bool_masked_pos a__ : Optional[Any] = torch.ones((1, 196) , dtype=torch.bool ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(pixel_values=lowerCamelCase__ , bool_masked_pos=lowerCamelCase__ ) a__ : Tuple = outputs.logits # verify the logits a__ : List[str] = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[int] = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , lowerCamelCase__ , atol=1E-2 ) ) @slow def _UpperCamelCase( self : Dict ): a__ : str = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(lowerCamelCase__ ) a__ : int = self.default_image_processor a__ : List[Any] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Union[str, Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Union[str, Any] = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Tuple = 281 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : Any ): a__ : Dict = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( lowerCamelCase__ ) a__ : str = self.default_image_processor a__ : List[str] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Dict = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Optional[int] = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[Any] = torch.tensor([1.6881, -0.2787, 0.5901] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Optional[Any] = 2_396 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): a__ : Optional[Any] = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : Tuple = model.to(lowerCamelCase__ ) a__ : List[Any] = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Tuple = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : Union[str, Any] = Image.open(ds[0]["file"] ) a__ : List[Any] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Optional[Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Tuple = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: a__ : Dict = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=lowerCamelCase__ , ) else: a__ : Dict = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=lowerCamelCase__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , lowerCamelCase__ , atol=1E-4 ) ) @slow def _UpperCamelCase( self : Tuple ): a__ : str = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : List[Any] = model.to(lowerCamelCase__ ) a__ : int = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Optional[int] = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : str = Image.open(ds[0]["file"] ) a__ : str = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : List[Any] = model(**lowerCamelCase__ ) a__ : Any = outputs.logits.detach().cpu() a__ : List[Any] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ , target_sizes=[(500, 300)] ) a__ : Optional[int] = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ ) a__ : List[str] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ ) a__ : Any = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ )
37
0
import requests __UpperCAmelCase = '''YOUR API KEY''' def UpperCamelCase ( snake_case__ : str , snake_case__ : str = giphy_api_key ) -> list: UpperCamelCase : Optional[int] = '+'.join(query.split() ) UpperCamelCase : List[str] = F"""https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}""" UpperCamelCase : Tuple = requests.get(snake_case__ ).json()['data'] return [gif["url"] for gif in gifs] if __name__ == "__main__": print('''\n'''.join(get_gifs('''space ship''')))
40
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) def UpperCamelCase_ ( __a ) -> Union[str, Any]: a__ : Tuple = R"\w+[.]\d+" a__ : List[Any] = re.findall(__a , __a ) for pat in pats: a__ : Union[str, Any] = key.replace(__a , "_".join(pat.split("." ) ) ) return key def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : List[str] = pt_tuple_key[:-1] + ("scale",) if ( any("norm" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): a__ : Any = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: a__ : Optional[Any] = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: a__ : Union[str, Any] = pt_tuple_key[:-1] + ("embedding",) return renamed_pt_tuple_key, pt_tensor # conv layer a__ : List[str] = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: a__ : str = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer a__ : Tuple = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight": a__ : Tuple = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight a__ : Optional[Any] = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias a__ : Union[str, Any] = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCamelCase_ ( __a , __a , __a=42 ) -> str: # Step 1: Convert pytorch tensor to numpy a__ : Optional[int] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params a__ : Tuple = flax_model.init_weights(PRNGKey(__a ) ) a__ : Optional[Any] = flatten_dict(__a ) a__ : Union[str, Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): a__ : Optional[int] = rename_key(__a ) a__ : Optional[int] = tuple(renamed_pt_key.split("." ) ) # Correctly rename weight parameters a__, a__ : Union[str, Any] = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ''' f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) # also add unexpected weight so that warning is thrown a__ : str = jnp.asarray(__a ) return unflatten_dict(__a )
37
0
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) def _A ( A__ ): """simple docstring""" __lowercase = torch.load(A__ , map_location='''cpu''' ) if "model" in sd.keys(): __lowercase = torch.load(A__ , map_location='''cpu''' )['''model'''] # pop unnecessary weights __lowercase = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(A__ ) __lowercase = { '''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: __lowercase = sd.pop(A__ ) __lowercase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: __lowercase = sd[key] # We split QKV in separate Q,K,V __lowercase = key.replace('''.qkv_proj.''' , '''.q_proj.''' ) __lowercase = key.replace('''.qkv_proj.''' , '''.k_proj.''' ) __lowercase = key.replace('''.qkv_proj.''' , '''.v_proj.''' ) __lowercase = 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 __lowercase , __lowercase , __lowercase = torch.split(A__ , depth // 3 , dim=0 ) __lowercase = q __lowercase = k __lowercase = v del sd[key] return sd @torch.no_grad() def _A ( A__ , A__ , A__=None ): """simple docstring""" __lowercase = load_checkpoint(A__ ) if config is not None: __lowercase = OPTConfig.from_pretrained(A__ ) else: __lowercase = OPTConfig() __lowercase = OPTModel(A__ ).half().eval() model.load_state_dict(A__ ) # Check results Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ = 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.''') lowerCAmelCase__ = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
41
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def UpperCamelCase_ ( ) -> int: a__ : Any = HfArgumentParser(__a ) a__ : Any = parser.parse_args_into_dataclasses()[0] a__ : Optional[int] = TensorFlowBenchmark(args=__a ) try: a__ : Optional[int] = parser.parse_args_into_dataclasses()[0] except ValueError as e: a__ : Tuple = "Arg --no_{0} is no longer used, please use --no-{0} instead." a__ : List[Any] = " ".join(str(__a ).split(" " )[:-1] ) a__ : str = "" a__ : List[Any] = eval(str(__a ).split(" " )[-1] ) a__ : List[str] = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(__a ) if len(__a ) > 0: a__ : Tuple = full_error_msg + begin_error_msg + str(__a ) raise ValueError(__a ) benchmark.run() if __name__ == "__main__": main()
37
0
'''simple docstring''' import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class UpperCAmelCase : '''simple docstring''' @staticmethod def UpperCamelCase( *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> int: '''simple docstring''' pass def _UpperCamelCase ( __UpperCamelCase ) -> str: lowerCamelCase_ = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE_ = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def UpperCamelCase( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: '''simple docstring''' lowerCamelCase_ = DepthEstimationPipeline(model=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def UpperCamelCase( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: '''simple docstring''' lowerCamelCase_ = depth_estimator('./tests/fixtures/tests_samples/COCO/000000039769.png' ) self.assertEqual({'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )} , SCREAMING_SNAKE_CASE_ ) import datasets lowerCamelCase_ = datasets.load_dataset('hf-internal-testing/fixtures_image_utils' , 'image' , split='test' ) lowerCamelCase_ = depth_estimator( [ 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'], ] ) self.assertEqual( [ {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, ] , SCREAMING_SNAKE_CASE_ , ) @require_tf @unittest.skip('Depth estimation is not implemented in TF' ) def UpperCamelCase( self ) -> Optional[int]: '''simple docstring''' pass @slow @require_torch def UpperCamelCase( self ) -> Any: '''simple docstring''' lowerCamelCase_ = 'Intel/dpt-large' lowerCamelCase_ = pipeline('depth-estimation' , model=SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ = depth_estimator('http://images.cocodataset.org/val2017/000000039769.jpg' ) lowerCamelCase_ = hashimage(outputs['depth'] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['predicted_depth'].max().item() ) , 29.304 ) self.assertEqual(nested_simplify(outputs['predicted_depth'].min().item() ) , 2.662 ) @require_torch def UpperCamelCase( self ) -> Dict: '''simple docstring''' self.skipTest('There is not hf-internal-testing tiny model for either GLPN nor DPT' )
42
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip UpperCamelCase : Optional[int] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def UpperCamelCase_ ( __a ) -> Any: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def UpperCamelCase_ ( __a , __a , __a ) -> Any: return max(metric_fn(__a , __a ) for gt in ground_truths ) def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [] if args.gold_data_mode == "qa": a__ : Any = pd.read_csv(__a , sep="\t" , header=__a ) for answer_list in data[1]: a__ : Union[str, Any] = ast.literal_eval(__a ) answers.append(__a ) else: a__ : List[str] = [line.strip() for line in open(__a , "r" ).readlines()] a__ : List[str] = [[reference] for reference in references] a__ : List[str] = 0 for prediction, ground_truths in zip(__a , __a ): total += 1 em += metric_max_over_ground_truths(__a , __a , __a ) fa += metric_max_over_ground_truths(__a , __a , __a ) a__ : Dict = 100.0 * em / total a__ : Optional[Any] = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = args.k a__ : str = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = 0 for hypo, reference in zip(__a , __a ): a__ : Any = set(hypo.split("\t" )[:k] ) a__ : Union[str, Any] = set(reference.split("\t" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k a__ : Union[str, Any] = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: def strip_title(__a ): if title.startswith("\"" ): a__ : Optional[Any] = title[1:] if title.endswith("\"" ): a__ : Union[str, Any] = title[:-1] return title a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a , )["input_ids"].to(args.device ) a__ : Optional[int] = rag_model.rag.question_encoder(__a ) a__ : Union[str, Any] = question_enc_outputs[0] a__ : Optional[int] = rag_model.retriever( __a , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="pt" , ) a__ : List[Any] = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) a__ : int = [] for docs in all_docs: a__ : Optional[int] = [strip_title(__a ) for title in docs["title"]] provenance_strings.append("\t".join(__a ) ) return provenance_strings def UpperCamelCase_ ( __a , __a , __a ) -> Dict: with torch.no_grad(): a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a ) a__ : Any = inputs_dict.input_ids.to(args.device ) a__ : Dict = inputs_dict.attention_mask.to(args.device ) a__ : Optional[int] = rag_model.generate( # rag_model overwrites generate __a , attention_mask=__a , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__a , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) a__ : int = rag_model.retriever.generator_tokenizer.batch_decode(__a , skip_special_tokens=__a ) if args.print_predictions: for q, a in zip(__a , __a ): logger.info("Q: {} - A: {}".format(__a , __a ) ) return answers def UpperCamelCase_ ( ) -> List[str]: a__ : int = argparse.ArgumentParser() parser.add_argument( "--model_type" , choices=["rag_sequence", "rag_token", "bart"] , type=__a , help=( "RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the" " model_name_or_path" ) , ) parser.add_argument( "--index_name" , default=__a , choices=["exact", "compressed", "legacy"] , type=__a , help="RAG model retriever type" , ) parser.add_argument( "--index_path" , default=__a , type=__a , help="Path to the retrieval index" , ) parser.add_argument("--n_docs" , default=5 , type=__a , help="Number of retrieved docs" ) parser.add_argument( "--model_name_or_path" , default=__a , type=__a , required=__a , help="Path to pretrained checkpoints or model identifier from huggingface.co/models" , ) parser.add_argument( "--eval_mode" , choices=["e2e", "retrieval"] , default="e2e" , type=__a , help=( "Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates" " precision@k." ) , ) parser.add_argument("--k" , default=1 , type=__a , help="k for the precision@k calculation" ) parser.add_argument( "--evaluation_set" , default=__a , type=__a , required=__a , help="Path to a file containing evaluation samples" , ) parser.add_argument( "--gold_data_path" , default=__a , type=__a , required=__a , help="Path to a tab-separated file with gold samples" , ) parser.add_argument( "--gold_data_mode" , default="qa" , type=__a , choices=["qa", "ans"] , help=( "Format of the gold data file" "qa - a single line in the following format: question [tab] answer_list" "ans - a single line of the gold file contains the expected answer string" ) , ) parser.add_argument( "--predictions_path" , type=__a , default="predictions.txt" , help="Name of the predictions file, to be stored in the checkpoints directory" , ) parser.add_argument( "--eval_all_checkpoints" , action="store_true" , help="Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number" , ) parser.add_argument( "--eval_batch_size" , default=8 , type=__a , help="Batch size per GPU/CPU for evaluation." , ) parser.add_argument( "--recalculate" , help="Recalculate predictions even if the prediction file exists" , action="store_true" , ) parser.add_argument( "--num_beams" , default=4 , type=__a , help="Number of beams to be used when generating answers" , ) parser.add_argument("--min_length" , default=1 , type=__a , help="Min length of the generated answers" ) parser.add_argument("--max_length" , default=50 , type=__a , help="Max length of the generated answers" ) parser.add_argument( "--print_predictions" , action="store_true" , help="If True, prints predictions while evaluating." , ) parser.add_argument( "--print_docs" , action="store_true" , help="If True, prints docs retried while generating." , ) a__ : int = parser.parse_args() a__ : Dict = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) return args def UpperCamelCase_ ( __a ) -> Optional[int]: a__ : Tuple = {} if args.model_type is None: a__ : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("rag" ): a__ : int = RagTokenForGeneration if args.model_type == "rag_token" else RagSequenceForGeneration a__ : Tuple = args.n_docs if args.index_name is not None: a__ : Any = args.index_name if args.index_path is not None: a__ : int = args.index_path else: a__ : Optional[Any] = BartForConditionalGeneration a__ : Tuple = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("Evaluate the following checkpoints: %s" , __a ) a__ : Any = get_scores if args.eval_mode == "e2e" else get_precision_at_k a__ : Union[str, Any] = evaluate_batch_eae if args.eval_mode == "e2e" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("Calculating metrics based on an existing predictions file: {}".format(args.predictions_path ) ) score_fn(__a , args.predictions_path , args.gold_data_path ) continue logger.info("***** Running evaluation for {} *****".format(__a ) ) logger.info(" Batch size = %d" , args.eval_batch_size ) logger.info(" Predictions will be stored under {}".format(args.predictions_path ) ) if args.model_type.startswith("rag" ): a__ : str = RagRetriever.from_pretrained(__a , **__a ) a__ : Optional[int] = model_class.from_pretrained(__a , retriever=__a , **__a ) model.retriever.init_retrieval() else: a__ : Dict = model_class.from_pretrained(__a , **__a ) model.to(args.device ) with open(args.evaluation_set , "r" ) as eval_file, open(args.predictions_path , "w" ) as preds_file: a__ : List[Any] = [] for line in tqdm(__a ): questions.append(line.strip() ) if len(__a ) == args.eval_batch_size: a__ : Union[str, Any] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) + "\n" ) preds_file.flush() a__ : Any = [] if len(__a ) > 0: a__ : List[str] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) ) preds_file.flush() score_fn(__a , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": UpperCamelCase : List[Any] = get_args() main(args)
37
0
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import PoolFormerConfig, PoolFormerForImageClassification, PoolFormerImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase = logging.get_logger(__name__) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = original_name.split('''.''' )[0] lowercase__ = key.split('''.''' ) lowercase__ = int(key_list[key_list.index(SCREAMING_SNAKE_CASE ) - 2] ) lowercase__ = int(key_list[key_list.index(SCREAMING_SNAKE_CASE ) - 1] ) lowercase__ = orig_block_num - offset lowercase__ = key.replace(f'{orig_block_num}.{layer_num}.{original_name}' , f'block.{new_block_num}.{layer_num}.{new_name}' ) return key def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = OrderedDict() lowercase__ , lowercase__ = 0, 0 for key, value in state_dict.items(): if key.startswith('''network''' ): lowercase__ = key.replace('''network''' , '''poolformer.encoder''' ) if "proj" in key: # Works for the first embedding as well as the internal embedding layers if key.endswith('''bias''' ) and "patch_embed" not in key: patch_emb_offset += 1 lowercase__ = key[: key.find('''proj''' )] lowercase__ = key.replace(SCREAMING_SNAKE_CASE , f'patch_embeddings.{total_embed_found}.' ) lowercase__ = key.replace('''proj''' , '''projection''' ) if key.endswith('''bias''' ): total_embed_found += 1 if "patch_embeddings" in key: lowercase__ = '''poolformer.encoder.''' + key if "mlp.fc1" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''mlp.fc1''' , '''output.conv1''' ) if "mlp.fc2" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''mlp.fc2''' , '''output.conv2''' ) if "norm1" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''norm1''' , '''before_norm''' ) if "norm2" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''norm2''' , '''after_norm''' ) if "layer_scale_1" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''layer_scale_1''' , '''layer_scale_1''' ) if "layer_scale_2" in key: lowercase__ = replace_key_with_offset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''layer_scale_2''' , '''layer_scale_2''' ) if "head" in key: lowercase__ = key.replace('''head''' , '''classifier''' ) lowercase__ = value return new_state_dict def _a ( ): """simple docstring""" lowercase__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowercase__ = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return image @torch.no_grad() def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = PoolFormerConfig() # set attributes based on model_name lowercase__ = '''huggingface/label-files''' lowercase__ = model_name[-3:] lowercase__ = 10_00 lowercase__ = '''imagenet-1k-id2label.json''' lowercase__ = (1, 10_00) # set config attributes lowercase__ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) , '''r''' ) ) lowercase__ = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} if size == "s12": lowercase__ = [2, 2, 6, 2] lowercase__ = [64, 1_28, 3_20, 5_12] lowercase__ = 4.0 lowercase__ = 0.9 elif size == "s24": lowercase__ = [4, 4, 12, 4] lowercase__ = [64, 1_28, 3_20, 5_12] lowercase__ = 4.0 lowercase__ = 0.9 elif size == "s36": lowercase__ = [6, 6, 18, 6] lowercase__ = [64, 1_28, 3_20, 5_12] lowercase__ = 4.0 lowercase__ = 1E-6 lowercase__ = 0.9 elif size == "m36": lowercase__ = [6, 6, 18, 6] lowercase__ = [96, 1_92, 3_84, 7_68] lowercase__ = 4.0 lowercase__ = 1E-6 lowercase__ = 0.95 elif size == "m48": lowercase__ = [8, 8, 24, 8] lowercase__ = [96, 1_92, 3_84, 7_68] lowercase__ = 4.0 lowercase__ = 1E-6 lowercase__ = 0.95 else: raise ValueError(f'Size {size} not supported' ) # load image processor lowercase__ = PoolFormerImageProcessor(crop_pct=SCREAMING_SNAKE_CASE ) # Prepare image lowercase__ = prepare_img() lowercase__ = image_processor(images=SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values logger.info(f'Converting model {model_name}...' ) # load original state dict lowercase__ = torch.load(SCREAMING_SNAKE_CASE , map_location=torch.device('''cpu''' ) ) # rename keys lowercase__ = rename_keys(SCREAMING_SNAKE_CASE ) # create HuggingFace model and load state dict lowercase__ = PoolFormerForImageClassification(SCREAMING_SNAKE_CASE ) model.load_state_dict(SCREAMING_SNAKE_CASE ) model.eval() # Define image processor lowercase__ = PoolFormerImageProcessor(crop_pct=SCREAMING_SNAKE_CASE ) lowercase__ = image_processor(images=prepare_img() , return_tensors='''pt''' ).pixel_values # forward pass lowercase__ = model(SCREAMING_SNAKE_CASE ) lowercase__ = outputs.logits # define expected logit slices for different models if size == "s12": lowercase__ = torch.tensor([-0.3_045, -0.6_758, -0.4_869] ) elif size == "s24": lowercase__ = torch.tensor([0.4_402, -0.1_374, -0.8_045] ) elif size == "s36": lowercase__ = torch.tensor([-0.6_080, -0.5_133, -0.5_898] ) elif size == "m36": lowercase__ = torch.tensor([0.3_952, 0.2_263, -1.2_668] ) elif size == "m48": lowercase__ = torch.tensor([0.1_167, -0.0_656, -0.3_423] ) else: raise ValueError(f'Size {size} not supported' ) # verify logits assert logits.shape == expected_shape assert torch.allclose(logits[0, :3] , SCREAMING_SNAKE_CASE , atol=1E-2 ) # finally, save model and image processor logger.info(f'Saving PyTorch model and image processor to {pytorch_dump_folder_path}...' ) Path(SCREAMING_SNAKE_CASE ).mkdir(exist_ok=SCREAMING_SNAKE_CASE ) model.save_pretrained(SCREAMING_SNAKE_CASE ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', default='poolformer_s12', type=str, help='Name of the model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default=None, type=str, help='Path to the original PyTorch checkpoint (.pth file).' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) lowerCAmelCase = parser.parse_args() convert_poolformer_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path)
43
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a = None , ) -> str: a__ : int = {} if train_file is not None: a__ : int = [train_file] if eval_file is not None: a__ : Union[str, Any] = [eval_file] if test_file is not None: a__ : str = [test_file] a__ : Optional[Any] = datasets.load_dataset("csv" , data_files=__a ) a__ : List[Any] = list(ds[list(files.keys() )[0]].features.keys() ) a__ : str = features_name.pop(__a ) a__ : Dict = list(set(ds[list(files.keys() )[0]][label_name] ) ) a__ : str = {label: i for i, label in enumerate(__a )} a__ : Tuple = tokenizer.model_input_names a__ : List[str] = {} if len(__a ) == 1: for k in files.keys(): a__ : Optional[Any] = ds[k].map( lambda __a : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=__a , max_length=__a , padding="max_length" ) , batched=__a , ) elif len(__a ) == 2: for k in files.keys(): a__ : Dict = ds[k].map( lambda __a : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=__a , max_length=__a , padding="max_length" , ) , batched=__a , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: a__ : str = {k: v for k, v in ex.items() if k in input_names} a__ : str = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: a__ : Tuple = {k: v for k, v in ex.items() if k in input_names} a__ : List[Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: a__ : List[Any] = {k: v for k, v in ex.items() if k in input_names} a__ : Optional[int] = labelaid[ex[label_name]] yield (d, label) a__ : Optional[Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: a__ : Optional[int] = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: a__ : Optional[Any] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: a__ : Tuple = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid UpperCamelCase : Optional[Any] = logging.getLogger(__name__) @dataclass class A__ : """simple docstring""" _lowercase = field(metadata={'help': 'Which column contains the label'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the training file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the development file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the test file'} ) _lowercase = field( default=1_2_8 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowercase = field( default=A__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) @dataclass class A__ : """simple docstring""" _lowercase = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowercase = field(default=A__ , metadata={'help': 'Set this flag to use fast tokenization.'} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. _lowercase = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) def UpperCamelCase_ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. a__ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) a__, a__, a__ : str = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.info( f'''n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, ''' f'''16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a__ : Union[str, Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) a__, a__, a__, a__ : Optional[Any] = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=__a , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) a__ : Optional[int] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(__a ) , labelaid=__a , idalabel={id: label for label, id in labelaid.items()} , finetuning_task="text-classification" , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): a__ : Any = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool(".bin" in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , ) def compute_metrics(__a ) -> Dict: a__ : Union[str, Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer a__ : Dict = TFTrainer( model=__a , args=__a , train_dataset=__a , eval_dataset=__a , compute_metrics=__a , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation a__ : Optional[Any] = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) a__ : Dict = trainer.evaluate() a__ : int = os.path.join(training_args.output_dir , "eval_results.txt" ) with open(__a , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(f''' {key} = {value}''' ) writer.write(f'''{key} = {value}\n''' ) results.update(__a ) return results if __name__ == "__main__": main()
37
0
'''simple docstring''' import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def A_ ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Dict , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : int=None , _lowerCAmelCase : Optional[Any]=None , _lowerCAmelCase : Any=None , ): """simple docstring""" if attention_mask is None: _lowerCamelCase : int = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _lowerCamelCase : str = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _lowerCamelCase : Union[str, Any] = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=_lowerCAmelCase ) if decoder_head_mask is None: _lowerCamelCase : Dict = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=_lowerCAmelCase ) if cross_attn_head_mask is None: _lowerCamelCase : Dict = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=_lowerCAmelCase ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class UpperCAmelCase__ : def __init__( self : List[str],__A : Dict,__A : Tuple=1_3,__A : Optional[Any]=7,__A : Optional[Any]=True,__A : Tuple=False,__A : Optional[int]=9_9,__A : List[Any]=1_6,__A : str=2,__A : Optional[Any]=4,__A : List[Any]=4,__A : Optional[int]="relu",__A : Optional[Any]=0.1,__A : Any=0.1,__A : Optional[Any]=0.0,__A : List[Any]=0.0,__A : List[Any]=2_0,__A : Dict=2,__A : Any=1,__A : Tuple=0,): _lowerCamelCase : Any = parent _lowerCamelCase : Dict = batch_size _lowerCamelCase : int = seq_length _lowerCamelCase : Tuple = is_training _lowerCamelCase : Tuple = use_labels _lowerCamelCase : Optional[int] = vocab_size _lowerCamelCase : Union[str, Any] = hidden_size _lowerCamelCase : Optional[int] = num_hidden_layers _lowerCamelCase : Optional[int] = num_attention_heads _lowerCamelCase : List[str] = intermediate_size _lowerCamelCase : str = hidden_act _lowerCamelCase : List[str] = hidden_dropout_prob _lowerCamelCase : int = attention_probs_dropout_prob _lowerCamelCase : Any = encoder_layerdrop _lowerCamelCase : Union[str, Any] = decoder_layerdrop _lowerCamelCase : str = max_position_embeddings _lowerCamelCase : int = eos_token_id _lowerCamelCase : Optional[Any] = pad_token_id _lowerCamelCase : List[Any] = bos_token_id def lowerCamelCase_ ( self : Union[str, Any] ): _lowerCamelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length],self.vocab_size ) _lowerCamelCase : List[str] = self.eos_token_id # Eos Token _lowerCamelCase : Any = ids_tensor([self.batch_size, self.seq_length],self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _lowerCamelCase : Tuple = input_ids.clamp(self.pad_token_id + 1 ) _lowerCamelCase : Any = decoder_input_ids.clamp(self.pad_token_id + 1 ) _lowerCamelCase : Any = self.get_config() _lowerCamelCase : Tuple = prepare_mam_aaa_inputs_dict(__A,__A,__A ) return config, inputs_dict def lowerCamelCase_ ( self : Union[str, Any] ): return MaMaaaConfig( vocab_size=self.vocab_size,d_model=self.hidden_size,encoder_layers=self.num_hidden_layers,decoder_layers=self.num_hidden_layers,encoder_attention_heads=self.num_attention_heads,decoder_attention_heads=self.num_attention_heads,encoder_ffn_dim=self.intermediate_size,decoder_ffn_dim=self.intermediate_size,dropout=self.hidden_dropout_prob,attention_dropout=self.attention_probs_dropout_prob,encoder_layerdrop=self.encoder_layerdrop,decoder_layerdrop=self.decoder_layerdrop,max_position_embeddings=self.max_position_embeddings,eos_token_id=self.eos_token_id,bos_token_id=self.bos_token_id,pad_token_id=self.pad_token_id,) def lowerCamelCase_ ( self : List[str] ): _lowerCamelCase , _lowerCamelCase : str = self.prepare_config_and_inputs() return config, inputs_dict def lowerCamelCase_ ( self : Optional[int],__A : Optional[int],__A : int ): _lowerCamelCase : Union[str, Any] = MaMaaaModel(config=__A ).get_decoder().to(__A ).eval() _lowerCamelCase : int = inputs_dict["input_ids"] _lowerCamelCase : Union[str, Any] = inputs_dict["attention_mask"] _lowerCamelCase : List[str] = inputs_dict["head_mask"] # first forward pass _lowerCamelCase : List[Any] = model(__A,attention_mask=__A,head_mask=__A,use_cache=__A ) _lowerCamelCase , _lowerCamelCase : int = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids _lowerCamelCase : int = ids_tensor((self.batch_size, 3),config.vocab_size ) _lowerCamelCase : Union[str, Any] = ids_tensor((self.batch_size, 3),2 ) # append to next input_ids and _lowerCamelCase : str = torch.cat([input_ids, next_tokens],dim=-1 ) _lowerCamelCase : List[Any] = torch.cat([attention_mask, next_attn_mask],dim=-1 ) _lowerCamelCase : Tuple = model(__A,attention_mask=__A )["last_hidden_state"] _lowerCamelCase : Any = model(__A,attention_mask=__A,past_key_values=__A )[ "last_hidden_state" ] # select random slice _lowerCamelCase : str = ids_tensor((1,),output_from_past.shape[-1] ).item() _lowerCamelCase : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() _lowerCamelCase : Union[str, Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__A,__A,atol=1e-2 ) ) def lowerCamelCase_ ( self : Any,__A : Union[str, Any],__A : Dict ): _lowerCamelCase : Tuple = MaMaaaModel(config=__A ).to(__A ).eval() _lowerCamelCase : List[str] = model(**__A ) _lowerCamelCase : Dict = outputs.encoder_last_hidden_state _lowerCamelCase : Any = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _lowerCamelCase : Optional[int] = model.get_encoder() encoder.save_pretrained(__A ) _lowerCamelCase : Dict = MaMaaaEncoder.from_pretrained(__A ).to(__A ) _lowerCamelCase : Any = encoder(inputs_dict["input_ids"],attention_mask=inputs_dict["attention_mask"] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) with tempfile.TemporaryDirectory() as tmpdirname: _lowerCamelCase : Tuple = model.get_decoder() decoder.save_pretrained(__A ) _lowerCamelCase : List[str] = MaMaaaDecoder.from_pretrained(__A ).to(__A ) _lowerCamelCase : Tuple = decoder( input_ids=inputs_dict["decoder_input_ids"],attention_mask=inputs_dict["decoder_attention_mask"],encoder_hidden_states=__A,encoder_attention_mask=inputs_dict["attention_mask"],)[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class UpperCAmelCase__ ( A , A , A , unittest.TestCase ): lowerCAmelCase_ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) lowerCAmelCase_ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () lowerCAmelCase_ = ( { 'conversational': MaMaaaForConditionalGeneration, 'feature-extraction': MaMaaaModel, 'summarization': MaMaaaForConditionalGeneration, 'text2text-generation': MaMaaaForConditionalGeneration, 'translation': MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) lowerCAmelCase_ = True lowerCAmelCase_ = True lowerCAmelCase_ = False lowerCAmelCase_ = False def lowerCamelCase_ ( self : str,__A : Any,__A : Dict,__A : Union[str, Any],__A : str,__A : str ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def lowerCamelCase_ ( self : int ): _lowerCamelCase : str = MaMaaaModelTester(self ) _lowerCamelCase : Optional[Any] = ConfigTester(self,config_class=__A ) def lowerCamelCase_ ( self : List[str] ): self.config_tester.run_common_tests() def lowerCamelCase_ ( self : Optional[Any] ): _lowerCamelCase , _lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _lowerCamelCase : Optional[int] = model_class(__A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__A ) _lowerCamelCase , _lowerCamelCase : Optional[int] = model_class.from_pretrained(__A,output_loading_info=__A ) self.assertEqual(info["missing_keys"],[] ) def lowerCamelCase_ ( self : Dict ): _lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*__A ) def lowerCamelCase_ ( self : List[Any] ): _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*__A ) def lowerCamelCase_ ( self : Any ): _lowerCamelCase , _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): _lowerCamelCase : int = model_class(__A ) model.to(__A ) model.eval() _lowerCamelCase : List[str] = copy.deepcopy(self._prepare_for_class(__A,__A ) ) if not self.is_encoder_decoder: _lowerCamelCase : List[str] = inputs["input_ids"] del inputs["input_ids"] else: _lowerCamelCase : Tuple = inputs["input_ids"] _lowerCamelCase : Union[str, Any] = inputs.get("decoder_input_ids",__A ) del inputs["input_ids"] inputs.pop("decoder_input_ids",__A ) _lowerCamelCase : Tuple = model.get_input_embeddings() if not self.is_encoder_decoder: _lowerCamelCase : List[Any] = wte(__A ) else: _lowerCamelCase : List[str] = wte(__A ) _lowerCamelCase : Dict = wte(__A ) with torch.no_grad(): model(**__A )[0] def lowerCamelCase_ ( self : Tuple ): _lowerCamelCase , _lowerCamelCase : int = self.model_tester.prepare_config_and_inputs() _lowerCamelCase : Union[str, Any] = input_dict["input_ids"] _lowerCamelCase : Union[str, Any] = input_ids.ne(1 ).to(__A ) _lowerCamelCase : Any = MaMaaaForConditionalGeneration(__A ).eval().to(__A ) if torch_device == "cuda": model.half() model.generate(__A,attention_mask=__A ) model.generate(num_beams=4,do_sample=__A,early_stopping=__A,num_return_sequences=3 ) def A_ ( _lowerCAmelCase : List[str] ): """simple docstring""" return torch.tensor(_lowerCAmelCase , dtype=torch.long , device=_lowerCAmelCase ) UpperCAmelCase_ : Optional[Any] = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class UpperCAmelCase__ ( unittest.TestCase ): @cached_property def lowerCamelCase_ ( self : Optional[int] ): return MaMaaaTokenizer.from_pretrained("facebook/m2m100_418M" ) def lowerCamelCase_ ( self : List[Any] ): _lowerCamelCase : Any = MaMaaaModel.from_pretrained("facebook/m2m100_418M" ).to(__A ) _lowerCamelCase : Optional[int] = _long_tensor([[1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8, 2]] ) _lowerCamelCase : List[Any] = _long_tensor([[2, 1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8]] ) _lowerCamelCase : List[Any] = prepare_mam_aaa_inputs_dict(model.config,__A,__A ) with torch.no_grad(): _lowerCamelCase : Any = model(**__A )[0] _lowerCamelCase : List[Any] = torch.Size((1, 1_1, 1_0_2_4) ) self.assertEqual(output.shape,__A ) # change to expected output here _lowerCamelCase : Union[str, Any] = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]],device=__A ) self.assertTrue(torch.allclose(output[:, :3, :3],__A,atol=__A ) ) def lowerCamelCase_ ( self : Tuple ): _lowerCamelCase : int = MaMaaaForConditionalGeneration.from_pretrained("facebook/m2m100_418M" ).to(__A ) # change to intended input _lowerCamelCase : Optional[Any] = _long_tensor([[1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8, 2]] ) _lowerCamelCase : List[str] = _long_tensor([[2, 1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8]] ) _lowerCamelCase : List[Any] = prepare_mam_aaa_inputs_dict(model.config,__A,__A ) with torch.no_grad(): _lowerCamelCase : Any = model(**__A )[0] _lowerCamelCase : str = torch.Size((1, 1_1, model.config.vocab_size) ) self.assertEqual(output.shape,__A ) # change to expected output here _lowerCamelCase : str = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]],device=__A ) self.assertTrue(torch.allclose(output[:, :3, :3],__A,atol=__A ) ) def lowerCamelCase_ ( self : Tuple ): _lowerCamelCase : List[str] = MaMaaaForConditionalGeneration.from_pretrained("facebook/m2m100_418M" ).to(__A ) _lowerCamelCase : Any = MaMaaaTokenizer.from_pretrained("facebook/m2m100_418M",src_lang="fr",tgt_lang="en" ) _lowerCamelCase : Union[str, Any] = [ "L'affaire NSA souligne l'absence totale de débat sur le renseignement", "Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.", "Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent" " Fabius convoque l'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de" " l'ampleur de la surveillance américaine sur l'ensemble des communications en France.", ] # The below article tests that we don't add any hypotheses outside of the top n_beams _lowerCamelCase : str = tokenizer(__A,padding=__A,return_tensors="pt" ) _lowerCamelCase : Tuple = model.generate( input_ids=dct["input_ids"].to(__A ),attention_mask=dct["attention_mask"].to(__A ),num_beams=5,forced_bos_token_id=tokenizer.get_lang_id("en" ),) _lowerCamelCase : str = [ "The NSA case highlights the total absence of intelligence debate", "I think there are two levels of response from the French government.", "When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S." " Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all" " communications in France.", ] _lowerCamelCase : str = tokenizer.batch_decode( hypotheses_batch.tolist(),clean_up_tokenization_spaces=__A,skip_special_tokens=__A ) assert generated == expected_en
44
import argparse import collections import json import os import re import string import sys import numpy as np UpperCamelCase : List[str] = re.compile(r"""\b(a|an|the)\b""", re.UNICODE) UpperCamelCase : Union[str, Any] = None def UpperCamelCase_ ( ) -> List[str]: a__ : List[Any] = 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 UpperCamelCase_ ( __a ) -> str: a__ : Optional[Any] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : Dict = bool(qa["answers"]["text"] ) return qid_to_has_ans def UpperCamelCase_ ( __a ) -> List[Any]: def remove_articles(__a ): return ARTICLES_REGEX.sub(" " , __a ) def white_space_fix(__a ): return " ".join(text.split() ) def remove_punc(__a ): a__ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def UpperCamelCase_ ( __a ) -> Dict: if not s: return [] return normalize_answer(__a ).split() def UpperCamelCase_ ( __a , __a ) -> str: return int(normalize_answer(__a ) == normalize_answer(__a ) ) def UpperCamelCase_ ( __a , __a ) -> Dict: a__ : int = get_tokens(__a ) a__ : Optional[Any] = get_tokens(__a ) a__ : Any = collections.Counter(__a ) & collections.Counter(__a ) a__ : Dict = 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 a__ : Tuple = 1.0 * num_same / len(__a ) a__ : str = 1.0 * num_same / len(__a ) a__ : str = (2 * precision * recall) / (precision + recall) return fa def UpperCamelCase_ ( __a , __a ) -> int: a__ : List[str] = {} a__ : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : List[Any] = qa["id"] a__ : Dict = [t for t in qa["answers"]["text"] if normalize_answer(__a )] if not gold_answers: # For unanswerable questions, only correct answer is empty string a__ : Tuple = [""] if qid not in preds: print(f'''Missing prediction for {qid}''' ) continue a__ : Tuple = preds[qid] # Take max over all gold answers a__ : Optional[int] = max(compute_exact(__a , __a ) for a in gold_answers ) a__ : str = max(compute_fa(__a , __a ) for a in gold_answers ) return exact_scores, fa_scores def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: a__ : Optional[Any] = {} for qid, s in scores.items(): a__ : Dict = na_probs[qid] > na_prob_thresh if pred_na: a__ : Dict = float(not qid_to_has_ans[qid] ) else: a__ : Optional[Any] = s return new_scores def UpperCamelCase_ ( __a , __a , __a=None ) -> Tuple: if not qid_list: a__ : Union[str, Any] = len(__a ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: a__ : int = 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 UpperCamelCase_ ( __a , __a , __a ) -> List[str]: for k in new_eval: a__ : Optional[Any] = new_eval[k] def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: 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 UpperCamelCase_ ( __a , __a , __a , __a , __a=None , __a=None ) -> Dict: a__ : Optional[Any] = sorted(__a , key=lambda __a : na_probs[k] ) a__ : Any = 0.0 a__ : Optional[int] = 1.0 a__ : Optional[int] = 0.0 a__ : Any = [1.0] a__ : Tuple = [0.0] a__ : List[str] = 0.0 for i, qid in enumerate(__a ): if qid_to_has_ans[qid]: true_pos += scores[qid] a__ : Any = true_pos / float(i + 1 ) a__ : int = 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 UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> str: if out_image_dir and not os.path.exists(__a ): os.makedirs(__a ) a__ : Optional[int] = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return a__ : Optional[int] = 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" , ) a__ : Optional[Any] = make_precision_recall_eval( __a , __a , __a , __a , out_image=os.path.join(__a , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) a__ : str = {k: float(__a ) for k, v in qid_to_has_ans.items()} a__ : Optional[Any] = 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> str: if not qid_list: return a__ : Optional[Any] = [na_probs[k] for k in qid_list] a__ : 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[Any]: a__ : str = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) a__ : Optional[Any] = num_no_ans a__ : Dict = cur_score a__ : Any = 0.0 a__ : Optional[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]: a__ : Optional[int] = scores[qid] else: if preds[qid]: a__ : str = -1 else: a__ : Union[str, Any] = 0 cur_score += diff if cur_score > best_score: a__ : Any = cur_score a__ : Dict = na_probs[qid] return 100.0 * best_score / len(__a ), best_thresh def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> Any: a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__ : Any = best_exact a__ : Any = exact_thresh a__ : List[Any] = best_fa a__ : Optional[int] = fa_thresh def UpperCamelCase_ ( ) -> Tuple: with open(OPTS.data_file ) as f: a__ : List[Any] = json.load(__a ) a__ : Any = dataset_json["data"] with open(OPTS.pred_file ) as f: a__ : int = json.load(__a ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: a__ : List[str] = json.load(__a ) else: a__ : Optional[int] = {k: 0.0 for k in preds} a__ : Optional[Any] = make_qid_to_has_ans(__a ) # maps qid to True/False a__ : List[Any] = [k for k, v in qid_to_has_ans.items() if v] a__ : Union[str, Any] = [k for k, v in qid_to_has_ans.items() if not v] a__, a__ : str = get_raw_scores(__a , __a ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : Tuple = make_eval_dict(__a , __a ) if has_ans_qids: a__ : str = make_eval_dict(__a , __a , qid_list=__a ) merge_eval(__a , __a , "HasAns" ) if no_ans_qids: a__ : List[Any] = 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__": UpperCamelCase : Any = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("""Agg""") import matplotlib.pyplot as plt main()
37
0
import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) UpperCamelCase = "\\n Text data.\n Second line of data." UpperCamelCase = "file" @pytest.fixture(scope="""session""" ) def A ( lowercase__ : List[str] ) -> Union[str, Any]: UpperCamelCase__ :Optional[int] = tmp_path_factory.mktemp("""data""" ) / (FILE_PATH + """.zstd""") UpperCamelCase__ :Optional[Any] = bytes(lowercase__ , """utf-8""" ) with zstd.open(lowercase__ , """wb""" ) as f: f.write(lowercase__ ) return path @pytest.fixture def A ( lowercase__ : str ) -> int: with open(os.path.join(tmpfs.local_root_dir , lowercase__ ) , """w""" ) as f: f.write(lowercase__ ) return FILE_PATH @pytest.mark.parametrize("""compression_format""" , ["""gzip""", """xz""", """zstd"""] ) def A ( lowercase__ : Optional[Any] , lowercase__ : Dict , lowercase__ : int , lowercase__ : List[str] , lowercase__ : Optional[int] , lowercase__ : Any ) -> Union[str, Any]: UpperCamelCase__ :Optional[int] = {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path} UpperCamelCase__ :List[Any] = input_paths[compression_format] UpperCamelCase__ :Tuple = tmp_path / """cache""" UpperCamelCase__ :Dict = DownloadConfig(cache_dir=lowercase__ , extract_compressed_file=lowercase__ ) UpperCamelCase__ :int = cached_path(lowercase__ , download_config=lowercase__ ) with open(lowercase__ ) as f: UpperCamelCase__ :int = f.read() with open(lowercase__ ) as f: UpperCamelCase__ :Tuple = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize("""default_extracted""" , [True, False] ) @pytest.mark.parametrize("""default_cache_dir""" , [True, False] ) def A ( lowercase__ : Union[str, Any] , lowercase__ : Dict , lowercase__ : Dict , lowercase__ : Any , lowercase__ : List[Any] ) -> List[str]: UpperCamelCase__ :Dict = """custom_cache""" UpperCamelCase__ :Union[str, Any] = """custom_extracted_dir""" UpperCamelCase__ :Optional[int] = tmp_path / """custom_extracted_path""" if default_extracted: UpperCamelCase__ :Union[str, Any] = ("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""") else: monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR""" , lowercase__ ) monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH""" , str(lowercase__ ) ) UpperCamelCase__ :Dict = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) UpperCamelCase__ :Optional[int] = xz_file UpperCamelCase__ :Optional[Any] = ( DownloadConfig(extract_compressed_file=lowercase__ ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir , extract_compressed_file=lowercase__ ) ) UpperCamelCase__ :str = cached_path(lowercase__ , download_config=lowercase__ ) assert Path(lowercase__ ).parent.parts[-2:] == expected def A ( lowercase__ : List[str] ) -> Dict: # absolute path UpperCamelCase__ :Optional[Any] = str(Path(lowercase__ ).resolve() ) assert cached_path(lowercase__ ) == text_file # relative path UpperCamelCase__ :str = str(Path(lowercase__ ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(lowercase__ ) == text_file def A ( lowercase__ : Optional[Any] ) -> Tuple: # absolute path UpperCamelCase__ :Tuple = str(tmp_path.resolve() / """__missing_file__.txt""" ) with pytest.raises(lowercase__ ): cached_path(lowercase__ ) # relative path UpperCamelCase__ :Tuple = """./__missing_file__.txt""" with pytest.raises(lowercase__ ): cached_path(lowercase__ ) def A ( lowercase__ : str ) -> Optional[int]: UpperCamelCase__ :Any = get_from_cache(f"""tmp://{tmpfs_file}""" ) with open(lowercase__ ) as f: UpperCamelCase__ :Tuple = f.read() assert output_file_content == FILE_CONTENT @patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase__ ) def A ( ) -> Tuple: with pytest.raises(lowercase__ ): cached_path("""https://huggingface.co""" ) @patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase__ ) def A ( lowercase__ : str ) -> Optional[int]: UpperCamelCase__ :Optional[Any] = tmp_path_factory.mktemp("""data""" ) / """file.html""" with pytest.raises(lowercase__ ): http_get("""https://huggingface.co""" , temp_file=lowercase__ ) with pytest.raises(lowercase__ ): http_head("""https://huggingface.co""" ) @patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase__ ) def A ( lowercase__ : List[Any] ) -> int: UpperCamelCase__ :List[str] = tmp_path_factory.mktemp("""data""" ) / """file.html""" with pytest.raises(lowercase__ ): ftp_get("""ftp://huggingface.co""" , temp_file=lowercase__ ) with pytest.raises(lowercase__ ): ftp_head("""ftp://huggingface.co""" ) @patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase__ ) def A ( lowercase__ : Optional[int] ) -> List[Any]: UpperCamelCase__ :Optional[Any] = tmp_path_factory.mktemp("""data""" ) / """file.html""" with pytest.raises(lowercase__ ): fsspec_get("""s3://huggingface.co""" , temp_file=lowercase__ ) with pytest.raises(lowercase__ ): fsspec_head("""s3://huggingface.co""" )
45
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = CLIPTokenizer _lowercase = CLIPTokenizerFast _lowercase = True _lowercase = {} _lowercase = False def _UpperCamelCase( self : List[Any] ): super().setUp() # fmt: off a__ : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on a__ : Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a__ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] a__ : Optional[Any] = {"unk_token": "<unk>"} a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def _UpperCamelCase( self : Dict , **lowerCamelCase__ : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] , **lowerCamelCase__ : Optional[int] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : int = "lower newer" a__ : Optional[int] = "lower newer" return input_text, output_text def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a__ : int = "lower newer" a__ : List[str] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] a__ : Union[str, Any] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = tokens + [tokenizer.unk_token] a__ : Union[str, Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) @require_ftfy def _UpperCamelCase( self : Optional[Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : List[str] = self.tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : Any = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : int = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." a__ : Optional[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : Dict = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways a__ : Optional[Any] = "xa\u0303y" + " " + "x\xe3y" a__ : Optional[int] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of space type a__ : str = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: a__ : Any = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of line break type a__ : Union[str, Any] = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: a__ : List[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : str = "hello" # `hello` is a token in the vocabulary of `pretrained_name` a__ : Tuple = f'''{text_of_1_token} {text_of_1_token}''' a__ : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Union[str, Any] = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCamelCase__ ) + 1, len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) a__ : Optional[Any] = f''' {text}''' a__ : str = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Dict = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCamelCase__ ) + 1, 1 + len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) def _UpperCamelCase( self : int ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCamelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _UpperCamelCase( self : int ): super().test_tokenization_python_rust_equals() def _UpperCamelCase( self : str ): # CLIP always lower cases letters pass
37
0
"""simple docstring""" def lowerCamelCase_( _lowerCamelCase = 50 ) -> int: '''simple docstring''' _lowerCamelCase : Optional[int] = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
46
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase : Dict = """<<<<<<< This should probably be modified because it mentions: """ UpperCamelCase : List[Any] = """======= >>>>>>> """ UpperCamelCase : Optional[Any] = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] UpperCamelCase : 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 UpperCamelCase_ ( __a ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class A__ ( A__ ): """simple docstring""" @staticmethod def _UpperCamelCase( lowerCamelCase__ : ArgumentParser ): a__ : List[str] = parser.add_parser( "convert" , help="Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset." , ) train_parser.add_argument( "--tfds_path" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to a TensorFlow Datasets folder to convert or a single tfds file to convert." , ) train_parser.add_argument( "--datasets_directory" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to the HuggingFace Datasets folder." ) train_parser.set_defaults(func=lowerCamelCase__ ) def __init__( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str , *lowerCamelCase__ : Tuple ): a__ : str = get_logger("datasets-cli/converting" ) a__ : Optional[Any] = tfds_path a__ : Optional[int] = datasets_directory def _UpperCamelCase( self : int ): if os.path.isdir(self._tfds_path ): a__ : List[str] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a__ : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("--tfds_path is neither a directory nor a file. Please check path." ) a__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a__ : Tuple = [] a__ : str = [] a__ : List[Any] = {} if os.path.isdir(self._tfds_path ): a__ : List[str] = os.listdir(lowerCamelCase__ ) else: a__ : Union[str, Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'''Looking at file {f_name}''' ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Dict = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) if not os.path.isfile(lowerCamelCase__ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("Skipping file" ) continue with open(lowerCamelCase__ , encoding="utf-8" ) as f: a__ : List[Any] = f.readlines() a__ : Union[str, Any] = [] a__ : Union[str, Any] = False a__ : Union[str, Any] = False a__ : Dict = [] for line in lines: a__ : Optional[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: a__ : List[Any] = "import datasets\n" elif "import tensorflow" in out_line: # order is important here a__ : List[str] = "" continue elif "from absl import logging" in out_line: a__ : Dict = "from datasets import logging\n" elif "getLogger" in out_line: a__ : List[Any] = out_line.replace("getLogger" , "get_logger" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a__ : List[str] = True a__ : Dict = list(filter(lambda lowerCamelCase__ : e in out_line , lowerCamelCase__ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(lowerCamelCase__ ) + "\n" ) out_lines.append(lowerCamelCase__ ) out_lines.append(lowerCamelCase__ ) continue else: for pattern, replacement in TO_CONVERT: a__ : Tuple = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a__ : Optional[int] = re.match(r"from\stensorflow_datasets.*import\s([^\.\r\n]+)" , lowerCamelCase__ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split("," ) ) a__ : Optional[Any] = "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: a__ : Optional[int] = True out_lines.append(lowerCamelCase__ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a__ : Dict = f_name.replace(".py" , "" ) a__ : Optional[int] = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) 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(lowerCamelCase__ ) if needs_manual_update: with_manual_update.append(lowerCamelCase__ ) with open(lowerCamelCase__ , "w" , encoding="utf-8" ) as f: f.writelines(lowerCamelCase__ ) self._logger.info(f'''Converted in {output_file}''' ) for utils_file in utils_files: try: a__ : Any = os.path.basename(lowerCamelCase__ ) a__ : Optional[int] = imports_to_builder_map[f_name.replace(".py" , "" )] self._logger.info(f'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(lowerCamelCase__ , lowerCamelCase__ ) 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\'.''' )
37
0
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/config.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/config.json''', } class _UpperCamelCase( __lowerCamelCase ): __SCREAMING_SNAKE_CASE : str = '''xlnet''' __SCREAMING_SNAKE_CASE : Dict = ['''mems'''] __SCREAMING_SNAKE_CASE : Dict = { '''n_token''': '''vocab_size''', # Backward compatibility '''hidden_size''': '''d_model''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[str]=3_2_0_0_0 , SCREAMING_SNAKE_CASE__ : Any=1_0_2_4 , SCREAMING_SNAKE_CASE__ : Dict=2_4 , SCREAMING_SNAKE_CASE__ : List[str]=1_6 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4_0_9_6 , SCREAMING_SNAKE_CASE__ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : str="bi" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.02 , SCREAMING_SNAKE_CASE__ : Tuple=1e-12 , SCREAMING_SNAKE_CASE__ : List[Any]=0.1 , SCREAMING_SNAKE_CASE__ : int=5_1_2 , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=True , SCREAMING_SNAKE_CASE__ : str=False , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Optional[Any]=-1 , SCREAMING_SNAKE_CASE__ : Dict=False , SCREAMING_SNAKE_CASE__ : Any="last" , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : List[Any]="tanh" , SCREAMING_SNAKE_CASE__ : List[Any]=0.1 , SCREAMING_SNAKE_CASE__ : int=5 , SCREAMING_SNAKE_CASE__ : List[Any]=5 , SCREAMING_SNAKE_CASE__ : int=5 , SCREAMING_SNAKE_CASE__ : Tuple=1 , SCREAMING_SNAKE_CASE__ : Tuple=2 , **SCREAMING_SNAKE_CASE__ : Tuple , ): '''simple docstring''' __a : Tuple = vocab_size __a : Dict = d_model __a : str = n_layer __a : str = n_head if d_model % n_head != 0: raise ValueError(f'''\'d_model % n_head\' ({d_model % n_head}) should be equal to 0''' ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( f'''`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})''' ) __a : str = d_model // n_head __a : str = ff_activation __a : Any = d_inner __a : Dict = untie_r __a : List[str] = attn_type __a : str = initializer_range __a : Tuple = layer_norm_eps __a : List[str] = dropout __a : str = mem_len __a : List[Any] = reuse_len __a : List[Any] = bi_data __a : str = clamp_len __a : List[str] = same_length __a : Optional[int] = summary_type __a : Any = summary_use_proj __a : List[str] = summary_activation __a : List[Any] = summary_last_dropout __a : Union[str, Any] = start_n_top __a : str = end_n_top __a : List[str] = bos_token_id __a : Union[str, Any] = pad_token_id __a : Tuple = eos_token_id if "use_cache" in kwargs: warnings.warn( 'The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`' ' instead.' , SCREAMING_SNAKE_CASE__ , ) __a : Dict = kwargs['use_cache'] __a : List[Any] = use_mems_eval __a : List[Any] = use_mems_train super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__ , bos_token_id=SCREAMING_SNAKE_CASE__ , eos_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) @property def __lowerCAmelCase ( self : Optional[Any] ): '''simple docstring''' logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' ) return -1 @max_position_embeddings.setter def __lowerCAmelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): '''simple docstring''' raise NotImplementedError( f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
47
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class A__ ( A__ ): """simple docstring""" _lowercase = '' _lowercase = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _lowercase = None # compression type in fsspec. ex: "gzip" _lowercase = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , lowerCamelCase__ : str = "" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , **lowerCamelCase__ : List[str] ): super().__init__(self , **lowerCamelCase__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode a__ : str = fsspec.open( lowerCamelCase__ , mode="rb" , protocol=lowerCamelCase__ , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) a__ : Optional[int] = os.path.basename(self.file.path.split("::" )[0] ) a__ : int = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) a__ : List[Any] = None @classmethod def _UpperCamelCase( cls : int , lowerCamelCase__ : int ): # compressed file paths are always relative to the archive root return super()._strip_protocol(lowerCamelCase__ ).lstrip("/" ) def _UpperCamelCase( self : Dict ): if self.dir_cache is None: a__ : Dict = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} a__ : int = {f["name"]: f} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : str ): return self.file.open().read() def _UpperCamelCase( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : int=None , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[str]=None , **lowerCamelCase__ : Optional[Any] , ): a__ : Optional[int] = self._strip_protocol(lowerCamelCase__ ) if mode != "rb": raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class A__ ( A__ ): """simple docstring""" _lowercase = 'bz2' _lowercase = 'bz2' _lowercase = '.bz2' class A__ ( A__ ): """simple docstring""" _lowercase = 'gzip' _lowercase = 'gzip' _lowercase = '.gz' class A__ ( A__ ): """simple docstring""" _lowercase = 'lz4' _lowercase = 'lz4' _lowercase = '.lz4' class A__ ( A__ ): """simple docstring""" _lowercase = 'xz' _lowercase = 'xz' _lowercase = '.xz' class A__ ( A__ ): """simple docstring""" _lowercase = 'zstd' _lowercase = 'zstd' _lowercase = '.zst' def __init__( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , lowerCamelCase__ : int = DEFAULT_BLOCK_SIZE , **lowerCamelCase__ : Tuple , ): super().__init__( fo=lowerCamelCase__ , mode=lowerCamelCase__ , target_protocol=lowerCamelCase__ , target_options=lowerCamelCase__ , block_size=lowerCamelCase__ , **lowerCamelCase__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 a__ : Any = self.file.__enter__ class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : str ): a__ : List[Any] = file_ def __enter__( self : str ): self._file.__enter__() return self def __exit__( self : int , *lowerCamelCase__ : List[str] , **lowerCamelCase__ : int ): self._file.__exit__(*lowerCamelCase__ , **lowerCamelCase__ ) def __iter__( self : List[str] ): return iter(self._file ) def _UpperCamelCase( self : Any ): return next(self._file ) def __getattr__( self : Optional[Any] , lowerCamelCase__ : Tuple ): return getattr(self._file , lowerCamelCase__ ) def fixed_enter(*lowerCamelCase__ : List[str] , **lowerCamelCase__ : str ): return WrappedFile(_enter(*lowerCamelCase__ , **lowerCamelCase__ ) ) a__ : Any = fixed_enter
37
0
'''simple docstring''' import gc import unittest import torch from parameterized import parameterized from diffusers import AutoencoderKL from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class A ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): snake_case__ :Optional[int] = AutoencoderKL snake_case__ :int = 'sample' snake_case__ :str = 1e-2 @property def __SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" lowerCAmelCase__ = 4 lowerCAmelCase__ = 3 lowerCAmelCase__ = (32, 32) lowerCAmelCase__ = floats_tensor((batch_size, num_channels) + sizes ).to(__magic_name__ ) return {"sample": image} @property def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" return (3, 32, 32) @property def __SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" return (3, 32, 32) def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" lowerCAmelCase__ = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } lowerCAmelCase__ = self.dummy_input return init_dict, inputs_dict def __SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" pass def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" pass @unittest.skipIf(torch_device == "mps" , "Gradient checkpointing skipped on MPS" ) def __SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = self.prepare_init_args_and_inputs_for_common() lowerCAmelCase__ = self.model_class(**__magic_name__ ) model.to(__magic_name__ ) assert not model.is_gradient_checkpointing and model.training lowerCAmelCase__ = model(**__magic_name__ ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model.zero_grad() lowerCAmelCase__ = torch.randn_like(__magic_name__ ) lowerCAmelCase__ = (out - labels).mean() loss.backward() # re-instantiate the model now enabling gradient checkpointing lowerCAmelCase__ = self.model_class(**__magic_name__ ) # clone model model_a.load_state_dict(model.state_dict() ) model_a.to(__magic_name__ ) model_a.enable_gradient_checkpointing() assert model_a.is_gradient_checkpointing and model_a.training lowerCAmelCase__ = model_a(**__magic_name__ ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model_a.zero_grad() lowerCAmelCase__ = (out_a - labels).mean() loss_a.backward() # compare the output and parameters gradients self.assertTrue((loss - loss_a).abs() < 1E-5 ) lowerCAmelCase__ = dict(model.named_parameters() ) lowerCAmelCase__ = dict(model_a.named_parameters() ) for name, param in named_params.items(): self.assertTrue(torch_all_close(param.grad.data , named_params_a[name].grad.data , atol=5E-5 ) ) def __SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy" , output_loading_info=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) self.assertEqual(len(loading_info["missing_keys"] ) , 0 ) model.to(__magic_name__ ) lowerCAmelCase__ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def __SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" lowerCAmelCase__ = AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy" ) lowerCAmelCase__ = model.to(__magic_name__ ) model.eval() if torch_device == "mps": lowerCAmelCase__ = torch.manual_seed(0 ) else: lowerCAmelCase__ = torch.Generator(device=__magic_name__ ).manual_seed(0 ) lowerCAmelCase__ = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) lowerCAmelCase__ = image.to(__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model(__magic_name__ , sample_posterior=__magic_name__ , generator=__magic_name__ ).sample lowerCAmelCase__ = output[0, -1, -3:, -3:].flatten().cpu() # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. if torch_device == "mps": lowerCAmelCase__ = torch.tensor( [ -4.0078E-01, -3.8323E-04, -1.2681E-01, -1.1462E-01, 2.0095E-01, 1.0893E-01, -8.8247E-02, -3.0361E-01, -9.8644E-03, ] ) elif torch_device == "cpu": lowerCAmelCase__ = torch.tensor( [-0.1352, 0.0878, 0.0419, -0.0818, -0.1069, 0.0688, -0.1458, -0.4446, -0.0026] ) else: lowerCAmelCase__ = torch.tensor( [-0.2421, 0.4642, 0.2507, -0.0438, 0.0682, 0.3160, -0.2018, -0.0727, 0.2485] ) self.assertTrue(torch_all_close(__magic_name__ , __magic_name__ , rtol=1E-2 ) ) @slow class A ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE ( self : Optional[Any] , __magic_name__ : List[Any] , __magic_name__ : Any ): """simple docstring""" return f"""gaussian_noise_s={seed}_shape={"_".join([str(__magic_name__ ) for s in shape] )}.npy""" def __SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __SCREAMING_SNAKE_CASE ( self : Tuple , __magic_name__ : List[str]=0 , __magic_name__ : str=(4, 3, 512, 512) , __magic_name__ : str=False ): """simple docstring""" lowerCAmelCase__ = torch.floataa if fpaa else torch.floataa lowerCAmelCase__ = torch.from_numpy(load_hf_numpy(self.get_file_format(__magic_name__ , __magic_name__ ) ) ).to(__magic_name__ ).to(__magic_name__ ) return image def __SCREAMING_SNAKE_CASE ( self : int , __magic_name__ : List[str]="CompVis/stable-diffusion-v1-4" , __magic_name__ : Optional[Any]=False ): """simple docstring""" lowerCAmelCase__ = "fp16" if fpaa else None lowerCAmelCase__ = torch.floataa if fpaa else torch.floataa lowerCAmelCase__ = AutoencoderKL.from_pretrained( __magic_name__ , subfolder="vae" , torch_dtype=__magic_name__ , revision=__magic_name__ , ) model.to(__magic_name__ ).eval() return model def __SCREAMING_SNAKE_CASE ( self : List[str] , __magic_name__ : Union[str, Any]=0 ): """simple docstring""" if torch_device == "mps": return torch.manual_seed(__magic_name__ ) return torch.Generator(device=__magic_name__ ).manual_seed(__magic_name__ ) @parameterized.expand( [ # fmt: off [33, [-0.1603, 0.9878, -0.0495, -0.0790, -0.2709, 0.8375, -0.2060, -0.0824], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2376, 0.1168, 0.1332, -0.4840, -0.2508, -0.0791, -0.0493, -0.4089], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def __SCREAMING_SNAKE_CASE ( self : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Optional[int] , __magic_name__ : Dict ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model() lowerCAmelCase__ = self.get_sd_image(__magic_name__ ) lowerCAmelCase__ = self.get_generator(__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model(__magic_name__ , generator=__magic_name__ , sample_posterior=__magic_name__ ).sample assert sample.shape == image.shape lowerCAmelCase__ = sample[-1, -2:, -2:, :2].flatten().float().cpu() lowerCAmelCase__ = torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice ) assert torch_all_close(__magic_name__ , __magic_name__ , atol=3E-3 ) @parameterized.expand( [ # fmt: off [33, [-0.0513, 0.0289, 1.3799, 0.2166, -0.2573, -0.0871, 0.5103, -0.0999]], [47, [-0.4128, -0.1320, -0.3704, 0.1965, -0.4116, -0.2332, -0.3340, 0.2247]], # fmt: on ] ) @require_torch_gpu def __SCREAMING_SNAKE_CASE ( self : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Any ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model(fpaa=__magic_name__ ) lowerCAmelCase__ = self.get_sd_image(__magic_name__ , fpaa=__magic_name__ ) lowerCAmelCase__ = self.get_generator(__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model(__magic_name__ , generator=__magic_name__ , sample_posterior=__magic_name__ ).sample assert sample.shape == image.shape lowerCAmelCase__ = sample[-1, -2:, :2, -2:].flatten().float().cpu() lowerCAmelCase__ = torch.tensor(__magic_name__ ) assert torch_all_close(__magic_name__ , __magic_name__ , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.1609, 0.9866, -0.0487, -0.0777, -0.2716, 0.8368, -0.2055, -0.0814], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2377, 0.1147, 0.1333, -0.4841, -0.2506, -0.0805, -0.0491, -0.4085], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def __SCREAMING_SNAKE_CASE ( self : str , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Dict ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model() lowerCAmelCase__ = self.get_sd_image(__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model(__magic_name__ ).sample assert sample.shape == image.shape lowerCAmelCase__ = sample[-1, -2:, -2:, :2].flatten().float().cpu() lowerCAmelCase__ = torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice ) assert torch_all_close(__magic_name__ , __magic_name__ , atol=3E-3 ) @parameterized.expand( [ # fmt: off [13, [-0.2051, -0.1803, -0.2311, -0.2114, -0.3292, -0.3574, -0.2953, -0.3323]], [37, [-0.2632, -0.2625, -0.2199, -0.2741, -0.4539, -0.4990, -0.3720, -0.4925]], # fmt: on ] ) @require_torch_gpu def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : List[str] , __magic_name__ : List[str] ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model() lowerCAmelCase__ = self.get_sd_image(__magic_name__ , shape=(3, 4, 64, 64) ) with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample assert list(sample.shape ) == [3, 3, 512, 512] lowerCAmelCase__ = sample[-1, -2:, :2, -2:].flatten().cpu() lowerCAmelCase__ = torch.tensor(__magic_name__ ) assert torch_all_close(__magic_name__ , __magic_name__ , atol=1E-3 ) @parameterized.expand( [ # fmt: off [27, [-0.0369, 0.0207, -0.0776, -0.0682, -0.1747, -0.1930, -0.1465, -0.2039]], [16, [-0.1628, -0.2134, -0.2747, -0.2642, -0.3774, -0.4404, -0.3687, -0.4277]], # fmt: on ] ) @require_torch_gpu def __SCREAMING_SNAKE_CASE ( self : str , __magic_name__ : Optional[int] , __magic_name__ : Tuple ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model(fpaa=__magic_name__ ) lowerCAmelCase__ = self.get_sd_image(__magic_name__ , shape=(3, 4, 64, 64) , fpaa=__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample assert list(sample.shape ) == [3, 3, 512, 512] lowerCAmelCase__ = sample[-1, -2:, :2, -2:].flatten().float().cpu() lowerCAmelCase__ = torch.tensor(__magic_name__ ) assert torch_all_close(__magic_name__ , __magic_name__ , atol=5E-3 ) @parameterized.expand([(13,), (16,), (27,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason="xformers is not required when using PyTorch 2.0." ) def __SCREAMING_SNAKE_CASE ( self : Any , __magic_name__ : Union[str, Any] ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model(fpaa=__magic_name__ ) lowerCAmelCase__ = self.get_sd_image(__magic_name__ , shape=(3, 4, 64, 64) , fpaa=__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(__magic_name__ , __magic_name__ , atol=1E-1 ) @parameterized.expand([(13,), (16,), (37,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason="xformers is not required when using PyTorch 2.0." ) def __SCREAMING_SNAKE_CASE ( self : Tuple , __magic_name__ : List[Any] ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model() lowerCAmelCase__ = self.get_sd_image(__magic_name__ , shape=(3, 4, 64, 64) ) with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): lowerCAmelCase__ = model.decode(__magic_name__ ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(__magic_name__ , __magic_name__ , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.3001, 0.0918, -2.6984, -3.9720, -3.2099, -5.0353, 1.7338, -0.2065, 3.4267]], [47, [-1.5030, -4.3871, -6.0355, -9.1157, -1.6661, -2.7853, 2.1607, -5.0823, 2.5633]], # fmt: on ] ) def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : Dict , __magic_name__ : Tuple ): """simple docstring""" lowerCAmelCase__ = self.get_sd_vae_model() lowerCAmelCase__ = self.get_sd_image(__magic_name__ ) lowerCAmelCase__ = self.get_generator(__magic_name__ ) with torch.no_grad(): lowerCAmelCase__ = model.encode(__magic_name__ ).latent_dist lowerCAmelCase__ = dist.sample(generator=__magic_name__ ) assert list(sample.shape ) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]] lowerCAmelCase__ = sample[0, -1, -3:, -3:].flatten().cpu() lowerCAmelCase__ = torch.tensor(__magic_name__ ) lowerCAmelCase__ = 3E-3 if torch_device != "mps" else 1E-2 assert torch_all_close(__magic_name__ , __magic_name__ , atol=__magic_name__ )
48
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Union[str, Any] = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") a__ : Union[str, Any] = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__a ): os.makedirs(__a ) a__ : Any = model.state_dict() def to_tf_var_name(__a ): for patt, repl in iter(__a ): a__ : Tuple = name.replace(__a , __a ) return f'''bert/{name}''' def create_tf_var(__a , __a , __a ): a__ : Tuple = tf.dtypes.as_dtype(tensor.dtype ) a__ : Dict = tf.get_variable(dtype=__a , shape=tensor.shape , name=__a , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__a ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: a__ : int = to_tf_var_name(__a ) a__ : Union[str, Any] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): a__ : int = torch_tensor.T a__ : Optional[Any] = create_tf_var(tensor=__a , name=__a , session=__a ) tf.keras.backend.set_value(__a , __a ) a__ : int = session.run(__a ) print(f'''Successfully created {tf_name}: {np.allclose(__a , __a )}''' ) a__ : Any = tf.train.Saver(tf.trainable_variables() ) saver.save(__a , os.path.join(__a , model_name.replace("-" , "_" ) + ".ckpt" ) ) def UpperCamelCase_ ( __a=None ) -> int: a__ : Dict = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__a , required=__a , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__a , default=__a , required=__a , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__a , required=__a , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__a , required=__a , help="Directory in which to save tensorflow model" ) a__ : Optional[Any] = parser.parse_args(__a ) a__ : Tuple = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__a , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
37
0
"""simple docstring""" from math import ceil def lowercase__ ( snake_case_ :int = 1_001 ): __UpperCAmelCase = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __UpperCAmelCase = 2 * i + 1 __UpperCAmelCase = 2 * i __UpperCAmelCase = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: _lowercase : Any = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number')
49
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=2 , lowerCamelCase__ : Any=24 , lowerCamelCase__ : Optional[Any]=16 , lowerCamelCase__ : int=True , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[Any]=32 , lowerCamelCase__ : List[str]=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Optional[Any]=37 , lowerCamelCase__ : Any="gelu" , lowerCamelCase__ : Union[str, Any]=0.1 , lowerCamelCase__ : Optional[int]=0.1 , lowerCamelCase__ : str=10 , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : str=None , lowerCamelCase__ : List[str]=2 , lowerCamelCase__ : Optional[Any]=2 , ): a__ : str = parent a__ : Any = batch_size a__ : Dict = patch_size a__ : List[Any] = max_length a__ : str = num_mel_bins a__ : Optional[Any] = is_training a__ : Optional[int] = use_labels a__ : List[Any] = hidden_size a__ : str = num_hidden_layers a__ : Any = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : List[str] = hidden_act a__ : str = hidden_dropout_prob a__ : Tuple = attention_probs_dropout_prob a__ : List[Any] = type_sequence_label_size a__ : Any = initializer_range a__ : str = scope a__ : List[str] = frequency_stride a__ : Union[str, Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) a__ : List[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 a__ : List[str] = (self.max_length - self.patch_size) // self.time_stride + 1 a__ : Tuple = frequency_out_dimension * time_out_dimension a__ : List[str] = num_patches + 2 def _UpperCamelCase( self : List[str] ): a__ : Any = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) a__ : List[Any] = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : List[str] = self.get_config() return config, input_values, labels def _UpperCamelCase( self : Optional[int] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] ): a__ : List[Any] = ASTModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Dict = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : str ): a__ : Dict = self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ) : Optional[int] = config_and_inputs a__ : List[Any] = {"input_values": input_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) _lowercase = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def _UpperCamelCase( self : str ): a__ : str = ASTModelTester(self ) a__ : Any = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[str] ): self.config_tester.run_common_tests() @unittest.skip(reason="AST does not use inputs_embeds" ) def _UpperCamelCase( self : List[str] ): pass def _UpperCamelCase( self : Optional[int] ): a__, a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Any = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : Tuple ): a__, a__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Dict = model_class(lowerCamelCase__ ) a__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Optional[Any] = ["input_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Union[str, Any] = ASTModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : Optional[int] = hf_hub_download( repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset" ) a__, a__ : List[str] = torchaudio.load(__a ) return audio, sampling_rate @require_torch @require_torchaudio class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _UpperCamelCase( self : List[str] ): return ( ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ) if is_torchaudio_available() else None ) @slow def _UpperCamelCase( self : Optional[int] ): a__ : int = self.default_feature_extractor a__ : Optional[Any] = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ).to(lowerCamelCase__ ) a__ : Any = self.default_feature_extractor a__, a__ : Dict = prepare_audio() a__ : str = audio.squeeze().numpy() a__ : Any = feature_extractor(lowerCamelCase__ , sampling_rate=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(**lowerCamelCase__ ) # verify the logits a__ : Union[str, Any] = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) a__ : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) )
37
0
'''simple docstring''' def A__ ( __lowerCAmelCase : int ): lowerCamelCase__ = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
50
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast 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 UpperCamelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = XGLMTokenizer _lowercase = XGLMTokenizerFast _lowercase = True _lowercase = True def _UpperCamelCase( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase( self : List[Any] ): a__ : int = "<pad>" a__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(lowerCamelCase__ ) , 1_008 ) def _UpperCamelCase( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def _UpperCamelCase( self : Optional[int] ): a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) a__ : List[str] = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowerCamelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) a__ : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a__ : List[str] = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) a__ : Dict = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _UpperCamelCase( self : Dict ): return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def _UpperCamelCase( self : Union[str, Any] ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCamelCase__ , f.name ) a__ : Any = XGLMTokenizer(f.name , keep_accents=lowerCamelCase__ ) a__ : List[str] = pickle.dumps(lowerCamelCase__ ) pickle.loads(lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): if not self.test_rust_tokenizer: return a__ : Any = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : Tuple = "I was born in 92000, and this is falsé." a__ : List[str] = tokenizer.tokenize(lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : Optional[int] = tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = self.get_rust_tokenizer() a__ : Tuple = tokenizer.encode(lowerCamelCase__ ) a__ : Optional[Any] = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = "Hello World!" a__ : List[str] = [2, 31_227, 4_447, 35] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Union[str, Any] ): a__ : Optional[int] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off a__ : Union[str, Any] = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : List[Any] ): # fmt: off a__ : Optional[int] = { "input_ids": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], "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, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name="facebook/xglm-564M" , padding=lowerCamelCase__ , )
37
0
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' _lowerCamelCase =42 class lowerCAmelCase__ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : List[Any] , a__ : int = 3 , a__ : int = 3 , a__ : Tuple[str] = ("DownEncoderBlock2D",) , a__ : Tuple[str] = ("UpDecoderBlock2D",) , a__ : Tuple[int] = (64,) , a__ : int = 1 , a__ : str = "silu" , a__ : int = 3 , a__ : int = 32 , a__ : int = 256 , a__ : int = 32 , a__ : Optional[int] = None , a__ : float = 0.18_215 , a__ : str = "group" , ): super().__init__() # pass init params to Encoder UpperCAmelCase = Encoder( in_channels=a__ , out_channels=a__ , down_block_types=a__ , block_out_channels=a__ , layers_per_block=a__ , act_fn=a__ , norm_num_groups=a__ , double_z=a__ , ) UpperCAmelCase = vq_embed_dim if vq_embed_dim is not None else latent_channels UpperCAmelCase = nn.Convad(a__ , a__ , 1 ) UpperCAmelCase = VectorQuantizer(a__ , a__ , beta=0.25 , remap=a__ , sane_index_shape=a__ ) UpperCAmelCase = nn.Convad(a__ , a__ , 1 ) # pass init params to Decoder UpperCAmelCase = Decoder( in_channels=a__ , out_channels=a__ , up_block_types=a__ , block_out_channels=a__ , layers_per_block=a__ , act_fn=a__ , norm_num_groups=a__ , norm_type=a__ , ) @apply_forward_hook def __snake_case ( self : Tuple , a__ : torch.FloatTensor , a__ : bool = True ): UpperCAmelCase = self.encoder(a__ ) UpperCAmelCase = self.quant_conv(a__ ) if not return_dict: return (h,) return VQEncoderOutput(latents=a__ ) @apply_forward_hook def __snake_case ( self : str , a__ : torch.FloatTensor , a__ : bool = False , a__ : bool = True ): # also go through quantization layer if not force_not_quantize: UpperCAmelCase, UpperCAmelCase, UpperCAmelCase = self.quantize(a__ ) else: UpperCAmelCase = h UpperCAmelCase = self.post_quant_conv(a__ ) UpperCAmelCase = self.decoder(a__ , quant if self.config.norm_type == '''spatial''' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=a__ ) def __snake_case ( self : List[Any] , a__ : torch.FloatTensor , a__ : bool = True ): UpperCAmelCase = sample UpperCAmelCase = self.encode(a__ ).latents UpperCAmelCase = self.decode(a__ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=a__ )
51
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCamelCase_ ( ) -> int: a__ : int = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg" a__ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ).convert("RGB" ) return image def UpperCamelCase_ ( __a ) -> Optional[Any]: a__ : Any = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.embeddings.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.embeddings.layernorm.bias") ) # fmt: on return rename_keys def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Union[str, Any] = dct.pop(__a ) a__ : List[str] = val def UpperCamelCase_ ( __a , __a ) -> Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__ : Any = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) a__ : Tuple = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict a__ : str = torch.cat((q_bias, torch.zeros_like(__a , requires_grad=__a ), v_bias) ) a__ : int = qkv_bias def UpperCamelCase_ ( __a ) -> Dict: a__ : Tuple = 364 if "coco" in model_name else 224 a__ : int = InstructBlipVisionConfig(image_size=__a ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: a__ : Tuple = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__ : Dict = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: a__ : List[Any] = LlamaConfig.from_pretrained("decapoda-research/llama-7b-hf" , vocab_size=32_001 ).to_dict() elif "vicuna-13b" in model_name: a__ : Optional[int] = LlamaConfig.from_pretrained("decapoda-research/llama-13b-hf" , vocab_size=32_001 ).to_dict() else: raise ValueError("Model name not supported" ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 a__ : Optional[Any] = InstructBlipQFormerConfig(vocab_size=30_523 ).to_dict() a__ : Any = InstructBlipConfig(vision_config=__a , text_config=__a , qformer_config=__a ) return config, image_size @torch.no_grad() def UpperCamelCase_ ( __a , __a=None , __a=False ) -> int: a__ : Tuple = AutoTokenizer.from_pretrained("bert-base-uncased" , truncation_side="left" ) qformer_tokenizer.add_special_tokens({"bos_token": "[DEC]"} ) if "t5" in model_name: a__ : List[Any] = TaTokenizerFast.from_pretrained("google/flan-t5-xl" , truncation_side="left" ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) a__ : Union[str, Any] = LlamaTokenizerFast.from_pretrained( "huggyllama/llama-7b" , truncation_side="left" , bos_token="</s>" , unk_token="</s>" ) tokenizer.add_special_tokens({"pad_token": "[PAD]"} ) a__, a__ : List[str] = get_blipa_config(__a ) a__ : Any = InstructBlipForConditionalGeneration(__a ).eval() a__ : Dict = { "instructblip-vicuna-7b": ("blip2_vicuna_instruct", "vicuna7b"), "instructblip-vicuna-13b": ("blip2_vicuna_instruct", "vicuna13b"), "instructblip-flan-t5-xl": ("blip2_t5_instruct", "flant5xl"), "instructblip-flan-t5-xxl": ("blip2_t5_instruct", "flant5xxl"), } a__, a__ : Dict = model_name_to_original[model_name] # load original model print("Loading original model..." ) a__ : Optional[Any] = "cuda:1" if torch.cuda.is_available() else "cpu" a__ : List[Any] = "cuda:2" if torch.cuda.is_available() else "cpu" a__, a__, a__ : Tuple = load_model_and_preprocess( name=__a , model_type=__a , is_eval=__a , device=__a ) original_model.eval() print("Done!" ) # update state dict keys a__ : Dict = original_model.state_dict() a__ : Optional[int] = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a , __a , __a ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__ : Optional[int] = state_dict.pop(__a ) if key.startswith("Qformer.bert" ): a__ : List[Any] = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: a__ : Any = key.replace("self" , "attention" ) if "llm_proj" in key: a__ : Dict = key.replace("llm_proj" , "language_projection" ) if "t5_proj" in key: a__ : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("llm_model" ): a__ : List[str] = key.replace("llm_model" , "language_model" ) if key.startswith("t5" ): a__ : str = key.replace("t5" , "language" ) a__ : Dict = val # read in qv biases read_in_q_v_bias(__a , __a ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(__a , strict=__a ) a__ : Union[str, Any] = load_demo_image() a__ : int = "What is unusual about this image?" # create processor a__ : Any = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__a , image_std=__a ) a__ : Tuple = InstructBlipProcessor( image_processor=__a , tokenizer=__a , qformer_tokenizer=__a , ) a__ : Tuple = processor(images=__a , text=__a , return_tensors="pt" ).to(__a ) # make sure processor creates exact same pixel values a__ : Optional[int] = vis_processors["eval"](__a ).unsqueeze(0 ).to(__a ) a__ : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , __a ) original_model.to(__a ) hf_model.to(__a ) with torch.no_grad(): if "vicuna" in model_name: a__ : str = original_model({"image": original_pixel_values, "text_input": [prompt]} ).logits a__ : List[str] = hf_model(**__a ).logits else: a__ : List[Any] = original_model( {"image": original_pixel_values, "text_input": [prompt], "text_output": ["\n"]} ).logits a__ : str = tokenizer("\n" , return_tensors="pt" ).input_ids.to(__a ) a__ : Dict = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) a__ : Any = hf_model(**__a , labels=__a ).logits print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape a__ : Tuple = 1e-4 if "vicuna" in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , __a , atol=__a ) print("Looks ok!" ) print("Generating with original model..." ) a__ : Tuple = original_model.generate({"image": original_pixel_values, "prompt": prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print("Generating with HF model..." ) a__ : int = hf_model.generate( **__a , do_sample=__a , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? a__ : int = 2 print("Original generation:" , __a ) a__ : str = processor.batch_decode(__a , skip_special_tokens=__a ) a__ : str = [text.strip() for text in output_text] print("HF generation:" , __a ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() UpperCamelCase : Optional[int] = [ """instructblip-vicuna-7b""", """instructblip-vicuna-13b""", """instructblip-flan-t5-xl""", """instructblip-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""instructblip-flan-t5-xl""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase : Dict = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
37
0
"""simple docstring""" def __A ( a_ :int) -> int: __a : str = abs(a_) __a : Any = 0 while n > 0: res += n % 10 n //= 10 return res def __A ( a_ :int) -> int: __a : int = abs(a_) return n if n < 10 else n % 10 + sum_of_digits(n // 10) def __A ( a_ :int) -> int: return sum(int(a_) for c in str(abs(a_))) def __A ( ) -> None: from collections.abc import Callable from timeit import timeit def benchmark_a_function(a_ :Callable , a_ :int) -> None: __a : Any = F"""{func.__name__}({value})""" __a : str = timeit(F"""__main__.{call}""" , setup='''import __main__''') print(F"""{call:56} = {func(a_)} -- {timing:.4f} seconds""") for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(a_ , a_) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
52
def UpperCamelCase_ ( __a , __a ) -> Tuple: a__ : Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 a__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. a__ : Any = min(__a , __a ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
37
0
import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format='%(message)s') def a_ ( lowerCAmelCase_ : np.ndarray ): return input_array.reshape((input_array.size, 1) ) def a_ ( lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : int ): __lowerCAmelCase = np.nan for i in range(lowerCAmelCase_ ): __lowerCAmelCase = features[:, labels == i] __lowerCAmelCase = data.mean(1 ) # Centralize the data of class i __lowerCAmelCase = data - column_reshape(lowerCAmelCase_ ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(lowerCAmelCase_, centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) __lowerCAmelCase = np.dot(lowerCAmelCase_, centered_data.T ) return covariance_sum / features.shape[1] def a_ ( lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : int ): __lowerCAmelCase = features.mean(1 ) __lowerCAmelCase = np.nan for i in range(lowerCAmelCase_ ): __lowerCAmelCase = features[:, labels == i] __lowerCAmelCase = data.shape[1] __lowerCAmelCase = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(lowerCAmelCase_ ) - column_reshape(lowerCAmelCase_ ), (column_reshape(lowerCAmelCase_ ) - column_reshape(lowerCAmelCase_ )).T, ) else: # If covariance_sum is np.nan (i.e. first loop) __lowerCAmelCase = device_data * np.dot( column_reshape(lowerCAmelCase_ ) - column_reshape(lowerCAmelCase_ ), (column_reshape(lowerCAmelCase_ ) - column_reshape(lowerCAmelCase_ )).T, ) return covariance_sum / features.shape[1] def a_ ( lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : int ): # Check if the features have been loaded if features.any(): __lowerCAmelCase = features.mean(1 ) # Center the dataset __lowerCAmelCase = features - np.reshape(lowerCAmelCase_, (data_mean.size, 1) ) __lowerCAmelCase = np.dot(lowerCAmelCase_, centered_data.T ) / features.shape[1] __lowerCAmelCase , __lowerCAmelCase = np.linalg.eigh(lowerCAmelCase_ ) # Take all the columns in the reverse order (-1), and then takes only the first __lowerCAmelCase = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space __lowerCAmelCase = np.dot(filtered_eigenvectors.T, lowerCAmelCase_ ) logging.info('Principal Component Analysis computed' ) return projected_data else: logging.basicConfig(level=logging.ERROR, format='%(message)s', force=lowerCAmelCase_ ) logging.error('Dataset empty' ) raise AssertionError def a_ ( lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : np.ndarray, lowerCAmelCase_ : int, lowerCAmelCase_ : int ): assert classes > dimensions # Check if features have been already loaded if features.any: __lowerCAmelCase , __lowerCAmelCase = eigh( covariance_between_classes(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ), covariance_within_classes(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ), ) __lowerCAmelCase = eigenvectors[:, ::-1][:, :dimensions] __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = np.linalg.svd(lowerCAmelCase_ ) __lowerCAmelCase = svd_matrix[:, 0:dimensions] __lowerCAmelCase = np.dot(filtered_svd_matrix.T, lowerCAmelCase_ ) logging.info('Linear Discriminant Analysis computed' ) return projected_data else: logging.basicConfig(level=logging.ERROR, format='%(message)s', force=lowerCAmelCase_ ) logging.error('Dataset empty' ) raise AssertionError def a_ ( ): # Create dummy dataset with 2 classes and 3 features __lowerCAmelCase = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) __lowerCAmelCase = np.array([0, 0, 0, 1, 1] ) __lowerCAmelCase = 2 __lowerCAmelCase = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(lowerCAmelCase_ ) as error_info: __lowerCAmelCase = linear_discriminant_analysis( lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) if isinstance(lowerCAmelCase_, np.ndarray ): raise AssertionError( 'Did not raise AssertionError for dimensions > classes' ) assert error_info.type is AssertionError def a_ ( ): __lowerCAmelCase = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) __lowerCAmelCase = 2 __lowerCAmelCase = np.array([[6.9282_0323, 8.6602_5404, 10.3923_0485], [3.0, 3.0, 3.0]] ) with pytest.raises(lowerCAmelCase_ ) as error_info: __lowerCAmelCase = principal_component_analysis(lowerCAmelCase_, lowerCAmelCase_ ) if not np.allclose(lowerCAmelCase_, lowerCAmelCase_ ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
53
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Optional[Any] = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } UpperCamelCase : Dict = { """allenai/led-base-16384""": 1_6384, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = LEDTokenizer _lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[str]=None , lowerCamelCase__ : Any=None , lowerCamelCase__ : int="replace" , lowerCamelCase__ : Union[str, Any]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Tuple="</s>" , lowerCamelCase__ : Optional[int]="<s>" , lowerCamelCase__ : str="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Any="<mask>" , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : int=True , **lowerCamelCase__ : Union[str, Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : List[str] = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : Optional[Any] = add_prefix_space a__ : List[str] = pre_tok_class(**lowerCamelCase__ ) a__ : Optional[int] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ : Any = "post_processor" a__ : str = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Optional[Any] = tuple(state["sep"] ) if "cls" in state: a__ : Optional[Any] = tuple(state["cls"] ) a__ : Optional[int] = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Dict = add_prefix_space a__ : int = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : List[Any] = trim_offsets a__ : List[str] = True if changes_to_apply: a__ : int = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : int = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Union[str, Any] ): a__ : Any = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : Union[str, Any] = value def _UpperCamelCase( self : Any , *lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : List[str] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Any , *lowerCamelCase__ : Dict , **lowerCamelCase__ : Optional[Any] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : List[str] = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[Any]=None ): a__ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : List[str] = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0] def _UpperCamelCase( self : Dict , lowerCamelCase__ : Union[Dict[str, EncodedInput], BatchEncoding] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[bool] = None , ): a__ : str = super()._pad( encoded_inputs=lowerCamelCase__ , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) # Load from model defaults if return_attention_mask is None: a__ : Optional[int] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ : Tuple = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ : Dict = len(encoded_inputs["global_attention_mask"] ) != len(lowerCamelCase__ ) if needs_to_be_padded: a__ : Union[str, Any] = len(lowerCamelCase__ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ : List[Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a__ : Any = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
37
0
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class A ( unittest.TestCase ): _snake_case =MODEL_FOR_MASKED_LM_MAPPING _snake_case =TF_MODEL_FOR_MASKED_LM_MAPPING def lowerCAmelCase__ ( self: List[str] ) -> Tuple: '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def lowerCAmelCase__ ( self: List[str] ) -> Optional[int]: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="sshleifer/tiny-distilroberta-base" , top_k=2 , framework="tf" ) UpperCAmelCase_ =unmasker("My name is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ {"sequence": "My name is grouped", "score": 2.1e-05, "token": 3_8015, "token_str": " grouped"}, {"sequence": "My name is accuser", "score": 2.1e-05, "token": 2_5506, "token_str": " accuser"}, ] , ) UpperCAmelCase_ =unmasker("The largest city in France is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ { "sequence": "The largest city in France is grouped", "score": 2.1e-05, "token": 3_8015, "token_str": " grouped", }, { "sequence": "The largest city in France is accuser", "score": 2.1e-05, "token": 2_5506, "token_str": " accuser", }, ] , ) UpperCAmelCase_ =unmasker("My name is <mask>" , targets=[" Patrick", " Clara", " Teven"] , top_k=3 ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ {"sequence": "My name is Clara", "score": 2e-05, "token": 1_3606, "token_str": " Clara"}, {"sequence": "My name is Patrick", "score": 2e-05, "token": 3499, "token_str": " Patrick"}, {"sequence": "My name is Te", "score": 1.9e-05, "token": 2941, "token_str": " Te"}, ] , ) @require_torch def lowerCAmelCase__ ( self: Optional[int] ) -> Dict: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="sshleifer/tiny-distilroberta-base" , top_k=2 , framework="pt" ) UpperCAmelCase_ =unmasker("My name is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ {"sequence": "My name is Maul", "score": 2.2e-05, "token": 3_5676, "token_str": " Maul"}, {"sequence": "My name isELS", "score": 2.2e-05, "token": 1_6416, "token_str": "ELS"}, ] , ) UpperCAmelCase_ =unmasker("The largest city in France is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ { "sequence": "The largest city in France is Maul", "score": 2.2e-05, "token": 3_5676, "token_str": " Maul", }, {"sequence": "The largest city in France isELS", "score": 2.2e-05, "token": 1_6416, "token_str": "ELS"}, ] , ) UpperCAmelCase_ =unmasker("My name is <mask>" , targets=[" Patrick", " Clara", " Teven"] , top_k=3 ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ {"sequence": "My name is Patrick", "score": 2.1e-05, "token": 3499, "token_str": " Patrick"}, {"sequence": "My name is Te", "score": 2e-05, "token": 2941, "token_str": " Te"}, {"sequence": "My name is Clara", "score": 2e-05, "token": 1_3606, "token_str": " Clara"}, ] , ) UpperCAmelCase_ =unmasker("My name is <mask> <mask>" , top_k=2 ) self.assertEqual( nested_simplify(_lowerCAmelCase , decimals=6 ) , [ [ { "score": 2.2e-05, "token": 3_5676, "token_str": " Maul", "sequence": "<s>My name is Maul<mask></s>", }, {"score": 2.2e-05, "token": 1_6416, "token_str": "ELS", "sequence": "<s>My name isELS<mask></s>"}, ], [ { "score": 2.2e-05, "token": 3_5676, "token_str": " Maul", "sequence": "<s>My name is<mask> Maul</s>", }, {"score": 2.2e-05, "token": 1_6416, "token_str": "ELS", "sequence": "<s>My name is<mask>ELS</s>"}, ], ] , ) @require_torch_gpu def lowerCAmelCase__ ( self: Tuple ) -> List[str]: '''simple docstring''' UpperCAmelCase_ =pipeline("fill-mask" , model="hf-internal-testing/tiny-random-distilbert" , device=0 , framework="pt" ) # convert model to fp16 pipe.model.half() UpperCAmelCase_ =pipe("Paris is the [MASK] of France." ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) @slow @require_torch def lowerCAmelCase__ ( self: Tuple ) -> List[str]: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="distilroberta-base" , top_k=2 , framework="pt" ) self.run_large_test(_lowerCAmelCase ) @slow @require_tf def lowerCAmelCase__ ( self: Dict ) -> List[Any]: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="distilroberta-base" , top_k=2 , framework="tf" ) self.run_large_test(_lowerCAmelCase ) def lowerCAmelCase__ ( self: str , _lowerCAmelCase: Optional[Any] ) -> Tuple: '''simple docstring''' UpperCAmelCase_ =unmasker("My name is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase ) , [ {"sequence": "My name is John", "score": 0.0_08, "token": 610, "token_str": " John"}, {"sequence": "My name is Chris", "score": 0.0_07, "token": 1573, "token_str": " Chris"}, ] , ) UpperCAmelCase_ =unmasker("The largest city in France is <mask>" ) self.assertEqual( nested_simplify(_lowerCAmelCase ) , [ { "sequence": "The largest city in France is Paris", "score": 0.2_51, "token": 2201, "token_str": " Paris", }, { "sequence": "The largest city in France is Lyon", "score": 0.2_14, "token": 1_2790, "token_str": " Lyon", }, ] , ) UpperCAmelCase_ =unmasker("My name is <mask>" , targets=[" Patrick", " Clara", " Teven"] , top_k=3 ) self.assertEqual( nested_simplify(_lowerCAmelCase ) , [ {"sequence": "My name is Patrick", "score": 0.0_05, "token": 3499, "token_str": " Patrick"}, {"sequence": "My name is Clara", "score": 0.0_00, "token": 1_3606, "token_str": " Clara"}, {"sequence": "My name is Te", "score": 0.0_00, "token": 2941, "token_str": " Te"}, ] , ) @require_torch def lowerCAmelCase__ ( self: str ) -> List[str]: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="sshleifer/tiny-distilroberta-base" , framework="pt" ) UpperCAmelCase_ =None UpperCAmelCase_ =None self.run_pipeline_test(_lowerCAmelCase , [] ) @require_tf def lowerCAmelCase__ ( self: Optional[int] ) -> Optional[int]: '''simple docstring''' UpperCAmelCase_ =pipeline(task="fill-mask" , model="sshleifer/tiny-distilroberta-base" , framework="tf" ) UpperCAmelCase_ =None UpperCAmelCase_ =None self.run_pipeline_test(_lowerCAmelCase , [] ) def lowerCAmelCase__ ( self: str , _lowerCAmelCase: Optional[int] , _lowerCAmelCase: Optional[Any] , _lowerCAmelCase: Union[str, Any] ) -> Tuple: '''simple docstring''' if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("The provided tokenizer has no mask token, (probably reformer or wav2vec2)" ) UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) UpperCAmelCase_ =[ F'This is another {tokenizer.mask_token} test', ] return fill_masker, examples def lowerCAmelCase__ ( self: int , _lowerCAmelCase: Union[str, Any] , _lowerCAmelCase: Union[str, Any] ) -> Any: '''simple docstring''' UpperCAmelCase_ =fill_masker.tokenizer UpperCAmelCase_ =fill_masker.model UpperCAmelCase_ =fill_masker( F'This is a {tokenizer.mask_token}' , ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) UpperCAmelCase_ =fill_masker([F'This is a {tokenizer.mask_token}'] ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) UpperCAmelCase_ =fill_masker([F'This is a {tokenizer.mask_token}', F'Another {tokenizer.mask_token} great test.'] ) self.assertEqual( _lowerCAmelCase , [ [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ], [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ], ] , ) with self.assertRaises(_lowerCAmelCase ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_lowerCAmelCase ): fill_masker("This is" ) self.run_test_top_k(_lowerCAmelCase , _lowerCAmelCase ) self.run_test_targets(_lowerCAmelCase , _lowerCAmelCase ) self.run_test_top_k_targets(_lowerCAmelCase , _lowerCAmelCase ) self.fill_mask_with_duplicate_targets_and_top_k(_lowerCAmelCase , _lowerCAmelCase ) self.fill_mask_with_multiple_masks(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase__ ( self: int , _lowerCAmelCase: Tuple , _lowerCAmelCase: Optional[int] ) -> Tuple: '''simple docstring''' UpperCAmelCase_ =tokenizer.get_vocab() UpperCAmelCase_ =sorted(vocab.keys() )[:2] # Pipeline argument UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase , targets=_lowerCAmelCase ) UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) UpperCAmelCase_ ={vocab[el] for el in targets} self.assertEqual({el["token"] for el in outputs} , _lowerCAmelCase ) UpperCAmelCase_ =[tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["token_str"] for el in outputs} , set(_lowerCAmelCase ) ) # Call argument UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets=_lowerCAmelCase ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) UpperCAmelCase_ ={vocab[el] for el in targets} self.assertEqual({el["token"] for el in outputs} , _lowerCAmelCase ) UpperCAmelCase_ =[tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["token_str"] for el in outputs} , set(_lowerCAmelCase ) ) # Score equivalence UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets=_lowerCAmelCase ) UpperCAmelCase_ =[top_mask["token_str"] for top_mask in outputs] UpperCAmelCase_ =[top_mask["score"] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_lowerCAmelCase ) == set(_lowerCAmelCase ): UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets=_lowerCAmelCase ) UpperCAmelCase_ =[top_mask["score"] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_lowerCAmelCase ) , nested_simplify(_lowerCAmelCase ) ) # Raises with invalid with self.assertRaises(_lowerCAmelCase ): UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_lowerCAmelCase ): UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets=[""] ) with self.assertRaises(_lowerCAmelCase ): UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , targets="" ) def lowerCAmelCase__ ( self: Dict , _lowerCAmelCase: Optional[int] , _lowerCAmelCase: List[str] ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase , top_k=2 ) UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , top_k=2 ) self.assertEqual( _lowerCAmelCase , [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ] , ) self.assertEqual(nested_simplify(_lowerCAmelCase ) , nested_simplify(_lowerCAmelCase ) ) def lowerCAmelCase__ ( self: Dict , _lowerCAmelCase: List[Any] , _lowerCAmelCase: Optional[int] ) -> List[str]: '''simple docstring''' UpperCAmelCase_ =tokenizer.get_vocab() UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) # top_k=2, ntargets=3 UpperCAmelCase_ =sorted(vocab.keys() )[:3] UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , top_k=2 , targets=_lowerCAmelCase ) # If we use the most probably targets, and filter differently, we should still # have the same results UpperCAmelCase_ =[el["token_str"] for el in sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x["score"] , reverse=_lowerCAmelCase )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_lowerCAmelCase ).issubset(_lowerCAmelCase ): UpperCAmelCase_ =fill_masker(F'This is a {tokenizer.mask_token}' , top_k=3 , targets=_lowerCAmelCase ) # They should yield exactly the same result self.assertEqual(nested_simplify(_lowerCAmelCase ) , nested_simplify(_lowerCAmelCase ) ) def lowerCAmelCase__ ( self: List[str] , _lowerCAmelCase: Dict , _lowerCAmelCase: List[str] ) -> Tuple: '''simple docstring''' UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) UpperCAmelCase_ =tokenizer.get_vocab() # String duplicates + id duplicates UpperCAmelCase_ =sorted(vocab.keys() )[:3] UpperCAmelCase_ =[targets[0], targets[1], targets[0], targets[2], targets[1]] UpperCAmelCase_ =fill_masker(F'My name is {tokenizer.mask_token}' , targets=_lowerCAmelCase , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_lowerCAmelCase ) , 3 ) def lowerCAmelCase__ ( self: List[str] , _lowerCAmelCase: Tuple , _lowerCAmelCase: Tuple ) -> str: '''simple docstring''' UpperCAmelCase_ =FillMaskPipeline(model=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) UpperCAmelCase_ =fill_masker( F'This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}' , top_k=2 ) self.assertEqual( _lowerCAmelCase , [ [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ], [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ], [ {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, {"sequence": ANY(_lowerCAmelCase ), "score": ANY(_lowerCAmelCase ), "token": ANY(_lowerCAmelCase ), "token_str": ANY(_lowerCAmelCase )}, ], ] , )
54
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Any = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Union[str, Any] = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : List[str] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = ['input_ids', 'attention_mask'] _lowercase = RobertaTokenizer def __init__( self : List[str] , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]="replace" , lowerCamelCase__ : List[str]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Any="</s>" , lowerCamelCase__ : Any="<s>" , lowerCamelCase__ : int="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Tuple="<mask>" , lowerCamelCase__ : Any=False , lowerCamelCase__ : Dict=True , **lowerCamelCase__ : Optional[Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Any = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : int = add_prefix_space a__ : Tuple = pre_tok_class(**lowerCamelCase__ ) a__ : str = add_prefix_space a__ : Tuple = "post_processor" a__ : Dict = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Tuple = tuple(state["sep"] ) if "cls" in state: a__ : str = tuple(state["cls"] ) a__ : str = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : str = add_prefix_space a__ : Any = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : int = trim_offsets a__ : Dict = True if changes_to_apply: a__ : Union[str, Any] = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : str = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : Tuple ): a__ : List[Any] = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : List[str] = value def _UpperCamelCase( self : Union[str, Any] , *lowerCamelCase__ : int , **lowerCamelCase__ : int ): a__ : Optional[int] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Tuple , *lowerCamelCase__ : Dict , **lowerCamelCase__ : List[str] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : int = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int]=None ): a__ : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Dict , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : Tuple = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0]
37
0
from numpy import exp, pi, sqrt def UpperCAmelCase ( a_ , a_ = 0.0 , a_ = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
55
from statistics import mean, stdev def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : List[str] = min(__a ) a__ : str = max(__a ) # normalize data return [round((x - x_min) / (x_max - x_min) , __a ) for x in data] def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : str = mean(__a ) a__ : List[str] = stdev(__a ) # standardize data return [round((x - mu) / (sigma) , __a ) for x in data]
37
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Optional[int] = { "configuration_nllb_moe": [ "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP", "NllbMoeConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : int = [ "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST", "NllbMoeForConditionalGeneration", "NllbMoeModel", "NllbMoePreTrainedModel", "NllbMoeTop2Router", "NllbMoeSparseMLP", ] if TYPE_CHECKING: from .configuration_nllb_moe import ( NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP, NllbMoeConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nllb_moe import ( NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST, NllbMoeForConditionalGeneration, NllbMoeModel, NllbMoePreTrainedModel, NllbMoeSparseMLP, NllbMoeTopaRouter, ) else: import sys _a : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
56
def UpperCamelCase_ ( __a = 50 ) -> int: a__ : Tuple = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
37
0
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def snake_case (UpperCAmelCase__ , UpperCAmelCase__="shi-labs/oneformer_demo" ) -> Any: with open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ , repo_type='dataset' ) , 'r' ) as f: UpperCamelCase_: Tuple = json.load(UpperCAmelCase__ ) UpperCamelCase_: Optional[int] = {} UpperCamelCase_: Dict = [] UpperCamelCase_: str = [] for key, info in class_info.items(): UpperCamelCase_: Optional[Any] = info['name'] class_names.append(info['name'] ) if info["isthing"]: thing_ids.append(int(UpperCAmelCase__ ) ) UpperCamelCase_: Tuple = thing_ids UpperCamelCase_: Optional[Any] = class_names return metadata class _lowerCAmelCase( unittest.TestCase ): """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase=7 , _lowerCamelCase=3 , _lowerCamelCase=3_0 , _lowerCamelCase=4_0_0 , _lowerCamelCase=None , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=[0.5, 0.5, 0.5] , _lowerCamelCase=[0.5, 0.5, 0.5] , _lowerCamelCase=1_0 , _lowerCamelCase=False , _lowerCamelCase=2_5_5 , _lowerCamelCase="shi-labs/oneformer_demo" , _lowerCamelCase="ade20k_panoptic.json" , _lowerCamelCase=1_0 , ): UpperCamelCase_: Union[str, Any] = parent UpperCamelCase_: Tuple = batch_size UpperCamelCase_: Tuple = num_channels UpperCamelCase_: Optional[Any] = min_resolution UpperCamelCase_: Any = max_resolution UpperCamelCase_: int = do_resize UpperCamelCase_: List[str] = {'shortest_edge': 3_2, 'longest_edge': 1_3_3_3} if size is None else size UpperCamelCase_: str = do_normalize UpperCamelCase_: str = image_mean UpperCamelCase_: Tuple = image_std UpperCamelCase_: Optional[Any] = class_info_file UpperCamelCase_: Optional[Any] = prepare_metadata(_lowerCamelCase , _lowerCamelCase ) UpperCamelCase_: str = num_text UpperCamelCase_: Tuple = repo_path # for the post_process_functions UpperCamelCase_: int = 2 UpperCamelCase_: List[str] = 1_0 UpperCamelCase_: List[Any] = 1_0 UpperCamelCase_: Optional[int] = 3 UpperCamelCase_: Dict = 4 UpperCamelCase_: str = num_labels UpperCamelCase_: List[str] = do_reduce_labels UpperCamelCase_: Tuple = ignore_index def _a ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def _a ( self , _lowerCamelCase , _lowerCamelCase=False ): if not batched: UpperCamelCase_: int = image_inputs[0] if isinstance(_lowerCamelCase , Image.Image ): UpperCamelCase_ ,UpperCamelCase_: str = image.size else: UpperCamelCase_ ,UpperCamelCase_: Any = image.shape[1], image.shape[2] if w < h: UpperCamelCase_: int = int(self.size['shortest_edge'] * h / w ) UpperCamelCase_: Dict = self.size['shortest_edge'] elif w > h: UpperCamelCase_: Tuple = self.size['shortest_edge'] UpperCamelCase_: Union[str, Any] = int(self.size['shortest_edge'] * w / h ) else: UpperCamelCase_: List[str] = self.size['shortest_edge'] UpperCamelCase_: Dict = self.size['shortest_edge'] else: UpperCamelCase_: Optional[int] = [] for image in image_inputs: UpperCamelCase_ ,UpperCamelCase_: Union[str, Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCamelCase_: List[Any] = max(_lowerCamelCase , key=lambda _lowerCamelCase : item[0] )[0] UpperCamelCase_: Tuple = max(_lowerCamelCase , key=lambda _lowerCamelCase : item[1] )[1] return expected_height, expected_width def _a ( self ): return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class _lowerCAmelCase( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" a : Any =OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string a : Any =image_processing_class def _a ( self ): UpperCamelCase_: Optional[int] = OneFormerImageProcessorTester(self ) @property def _a ( self ): return self.image_processing_tester.prepare_image_processor_dict() def _a ( self ): UpperCamelCase_: Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowerCamelCase , 'image_mean' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'image_std' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'do_normalize' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'do_resize' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'size' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'ignore_index' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'class_info_file' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'num_text' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'repo_path' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'metadata' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'do_reduce_labels' ) ) def _a ( self ): pass def _a ( self ): # Initialize image_processor UpperCamelCase_: Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase_: List[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , Image.Image ) # Test not batched input UpperCamelCase_: str = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_ ,UpperCamelCase_: Union[str, Any] = self.image_processing_tester.get_expected_values(_lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_ ,UpperCamelCase_: List[Any] = self.image_processing_tester.get_expected_values(_lowerCamelCase , batched=_lowerCamelCase ) UpperCamelCase_: str = image_processor( _lowerCamelCase , ['semantic'] * len(_lowerCamelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def _a ( self ): # Initialize image_processor UpperCamelCase_: Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase_: Optional[int] = prepare_image_inputs(self.image_processing_tester , equal_resolution=_lowerCamelCase , numpify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , np.ndarray ) # Test not batched input UpperCamelCase_: int = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_ ,UpperCamelCase_: Optional[Any] = self.image_processing_tester.get_expected_values(_lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_ ,UpperCamelCase_: str = self.image_processing_tester.get_expected_values(_lowerCamelCase , batched=_lowerCamelCase ) UpperCamelCase_: int = image_processor( _lowerCamelCase , ['semantic'] * len(_lowerCamelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def _a ( self ): # Initialize image_processor UpperCamelCase_: Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase_: Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=_lowerCamelCase , torchify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , torch.Tensor ) # Test not batched input UpperCamelCase_: Union[str, Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_ ,UpperCamelCase_: int = self.image_processing_tester.get_expected_values(_lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_ ,UpperCamelCase_: str = self.image_processing_tester.get_expected_values(_lowerCamelCase , batched=_lowerCamelCase ) UpperCamelCase_: str = image_processor( _lowerCamelCase , ['semantic'] * len(_lowerCamelCase ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def _a ( self , _lowerCamelCase=False , _lowerCamelCase=False , _lowerCamelCase="np" ): UpperCamelCase_: List[str] = self.image_processing_class(**self.image_processor_dict ) # prepare image and target UpperCamelCase_: List[str] = self.image_processing_tester.num_labels UpperCamelCase_: Union[str, Any] = None UpperCamelCase_: Optional[int] = None UpperCamelCase_: Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=_lowerCamelCase ) if with_segmentation_maps: UpperCamelCase_: Dict = num_labels if is_instance_map: UpperCamelCase_: List[Any] = list(range(_lowerCamelCase ) ) * 2 UpperCamelCase_: Dict = dict(enumerate(_lowerCamelCase ) ) UpperCamelCase_: Dict = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": UpperCamelCase_: Dict = [Image.fromarray(_lowerCamelCase ) for annotation in annotations] UpperCamelCase_: Dict = image_processor( _lowerCamelCase , ['semantic'] * len(_lowerCamelCase ) , _lowerCamelCase , return_tensors='pt' , instance_id_to_semantic_id=_lowerCamelCase , pad_and_return_pixel_mask=_lowerCamelCase , ) return inputs def _a ( self ): pass def _a ( self ): def common(_lowerCamelCase=False , _lowerCamelCase=None ): UpperCamelCase_: Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=_lowerCamelCase , is_instance_map=_lowerCamelCase , segmentation_type=_lowerCamelCase ) UpperCamelCase_: str = inputs['mask_labels'] UpperCamelCase_: Any = inputs['class_labels'] UpperCamelCase_: List[str] = inputs['pixel_values'] UpperCamelCase_: Optional[int] = inputs['text_inputs'] # check the batch_size for mask_label, class_label, text_input in zip(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(_lowerCamelCase ) , self.image_processing_tester.num_text ) common() common(is_instance_map=_lowerCamelCase ) common(is_instance_map=_lowerCamelCase , segmentation_type='pil' ) common(is_instance_map=_lowerCamelCase , segmentation_type='pil' ) def _a ( self ): UpperCamelCase_: List[Any] = np.zeros((2_0, 5_0) ) UpperCamelCase_: int = 1 UpperCamelCase_: List[Any] = 1 UpperCamelCase_: Any = 1 UpperCamelCase_: int = binary_mask_to_rle(_lowerCamelCase ) self.assertEqual(len(_lowerCamelCase ) , 4 ) self.assertEqual(rle[0] , 2_1 ) self.assertEqual(rle[1] , 4_5 ) def _a ( self ): UpperCamelCase_: Any = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_: str = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_: Any = fature_extractor.post_process_semantic_segmentation(_lowerCamelCase ) self.assertEqual(len(_lowerCamelCase ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) UpperCamelCase_: Dict = [(1, 4) for i in range(self.image_processing_tester.batch_size )] UpperCamelCase_: List[Any] = fature_extractor.post_process_semantic_segmentation(_lowerCamelCase , target_sizes=_lowerCamelCase ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def _a ( self ): UpperCamelCase_: Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_: List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_: Union[str, Any] = image_processor.post_process_instance_segmentation(_lowerCamelCase , threshold=0 ) self.assertTrue(len(_lowerCamelCase ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , _lowerCamelCase ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def _a ( self ): UpperCamelCase_: List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_: List[str] = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_: Optional[int] = image_processor.post_process_panoptic_segmentation(_lowerCamelCase , threshold=0 ) self.assertTrue(len(_lowerCamelCase ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , _lowerCamelCase ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
57
class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] ): a__ : str = name a__ : Optional[int] = value a__ : Dict = weight def __repr__( self : Union[str, Any] ): return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def _UpperCamelCase( self : Dict ): return self.value def _UpperCamelCase( self : Optional[Any] ): return self.name def _UpperCamelCase( self : Optional[Any] ): return self.weight def _UpperCamelCase( self : Optional[int] ): return self.value / self.weight def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = [] for i in range(len(__a ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def UpperCamelCase_ ( __a , __a , __a ) -> Union[str, Any]: a__ : List[str] = sorted(__a , key=__a , reverse=__a ) a__ : List[Any] = [] a__, a__ : Union[str, Any] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def UpperCamelCase_ ( ) -> Union[str, Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
37
0
"""simple docstring""" import pprint import requests __lowerCAmelCase : Optional[Any] = '''https://zenquotes.io/api''' def __lowerCAmelCase ( ): '''simple docstring''' return requests.get(API_ENDPOINT_URL + """/today""" ).json() def __lowerCAmelCase ( ): '''simple docstring''' return requests.get(API_ENDPOINT_URL + """/random""" ).json() if __name__ == "__main__": __lowerCAmelCase : Dict = random_quotes() pprint.pprint(response)
58
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class A__ ( A__ ): """simple docstring""" def __init__( self : Dict , lowerCamelCase__ : Union[str, "sqlalchemy.sql.Selectable"] , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[Features] = None , lowerCamelCase__ : str = None , lowerCamelCase__ : bool = False , **lowerCamelCase__ : Optional[int] , ): super().__init__(features=lowerCamelCase__ , cache_dir=lowerCamelCase__ , keep_in_memory=lowerCamelCase__ , **lowerCamelCase__ ) a__ : str = Sql( cache_dir=lowerCamelCase__ , features=lowerCamelCase__ , sql=lowerCamelCase__ , con=lowerCamelCase__ , **lowerCamelCase__ , ) def _UpperCamelCase( self : Tuple ): a__ : Optional[Any] = None a__ : Dict = None a__ : Union[str, Any] = None a__ : Union[str, Any] = None self.builder.download_and_prepare( download_config=lowerCamelCase__ , download_mode=lowerCamelCase__ , verification_mode=lowerCamelCase__ , base_path=lowerCamelCase__ , ) # Build dataset for splits a__ : List[str] = self.builder.as_dataset( split="train" , verification_mode=lowerCamelCase__ , in_memory=self.keep_in_memory ) return dataset class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : Dataset , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[int] = None , **lowerCamelCase__ : Optional[Any] , ): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) a__ : Any = dataset a__ : str = name a__ : Tuple = con a__ : List[Any] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a__ : Any = num_proc a__ : Tuple = to_sql_kwargs def _UpperCamelCase( self : List[Any] ): a__ : Any = self.to_sql_kwargs.pop("sql" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("con" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("index" , lowerCamelCase__ ) a__ : int = self._write(index=lowerCamelCase__ , **self.to_sql_kwargs ) return written def _UpperCamelCase( self : Any , lowerCamelCase__ : List[str] ): a__, a__, a__ : Union[str, Any] = args a__ : Any = {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs a__ : Tuple = query_table( table=self.dataset.data , key=slice(lowerCamelCase__ , offset + self.batch_size ) , indices=self.dataset._indices , ) a__ : str = batch.to_pandas() a__ : List[Any] = df.to_sql(self.name , self.con , index=lowerCamelCase__ , **lowerCamelCase__ ) return num_rows or len(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] , lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : str = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: a__, a__ : List[str] = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , lowerCamelCase__ , lowerCamelCase__ )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += num_rows return written
37
0
import argparse from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase_ ( __a , __a , __a ) -> List[Any]: """simple docstring""" lowerCamelCase__: Any =TaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) lowerCamelCase__: Union[str, Any] =TaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tf_weights_in_ta(__a , __a , __a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) if __name__ == "__main__": __A = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __A = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
59
import math from datetime import datetime, timedelta def UpperCamelCase_ ( __a ) -> datetime: a__ : Union[str, Any] = year % 19 a__ : List[str] = year % 4 a__ : str = year % 7 a__ : Any = math.floor(year / 100 ) a__ : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) a__ : Optional[int] = leap_day_inhibits / 4 a__ : Union[str, Any] = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 a__ : Dict = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 a__ : Any = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon a__ : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 18 ) else: return datetime(__a , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): UpperCamelCase : Tuple = """will be""" if year > datetime.now().year else """was""" print(f"""Easter in {year} {tense} {gauss_easter(year)}""")
37
0
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa lowerCAmelCase_ = logging.getLogger(__name__) class __lowerCAmelCase ( _a ): lowerCamelCase_ : int = '''summarization''' lowerCamelCase_ : int = ['''loss'''] lowerCamelCase_ : List[str] = ROUGE_KEYS lowerCamelCase_ : Tuple = '''rouge2''' def __init__(self , __magic_name__ , **__magic_name__ ) -> Dict: '''simple docstring''' if hparams.sortish_sampler and hparams.gpus > 1: snake_case_ : List[Any] = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError('''Dynamic Batch size does not work for multi-gpu training''' ) if hparams.sortish_sampler: raise ValueError('''--sortish_sampler and --max_tokens_per_batch may not be used simultaneously''' ) super().__init__(__magic_name__ , num_labels=__magic_name__ , mode=self.mode , **__magic_name__ ) use_task_specific_params(self.model , '''summarization''' ) save_git_info(self.hparams.output_dir ) snake_case_ : Union[str, Any] = Path(self.output_dir ) / '''metrics.json''' snake_case_ : Union[str, Any] = Path(self.output_dir ) / '''hparams.pkl''' pickle_save(self.hparams , self.hparams_save_path ) snake_case_ : Any = 0 snake_case_ : Any = defaultdict(__magic_name__ ) snake_case_ : List[Any] = self.config.model_type snake_case_ : str = self.config.tgt_vocab_size if self.model_type == '''fsmt''' else self.config.vocab_size snake_case_ : dict = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } snake_case_ : List[Any] = { '''train''': self.hparams.n_train, '''val''': self.hparams.n_val, '''test''': self.hparams.n_test, } snake_case_ : str = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} snake_case_ : Dict = { '''train''': self.hparams.max_target_length, '''val''': self.hparams.val_max_target_length, '''test''': self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], F'''target_lens: {self.target_lens}''' assert self.target_lens["train"] <= self.target_lens["test"], F'''target_lens: {self.target_lens}''' if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) snake_case_ : Tuple = get_git_info()['''repo_sha'''] snake_case_ : str = hparams.num_workers snake_case_ : List[Any] = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , __magic_name__ ): snake_case_ : str = self.tokenizer.lang_code_to_id[hparams.tgt_lang] snake_case_ : Any = self.decoder_start_token_id snake_case_ : Dict = ( SeqaSeqDataset if hasattr(self.tokenizer , '''prepare_seq2seq_batch''' ) else LegacySeqaSeqDataset ) snake_case_ : str = False snake_case_ : List[Any] = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: snake_case_ : Any = self.hparams.eval_max_gen_length else: snake_case_ : Optional[Any] = self.model.config.max_length snake_case_ : Union[str, Any] = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def lowerCamelCase (self , __magic_name__ ) -> Dict[str, List[str]]: '''simple docstring''' snake_case_ : Tuple = { k: self.tokenizer.batch_decode(v.tolist() ) if '''mask''' not in k else v.shape for k, v in batch.items() } save_json(__magic_name__ , Path(self.output_dir ) / '''text_batch.json''' ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / '''tok_batch.json''' ) snake_case_ : int = True return readable_batch def lowerCamelCase (self , __magic_name__ , **__magic_name__ ) -> Tuple: '''simple docstring''' return self.model(__magic_name__ , **__magic_name__ ) def lowerCamelCase (self , __magic_name__ ) -> Any: '''simple docstring''' snake_case_ : Union[str, Any] = self.tokenizer.batch_decode( __magic_name__ , skip_special_tokens=__magic_name__ , clean_up_tokenization_spaces=__magic_name__ ) return lmap(str.strip , __magic_name__ ) def lowerCamelCase (self , __magic_name__ ) -> Tuple: '''simple docstring''' snake_case_ : int = self.tokenizer.pad_token_id snake_case_ , snake_case_ : Tuple = batch['''input_ids'''], batch['''attention_mask'''] snake_case_ : Dict = batch['''labels'''] if isinstance(self.model , __magic_name__ ): snake_case_ : Tuple = self.model._shift_right(__magic_name__ ) else: snake_case_ : Any = shift_tokens_right(__magic_name__ , __magic_name__ ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero snake_case_ : Union[str, Any] = decoder_input_ids self.save_readable_batch(__magic_name__ ) snake_case_ : List[Any] = self(__magic_name__ , attention_mask=__magic_name__ , decoder_input_ids=__magic_name__ , use_cache=__magic_name__ ) snake_case_ : List[Any] = outputs['''logits'''] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id snake_case_ : Dict = nn.CrossEntropyLoss(ignore_index=__magic_name__ ) assert lm_logits.shape[-1] == self.vocab_size snake_case_ : List[str] = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: snake_case_ : Union[str, Any] = nn.functional.log_softmax(__magic_name__ , dim=-1 ) snake_case_ , snake_case_ : List[Any] = label_smoothed_nll_loss( __magic_name__ , __magic_name__ , self.hparams.label_smoothing , ignore_index=__magic_name__ ) return (loss,) @property def lowerCamelCase (self ) -> int: '''simple docstring''' return self.tokenizer.pad_token_id def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> Dict: '''simple docstring''' snake_case_ : Any = self._step(__magic_name__ ) snake_case_ : Union[str, Any] = dict(zip(self.loss_names , __magic_name__ ) ) # tokens per batch snake_case_ : Union[str, Any] = batch['''input_ids'''].ne(self.pad ).sum() + batch['''labels'''].ne(self.pad ).sum() snake_case_ : Union[str, Any] = batch['''input_ids'''].shape[0] snake_case_ : Any = batch['''input_ids'''].eq(self.pad ).sum() snake_case_ : Optional[int] = batch['''input_ids'''].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> Dict: '''simple docstring''' return self._generative_step(__magic_name__ ) def lowerCamelCase (self , __magic_name__ , __magic_name__="val" ) -> Dict: '''simple docstring''' self.step_count += 1 snake_case_ : str = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} snake_case_ : Dict = losses['''loss'''] snake_case_ : int = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ['''gen_time''', '''gen_len'''] } snake_case_ : Tuple = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) snake_case_ : torch.FloatTensor = torch.tensor(__magic_name__ ).type_as(__magic_name__ ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(__magic_name__ ) snake_case_ : int = {F'''{prefix}_avg_{k}''': x for k, x in losses.items()} snake_case_ : Union[str, Any] = self.step_count self.metrics[prefix].append(__magic_name__ ) # callback writes this to self.metrics_save_path snake_case_ : Union[str, Any] = flatten_list([x['''preds'''] for x in outputs] ) return { "log": all_metrics, "preds": preds, F'''{prefix}_loss''': loss, F'''{prefix}_{self.val_metric}''': metric_tensor, } def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> Dict: '''simple docstring''' return calculate_rouge(__magic_name__ , __magic_name__ ) def lowerCamelCase (self , __magic_name__ ) -> dict: '''simple docstring''' snake_case_ : int = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') snake_case_ : List[Any] = self.model.generate( batch['''input_ids'''] , attention_mask=batch['''attention_mask'''] , use_cache=__magic_name__ , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) snake_case_ : str = (time.time() - ta) / batch['''input_ids'''].shape[0] snake_case_ : List[str] = self.ids_to_clean_text(__magic_name__ ) snake_case_ : List[str] = self.ids_to_clean_text(batch['''labels'''] ) snake_case_ : Any = self._step(__magic_name__ ) snake_case_ : List[str] = dict(zip(self.loss_names , __magic_name__ ) ) snake_case_ : Dict = self.calc_generative_metrics(__magic_name__ , __magic_name__ ) snake_case_ : Any = np.mean(lmap(__magic_name__ , __magic_name__ ) ) base_metrics.update(gen_time=__magic_name__ , gen_len=__magic_name__ , preds=__magic_name__ , target=__magic_name__ , **__magic_name__ ) return base_metrics def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> Optional[int]: '''simple docstring''' return self._generative_step(__magic_name__ ) def lowerCamelCase (self , __magic_name__ ) -> Optional[Any]: '''simple docstring''' return self.validation_epoch_end(__magic_name__ , prefix='''test''' ) def lowerCamelCase (self , __magic_name__ ) -> SeqaSeqDataset: '''simple docstring''' snake_case_ : Dict = self.n_obs[type_path] snake_case_ : Tuple = self.target_lens[type_path] snake_case_ : Tuple = self.dataset_class( self.tokenizer , type_path=__magic_name__ , n_obs=__magic_name__ , max_target_length=__magic_name__ , **self.dataset_kwargs , ) return dataset def lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ = False ) -> DataLoader: '''simple docstring''' snake_case_ : Tuple = self.get_dataset(__magic_name__ ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": snake_case_ : Union[str, Any] = dataset.make_sortish_sampler(__magic_name__ , distributed=self.hparams.gpus > 1 ) return DataLoader( __magic_name__ , batch_size=__magic_name__ , collate_fn=dataset.collate_fn , shuffle=__magic_name__ , num_workers=self.num_workers , sampler=__magic_name__ , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": snake_case_ : Tuple = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( __magic_name__ , batch_sampler=__magic_name__ , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( __magic_name__ , batch_size=__magic_name__ , collate_fn=dataset.collate_fn , shuffle=__magic_name__ , num_workers=self.num_workers , sampler=__magic_name__ , ) def lowerCamelCase (self ) -> DataLoader: '''simple docstring''' snake_case_ : List[str] = self.get_dataloader('''train''' , batch_size=self.hparams.train_batch_size , shuffle=__magic_name__ ) return dataloader def lowerCamelCase (self ) -> DataLoader: '''simple docstring''' return self.get_dataloader('''val''' , batch_size=self.hparams.eval_batch_size ) def lowerCamelCase (self ) -> DataLoader: '''simple docstring''' return self.get_dataloader('''test''' , batch_size=self.hparams.eval_batch_size ) @staticmethod def lowerCamelCase (__magic_name__ , __magic_name__ ) -> Optional[Any]: '''simple docstring''' BaseTransformer.add_model_specific_args(__magic_name__ , __magic_name__ ) add_generic_args(__magic_name__ , __magic_name__ ) parser.add_argument( '''--max_source_length''' , default=1024 , type=__magic_name__ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--max_target_length''' , default=56 , type=__magic_name__ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--val_max_target_length''' , default=142 , type=__magic_name__ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--test_max_target_length''' , default=142 , type=__magic_name__ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument('''--freeze_encoder''' , action='''store_true''' ) parser.add_argument('''--freeze_embeds''' , action='''store_true''' ) parser.add_argument('''--sortish_sampler''' , action='''store_true''' , default=__magic_name__ ) parser.add_argument('''--overwrite_output_dir''' , action='''store_true''' , default=__magic_name__ ) parser.add_argument('''--max_tokens_per_batch''' , type=__magic_name__ , default=__magic_name__ ) parser.add_argument('''--logger_name''' , type=__magic_name__ , choices=['''default''', '''wandb''', '''wandb_shared'''] , default='''default''' ) parser.add_argument('''--n_train''' , type=__magic_name__ , default=-1 , required=__magic_name__ , help='''# examples. -1 means use all.''' ) parser.add_argument('''--n_val''' , type=__magic_name__ , default=500 , required=__magic_name__ , help='''# examples. -1 means use all.''' ) parser.add_argument('''--n_test''' , type=__magic_name__ , default=-1 , required=__magic_name__ , help='''# examples. -1 means use all.''' ) parser.add_argument( '''--task''' , type=__magic_name__ , default='''summarization''' , required=__magic_name__ , help='''# examples. -1 means use all.''' ) parser.add_argument('''--label_smoothing''' , type=__magic_name__ , default=0.0 , required=__magic_name__ ) parser.add_argument('''--src_lang''' , type=__magic_name__ , default='''''' , required=__magic_name__ ) parser.add_argument('''--tgt_lang''' , type=__magic_name__ , default='''''' , required=__magic_name__ ) parser.add_argument('''--eval_beams''' , type=__magic_name__ , default=__magic_name__ , required=__magic_name__ ) parser.add_argument( '''--val_metric''' , type=__magic_name__ , default=__magic_name__ , required=__magic_name__ , choices=['''bleu''', '''rouge2''', '''loss''', None] ) parser.add_argument('''--eval_max_gen_length''' , type=__magic_name__ , default=__magic_name__ , help='''never generate more than n tokens''' ) parser.add_argument('''--save_top_k''' , type=__magic_name__ , default=1 , required=__magic_name__ , help='''How many checkpoints to save''' ) parser.add_argument( '''--early_stopping_patience''' , type=__magic_name__ , default=-1 , required=__magic_name__ , help=( '''-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So''' ''' val_check_interval will effect it.''' ) , ) return parser class __lowerCAmelCase ( _a ): lowerCamelCase_ : Dict = '''translation''' lowerCamelCase_ : List[str] = ['''loss'''] lowerCamelCase_ : Any = ['''bleu'''] lowerCamelCase_ : Dict = '''bleu''' def __init__(self , __magic_name__ , **__magic_name__ ) -> List[str]: '''simple docstring''' super().__init__(__magic_name__ , **__magic_name__ ) snake_case_ : Union[str, Any] = hparams.src_lang snake_case_ : Optional[int] = hparams.tgt_lang def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> dict: '''simple docstring''' return calculate_bleu(__magic_name__ , __magic_name__ ) def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase=None ) -> SummarizationModule: """simple docstring""" Path(args.output_dir ).mkdir(exist_ok=_UpperCamelCase ) check_output_dir(_UpperCamelCase , expected_items=3 ) if model is None: if "summarization" in args.task: snake_case_ : SummarizationModule = SummarizationModule(_UpperCamelCase ) else: snake_case_ : SummarizationModule = TranslationModule(_UpperCamelCase ) snake_case_ : int = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith('''/tmp''' ) or str(args.output_dir ).startswith('''/var''' ) ): snake_case_ : List[str] = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger snake_case_ : Dict = os.environ.get('''WANDB_PROJECT''' , _UpperCamelCase ) snake_case_ : str = WandbLogger(name=model.output_dir.name , project=_UpperCamelCase ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger snake_case_ : str = WandbLogger(name=model.output_dir.name , project=f'''hf_{dataset}''' ) if args.early_stopping_patience >= 0: snake_case_ : List[str] = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: snake_case_ : Tuple = False snake_case_ : List[str] = args.val_metric == '''loss''' snake_case_ : pl.Trainer = generic_train( _UpperCamelCase , _UpperCamelCase , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , _UpperCamelCase ) , early_stopping_callback=_UpperCamelCase , logger=_UpperCamelCase , ) pickle_save(model.hparams , model.output_dir / '''hparams.pkl''' ) if not args.do_predict: return model snake_case_ : List[Any] = '''''' snake_case_ : Union[str, Any] = sorted(glob.glob(os.path.join(args.output_dir , '''*.ckpt''' ) , recursive=_UpperCamelCase ) ) if checkpoints: snake_case_ : int = checkpoints[-1] snake_case_ : Tuple = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() lowerCAmelCase_ = pl.Trainer.add_argparse_args(parser) lowerCAmelCase_ = SummarizationModule.add_model_specific_args(parser, os.getcwd()) lowerCAmelCase_ = parser.parse_args() main(args)
60
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def UpperCamelCase_ ( __a ) -> Union[str, Any]: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class A__ ( nn.Module ): """simple docstring""" def __init__( self : List[str] , lowerCamelCase__ : nn.Module , lowerCamelCase__ : int ): super().__init__() a__ : int = module a__ : Any = nn.Sequential( nn.Linear(module.in_features , lowerCamelCase__ , bias=lowerCamelCase__ ) , nn.Linear(lowerCamelCase__ , module.out_features , bias=lowerCamelCase__ ) , ) a__ : Tuple = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=lowerCamelCase__ ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Optional[int] , *lowerCamelCase__ : int , **lowerCamelCase__ : Dict ): return self.module(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) + self.adapter(lowerCamelCase__ ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" _lowercase = 'bigscience/bloom-1b7' # Constant values _lowercase = 2.1_09_65_95_52_69_25_74 _lowercase = 'Hello my name is' _lowercase = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) _lowercase = 1_0 def _UpperCamelCase( self : Dict ): # Models and tokenizer a__ : List[str] = AutoTokenizer.from_pretrained(self.model_name ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Union[str, Any] ): super().setUp() # Models and tokenizer a__ : List[Any] = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map="auto" ) a__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : List[Any] ): a__ : str = self.model_abit.config self.assertTrue(hasattr(lowerCamelCase__ , "quantization_config" ) ) a__ : Optional[Any] = config.to_dict() a__ : int = config.to_diff_dict() a__ : List[str] = config.to_json_string() def _UpperCamelCase( self : int ): from bitsandbytes.nn import Paramsabit a__ : List[Any] = self.model_fpaa.get_memory_footprint() a__ : str = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) a__ : Optional[Any] = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def _UpperCamelCase( self : Tuple ): from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(lowerCamelCase__ , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def _UpperCamelCase( self : str ): a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Tuple = self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[Any] = BitsAndBytesConfig() a__ : Optional[int] = True a__ : int = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , device_map="auto" ) a__ : str = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : int = model_abit_from_config.generate( input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : Dict ): with self.assertRaises(lowerCamelCase__ ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] ): a__ : int = BitsAndBytesConfig() with self.assertRaises(lowerCamelCase__ ): a__ : Dict = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , load_in_abit=lowerCamelCase__ , device_map="auto" , bnb_abit_quant_type="nf4" , ) def _UpperCamelCase( self : int ): with self.assertRaises(lowerCamelCase__ ): # Tries with `str` self.model_abit.to("cpu" ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.to(torch.device("cuda:0" ) ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.float() with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything a__ : int = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Any = self.model_fpaa.to(torch.floataa ) a__ : List[Any] = self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.to("cpu" ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.half() # Check this does not throw an error a__ : Dict = self.model_fpaa.float() def _UpperCamelCase( self : Dict ): a__ : List[str] = AutoModelForSeqaSeqLM.from_pretrained("t5-small" , load_in_abit=lowerCamelCase__ , device_map="auto" ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" @classmethod def _UpperCamelCase( cls : str ): a__ : Dict = "t5-small" a__ : List[Any] = "google/flan-t5-small" # flan-t5 uses dense-act instead of dense-relu-dense a__ : int = AutoTokenizer.from_pretrained(cls.model_name ) a__ : str = "Translate in German: Hello, my dog is cute" def _UpperCamelCase( self : Optional[int] ): gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Optional[int] ): from transformers import TaForConditionalGeneration a__ : List[Any] = TaForConditionalGeneration._keep_in_fpaa_modules a__ : Optional[Any] = None # test with `t5-small` a__ : Any = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : Dict = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Any = model.generate(**lowerCamelCase__ ) a__ : Union[str, Any] = modules def _UpperCamelCase( self : List[Any] ): import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` a__ : List[str] = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) a__ : Union[str, Any] = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : int = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Any = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Optional[int] = model.generate(**lowerCamelCase__ ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : List[str] ): super().setUp() # model_name a__ : Union[str, Any] = "bigscience/bloom-560m" a__ : Union[str, Any] = "t5-small" # Different types of model a__ : int = AutoModel.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Sequence classification model a__ : Dict = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # CausalLM model a__ : str = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Seq2seq model a__ : Dict = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Union[str, Any] ): from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): super().setUp() def _UpperCamelCase( self : int ): del self.pipe gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Tuple ): a__ : int = pipeline( "text-generation" , model=self.model_name , model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass a__ : Tuple = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]["generated_text"] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Tuple ): super().setUp() def _UpperCamelCase( self : List[Any] ): a__ : str = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="balanced" ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model a__ : List[Any] = self.tokenizer(self.input_text , return_tensors="pt" ) # Second real batch a__ : List[Any] = model_parallel.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): a__ : Any = "facebook/opt-350m" super().setUp() def _UpperCamelCase( self : int ): if version.parse(importlib.metadata.version("bitsandbytes" ) ) < version.parse("0.37.0" ): return # Step 1: freeze all parameters a__ : Tuple = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): a__ : Any = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability a__ : Tuple = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(lowerCamelCase__ ) ): a__ : Dict = LoRALayer(module.q_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.k_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch a__ : Dict = self.tokenizer("Test batch " , return_tensors="pt" ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): a__ : Optional[Any] = model.forward(**lowerCamelCase__ ) out.logits.norm().backward() for module in model.modules(): if isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(lowerCamelCase__ , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class A__ ( A__ ): """simple docstring""" _lowercase = 'gpt2-xl' _lowercase = 3.31_91_85_48_54_15_21_87
37
0
import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def _A ( lowerCAmelCase_ : int , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Optional[int] ): """simple docstring""" lowerCAmelCase__ = RemBertConfig.from_json_file(lowerCAmelCase_ ) print("Building PyTorch model from configuration: {}".format(str(lowerCAmelCase_ ) ) ) lowerCAmelCase__ = RemBertModel(lowerCAmelCase_ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # Save pytorch-model print("Save PyTorch model to {}".format(lowerCAmelCase_ ) ) torch.save(model.state_dict() , lowerCAmelCase_ ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--rembert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained RemBERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) UpperCamelCase = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
61
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Optional[int]=100 , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[int]=30 , lowerCamelCase__ : Union[str, Any]=2 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Tuple=True , lowerCamelCase__ : int=32 , lowerCamelCase__ : Union[str, Any]=4 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Union[str, Any]=37 , lowerCamelCase__ : List[Any]="gelu" , lowerCamelCase__ : List[Any]=0.1 , lowerCamelCase__ : int=0.1 , lowerCamelCase__ : Union[str, Any]=10 , lowerCamelCase__ : str=0.02 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]=[0, 1, 2, 3] , ): a__ : Dict = parent a__ : Dict = 100 a__ : Optional[int] = batch_size a__ : Union[str, Any] = image_size a__ : Any = patch_size a__ : Optional[Any] = num_channels a__ : int = is_training a__ : List[str] = use_labels a__ : Optional[Any] = hidden_size a__ : List[Any] = num_hidden_layers a__ : str = num_attention_heads a__ : str = intermediate_size a__ : int = hidden_act a__ : List[Any] = hidden_dropout_prob a__ : Dict = attention_probs_dropout_prob a__ : Union[str, Any] = type_sequence_label_size a__ : Optional[Any] = initializer_range a__ : List[str] = scope a__ : int = out_indices a__ : List[str] = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a__ : Optional[int] = (image_size // patch_size) ** 2 a__ : Union[str, Any] = num_patches + 1 def _UpperCamelCase( self : int ): a__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a__ : Optional[Any] = None a__ : Tuple = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) a__ : Optional[int] = self.get_config() return config, pixel_values, labels, pixel_labels def _UpperCamelCase( self : Tuple ): return BeitConfig( vocab_size=self.vocab_size , 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=lowerCamelCase__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any ): a__ : str = BeitModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : List[str] , lowerCamelCase__ : Tuple ): a__ : int = BeitForMaskedImageModeling(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _UpperCamelCase( self : str , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Any ): a__ : List[str] = self.type_sequence_label_size a__ : Optional[Any] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a__ : Optional[Any] = 1 a__ : List[str] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a__ : Union[str, Any] = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _UpperCamelCase( self : Any , lowerCamelCase__ : str , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): a__ : int = self.num_labels a__ : List[str] = BeitForSemanticSegmentation(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Tuple = model(lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def _UpperCamelCase( self : Optional[int] ): a__ : Any = self.prepare_config_and_inputs() a__, a__, a__, a__ : Union[str, Any] = config_and_inputs a__ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) _lowercase = ( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Any ): a__ : int = BeitModelTester(self ) a__ : Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def _UpperCamelCase( self : str ): pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _UpperCamelCase( self : Dict ): pass def _UpperCamelCase( self : Optional[Any] ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : List[str] = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : str ): a__, a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : int = model_class(lowerCamelCase__ ) a__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def _UpperCamelCase( self : int ): a__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] ): a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): if not self.model_tester.is_training: return a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : str = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling]: continue a__ : List[str] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.train() a__ : Any = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : Tuple = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : Tuple ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return a__ : List[Any] = False a__ : List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue a__ : Optional[Any] = model_class(lowerCamelCase__ ) model.gradient_checkpointing_enable() model.to(lowerCamelCase__ ) model.train() a__ : Union[str, Any] = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : int = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : List[str] ): a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : Dict = _config_zero_init(lowerCamelCase__ ) for model_class in self.all_model_classes: a__ : str = model_class(config=lowerCamelCase__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _UpperCamelCase( self : Optional[int] ): for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Tuple = BeitModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : List[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 _UpperCamelCase( self : Optional[int] ): return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def _UpperCamelCase( self : str ): a__ : int = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(lowerCamelCase__ ) a__ : Optional[Any] = self.default_image_processor a__ : Dict = prepare_img() a__ : Optional[int] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).pixel_values.to(lowerCamelCase__ ) # prepare bool_masked_pos a__ : Optional[Any] = torch.ones((1, 196) , dtype=torch.bool ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(pixel_values=lowerCamelCase__ , bool_masked_pos=lowerCamelCase__ ) a__ : Tuple = outputs.logits # verify the logits a__ : List[str] = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[int] = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , lowerCamelCase__ , atol=1E-2 ) ) @slow def _UpperCamelCase( self : Dict ): a__ : str = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(lowerCamelCase__ ) a__ : int = self.default_image_processor a__ : List[Any] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Union[str, Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Union[str, Any] = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Tuple = 281 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : Any ): a__ : Dict = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( lowerCamelCase__ ) a__ : str = self.default_image_processor a__ : List[str] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Dict = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Optional[int] = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[Any] = torch.tensor([1.6881, -0.2787, 0.5901] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Optional[Any] = 2_396 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): a__ : Optional[Any] = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : Tuple = model.to(lowerCamelCase__ ) a__ : List[Any] = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Tuple = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : Union[str, Any] = Image.open(ds[0]["file"] ) a__ : List[Any] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Optional[Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Tuple = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: a__ : Dict = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=lowerCamelCase__ , ) else: a__ : Dict = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=lowerCamelCase__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , lowerCamelCase__ , atol=1E-4 ) ) @slow def _UpperCamelCase( self : Tuple ): a__ : str = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : List[Any] = model.to(lowerCamelCase__ ) a__ : int = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Optional[int] = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : str = Image.open(ds[0]["file"] ) a__ : str = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : List[Any] = model(**lowerCamelCase__ ) a__ : Any = outputs.logits.detach().cpu() a__ : List[Any] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ , target_sizes=[(500, 300)] ) a__ : Optional[int] = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ ) a__ : List[str] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ ) a__ : Any = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ )
37
0
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : int=2 , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Dict=10 , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : int=32 * 4 , UpperCAmelCase_ : str=32 * 6 , UpperCAmelCase_ : Optional[Any]=4 , UpperCAmelCase_ : Tuple=32 , ): SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[Any] = is_training SCREAMING_SNAKE_CASE : Union[str, Any] = use_auxiliary_loss SCREAMING_SNAKE_CASE : Tuple = num_queries SCREAMING_SNAKE_CASE : Optional[int] = num_channels SCREAMING_SNAKE_CASE : Any = min_size SCREAMING_SNAKE_CASE : Union[str, Any] = max_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_labels SCREAMING_SNAKE_CASE : Tuple = mask_feature_size def _A ( self : Dict ): SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = torch.ones([self.batch_size, self.min_size, self.max_size] , device=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=UpperCAmelCase_ ) > 0.5 ).float() SCREAMING_SNAKE_CASE : Optional[Any] = (torch.rand((self.batch_size, self.num_labels) , device=UpperCAmelCase_ ) > 0.5).long() SCREAMING_SNAKE_CASE : List[str] = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def _A ( self : Any ): return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=128 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def _A ( self : List[str] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE : Dict = {"pixel_values": pixel_values, "pixel_mask": pixel_mask} return config, inputs_dict def _A ( self : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] ): SCREAMING_SNAKE_CASE : Optional[int] = output.encoder_hidden_states SCREAMING_SNAKE_CASE : List[Any] = output.pixel_decoder_hidden_states SCREAMING_SNAKE_CASE : str = output.transformer_decoder_hidden_states self.parent.assertTrue(len(UpperCAmelCase_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCAmelCase_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCAmelCase_ ) , config.decoder_config.decoder_layers ) def _A ( self : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str]=False ): with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = MaskFormerModel(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=UpperCAmelCase_ , pixel_mask=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(UpperCAmelCase_ , output_hidden_states=UpperCAmelCase_ ) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(UpperCAmelCase_ , UpperCAmelCase_ ) def _A ( self : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str ): SCREAMING_SNAKE_CASE : Optional[int] = MaskFormerForInstanceSegmentation(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() def comm_check_on_output(UpperCAmelCase_ : int ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(pixel_values=UpperCAmelCase_ , pixel_mask=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model(UpperCAmelCase_ ) comm_check_on_output(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model( pixel_values=UpperCAmelCase_ , pixel_mask=UpperCAmelCase_ , mask_labels=UpperCAmelCase_ , class_labels=UpperCAmelCase_ ) comm_check_on_output(UpperCAmelCase_ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class SCREAMING_SNAKE_CASE ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () UpperCamelCase_ : List[str] = ( {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) UpperCamelCase_ : int = False UpperCamelCase_ : Union[str, Any] = False UpperCamelCase_ : int = False UpperCamelCase_ : Optional[Any] = False def _A ( self : Dict ): SCREAMING_SNAKE_CASE : Any = MaskFormerModelTester(self ) SCREAMING_SNAKE_CASE : Tuple = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ ) def _A ( self : List[str] ): self.config_tester.run_common_tests() def _A ( self : Dict ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(UpperCAmelCase_ , **UpperCAmelCase_ , output_hidden_states=UpperCAmelCase_ ) def _A ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*UpperCAmelCase_ ) @unittest.skip(reason="MaskFormer does not use inputs_embeds" ) def _A ( self : List[str] ): pass @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method" ) def _A ( self : Optional[int] ): pass @unittest.skip(reason="MaskFormer is not a generative model" ) def _A ( self : List[str] ): pass @unittest.skip(reason="MaskFormer does not use token embeddings" ) def _A ( self : Union[str, Any] ): pass @require_torch_multi_gpu @unittest.skip( reason="MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _A ( self : List[Any] ): pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def _A ( self : Tuple ): pass def _A ( self : Any ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = model_class(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCAmelCase_ ) @slow def _A ( self : List[Any] ): for model_name in ["facebook/maskformer-swin-small-coco"]: SCREAMING_SNAKE_CASE : Any = MaskFormerModel.from_pretrained(UpperCAmelCase_ ) self.assertIsNotNone(UpperCAmelCase_ ) def _A ( self : Tuple ): SCREAMING_SNAKE_CASE : List[str] = (self.model_tester.min_size,) * 2 SCREAMING_SNAKE_CASE : Any = { "pixel_values": torch.randn((2, 3, *size) , device=UpperCAmelCase_ ), "mask_labels": torch.randn((2, 10, *size) , device=UpperCAmelCase_ ), "class_labels": torch.zeros(2 , 10 , device=UpperCAmelCase_ ).long(), } SCREAMING_SNAKE_CASE : Tuple = MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : int = model(**UpperCAmelCase_ ) self.assertTrue(outputs.loss is not None ) def _A ( self : List[Any] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(UpperCAmelCase_ , **UpperCAmelCase_ , output_hidden_states=UpperCAmelCase_ ) def _A ( self : List[str] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(UpperCAmelCase_ ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Any = model(**UpperCAmelCase_ , output_attentions=UpperCAmelCase_ ) self.assertTrue(outputs.attentions is not None ) def _A ( self : Tuple ): if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss SCREAMING_SNAKE_CASE : str = self.all_model_classes[1] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE : Dict = model_class(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.train() SCREAMING_SNAKE_CASE : Dict = model(UpperCAmelCase_ , mask_labels=UpperCAmelCase_ , class_labels=UpperCAmelCase_ ).loss loss.backward() def _A ( self : Optional[Any] ): # only MaskFormerForInstanceSegmentation has the loss SCREAMING_SNAKE_CASE : str = self.all_model_classes[1] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : Optional[int] = model_class(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.train() SCREAMING_SNAKE_CASE : Tuple = model(UpperCAmelCase_ , mask_labels=UpperCAmelCase_ , class_labels=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : str = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE : str = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't SCREAMING_SNAKE_CASE : List[Any] = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE : List[Any] = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=UpperCAmelCase_ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) snake_case = 1e-4 def lowerCamelCase__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @slow class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @cached_property def _A ( self : Tuple ): return ( MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco" ) if is_vision_available() else None ) def _A ( self : Optional[int] ): SCREAMING_SNAKE_CASE : List[Any] = MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco" ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = self.default_image_processor SCREAMING_SNAKE_CASE : Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE : Any = image_processor(UpperCAmelCase_ , return_tensors="pt" ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(UpperCAmelCase_ , (1, 3, 800, 1088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = torch.tensor( [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]] ).to(UpperCAmelCase_ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE : int = torch.tensor( [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]] ).to(UpperCAmelCase_ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE : List[str] = torch.tensor( [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]] ).to(UpperCAmelCase_ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) def _A ( self : Optional[Any] ): SCREAMING_SNAKE_CASE : Optional[Any] = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(UpperCAmelCase_ ) .eval() ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : List[str] = prepare_img() SCREAMING_SNAKE_CASE : List[str] = image_processor(UpperCAmelCase_ , return_tensors="pt" ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(UpperCAmelCase_ , (1, 3, 800, 1088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**UpperCAmelCase_ ) # masks_queries_logits SCREAMING_SNAKE_CASE : Optional[Any] = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) SCREAMING_SNAKE_CASE : Optional[int] = [ [-1.3_737_124, -1.7_724_937, -1.9_364_233], [-1.5_977_281, -1.9_867_939, -2.1_523_695], [-1.5_795_398, -1.9_269_832, -2.093_942], ] SCREAMING_SNAKE_CASE : List[Any] = torch.tensor(UpperCAmelCase_ ).to(UpperCAmelCase_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) # class_queries_logits SCREAMING_SNAKE_CASE : int = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor( [ [1.6_512E00, -5.2_572E00, -3.3_519E00], [3.6_169E-02, -5.9_025E00, -2.9_313E00], [1.0_766E-04, -7.7_630E00, -5.1_263E00], ] ).to(UpperCAmelCase_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) def _A ( self : int ): SCREAMING_SNAKE_CASE : List[Any] = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff" ) .to(UpperCAmelCase_ ) .eval() ) SCREAMING_SNAKE_CASE : int = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = prepare_img() SCREAMING_SNAKE_CASE : Any = image_processor(UpperCAmelCase_ , return_tensors="pt" ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(UpperCAmelCase_ , (1, 3, 800, 1088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(**UpperCAmelCase_ ) # masks_queries_logits SCREAMING_SNAKE_CASE : int = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) SCREAMING_SNAKE_CASE : Dict = [[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]] SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor(UpperCAmelCase_ ).to(UpperCAmelCase_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) # class_queries_logits SCREAMING_SNAKE_CASE : str = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]] ).to(UpperCAmelCase_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , UpperCAmelCase_ , atol=UpperCAmelCase_ ) ) def _A ( self : List[Any] ): SCREAMING_SNAKE_CASE : Optional[Any] = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(UpperCAmelCase_ ) .eval() ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = image_processor( [np.zeros((3, 800, 1333) ), np.zeros((3, 800, 1333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors="pt" , ) SCREAMING_SNAKE_CASE : Dict = inputs["pixel_values"].to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = [el.to(UpperCAmelCase_ ) for el in inputs["mask_labels"]] SCREAMING_SNAKE_CASE : Optional[int] = [el.to(UpperCAmelCase_ ) for el in inputs["class_labels"]] with torch.no_grad(): SCREAMING_SNAKE_CASE : Any = model(**UpperCAmelCase_ ) self.assertTrue(outputs.loss is not None )
62
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) def UpperCamelCase_ ( __a ) -> Union[str, Any]: a__ : Tuple = R"\w+[.]\d+" a__ : List[Any] = re.findall(__a , __a ) for pat in pats: a__ : Union[str, Any] = key.replace(__a , "_".join(pat.split("." ) ) ) return key def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : List[str] = pt_tuple_key[:-1] + ("scale",) if ( any("norm" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): a__ : Any = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: a__ : Optional[Any] = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: a__ : Union[str, Any] = pt_tuple_key[:-1] + ("embedding",) return renamed_pt_tuple_key, pt_tensor # conv layer a__ : List[str] = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: a__ : str = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer a__ : Tuple = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight": a__ : Tuple = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight a__ : Optional[Any] = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias a__ : Union[str, Any] = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCamelCase_ ( __a , __a , __a=42 ) -> str: # Step 1: Convert pytorch tensor to numpy a__ : Optional[int] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params a__ : Tuple = flax_model.init_weights(PRNGKey(__a ) ) a__ : Optional[Any] = flatten_dict(__a ) a__ : Union[str, Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): a__ : Optional[int] = rename_key(__a ) a__ : Optional[int] = tuple(renamed_pt_key.split("." ) ) # Correctly rename weight parameters a__, a__ : Union[str, Any] = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ''' f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) # also add unexpected weight so that warning is thrown a__ : str = jnp.asarray(__a ) return unflatten_dict(__a )
37
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available a : List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : List[Any] = ["MLukeTokenizer"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys a : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
63
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def UpperCamelCase_ ( ) -> int: a__ : Any = HfArgumentParser(__a ) a__ : Any = parser.parse_args_into_dataclasses()[0] a__ : Optional[int] = TensorFlowBenchmark(args=__a ) try: a__ : Optional[int] = parser.parse_args_into_dataclasses()[0] except ValueError as e: a__ : Tuple = "Arg --no_{0} is no longer used, please use --no-{0} instead." a__ : List[Any] = " ".join(str(__a ).split(" " )[:-1] ) a__ : str = "" a__ : List[Any] = eval(str(__a ).split(" " )[-1] ) a__ : List[str] = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(__a ) if len(__a ) > 0: a__ : Tuple = full_error_msg + begin_error_msg + str(__a ) raise ValueError(__a ) benchmark.run() if __name__ == "__main__": main()
37
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
64
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip UpperCamelCase : Optional[int] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def UpperCamelCase_ ( __a ) -> Any: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def UpperCamelCase_ ( __a , __a , __a ) -> Any: return max(metric_fn(__a , __a ) for gt in ground_truths ) def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [] if args.gold_data_mode == "qa": a__ : Any = pd.read_csv(__a , sep="\t" , header=__a ) for answer_list in data[1]: a__ : Union[str, Any] = ast.literal_eval(__a ) answers.append(__a ) else: a__ : List[str] = [line.strip() for line in open(__a , "r" ).readlines()] a__ : List[str] = [[reference] for reference in references] a__ : List[str] = 0 for prediction, ground_truths in zip(__a , __a ): total += 1 em += metric_max_over_ground_truths(__a , __a , __a ) fa += metric_max_over_ground_truths(__a , __a , __a ) a__ : Dict = 100.0 * em / total a__ : Optional[Any] = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = args.k a__ : str = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = 0 for hypo, reference in zip(__a , __a ): a__ : Any = set(hypo.split("\t" )[:k] ) a__ : Union[str, Any] = set(reference.split("\t" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k a__ : Union[str, Any] = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: def strip_title(__a ): if title.startswith("\"" ): a__ : Optional[Any] = title[1:] if title.endswith("\"" ): a__ : Union[str, Any] = title[:-1] return title a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a , )["input_ids"].to(args.device ) a__ : Optional[int] = rag_model.rag.question_encoder(__a ) a__ : Union[str, Any] = question_enc_outputs[0] a__ : Optional[int] = rag_model.retriever( __a , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="pt" , ) a__ : List[Any] = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) a__ : int = [] for docs in all_docs: a__ : Optional[int] = [strip_title(__a ) for title in docs["title"]] provenance_strings.append("\t".join(__a ) ) return provenance_strings def UpperCamelCase_ ( __a , __a , __a ) -> Dict: with torch.no_grad(): a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a ) a__ : Any = inputs_dict.input_ids.to(args.device ) a__ : Dict = inputs_dict.attention_mask.to(args.device ) a__ : Optional[int] = rag_model.generate( # rag_model overwrites generate __a , attention_mask=__a , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__a , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) a__ : int = rag_model.retriever.generator_tokenizer.batch_decode(__a , skip_special_tokens=__a ) if args.print_predictions: for q, a in zip(__a , __a ): logger.info("Q: {} - A: {}".format(__a , __a ) ) return answers def UpperCamelCase_ ( ) -> List[str]: a__ : int = argparse.ArgumentParser() parser.add_argument( "--model_type" , choices=["rag_sequence", "rag_token", "bart"] , type=__a , help=( "RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the" " model_name_or_path" ) , ) parser.add_argument( "--index_name" , default=__a , choices=["exact", "compressed", "legacy"] , type=__a , help="RAG model retriever type" , ) parser.add_argument( "--index_path" , default=__a , type=__a , help="Path to the retrieval index" , ) parser.add_argument("--n_docs" , default=5 , type=__a , help="Number of retrieved docs" ) parser.add_argument( "--model_name_or_path" , default=__a , type=__a , required=__a , help="Path to pretrained checkpoints or model identifier from huggingface.co/models" , ) parser.add_argument( "--eval_mode" , choices=["e2e", "retrieval"] , default="e2e" , type=__a , help=( "Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates" " precision@k." ) , ) parser.add_argument("--k" , default=1 , type=__a , help="k for the precision@k calculation" ) parser.add_argument( "--evaluation_set" , default=__a , type=__a , required=__a , help="Path to a file containing evaluation samples" , ) parser.add_argument( "--gold_data_path" , default=__a , type=__a , required=__a , help="Path to a tab-separated file with gold samples" , ) parser.add_argument( "--gold_data_mode" , default="qa" , type=__a , choices=["qa", "ans"] , help=( "Format of the gold data file" "qa - a single line in the following format: question [tab] answer_list" "ans - a single line of the gold file contains the expected answer string" ) , ) parser.add_argument( "--predictions_path" , type=__a , default="predictions.txt" , help="Name of the predictions file, to be stored in the checkpoints directory" , ) parser.add_argument( "--eval_all_checkpoints" , action="store_true" , help="Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number" , ) parser.add_argument( "--eval_batch_size" , default=8 , type=__a , help="Batch size per GPU/CPU for evaluation." , ) parser.add_argument( "--recalculate" , help="Recalculate predictions even if the prediction file exists" , action="store_true" , ) parser.add_argument( "--num_beams" , default=4 , type=__a , help="Number of beams to be used when generating answers" , ) parser.add_argument("--min_length" , default=1 , type=__a , help="Min length of the generated answers" ) parser.add_argument("--max_length" , default=50 , type=__a , help="Max length of the generated answers" ) parser.add_argument( "--print_predictions" , action="store_true" , help="If True, prints predictions while evaluating." , ) parser.add_argument( "--print_docs" , action="store_true" , help="If True, prints docs retried while generating." , ) a__ : int = parser.parse_args() a__ : Dict = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) return args def UpperCamelCase_ ( __a ) -> Optional[int]: a__ : Tuple = {} if args.model_type is None: a__ : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("rag" ): a__ : int = RagTokenForGeneration if args.model_type == "rag_token" else RagSequenceForGeneration a__ : Tuple = args.n_docs if args.index_name is not None: a__ : Any = args.index_name if args.index_path is not None: a__ : int = args.index_path else: a__ : Optional[Any] = BartForConditionalGeneration a__ : Tuple = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("Evaluate the following checkpoints: %s" , __a ) a__ : Any = get_scores if args.eval_mode == "e2e" else get_precision_at_k a__ : Union[str, Any] = evaluate_batch_eae if args.eval_mode == "e2e" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("Calculating metrics based on an existing predictions file: {}".format(args.predictions_path ) ) score_fn(__a , args.predictions_path , args.gold_data_path ) continue logger.info("***** Running evaluation for {} *****".format(__a ) ) logger.info(" Batch size = %d" , args.eval_batch_size ) logger.info(" Predictions will be stored under {}".format(args.predictions_path ) ) if args.model_type.startswith("rag" ): a__ : str = RagRetriever.from_pretrained(__a , **__a ) a__ : Optional[int] = model_class.from_pretrained(__a , retriever=__a , **__a ) model.retriever.init_retrieval() else: a__ : Dict = model_class.from_pretrained(__a , **__a ) model.to(args.device ) with open(args.evaluation_set , "r" ) as eval_file, open(args.predictions_path , "w" ) as preds_file: a__ : List[Any] = [] for line in tqdm(__a ): questions.append(line.strip() ) if len(__a ) == args.eval_batch_size: a__ : Union[str, Any] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) + "\n" ) preds_file.flush() a__ : Any = [] if len(__a ) > 0: a__ : List[str] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) ) preds_file.flush() score_fn(__a , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": UpperCamelCase : List[Any] = get_args() main(args)
37
0
"""simple docstring""" import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller __UpperCAmelCase = 3 def lowerCAmelCase ( __UpperCamelCase ): '''simple docstring''' print("""Generating primitive root of p""" ) while True: UpperCAmelCase__ : List[Any] = random.randrange(3 , __UpperCamelCase ) if pow(__UpperCamelCase , 2 , __UpperCamelCase ) == 1: continue if pow(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) == 1: continue return g def lowerCAmelCase ( __UpperCamelCase ): '''simple docstring''' print("""Generating prime p...""" ) UpperCAmelCase__ : str = rabin_miller.generate_large_prime(__UpperCamelCase ) # select large prime number. UpperCAmelCase__ : Optional[Any] = primitive_root(__UpperCamelCase ) # one primitive root on modulo p. UpperCAmelCase__ : List[str] = random.randrange(3 , __UpperCamelCase ) # private_key -> have to be greater than 2 for safety. UpperCAmelCase__ : List[str] = cryptomath.find_mod_inverse(pow(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) UpperCAmelCase__ : Any = (key_size, e_a, e_a, p) UpperCAmelCase__ : List[str] = (key_size, d) return public_key, private_key def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase ): '''simple docstring''' if os.path.exists(F"{name}_pubkey.txt" ) or os.path.exists(F"{name}_privkey.txt" ): print("""\nWARNING:""" ) print( F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n" """Use a different name or delete these files and re-run this program.""" ) sys.exit() UpperCAmelCase__ , UpperCAmelCase__ : str = generate_key(__UpperCamelCase ) print(F"\nWriting public key to file {name}_pubkey.txt..." ) with open(F"{name}_pubkey.txt" , """w""" ) as fo: fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}" ) print(F"Writing private key to file {name}_privkey.txt..." ) with open(F"{name}_privkey.txt" , """w""" ) as fo: fo.write(F"{private_key[0]},{private_key[1]}" ) def lowerCAmelCase ( ): '''simple docstring''' print("""Making key files...""" ) make_key_files("""elgamal""" , 2048 ) print("""Key files generation successful""" ) if __name__ == "__main__": main()
65
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a = None , ) -> str: a__ : int = {} if train_file is not None: a__ : int = [train_file] if eval_file is not None: a__ : Union[str, Any] = [eval_file] if test_file is not None: a__ : str = [test_file] a__ : Optional[Any] = datasets.load_dataset("csv" , data_files=__a ) a__ : List[Any] = list(ds[list(files.keys() )[0]].features.keys() ) a__ : str = features_name.pop(__a ) a__ : Dict = list(set(ds[list(files.keys() )[0]][label_name] ) ) a__ : str = {label: i for i, label in enumerate(__a )} a__ : Tuple = tokenizer.model_input_names a__ : List[str] = {} if len(__a ) == 1: for k in files.keys(): a__ : Optional[Any] = ds[k].map( lambda __a : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=__a , max_length=__a , padding="max_length" ) , batched=__a , ) elif len(__a ) == 2: for k in files.keys(): a__ : Dict = ds[k].map( lambda __a : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=__a , max_length=__a , padding="max_length" , ) , batched=__a , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: a__ : str = {k: v for k, v in ex.items() if k in input_names} a__ : str = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: a__ : Tuple = {k: v for k, v in ex.items() if k in input_names} a__ : List[Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: a__ : List[Any] = {k: v for k, v in ex.items() if k in input_names} a__ : Optional[int] = labelaid[ex[label_name]] yield (d, label) a__ : Optional[Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: a__ : Optional[int] = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: a__ : Optional[Any] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: a__ : Tuple = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid UpperCamelCase : Optional[Any] = logging.getLogger(__name__) @dataclass class A__ : """simple docstring""" _lowercase = field(metadata={'help': 'Which column contains the label'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the training file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the development file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the test file'} ) _lowercase = field( default=1_2_8 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowercase = field( default=A__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) @dataclass class A__ : """simple docstring""" _lowercase = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowercase = field(default=A__ , metadata={'help': 'Set this flag to use fast tokenization.'} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. _lowercase = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) def UpperCamelCase_ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. a__ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) a__, a__, a__ : str = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.info( f'''n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, ''' f'''16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a__ : Union[str, Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) a__, a__, a__, a__ : Optional[Any] = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=__a , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) a__ : Optional[int] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(__a ) , labelaid=__a , idalabel={id: label for label, id in labelaid.items()} , finetuning_task="text-classification" , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): a__ : Any = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool(".bin" in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , ) def compute_metrics(__a ) -> Dict: a__ : Union[str, Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer a__ : Dict = TFTrainer( model=__a , args=__a , train_dataset=__a , eval_dataset=__a , compute_metrics=__a , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation a__ : Optional[Any] = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) a__ : Dict = trainer.evaluate() a__ : int = os.path.join(training_args.output_dir , "eval_results.txt" ) with open(__a , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(f''' {key} = {value}''' ) writer.write(f'''{key} = {value}\n''' ) results.update(__a ) return results if __name__ == "__main__": main()
37
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCamelCase = { "configuration_convnext": ["CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvNextConfig", "ConvNextOnnxConfig"] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ["ConvNextFeatureExtractor"] UpperCamelCase = ["ConvNextImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ "CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "ConvNextForImageClassification", "ConvNextModel", "ConvNextPreTrainedModel", "ConvNextBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ "TFConvNextForImageClassification", "TFConvNextModel", "TFConvNextPreTrainedModel", ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure)
66
import argparse import collections import json import os import re import string import sys import numpy as np UpperCamelCase : List[str] = re.compile(r"""\b(a|an|the)\b""", re.UNICODE) UpperCamelCase : Union[str, Any] = None def UpperCamelCase_ ( ) -> List[str]: a__ : List[Any] = 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 UpperCamelCase_ ( __a ) -> str: a__ : Optional[Any] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : Dict = bool(qa["answers"]["text"] ) return qid_to_has_ans def UpperCamelCase_ ( __a ) -> List[Any]: def remove_articles(__a ): return ARTICLES_REGEX.sub(" " , __a ) def white_space_fix(__a ): return " ".join(text.split() ) def remove_punc(__a ): a__ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def UpperCamelCase_ ( __a ) -> Dict: if not s: return [] return normalize_answer(__a ).split() def UpperCamelCase_ ( __a , __a ) -> str: return int(normalize_answer(__a ) == normalize_answer(__a ) ) def UpperCamelCase_ ( __a , __a ) -> Dict: a__ : int = get_tokens(__a ) a__ : Optional[Any] = get_tokens(__a ) a__ : Any = collections.Counter(__a ) & collections.Counter(__a ) a__ : Dict = 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 a__ : Tuple = 1.0 * num_same / len(__a ) a__ : str = 1.0 * num_same / len(__a ) a__ : str = (2 * precision * recall) / (precision + recall) return fa def UpperCamelCase_ ( __a , __a ) -> int: a__ : List[str] = {} a__ : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : List[Any] = qa["id"] a__ : Dict = [t for t in qa["answers"]["text"] if normalize_answer(__a )] if not gold_answers: # For unanswerable questions, only correct answer is empty string a__ : Tuple = [""] if qid not in preds: print(f'''Missing prediction for {qid}''' ) continue a__ : Tuple = preds[qid] # Take max over all gold answers a__ : Optional[int] = max(compute_exact(__a , __a ) for a in gold_answers ) a__ : str = max(compute_fa(__a , __a ) for a in gold_answers ) return exact_scores, fa_scores def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: a__ : Optional[Any] = {} for qid, s in scores.items(): a__ : Dict = na_probs[qid] > na_prob_thresh if pred_na: a__ : Dict = float(not qid_to_has_ans[qid] ) else: a__ : Optional[Any] = s return new_scores def UpperCamelCase_ ( __a , __a , __a=None ) -> Tuple: if not qid_list: a__ : Union[str, Any] = len(__a ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: a__ : int = 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 UpperCamelCase_ ( __a , __a , __a ) -> List[str]: for k in new_eval: a__ : Optional[Any] = new_eval[k] def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: 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 UpperCamelCase_ ( __a , __a , __a , __a , __a=None , __a=None ) -> Dict: a__ : Optional[Any] = sorted(__a , key=lambda __a : na_probs[k] ) a__ : Any = 0.0 a__ : Optional[int] = 1.0 a__ : Optional[int] = 0.0 a__ : Any = [1.0] a__ : Tuple = [0.0] a__ : List[str] = 0.0 for i, qid in enumerate(__a ): if qid_to_has_ans[qid]: true_pos += scores[qid] a__ : Any = true_pos / float(i + 1 ) a__ : int = 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 UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> str: if out_image_dir and not os.path.exists(__a ): os.makedirs(__a ) a__ : Optional[int] = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return a__ : Optional[int] = 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" , ) a__ : Optional[Any] = make_precision_recall_eval( __a , __a , __a , __a , out_image=os.path.join(__a , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) a__ : str = {k: float(__a ) for k, v in qid_to_has_ans.items()} a__ : Optional[Any] = 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> str: if not qid_list: return a__ : Optional[Any] = [na_probs[k] for k in qid_list] a__ : 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[Any]: a__ : str = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) a__ : Optional[Any] = num_no_ans a__ : Dict = cur_score a__ : Any = 0.0 a__ : Optional[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]: a__ : Optional[int] = scores[qid] else: if preds[qid]: a__ : str = -1 else: a__ : Union[str, Any] = 0 cur_score += diff if cur_score > best_score: a__ : Any = cur_score a__ : Dict = na_probs[qid] return 100.0 * best_score / len(__a ), best_thresh def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> Any: a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__ : Any = best_exact a__ : Any = exact_thresh a__ : List[Any] = best_fa a__ : Optional[int] = fa_thresh def UpperCamelCase_ ( ) -> Tuple: with open(OPTS.data_file ) as f: a__ : List[Any] = json.load(__a ) a__ : Any = dataset_json["data"] with open(OPTS.pred_file ) as f: a__ : int = json.load(__a ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: a__ : List[str] = json.load(__a ) else: a__ : Optional[int] = {k: 0.0 for k in preds} a__ : Optional[Any] = make_qid_to_has_ans(__a ) # maps qid to True/False a__ : List[Any] = [k for k, v in qid_to_has_ans.items() if v] a__ : Union[str, Any] = [k for k, v in qid_to_has_ans.items() if not v] a__, a__ : str = get_raw_scores(__a , __a ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : Tuple = make_eval_dict(__a , __a ) if has_ans_qids: a__ : str = make_eval_dict(__a , __a , qid_list=__a ) merge_eval(__a , __a , "HasAns" ) if no_ans_qids: a__ : List[Any] = 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__": UpperCamelCase : Any = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("""Agg""") import matplotlib.pyplot as plt main()
37
0
import math def SCREAMING_SNAKE_CASE__ ( snake_case__ :int ) -> list[int]: _lowercase = [] _lowercase = 2 _lowercase = int(math.sqrt(snake_case__ ) ) # Size of every segment _lowercase = [True] * (end + 1) _lowercase = [] while start <= end: if temp[start] is True: in_prime.append(snake_case__ ) for i in range(start * start , end + 1 , snake_case__ ): _lowercase = False start += 1 prime += in_prime _lowercase = end + 1 _lowercase = min(2 * end , snake_case__ ) while low <= n: _lowercase = [True] * (high - low + 1) for each in in_prime: _lowercase = math.floor(low / each ) * each if t < low: t += each for j in range(snake_case__ , high + 1 , snake_case__ ): _lowercase = False for j in range(len(snake_case__ ) ): if temp[j] is True: prime.append(j + low ) _lowercase = high + 1 _lowercase = min(high + end , snake_case__ ) return prime print(sieve(1_0**6))
67
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = CLIPTokenizer _lowercase = CLIPTokenizerFast _lowercase = True _lowercase = {} _lowercase = False def _UpperCamelCase( self : List[Any] ): super().setUp() # fmt: off a__ : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on a__ : Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a__ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] a__ : Optional[Any] = {"unk_token": "<unk>"} a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def _UpperCamelCase( self : Dict , **lowerCamelCase__ : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] , **lowerCamelCase__ : Optional[int] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : int = "lower newer" a__ : Optional[int] = "lower newer" return input_text, output_text def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a__ : int = "lower newer" a__ : List[str] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] a__ : Union[str, Any] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = tokens + [tokenizer.unk_token] a__ : Union[str, Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) @require_ftfy def _UpperCamelCase( self : Optional[Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : List[str] = self.tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : Any = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : int = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." a__ : Optional[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : Dict = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways a__ : Optional[Any] = "xa\u0303y" + " " + "x\xe3y" a__ : Optional[int] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of space type a__ : str = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: a__ : Any = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of line break type a__ : Union[str, Any] = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: a__ : List[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : str = "hello" # `hello` is a token in the vocabulary of `pretrained_name` a__ : Tuple = f'''{text_of_1_token} {text_of_1_token}''' a__ : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Union[str, Any] = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCamelCase__ ) + 1, len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) a__ : Optional[Any] = f''' {text}''' a__ : str = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Dict = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCamelCase__ ) + 1, 1 + len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) def _UpperCamelCase( self : int ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCamelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _UpperCamelCase( self : int ): super().test_tokenization_python_rust_equals() def _UpperCamelCase( self : str ): # CLIP always lower cases letters pass
37
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class _A ( unittest.TestCase ): """simple docstring""" def __init__( self : str , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : str=7 , __SCREAMING_SNAKE_CASE : Union[str, Any]=3 , __SCREAMING_SNAKE_CASE : Tuple=18 , __SCREAMING_SNAKE_CASE : Tuple=30 , __SCREAMING_SNAKE_CASE : Union[str, Any]=400 , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : Optional[Any]=[0.48_145_466, 0.4_578_275, 0.40_821_073] , __SCREAMING_SNAKE_CASE : int=[0.26_862_954, 0.26_130_258, 0.27_577_711] , __SCREAMING_SNAKE_CASE : Dict=True , ) -> Optional[Any]: __UpperCAmelCase =size if size is not None else {"""height""": 224, """width""": 224} __UpperCAmelCase =crop_size if crop_size is not None else {"""height""": 18, """width""": 18} __UpperCAmelCase =parent __UpperCAmelCase =batch_size __UpperCAmelCase =num_channels __UpperCAmelCase =image_size __UpperCAmelCase =min_resolution __UpperCAmelCase =max_resolution __UpperCAmelCase =do_resize __UpperCAmelCase =size __UpperCAmelCase =do_center_crop __UpperCAmelCase =crop_size __UpperCAmelCase =do_normalize __UpperCAmelCase =image_mean __UpperCAmelCase =image_std __UpperCAmelCase =do_convert_rgb def _a ( self : Any ) -> Any: return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def _a ( self : str , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : int=False ) -> List[Any]: assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: __UpperCAmelCase =[] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: __UpperCAmelCase =[] for i in range(self.batch_size ): __UpperCAmelCase , __UpperCAmelCase =np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension __UpperCAmelCase =[Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] if torchify: __UpperCAmelCase =[torch.from_numpy(__SCREAMING_SNAKE_CASE ) for x in image_inputs] return image_inputs @require_torch @require_vision class _A ( UpperCamelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase : Dict = ChineseCLIPImageProcessor if is_vision_available() else None def _a ( self : List[Any] ) -> List[Any]: __UpperCAmelCase =ChineseCLIPImageProcessingTester(self , do_center_crop=__SCREAMING_SNAKE_CASE ) @property def _a ( self : Optional[int] ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : Union[str, Any] ) -> Optional[Any]: __UpperCAmelCase =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 , """do_center_crop""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """center_crop""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_std""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_convert_rgb""" ) ) def _a ( self : Optional[int] ) -> str: __UpperCAmelCase =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 224, """width""": 224} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) __UpperCAmelCase =self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def _a ( self : Optional[int] ) -> Optional[int]: pass def _a ( self : Optional[int] ) -> Optional[int]: # Initialize image_processing __UpperCAmelCase =self.image_processing_class(**self.image_processor_dict ) # create random PIL images __UpperCAmelCase =self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __UpperCAmelCase =image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched __UpperCAmelCase =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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def _a ( self : Tuple ) -> Union[str, Any]: # Initialize image_processing __UpperCAmelCase =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __UpperCAmelCase =self.image_processor_tester.prepare_inputs(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 __UpperCAmelCase =image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched __UpperCAmelCase =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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def _a ( self : Tuple ) -> Any: # Initialize image_processing __UpperCAmelCase =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __UpperCAmelCase =self.image_processor_tester.prepare_inputs(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 __UpperCAmelCase =image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched __UpperCAmelCase =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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) @require_torch @require_vision class _A ( UpperCamelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase : Optional[int] = ChineseCLIPImageProcessor if is_vision_available() else None def _a ( self : Tuple ) -> Union[str, Any]: __UpperCAmelCase =ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=__SCREAMING_SNAKE_CASE ) __UpperCAmelCase =3 @property def _a ( self : Optional[Any] ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : Dict ) -> Optional[int]: __UpperCAmelCase =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 , """do_center_crop""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """center_crop""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_std""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_convert_rgb""" ) ) def _a ( self : List[Any] ) -> Tuple: pass def _a ( self : List[Any] ) -> Dict: # Initialize image_processing __UpperCAmelCase =self.image_processing_class(**self.image_processor_dict ) # create random PIL images __UpperCAmelCase =self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __UpperCAmelCase =image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched __UpperCAmelCase =image_processing(__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
68
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase : Dict = """<<<<<<< This should probably be modified because it mentions: """ UpperCamelCase : List[Any] = """======= >>>>>>> """ UpperCamelCase : Optional[Any] = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] UpperCamelCase : 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 UpperCamelCase_ ( __a ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class A__ ( A__ ): """simple docstring""" @staticmethod def _UpperCamelCase( lowerCamelCase__ : ArgumentParser ): a__ : List[str] = parser.add_parser( "convert" , help="Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset." , ) train_parser.add_argument( "--tfds_path" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to a TensorFlow Datasets folder to convert or a single tfds file to convert." , ) train_parser.add_argument( "--datasets_directory" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to the HuggingFace Datasets folder." ) train_parser.set_defaults(func=lowerCamelCase__ ) def __init__( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str , *lowerCamelCase__ : Tuple ): a__ : str = get_logger("datasets-cli/converting" ) a__ : Optional[Any] = tfds_path a__ : Optional[int] = datasets_directory def _UpperCamelCase( self : int ): if os.path.isdir(self._tfds_path ): a__ : List[str] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a__ : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("--tfds_path is neither a directory nor a file. Please check path." ) a__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a__ : Tuple = [] a__ : str = [] a__ : List[Any] = {} if os.path.isdir(self._tfds_path ): a__ : List[str] = os.listdir(lowerCamelCase__ ) else: a__ : Union[str, Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'''Looking at file {f_name}''' ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Dict = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) if not os.path.isfile(lowerCamelCase__ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("Skipping file" ) continue with open(lowerCamelCase__ , encoding="utf-8" ) as f: a__ : List[Any] = f.readlines() a__ : Union[str, Any] = [] a__ : Union[str, Any] = False a__ : Union[str, Any] = False a__ : Dict = [] for line in lines: a__ : Optional[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: a__ : List[Any] = "import datasets\n" elif "import tensorflow" in out_line: # order is important here a__ : List[str] = "" continue elif "from absl import logging" in out_line: a__ : Dict = "from datasets import logging\n" elif "getLogger" in out_line: a__ : List[Any] = out_line.replace("getLogger" , "get_logger" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a__ : List[str] = True a__ : Dict = list(filter(lambda lowerCamelCase__ : e in out_line , lowerCamelCase__ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(lowerCamelCase__ ) + "\n" ) out_lines.append(lowerCamelCase__ ) out_lines.append(lowerCamelCase__ ) continue else: for pattern, replacement in TO_CONVERT: a__ : Tuple = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a__ : Optional[int] = re.match(r"from\stensorflow_datasets.*import\s([^\.\r\n]+)" , lowerCamelCase__ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split("," ) ) a__ : Optional[Any] = "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: a__ : Optional[int] = True out_lines.append(lowerCamelCase__ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a__ : Dict = f_name.replace(".py" , "" ) a__ : Optional[int] = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) 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(lowerCamelCase__ ) if needs_manual_update: with_manual_update.append(lowerCamelCase__ ) with open(lowerCamelCase__ , "w" , encoding="utf-8" ) as f: f.writelines(lowerCamelCase__ ) self._logger.info(f'''Converted in {output_file}''' ) for utils_file in utils_files: try: a__ : Any = os.path.basename(lowerCamelCase__ ) a__ : Optional[int] = imports_to_builder_map[f_name.replace(".py" , "" )] self._logger.info(f'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(lowerCamelCase__ , lowerCamelCase__ ) 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\'.''' )
37
0
'''simple docstring''' import torch from diffusers import EulerDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE__ ( _UpperCamelCase ): __SCREAMING_SNAKE_CASE = (EulerDiscreteScheduler,) __SCREAMING_SNAKE_CASE = 10 def A ( self : int , **a_ : int ): """simple docstring""" __snake_case = { "num_train_timesteps": 1_100, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**a_ ) return config def A ( self : Tuple ): """simple docstring""" for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=a_ ) def A ( self : Optional[Any] ): """simple docstring""" for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=a_ , beta_end=a_ ) def A ( self : Optional[Any] ): """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a_ ) def A ( self : List[Any] ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a_ ) def A ( self : Optional[Any] ): """simple docstring""" __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config() __snake_case = scheduler_class(**a_ ) scheduler.set_timesteps(self.num_inference_steps ) __snake_case = torch.manual_seed(0 ) __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter * scheduler.init_noise_sigma __snake_case = sample.to(a_ ) for i, t in enumerate(scheduler.timesteps ): __snake_case = scheduler.scale_model_input(a_ , a_ ) __snake_case = model(a_ , a_ ) __snake_case = scheduler.step(a_ , a_ , a_ , generator=a_ ) __snake_case = output.prev_sample __snake_case = torch.sum(torch.abs(a_ ) ) __snake_case = torch.mean(torch.abs(a_ ) ) assert abs(result_sum.item() - 10.0807 ) < 1e-2 assert abs(result_mean.item() - 0.0131 ) < 1e-3 def A ( self : Tuple ): """simple docstring""" __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config(prediction_type="v_prediction" ) __snake_case = scheduler_class(**a_ ) scheduler.set_timesteps(self.num_inference_steps ) __snake_case = torch.manual_seed(0 ) __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter * scheduler.init_noise_sigma __snake_case = sample.to(a_ ) for i, t in enumerate(scheduler.timesteps ): __snake_case = scheduler.scale_model_input(a_ , a_ ) __snake_case = model(a_ , a_ ) __snake_case = scheduler.step(a_ , a_ , a_ , generator=a_ ) __snake_case = output.prev_sample __snake_case = torch.sum(torch.abs(a_ ) ) __snake_case = torch.mean(torch.abs(a_ ) ) assert abs(result_sum.item() - 0.0002 ) < 1e-2 assert abs(result_mean.item() - 2.2_676e-06 ) < 1e-3 def A ( self : Optional[Any] ): """simple docstring""" __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config() __snake_case = scheduler_class(**a_ ) scheduler.set_timesteps(self.num_inference_steps , device=a_ ) __snake_case = torch.manual_seed(0 ) __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() __snake_case = sample.to(a_ ) for t in scheduler.timesteps: __snake_case = scheduler.scale_model_input(a_ , a_ ) __snake_case = model(a_ , a_ ) __snake_case = scheduler.step(a_ , a_ , a_ , generator=a_ ) __snake_case = output.prev_sample __snake_case = torch.sum(torch.abs(a_ ) ) __snake_case = torch.mean(torch.abs(a_ ) ) assert abs(result_sum.item() - 10.0807 ) < 1e-2 assert abs(result_mean.item() - 0.0131 ) < 1e-3 def A ( self : List[Any] ): """simple docstring""" __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config() __snake_case = scheduler_class(**a_ , use_karras_sigmas=a_ ) scheduler.set_timesteps(self.num_inference_steps , device=a_ ) __snake_case = torch.manual_seed(0 ) __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() __snake_case = sample.to(a_ ) for t in scheduler.timesteps: __snake_case = scheduler.scale_model_input(a_ , a_ ) __snake_case = model(a_ , a_ ) __snake_case = scheduler.step(a_ , a_ , a_ , generator=a_ ) __snake_case = output.prev_sample __snake_case = torch.sum(torch.abs(a_ ) ) __snake_case = torch.mean(torch.abs(a_ ) ) assert abs(result_sum.item() - 124.52299499511719 ) < 1e-2 assert abs(result_mean.item() - 0.16213932633399963 ) < 1e-3
69
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class A__ ( A__ ): """simple docstring""" _lowercase = '' _lowercase = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _lowercase = None # compression type in fsspec. ex: "gzip" _lowercase = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , lowerCamelCase__ : str = "" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , **lowerCamelCase__ : List[str] ): super().__init__(self , **lowerCamelCase__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode a__ : str = fsspec.open( lowerCamelCase__ , mode="rb" , protocol=lowerCamelCase__ , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) a__ : Optional[int] = os.path.basename(self.file.path.split("::" )[0] ) a__ : int = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) a__ : List[Any] = None @classmethod def _UpperCamelCase( cls : int , lowerCamelCase__ : int ): # compressed file paths are always relative to the archive root return super()._strip_protocol(lowerCamelCase__ ).lstrip("/" ) def _UpperCamelCase( self : Dict ): if self.dir_cache is None: a__ : Dict = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} a__ : int = {f["name"]: f} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : str ): return self.file.open().read() def _UpperCamelCase( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : int=None , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[str]=None , **lowerCamelCase__ : Optional[Any] , ): a__ : Optional[int] = self._strip_protocol(lowerCamelCase__ ) if mode != "rb": raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class A__ ( A__ ): """simple docstring""" _lowercase = 'bz2' _lowercase = 'bz2' _lowercase = '.bz2' class A__ ( A__ ): """simple docstring""" _lowercase = 'gzip' _lowercase = 'gzip' _lowercase = '.gz' class A__ ( A__ ): """simple docstring""" _lowercase = 'lz4' _lowercase = 'lz4' _lowercase = '.lz4' class A__ ( A__ ): """simple docstring""" _lowercase = 'xz' _lowercase = 'xz' _lowercase = '.xz' class A__ ( A__ ): """simple docstring""" _lowercase = 'zstd' _lowercase = 'zstd' _lowercase = '.zst' def __init__( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , lowerCamelCase__ : int = DEFAULT_BLOCK_SIZE , **lowerCamelCase__ : Tuple , ): super().__init__( fo=lowerCamelCase__ , mode=lowerCamelCase__ , target_protocol=lowerCamelCase__ , target_options=lowerCamelCase__ , block_size=lowerCamelCase__ , **lowerCamelCase__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 a__ : Any = self.file.__enter__ class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : str ): a__ : List[Any] = file_ def __enter__( self : str ): self._file.__enter__() return self def __exit__( self : int , *lowerCamelCase__ : List[str] , **lowerCamelCase__ : int ): self._file.__exit__(*lowerCamelCase__ , **lowerCamelCase__ ) def __iter__( self : List[str] ): return iter(self._file ) def _UpperCamelCase( self : Any ): return next(self._file ) def __getattr__( self : Optional[Any] , lowerCamelCase__ : Tuple ): return getattr(self._file , lowerCamelCase__ ) def fixed_enter(*lowerCamelCase__ : List[str] , **lowerCamelCase__ : str ): return WrappedFile(_enter(*lowerCamelCase__ , **lowerCamelCase__ ) ) a__ : Any = fixed_enter
37
0
import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TextClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. lowerCamelCase : Dict = {"LayoutLMv2Config", "LayoutLMv3Config"} @is_pipeline_test class A( unittest.TestCase ): '''simple docstring''' UpperCamelCase = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING UpperCamelCase = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: UpperCamelCase = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: UpperCamelCase = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } @require_torch def a__ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = pipeline( task='text-classification' , model='hf-internal-testing/tiny-random-distilbert' , framework='pt' ) lowerCamelCase_ = text_classifier('This is great !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}] ) lowerCamelCase_ = text_classifier('This is great !' , top_k=2 ) self.assertEqual( nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}] ) lowerCamelCase_ = text_classifier(['This is great !', 'This is bad'] , top_k=2 ) self.assertEqual( nested_simplify(A_ ) , [ [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}], [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}], ] , ) lowerCamelCase_ = text_classifier('This is great !' , top_k=1 ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}] ) # Legacy behavior lowerCamelCase_ = text_classifier('This is great !' , return_all_scores=A_ ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}] ) lowerCamelCase_ = text_classifier('This is great !' , return_all_scores=A_ ) self.assertEqual( nested_simplify(A_ ) , [[{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}]] ) lowerCamelCase_ = text_classifier(['This is great !', 'Something else'] , return_all_scores=A_ ) self.assertEqual( nested_simplify(A_ ) , [ [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}], [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}], ] , ) lowerCamelCase_ = text_classifier(['This is great !', 'Something else'] , return_all_scores=A_ ) self.assertEqual( nested_simplify(A_ ) , [ {'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_0', 'score': 0.504}, ] , ) @require_torch def a__ ( self : Tuple ) -> str: """simple docstring""" import torch lowerCamelCase_ = pipeline( task='text-classification' , model='hf-internal-testing/tiny-random-distilbert' , framework='pt' , device=torch.device('cpu' ) , ) lowerCamelCase_ = text_classifier('This is great !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}] ) @require_tf def a__ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ = pipeline( task='text-classification' , model='hf-internal-testing/tiny-random-distilbert' , framework='tf' ) lowerCamelCase_ = text_classifier('This is great !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'LABEL_0', 'score': 0.504}] ) @slow @require_torch def a__ ( self : Any ) -> Dict: """simple docstring""" lowerCamelCase_ = pipeline('text-classification' ) lowerCamelCase_ = text_classifier('This is great !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'POSITIVE', 'score': 1.0}] ) lowerCamelCase_ = text_classifier('This is bad !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'NEGATIVE', 'score': 1.0}] ) lowerCamelCase_ = text_classifier('Birds are a type of animal' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'POSITIVE', 'score': 0.988}] ) @slow @require_tf def a__ ( self : Optional[Any] ) -> str: """simple docstring""" lowerCamelCase_ = pipeline('text-classification' , framework='tf' ) lowerCamelCase_ = text_classifier('This is great !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'POSITIVE', 'score': 1.0}] ) lowerCamelCase_ = text_classifier('This is bad !' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'NEGATIVE', 'score': 1.0}] ) lowerCamelCase_ = text_classifier('Birds are a type of animal' ) self.assertEqual(nested_simplify(A_ ) , [{'label': 'POSITIVE', 'score': 0.988}] ) def a__ ( self : str , A_ : List[str] , A_ : Any , A_ : Optional[Any] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ = TextClassificationPipeline(model=A_ , tokenizer=A_ ) return text_classifier, ["HuggingFace is in", "This is another test"] def a__ ( self : int , A_ : Union[str, Any] , A_ : str ) -> Dict: """simple docstring""" lowerCamelCase_ = text_classifier.model # Small inputs because BartTokenizer tiny has maximum position embeddings = 22 lowerCamelCase_ = 'HuggingFace is in' lowerCamelCase_ = text_classifier(A_ ) self.assertEqual(nested_simplify(A_ ) , [{'label': ANY(A_ ), 'score': ANY(A_ )}] ) self.assertTrue(outputs[0]['label'] in model.config.idalabel.values() ) lowerCamelCase_ = ['HuggingFace is in ', 'Paris is in France'] lowerCamelCase_ = text_classifier(A_ ) self.assertEqual( nested_simplify(A_ ) , [{'label': ANY(A_ ), 'score': ANY(A_ )}, {'label': ANY(A_ ), 'score': ANY(A_ )}] , ) self.assertTrue(outputs[0]['label'] in model.config.idalabel.values() ) self.assertTrue(outputs[1]['label'] in model.config.idalabel.values() ) # Forcing to get all results with `top_k=None` # This is NOT the legacy format lowerCamelCase_ = text_classifier(A_ , top_k=A_ ) lowerCamelCase_ = len(model.config.idalabel.values() ) self.assertEqual( nested_simplify(A_ ) , [[{'label': ANY(A_ ), 'score': ANY(A_ )}] * N, [{'label': ANY(A_ ), 'score': ANY(A_ )}] * N] , ) lowerCamelCase_ = {'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'} lowerCamelCase_ = text_classifier(A_ ) self.assertEqual( nested_simplify(A_ ) , {'label': ANY(A_ ), 'score': ANY(A_ )} , ) self.assertTrue(outputs['label'] in model.config.idalabel.values() ) # This might be used a text pair, but tokenizer + pipe interaction # makes it hard to understand that it's not using the pair properly # https://github.com/huggingface/transformers/issues/17305 # We disabled this usage instead as it was outputting wrong outputs. lowerCamelCase_ = [['HuggingFace is in ', 'Paris is in France']] with self.assertRaises(A_ ): text_classifier(A_ ) # This used to be valid for doing text pairs # We're keeping it working because of backward compatibility lowerCamelCase_ = text_classifier([[['HuggingFace is in ', 'Paris is in France']]] ) self.assertEqual( nested_simplify(A_ ) , [{'label': ANY(A_ ), 'score': ANY(A_ )}] , ) self.assertTrue(outputs[0]['label'] in model.config.idalabel.values() )
70
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Union[str, Any] = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") a__ : Union[str, Any] = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__a ): os.makedirs(__a ) a__ : Any = model.state_dict() def to_tf_var_name(__a ): for patt, repl in iter(__a ): a__ : Tuple = name.replace(__a , __a ) return f'''bert/{name}''' def create_tf_var(__a , __a , __a ): a__ : Tuple = tf.dtypes.as_dtype(tensor.dtype ) a__ : Dict = tf.get_variable(dtype=__a , shape=tensor.shape , name=__a , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__a ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: a__ : int = to_tf_var_name(__a ) a__ : Union[str, Any] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): a__ : int = torch_tensor.T a__ : Optional[Any] = create_tf_var(tensor=__a , name=__a , session=__a ) tf.keras.backend.set_value(__a , __a ) a__ : int = session.run(__a ) print(f'''Successfully created {tf_name}: {np.allclose(__a , __a )}''' ) a__ : Any = tf.train.Saver(tf.trainable_variables() ) saver.save(__a , os.path.join(__a , model_name.replace("-" , "_" ) + ".ckpt" ) ) def UpperCamelCase_ ( __a=None ) -> int: a__ : Dict = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__a , required=__a , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__a , default=__a , required=__a , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__a , required=__a , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__a , required=__a , help="Directory in which to save tensorflow model" ) a__ : Optional[Any] = parser.parse_args(__a ) a__ : Tuple = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__a , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
37
0
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> float: """simple docstring""" UpperCAmelCase_ : Optional[int] = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def a__ ( ) -> Optional[int]: """simple docstring""" print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
71
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=2 , lowerCamelCase__ : Any=24 , lowerCamelCase__ : Optional[Any]=16 , lowerCamelCase__ : int=True , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[Any]=32 , lowerCamelCase__ : List[str]=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Optional[Any]=37 , lowerCamelCase__ : Any="gelu" , lowerCamelCase__ : Union[str, Any]=0.1 , lowerCamelCase__ : Optional[int]=0.1 , lowerCamelCase__ : str=10 , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : str=None , lowerCamelCase__ : List[str]=2 , lowerCamelCase__ : Optional[Any]=2 , ): a__ : str = parent a__ : Any = batch_size a__ : Dict = patch_size a__ : List[Any] = max_length a__ : str = num_mel_bins a__ : Optional[Any] = is_training a__ : Optional[int] = use_labels a__ : List[Any] = hidden_size a__ : str = num_hidden_layers a__ : Any = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : List[str] = hidden_act a__ : str = hidden_dropout_prob a__ : Tuple = attention_probs_dropout_prob a__ : List[Any] = type_sequence_label_size a__ : Any = initializer_range a__ : str = scope a__ : List[str] = frequency_stride a__ : Union[str, Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) a__ : List[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 a__ : List[str] = (self.max_length - self.patch_size) // self.time_stride + 1 a__ : Tuple = frequency_out_dimension * time_out_dimension a__ : List[str] = num_patches + 2 def _UpperCamelCase( self : List[str] ): a__ : Any = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) a__ : List[Any] = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : List[str] = self.get_config() return config, input_values, labels def _UpperCamelCase( self : Optional[int] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] ): a__ : List[Any] = ASTModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Dict = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : str ): a__ : Dict = self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ) : Optional[int] = config_and_inputs a__ : List[Any] = {"input_values": input_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) _lowercase = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def _UpperCamelCase( self : str ): a__ : str = ASTModelTester(self ) a__ : Any = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[str] ): self.config_tester.run_common_tests() @unittest.skip(reason="AST does not use inputs_embeds" ) def _UpperCamelCase( self : List[str] ): pass def _UpperCamelCase( self : Optional[int] ): a__, a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Any = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : Tuple ): a__, a__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Dict = model_class(lowerCamelCase__ ) a__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Optional[Any] = ["input_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Union[str, Any] = ASTModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : Optional[int] = hf_hub_download( repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset" ) a__, a__ : List[str] = torchaudio.load(__a ) return audio, sampling_rate @require_torch @require_torchaudio class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _UpperCamelCase( self : List[str] ): return ( ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ) if is_torchaudio_available() else None ) @slow def _UpperCamelCase( self : Optional[int] ): a__ : int = self.default_feature_extractor a__ : Optional[Any] = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ).to(lowerCamelCase__ ) a__ : Any = self.default_feature_extractor a__, a__ : Dict = prepare_audio() a__ : str = audio.squeeze().numpy() a__ : Any = feature_extractor(lowerCamelCase__ , sampling_rate=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(**lowerCamelCase__ ) # verify the logits a__ : Union[str, Any] = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) a__ : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) )
37
0
'''simple docstring''' # Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def UpperCamelCase ( lowercase_ : Tuple ) -> Optional[Any]: '''simple docstring''' return 1 / (1 + np.exp(-z )) def UpperCamelCase ( lowercase_ : Optional[int] , lowercase_ : Dict ) -> List[str]: '''simple docstring''' return (-y * np.log(lowercase_ ) - (1 - y) * np.log(1 - h )).mean() def UpperCamelCase ( lowercase_ : Optional[int] , lowercase_ : Tuple , lowercase_ : Tuple ) -> Union[str, Any]: '''simple docstring''' lowercase =np.dot(lowercase_ , lowercase_ ) return np.sum(y * scores - np.log(1 + np.exp(lowercase_ ) ) ) def UpperCamelCase ( lowercase_ : int , lowercase_ : Tuple , lowercase_ : str , lowercase_ : Optional[int]=7_0_0_0_0 ) -> str: '''simple docstring''' lowercase =np.zeros(x.shape[1] ) for iterations in range(lowercase_ ): lowercase =np.dot(lowercase_ , lowercase_ ) lowercase =sigmoid_function(lowercase_ ) lowercase =np.dot(x.T , h - y ) / y.size lowercase =theta - alpha * gradient # updating the weights lowercase =np.dot(lowercase_ , lowercase_ ) lowercase =sigmoid_function(lowercase_ ) lowercase =cost_function(lowercase_ , lowercase_ ) if iterations % 1_0_0 == 0: print(f'loss: {j} \t' ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": _UpperCAmelCase : List[str] = datasets.load_iris() _UpperCAmelCase : Optional[Any] = iris.data[:, :2] _UpperCAmelCase : Union[str, Any] = (iris.target != 0) * 1 _UpperCAmelCase : List[str] = 0.1 _UpperCAmelCase : List[Any] = logistic_reg(alpha, x, y, max_iterations=7_00_00) print('''theta: ''', theta) # printing the theta i.e our weights vector def UpperCamelCase ( lowercase_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' return sigmoid_function( np.dot(lowercase_ , lowercase_ ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color='''b''', label='''0''') plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color='''r''', label='''1''') ((_UpperCAmelCase) , (_UpperCAmelCase)) : List[Any] = (x[:, 0].min(), x[:, 0].max()) ((_UpperCAmelCase) , (_UpperCAmelCase)) : Union[str, Any] = (x[:, 1].min(), x[:, 1].max()) ((_UpperCAmelCase) , (_UpperCAmelCase)) : int = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) _UpperCAmelCase : int = np.c_[xxa.ravel(), xxa.ravel()] _UpperCAmelCase : Union[str, Any] = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='''black''') plt.legend() plt.show()
72
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast 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 UpperCamelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = XGLMTokenizer _lowercase = XGLMTokenizerFast _lowercase = True _lowercase = True def _UpperCamelCase( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase( self : List[Any] ): a__ : int = "<pad>" a__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(lowerCamelCase__ ) , 1_008 ) def _UpperCamelCase( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def _UpperCamelCase( self : Optional[int] ): a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) a__ : List[str] = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowerCamelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) a__ : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a__ : List[str] = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) a__ : Dict = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _UpperCamelCase( self : Dict ): return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def _UpperCamelCase( self : Union[str, Any] ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCamelCase__ , f.name ) a__ : Any = XGLMTokenizer(f.name , keep_accents=lowerCamelCase__ ) a__ : List[str] = pickle.dumps(lowerCamelCase__ ) pickle.loads(lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): if not self.test_rust_tokenizer: return a__ : Any = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : Tuple = "I was born in 92000, and this is falsé." a__ : List[str] = tokenizer.tokenize(lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : Optional[int] = tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = self.get_rust_tokenizer() a__ : Tuple = tokenizer.encode(lowerCamelCase__ ) a__ : Optional[Any] = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = "Hello World!" a__ : List[str] = [2, 31_227, 4_447, 35] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Union[str, Any] ): a__ : Optional[int] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off a__ : Union[str, Any] = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : List[Any] ): # fmt: off a__ : Optional[int] = { "input_ids": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], "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, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name="facebook/xglm-564M" , padding=lowerCamelCase__ , )
37
0
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ : str = logging.get_logger(__name__) a_ : Any = { 'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json', } class _snake_case ( A__ ): _lowercase : List[Any] = '''lxmert''' _lowercase : Any = {} def __init__( self , a=3_0522 , a=768 , a=12 , a=9500 , a=1600 , a=400 , a=3072 , a="gelu" , a=0.1 , a=0.1 , a=512 , a=2 , a=0.02 , a=1E-12 , a=9 , a=5 , a=5 , a=2048 , a=4 , a=6.67 , a=True , a=True , a=True , a=True , a=True , a=True , a=True , **a , ) -> Optional[int]: SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = type_vocab_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = num_qa_labels SCREAMING_SNAKE_CASE = num_object_labels SCREAMING_SNAKE_CASE = num_attr_labels SCREAMING_SNAKE_CASE = l_layers SCREAMING_SNAKE_CASE = x_layers SCREAMING_SNAKE_CASE = r_layers SCREAMING_SNAKE_CASE = visual_feat_dim SCREAMING_SNAKE_CASE = visual_pos_dim SCREAMING_SNAKE_CASE = visual_loss_normalizer SCREAMING_SNAKE_CASE = task_matched SCREAMING_SNAKE_CASE = task_mask_lm SCREAMING_SNAKE_CASE = task_obj_predict SCREAMING_SNAKE_CASE = task_qa SCREAMING_SNAKE_CASE = visual_obj_loss SCREAMING_SNAKE_CASE = visual_attr_loss SCREAMING_SNAKE_CASE = visual_feat_loss SCREAMING_SNAKE_CASE = {'vision': r_layers, 'cross_encoder': x_layers, 'language': l_layers} super().__init__(**a)
73
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCamelCase_ ( ) -> int: a__ : int = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg" a__ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ).convert("RGB" ) return image def UpperCamelCase_ ( __a ) -> Optional[Any]: a__ : Any = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.embeddings.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.embeddings.layernorm.bias") ) # fmt: on return rename_keys def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Union[str, Any] = dct.pop(__a ) a__ : List[str] = val def UpperCamelCase_ ( __a , __a ) -> Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__ : Any = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) a__ : Tuple = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict a__ : str = torch.cat((q_bias, torch.zeros_like(__a , requires_grad=__a ), v_bias) ) a__ : int = qkv_bias def UpperCamelCase_ ( __a ) -> Dict: a__ : Tuple = 364 if "coco" in model_name else 224 a__ : int = InstructBlipVisionConfig(image_size=__a ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: a__ : Tuple = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__ : Dict = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: a__ : List[Any] = LlamaConfig.from_pretrained("decapoda-research/llama-7b-hf" , vocab_size=32_001 ).to_dict() elif "vicuna-13b" in model_name: a__ : Optional[int] = LlamaConfig.from_pretrained("decapoda-research/llama-13b-hf" , vocab_size=32_001 ).to_dict() else: raise ValueError("Model name not supported" ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 a__ : Optional[Any] = InstructBlipQFormerConfig(vocab_size=30_523 ).to_dict() a__ : Any = InstructBlipConfig(vision_config=__a , text_config=__a , qformer_config=__a ) return config, image_size @torch.no_grad() def UpperCamelCase_ ( __a , __a=None , __a=False ) -> int: a__ : Tuple = AutoTokenizer.from_pretrained("bert-base-uncased" , truncation_side="left" ) qformer_tokenizer.add_special_tokens({"bos_token": "[DEC]"} ) if "t5" in model_name: a__ : List[Any] = TaTokenizerFast.from_pretrained("google/flan-t5-xl" , truncation_side="left" ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) a__ : Union[str, Any] = LlamaTokenizerFast.from_pretrained( "huggyllama/llama-7b" , truncation_side="left" , bos_token="</s>" , unk_token="</s>" ) tokenizer.add_special_tokens({"pad_token": "[PAD]"} ) a__, a__ : List[str] = get_blipa_config(__a ) a__ : Any = InstructBlipForConditionalGeneration(__a ).eval() a__ : Dict = { "instructblip-vicuna-7b": ("blip2_vicuna_instruct", "vicuna7b"), "instructblip-vicuna-13b": ("blip2_vicuna_instruct", "vicuna13b"), "instructblip-flan-t5-xl": ("blip2_t5_instruct", "flant5xl"), "instructblip-flan-t5-xxl": ("blip2_t5_instruct", "flant5xxl"), } a__, a__ : Dict = model_name_to_original[model_name] # load original model print("Loading original model..." ) a__ : Optional[Any] = "cuda:1" if torch.cuda.is_available() else "cpu" a__ : List[Any] = "cuda:2" if torch.cuda.is_available() else "cpu" a__, a__, a__ : Tuple = load_model_and_preprocess( name=__a , model_type=__a , is_eval=__a , device=__a ) original_model.eval() print("Done!" ) # update state dict keys a__ : Dict = original_model.state_dict() a__ : Optional[int] = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a , __a , __a ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__ : Optional[int] = state_dict.pop(__a ) if key.startswith("Qformer.bert" ): a__ : List[Any] = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: a__ : Any = key.replace("self" , "attention" ) if "llm_proj" in key: a__ : Dict = key.replace("llm_proj" , "language_projection" ) if "t5_proj" in key: a__ : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("llm_model" ): a__ : List[str] = key.replace("llm_model" , "language_model" ) if key.startswith("t5" ): a__ : str = key.replace("t5" , "language" ) a__ : Dict = val # read in qv biases read_in_q_v_bias(__a , __a ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(__a , strict=__a ) a__ : Union[str, Any] = load_demo_image() a__ : int = "What is unusual about this image?" # create processor a__ : Any = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__a , image_std=__a ) a__ : Tuple = InstructBlipProcessor( image_processor=__a , tokenizer=__a , qformer_tokenizer=__a , ) a__ : Tuple = processor(images=__a , text=__a , return_tensors="pt" ).to(__a ) # make sure processor creates exact same pixel values a__ : Optional[int] = vis_processors["eval"](__a ).unsqueeze(0 ).to(__a ) a__ : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , __a ) original_model.to(__a ) hf_model.to(__a ) with torch.no_grad(): if "vicuna" in model_name: a__ : str = original_model({"image": original_pixel_values, "text_input": [prompt]} ).logits a__ : List[str] = hf_model(**__a ).logits else: a__ : List[Any] = original_model( {"image": original_pixel_values, "text_input": [prompt], "text_output": ["\n"]} ).logits a__ : str = tokenizer("\n" , return_tensors="pt" ).input_ids.to(__a ) a__ : Dict = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) a__ : Any = hf_model(**__a , labels=__a ).logits print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape a__ : Tuple = 1e-4 if "vicuna" in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , __a , atol=__a ) print("Looks ok!" ) print("Generating with original model..." ) a__ : Tuple = original_model.generate({"image": original_pixel_values, "prompt": prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print("Generating with HF model..." ) a__ : int = hf_model.generate( **__a , do_sample=__a , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? a__ : int = 2 print("Original generation:" , __a ) a__ : str = processor.batch_decode(__a , skip_special_tokens=__a ) a__ : str = [text.strip() for text in output_text] print("HF generation:" , __a ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() UpperCamelCase : Optional[int] = [ """instructblip-vicuna-7b""", """instructblip-vicuna-13b""", """instructblip-flan-t5-xl""", """instructblip-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""instructblip-flan-t5-xl""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase : Dict = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
37
0
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowercase_ = datasets.utils.logging.get_logger(__name__) class __UpperCamelCase ( folder_based_builder.FolderBasedBuilderConfig ): """simple docstring""" lowerCAmelCase_ = None lowerCAmelCase_ = None class __UpperCamelCase ( folder_based_builder.FolderBasedBuilder ): """simple docstring""" lowerCAmelCase_ = datasets.Audio() lowerCAmelCase_ = '''audio''' lowerCAmelCase_ = AudioFolderConfig lowerCAmelCase_ = 42 # definition at the bottom of the script lowerCAmelCase_ = AudioClassification(audio_column='''audio''' , label_column='''label''' ) lowercase_ = [ """.aiff""", """.au""", """.avr""", """.caf""", """.flac""", """.htk""", """.svx""", """.mat4""", """.mat5""", """.mpc2k""", """.ogg""", """.paf""", """.pvf""", """.raw""", """.rf64""", """.sd2""", """.sds""", """.ircam""", """.voc""", """.w64""", """.wav""", """.nist""", """.wavex""", """.wve""", """.xi""", """.mp3""", """.opus""", ] lowercase_ = AUDIO_EXTENSIONS
74
def UpperCamelCase_ ( __a , __a ) -> Tuple: a__ : Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 a__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. a__ : Any = min(__a , __a ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
37
0
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCamelCase_ ( __a ): lowerCAmelCase__ = ['image_processor', 'tokenizer'] lowerCAmelCase__ = 'ViTImageProcessor' lowerCAmelCase__ = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : int , _A : Tuple=None , _A : Optional[Any]=None , **_A : Any ): '''simple docstring''' UpperCAmelCase__ : Dict = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _A , ) UpperCAmelCase__ : Optional[Any] = kwargs.pop('''feature_extractor''' ) UpperCAmelCase__ : Optional[Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(_A , _A ) def __call__( self : Optional[Any] , _A : str=None , _A : Union[str, Any]=None , _A : int=None , _A : Tuple=None , **_A : int ): '''simple docstring''' if text is None and visual_prompt is None and images is None: raise ValueError('''You have to specify either text, visual prompt or images.''' ) if text is not None and visual_prompt is not None: raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.''' ) if text is not None: UpperCAmelCase__ : List[str] = self.tokenizer(_A , return_tensors=_A , **_A ) if visual_prompt is not None: UpperCAmelCase__ : Any = self.image_processor(_A , return_tensors=_A , **_A ) if images is not None: UpperCAmelCase__ : List[Any] = self.image_processor(_A , return_tensors=_A , **_A ) if visual_prompt is not None and images is not None: UpperCAmelCase__ : Optional[Any] = { '''pixel_values''': image_features.pixel_values, '''conditional_pixel_values''': prompt_features.pixel_values, } return encoding elif text is not None and images is not None: UpperCAmelCase__ : Optional[Any] = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: UpperCAmelCase__ : List[str] = { '''conditional_pixel_values''': prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**_A ) , tensor_type=_A ) def lowercase_ ( self : Optional[Any] , *_A : List[Any] , **_A : List[str] ): '''simple docstring''' return self.tokenizer.batch_decode(*_A , **_A ) def lowercase_ ( self : Tuple , *_A : List[Any] , **_A : List[str] ): '''simple docstring''' return self.tokenizer.decode(*_A , **_A ) @property def lowercase_ ( self : Tuple ): '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _A , ) return self.image_processor_class @property def lowercase_ ( self : Dict ): '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _A , ) return self.image_processor
75
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Optional[Any] = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } UpperCamelCase : Dict = { """allenai/led-base-16384""": 1_6384, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = LEDTokenizer _lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[str]=None , lowerCamelCase__ : Any=None , lowerCamelCase__ : int="replace" , lowerCamelCase__ : Union[str, Any]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Tuple="</s>" , lowerCamelCase__ : Optional[int]="<s>" , lowerCamelCase__ : str="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Any="<mask>" , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : int=True , **lowerCamelCase__ : Union[str, Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : List[str] = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : Optional[Any] = add_prefix_space a__ : List[str] = pre_tok_class(**lowerCamelCase__ ) a__ : Optional[int] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ : Any = "post_processor" a__ : str = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Optional[Any] = tuple(state["sep"] ) if "cls" in state: a__ : Optional[Any] = tuple(state["cls"] ) a__ : Optional[int] = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Dict = add_prefix_space a__ : int = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : List[Any] = trim_offsets a__ : List[str] = True if changes_to_apply: a__ : int = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : int = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Union[str, Any] ): a__ : Any = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : Union[str, Any] = value def _UpperCamelCase( self : Any , *lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : List[str] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Any , *lowerCamelCase__ : Dict , **lowerCamelCase__ : Optional[Any] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : List[str] = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[Any]=None ): a__ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : List[str] = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0] def _UpperCamelCase( self : Dict , lowerCamelCase__ : Union[Dict[str, EncodedInput], BatchEncoding] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[bool] = None , ): a__ : str = super()._pad( encoded_inputs=lowerCamelCase__ , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) # Load from model defaults if return_attention_mask is None: a__ : Optional[int] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ : Tuple = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ : Dict = len(encoded_inputs["global_attention_mask"] ) != len(lowerCamelCase__ ) if needs_to_be_padded: a__ : Union[str, Any] = len(lowerCamelCase__ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ : List[Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a__ : Any = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
37
0
"""simple docstring""" import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def __UpperCAmelCase ( __UpperCamelCase ): __lowercase : Optional[Any] = fname.split(os.path.sep )[-1] return re.search(R'''^(.*)_\d+\.jpg$''' , __UpperCamelCase ).groups()[0] class UpperCAmelCase_ ( snake_case ): def __init__( self , UpperCamelCase_ , UpperCamelCase_=None , UpperCamelCase_=None ) -> int: __lowercase : Union[str, Any] = file_names __lowercase : int = image_transform __lowercase : int = label_to_id def __len__( self ) -> Dict: return len(self.file_names ) def __getitem__( self , UpperCamelCase_ ) -> List[Any]: __lowercase : Optional[Any] = self.file_names[idx] __lowercase : Tuple = PIL.Image.open(UpperCamelCase_ ) __lowercase : Any = raw_image.convert('''RGB''' ) if self.image_transform is not None: __lowercase : List[Any] = self.image_transform(UpperCamelCase_ ) __lowercase : int = extract_label(UpperCamelCase_ ) if self.label_to_id is not None: __lowercase : List[Any] = self.label_to_id[label] return {"image": image, "label": label} def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase ): # Initialize accelerator if args.with_tracking: __lowercase : int = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with='''all''' , project_dir=args.project_dir ) else: __lowercase : Dict = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __lowercase : Any = config['''lr'''] __lowercase : Optional[Any] = int(config['''num_epochs'''] ) __lowercase : Union[str, Any] = int(config['''seed'''] ) __lowercase : Tuple = int(config['''batch_size'''] ) __lowercase : Union[str, Any] = config['''image_size'''] if not isinstance(__UpperCamelCase , (list, tuple) ): __lowercase : Any = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , '''isdigit''' ): if args.checkpointing_steps == "epoch": __lowercase : str = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): __lowercase : Optional[int] = int(args.checkpointing_steps ) else: raise ValueError( f"""Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed.""" ) else: __lowercase : Union[str, Any] = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: __lowercase : int = os.path.split(__UpperCamelCase )[-1].split('''.''' )[0] accelerator.init_trackers(__UpperCamelCase , __UpperCamelCase ) # Grab all the image filenames __lowercase : Optional[int] = [os.path.join(args.data_dir , __UpperCamelCase ) for fname in os.listdir(args.data_dir ) if fname.endswith('''.jpg''' )] # Build the label correspondences __lowercase : List[Any] = [extract_label(__UpperCamelCase ) for fname in file_names] __lowercase : int = list(set(__UpperCamelCase ) ) id_to_label.sort() __lowercase : int = {lbl: i for i, lbl in enumerate(__UpperCamelCase )} # Set the seed before splitting the data. np.random.seed(__UpperCamelCase ) torch.manual_seed(__UpperCamelCase ) torch.cuda.manual_seed_all(__UpperCamelCase ) # Split our filenames between train and validation __lowercase : Tuple = np.random.permutation(len(__UpperCamelCase ) ) __lowercase : Dict = int(0.8 * len(__UpperCamelCase ) ) __lowercase : List[str] = random_perm[:cut] __lowercase : Tuple = random_perm[cut:] # For training we use a simple RandomResizedCrop __lowercase : str = Compose([RandomResizedCrop(__UpperCamelCase , scale=(0.5, 1.0) ), ToTensor()] ) __lowercase : Tuple = PetsDataset( [file_names[i] for i in train_split] , image_transform=__UpperCamelCase , label_to_id=__UpperCamelCase ) # For evaluation, we use a deterministic Resize __lowercase : Tuple = Compose([Resize(__UpperCamelCase ), ToTensor()] ) __lowercase : Union[str, Any] = PetsDataset([file_names[i] for i in eval_split] , image_transform=__UpperCamelCase , label_to_id=__UpperCamelCase ) # Instantiate dataloaders. __lowercase : Any = DataLoader(__UpperCamelCase , shuffle=__UpperCamelCase , batch_size=__UpperCamelCase , num_workers=4 ) __lowercase : Optional[int] = DataLoader(__UpperCamelCase , shuffle=__UpperCamelCase , batch_size=__UpperCamelCase , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __lowercase : Optional[Any] = create_model('''resnet50d''' , pretrained=__UpperCamelCase , num_classes=len(__UpperCamelCase ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). __lowercase : List[str] = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): __lowercase : Optional[Any] = False for param in model.get_classifier().parameters(): __lowercase : Dict = True # We normalize the batches of images to be a bit faster. __lowercase : Optional[Any] = torch.tensor(model.default_cfg['''mean'''] )[None, :, None, None].to(accelerator.device ) __lowercase : int = torch.tensor(model.default_cfg['''std'''] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer __lowercase : Union[str, Any] = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler __lowercase : Optional[int] = OneCycleLR(optimizer=__UpperCamelCase , max_lr=__UpperCamelCase , epochs=__UpperCamelCase , steps_per_epoch=len(__UpperCamelCase ) ) # 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. __lowercase ,__lowercase ,__lowercase ,__lowercase ,__lowercase : Optional[Any] = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # We need to keep track of how many total steps we have iterated over __lowercase : Optional[Any] = 0 # We also need to keep track of the starting epoch so files are named properly __lowercase : Optional[Any] = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(f"""Resumed from checkpoint: {args.resume_from_checkpoint}""" ) accelerator.load_state(args.resume_from_checkpoint ) __lowercase : Optional[int] = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint __lowercase : List[Any] = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) __lowercase : Dict = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` __lowercase : Union[str, Any] = os.path.splitext(__UpperCamelCase )[0] if "epoch" in training_difference: __lowercase : Any = int(training_difference.replace('''epoch_''' , '''''' ) ) + 1 __lowercase : Optional[int] = None else: __lowercase : Union[str, Any] = int(training_difference.replace('''step_''' , '''''' ) ) __lowercase : Dict = resume_step // len(__UpperCamelCase ) resume_step -= starting_epoch * len(__UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase , __UpperCamelCase ): model.train() if args.with_tracking: __lowercase : Optional[int] = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step __lowercase : str = accelerator.skip_first_batches(__UpperCamelCase , __UpperCamelCase ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader __lowercase : Optional[Any] = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. __lowercase : int = {k: v.to(accelerator.device ) for k, v in batch.items()} __lowercase : Dict = (batch['''image'''] - mean) / std __lowercase : List[str] = model(__UpperCamelCase ) __lowercase : str = torch.nn.functional.cross_entropy(__UpperCamelCase , batch['''label'''] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__UpperCamelCase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__UpperCamelCase , __UpperCamelCase ): __lowercase : Union[str, Any] = f"""step_{overall_step}""" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: __lowercase : Optional[int] = os.path.join(args.output_dir , __UpperCamelCase ) accelerator.save_state(__UpperCamelCase ) model.eval() __lowercase : List[str] = 0 __lowercase : Tuple = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. __lowercase : int = {k: v.to(accelerator.device ) for k, v in batch.items()} __lowercase : int = (batch['''image'''] - mean) / std with torch.no_grad(): __lowercase : Dict = model(__UpperCamelCase ) __lowercase : Optional[Any] = outputs.argmax(dim=-1 ) __lowercase ,__lowercase : List[str] = accelerator.gather_for_metrics((predictions, batch['''label''']) ) __lowercase : Optional[int] = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() __lowercase : Tuple = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}: {1_00 * eval_metric:.2f}""" ) if args.with_tracking: accelerator.log( { '''accuracy''': 1_00 * eval_metric, '''train_loss''': total_loss.item() / len(__UpperCamelCase ), '''epoch''': epoch, } , step=__UpperCamelCase , ) if checkpointing_steps == "epoch": __lowercase : Optional[Any] = f"""epoch_{epoch}""" if args.output_dir is not None: __lowercase : Optional[int] = os.path.join(args.output_dir , __UpperCamelCase ) accelerator.save_state(__UpperCamelCase ) if args.with_tracking: accelerator.end_training() def __UpperCAmelCase ( ): __lowercase : Any = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument('''--data_dir''' , required=__UpperCamelCase , help='''The data folder on disk.''' ) parser.add_argument('''--fp16''' , action='''store_true''' , help='''If passed, will use FP16 training.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) parser.add_argument( '''--checkpointing_steps''' , type=__UpperCamelCase , default=__UpperCamelCase , help='''Whether the various states should be saved at the end of every n steps, or \'epoch\' for each epoch.''' , ) parser.add_argument( '''--output_dir''' , type=__UpperCamelCase , 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=__UpperCamelCase , default=__UpperCamelCase , help='''If the training should continue from a checkpoint folder.''' , ) parser.add_argument( '''--with_tracking''' , action='''store_true''' , help='''Whether to load in all available experiment trackers from the environment and use them for logging.''' , ) parser.add_argument( '''--project_dir''' , type=__UpperCamelCase , default='''logs''' , help='''Location on where to store experiment tracking logs` and relevent project information''' , ) __lowercase : Optional[Any] = parser.parse_args() __lowercase : str = {'''lr''': 3e-2, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 64, '''image_size''': 2_24} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
76
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Any = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Union[str, Any] = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : List[str] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = ['input_ids', 'attention_mask'] _lowercase = RobertaTokenizer def __init__( self : List[str] , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]="replace" , lowerCamelCase__ : List[str]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Any="</s>" , lowerCamelCase__ : Any="<s>" , lowerCamelCase__ : int="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Tuple="<mask>" , lowerCamelCase__ : Any=False , lowerCamelCase__ : Dict=True , **lowerCamelCase__ : Optional[Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Any = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : int = add_prefix_space a__ : Tuple = pre_tok_class(**lowerCamelCase__ ) a__ : str = add_prefix_space a__ : Tuple = "post_processor" a__ : Dict = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Tuple = tuple(state["sep"] ) if "cls" in state: a__ : str = tuple(state["cls"] ) a__ : str = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : str = add_prefix_space a__ : Any = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : int = trim_offsets a__ : Dict = True if changes_to_apply: a__ : Union[str, Any] = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : str = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : Tuple ): a__ : List[Any] = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : List[str] = value def _UpperCamelCase( self : Union[str, Any] , *lowerCamelCase__ : int , **lowerCamelCase__ : int ): a__ : Optional[int] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Tuple , *lowerCamelCase__ : Dict , **lowerCamelCase__ : List[str] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : int = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int]=None ): a__ : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Dict , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : Tuple = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0]
37
0
"""simple docstring""" def _UpperCamelCase ( ) -> int: """simple docstring""" return [ a * b * (1000 - a - b) for a in range(1 , 999 ) for b in range(UpperCamelCase , 999 ) if (a * a + b * b == (1000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f'''{solution() = }''')
77
from statistics import mean, stdev def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : List[str] = min(__a ) a__ : str = max(__a ) # normalize data return [round((x - x_min) / (x_max - x_min) , __a ) for x in data] def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : str = mean(__a ) a__ : List[str] = stdev(__a ) # standardize data return [round((x - mu) / (sigma) , __a ) for x in data]
37
0
'''simple docstring''' def lowerCAmelCase_ ( snake_case_ : list ) -> list: '''simple docstring''' UpperCAmelCase_ = len(snake_case_ ) for _ in range(snake_case_ ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: UpperCAmelCase_ , UpperCAmelCase_ = arr[i + 1], arr[i] return arr if __name__ == "__main__": SCREAMING_SNAKE_CASE_: Optional[int] =list(range(10, 0, -1)) print(f"Original: {arr}. Sorted: {odd_even_transposition(arr)}")
78
def UpperCamelCase_ ( __a = 50 ) -> int: a__ : Tuple = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
37
0
from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
79
class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] ): a__ : str = name a__ : Optional[int] = value a__ : Dict = weight def __repr__( self : Union[str, Any] ): return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def _UpperCamelCase( self : Dict ): return self.value def _UpperCamelCase( self : Optional[Any] ): return self.name def _UpperCamelCase( self : Optional[Any] ): return self.weight def _UpperCamelCase( self : Optional[int] ): return self.value / self.weight def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = [] for i in range(len(__a ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def UpperCamelCase_ ( __a , __a , __a ) -> Union[str, Any]: a__ : List[str] = sorted(__a , key=__a , reverse=__a ) a__ : List[Any] = [] a__, a__ : Union[str, Any] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def UpperCamelCase_ ( ) -> Union[str, Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
37
0
import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": __UpperCamelCase : Tuple = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( """--original_config_file""", default=None, type=str, 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( """--scheduler_type""", default="""pndm""", type=str, help="""Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']""", ) parser.add_argument( """--pipeline_type""", default=None, type=str, help=( """The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'""" """. If `None` pipeline will be automatically inferred.""" ), ) parser.add_argument( """--image_size""", default=None, 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( """--prediction_type""", default=None, type=str, help=( """The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable""" """ Diffusion v2 Base. Use 'v_prediction' 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.)""") parser.add_argument( """--stable_unclip""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.""", ) parser.add_argument( """--stable_unclip_prior""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.""", ) parser.add_argument( """--clip_stats_path""", type=str, help="""Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.""", required=False, ) parser.add_argument( """--controlnet""", action="""store_true""", default=None, help="""Set flag if this is a controlnet checkpoint.""" ) parser.add_argument("""--half""", action="""store_true""", help="""Save weights in half precision.""") parser.add_argument( """--vae_path""", type=str, default=None, required=False, help="""Set to a path, hub id to an already converted vae to not convert it again.""", ) __UpperCamelCase : str = parser.parse_args() __UpperCamelCase : str = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
80
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class A__ ( A__ ): """simple docstring""" def __init__( self : Dict , lowerCamelCase__ : Union[str, "sqlalchemy.sql.Selectable"] , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[Features] = None , lowerCamelCase__ : str = None , lowerCamelCase__ : bool = False , **lowerCamelCase__ : Optional[int] , ): super().__init__(features=lowerCamelCase__ , cache_dir=lowerCamelCase__ , keep_in_memory=lowerCamelCase__ , **lowerCamelCase__ ) a__ : str = Sql( cache_dir=lowerCamelCase__ , features=lowerCamelCase__ , sql=lowerCamelCase__ , con=lowerCamelCase__ , **lowerCamelCase__ , ) def _UpperCamelCase( self : Tuple ): a__ : Optional[Any] = None a__ : Dict = None a__ : Union[str, Any] = None a__ : Union[str, Any] = None self.builder.download_and_prepare( download_config=lowerCamelCase__ , download_mode=lowerCamelCase__ , verification_mode=lowerCamelCase__ , base_path=lowerCamelCase__ , ) # Build dataset for splits a__ : List[str] = self.builder.as_dataset( split="train" , verification_mode=lowerCamelCase__ , in_memory=self.keep_in_memory ) return dataset class A__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase__ : Dataset , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[int] = None , **lowerCamelCase__ : Optional[Any] , ): if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) a__ : Any = dataset a__ : str = name a__ : Tuple = con a__ : List[Any] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a__ : Any = num_proc a__ : Tuple = to_sql_kwargs def _UpperCamelCase( self : List[Any] ): a__ : Any = self.to_sql_kwargs.pop("sql" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("con" , lowerCamelCase__ ) a__ : int = self.to_sql_kwargs.pop("index" , lowerCamelCase__ ) a__ : int = self._write(index=lowerCamelCase__ , **self.to_sql_kwargs ) return written def _UpperCamelCase( self : Any , lowerCamelCase__ : List[str] ): a__, a__, a__ : Union[str, Any] = args a__ : Any = {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs a__ : Tuple = query_table( table=self.dataset.data , key=slice(lowerCamelCase__ , offset + self.batch_size ) , indices=self.dataset._indices , ) a__ : str = batch.to_pandas() a__ : List[Any] = df.to_sql(self.name , self.con , index=lowerCamelCase__ , **lowerCamelCase__ ) return num_rows or len(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] , lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : str = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: a__, a__ : List[str] = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , lowerCamelCase__ , lowerCamelCase__ )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit="ba" , disable=not logging.is_progress_bar_enabled() , desc="Creating SQL from Arrow format" , ): written += num_rows return written
37
0
# Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() _snake_case : Any = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model _snake_case : List[str] = { # fairseq: "wmt19-ru-en": {"length_penalty": 1.1}, "wmt19-en-ru": {"length_penalty": 1.15}, "wmt19-en-de": {"length_penalty": 1.0}, "wmt19-de-en": {"length_penalty": 1.1}, # allenai: "wmt16-en-de-dist-12-1": {"length_penalty": 0.6}, "wmt16-en-de-dist-6-1": {"length_penalty": 0.6}, "wmt16-en-de-12-1": {"length_penalty": 0.8}, "wmt19-de-en-6-6-base": {"length_penalty": 0.6}, "wmt19-de-en-6-6-big": {"length_penalty": 0.6}, } # this remaps the different models to their organization names _snake_case : int = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: _snake_case : Optional[Any] = "facebook" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: _snake_case : Optional[Any] = "allenai" def lowerCAmelCase_ ( __lowerCamelCase ): # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} __snake_case : List[Any] = dict((re.sub(R"@@$" , "" , __lowerCamelCase ), v) if k.endswith("@@" ) else (re.sub(R"$" , "</w>" , __lowerCamelCase ), v) for k, v in d.items() ) __snake_case : Tuple = "<s> <pad> </s> <unk>".split() # restore the special tokens for k in keep_keys: del da[F'{k}</w>'] __snake_case : Optional[int] = d[k] # restore return da def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): # prep assert os.path.exists(__lowerCamelCase ) os.makedirs(__lowerCamelCase , exist_ok=__lowerCamelCase ) print(F'Writing results to {pytorch_dump_folder_path}' ) # handle various types of models __snake_case : Any = basename(__lowerCamelCase ) __snake_case : Union[str, Any] = dirname(__lowerCamelCase ) __snake_case : int = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel __snake_case : Optional[Any] = cls.hub_models() __snake_case : Union[str, Any] = {"bpe": "fastbpe", "tokenizer": "moses"} __snake_case : Any = "." # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(F'using checkpoint {checkpoint_file}' ) __snake_case : Dict = hub_utils.from_pretrained( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , archive_map=__lowerCamelCase , **__lowerCamelCase ) __snake_case : List[Any] = vars(chkpt["args"]["model"] ) __snake_case : int = args["source_lang"] __snake_case : str = args["target_lang"] __snake_case : List[str] = dirname(__lowerCamelCase ) __snake_case : str = basename(__lowerCamelCase ) # dicts __snake_case : str = os.path.join(__lowerCamelCase , F'dict.{src_lang}.txt' ) __snake_case : List[str] = os.path.join(__lowerCamelCase , F'dict.{tgt_lang}.txt' ) __snake_case : List[str] = Dictionary.load(__lowerCamelCase ) __snake_case : Any = rewrite_dict_keys(src_dict.indices ) __snake_case : str = len(__lowerCamelCase ) __snake_case : List[str] = os.path.join(__lowerCamelCase , "vocab-src.json" ) print(F'Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records' ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__lowerCamelCase , ensure_ascii=__lowerCamelCase , indent=__lowerCamelCase ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab __snake_case : Tuple = True for k in src_vocab.keys(): if not k.islower(): __snake_case : Tuple = False break __snake_case : Dict = Dictionary.load(__lowerCamelCase ) __snake_case : List[Any] = rewrite_dict_keys(tgt_dict.indices ) __snake_case : Any = len(__lowerCamelCase ) __snake_case : List[str] = os.path.join(__lowerCamelCase , "vocab-tgt.json" ) print(F'Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records' ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__lowerCamelCase , ensure_ascii=__lowerCamelCase , indent=__lowerCamelCase ) ) # merges_file (bpecodes) __snake_case : Union[str, Any] = os.path.join(__lowerCamelCase , VOCAB_FILES_NAMES["merges_file"] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" __snake_case : Any = os.path.join(__lowerCamelCase , __lowerCamelCase ) if os.path.exists(__lowerCamelCase ): break with open(__lowerCamelCase , encoding="utf-8" ) as fin: __snake_case : int = fin.read() __snake_case : Any = re.sub(R" \d+$" , "" , __lowerCamelCase , 0 , re.M ) # remove frequency number print(F'Generating {merges_file}' ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as fout: fout.write(__lowerCamelCase ) # model config __snake_case : str = os.path.join(__lowerCamelCase , "config.json" ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", F'need to extend tokenizer to support bpe={args["bpe"]}' assert args["tokenizer"] == "moses", F'need to extend tokenizer to support bpe={args["tokenizer"]}' __snake_case : List[Any] = { "architectures": ["FSMTForConditionalGeneration"], "model_type": "fsmt", "activation_dropout": args["activation_dropout"], "activation_function": "relu", "attention_dropout": args["attention_dropout"], "d_model": args["decoder_embed_dim"], "dropout": args["dropout"], "init_std": 0.0_2, "max_position_embeddings": args["max_source_positions"], "num_hidden_layers": args["encoder_layers"], "src_vocab_size": src_vocab_size, "tgt_vocab_size": tgt_vocab_size, "langs": [src_lang, tgt_lang], "encoder_attention_heads": args["encoder_attention_heads"], "encoder_ffn_dim": args["encoder_ffn_embed_dim"], "encoder_layerdrop": args["encoder_layerdrop"], "encoder_layers": args["encoder_layers"], "decoder_attention_heads": args["decoder_attention_heads"], "decoder_ffn_dim": args["decoder_ffn_embed_dim"], "decoder_layerdrop": args["decoder_layerdrop"], "decoder_layers": args["decoder_layers"], "bos_token_id": 0, "pad_token_id": 1, "eos_token_id": 2, "is_encoder_decoder": True, "scale_embedding": not args["no_scale_embedding"], "tie_word_embeddings": args["share_all_embeddings"], } # good hparam defaults to start with __snake_case : Optional[Any] = 5 __snake_case : List[Any] = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: __snake_case : List[str] = best_score_hparams[model_dir]["length_penalty"] else: __snake_case : Tuple = 1.0 print(F'Generating {fsmt_model_config_file}' ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__lowerCamelCase , ensure_ascii=__lowerCamelCase , indent=__lowerCamelCase ) ) # tokenizer config __snake_case : List[str] = os.path.join(__lowerCamelCase , __lowerCamelCase ) __snake_case : Tuple = { "langs": [src_lang, tgt_lang], "model_max_length": 1_0_2_4, "do_lower_case": do_lower_case, } print(F'Generating {fsmt_tokenizer_config_file}' ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__lowerCamelCase , ensure_ascii=__lowerCamelCase , indent=__lowerCamelCase ) ) # model __snake_case : List[Any] = chkpt["models"][0] __snake_case : Tuple = model.state_dict() # rename keys to start with 'model.' __snake_case : Union[str, Any] = OrderedDict(("model." + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys __snake_case : Any = [ "model.model", "model.encoder.version", "model.decoder.version", "model.encoder_embed_tokens.weight", "model.decoder_embed_tokens.weight", "model.encoder.embed_positions._float_tensor", "model.decoder.embed_positions._float_tensor", ] for k in ignore_keys: model_state_dict.pop(__lowerCamelCase , __lowerCamelCase ) __snake_case : Union[str, Any] = FSMTConfig.from_pretrained(__lowerCamelCase ) __snake_case : Union[str, Any] = FSMTForConditionalGeneration(__lowerCamelCase ) # check that it loads ok model_new.load_state_dict(__lowerCamelCase , strict=__lowerCamelCase ) # save __snake_case : int = os.path.join(__lowerCamelCase , __lowerCamelCase ) print(F'Generating {pytorch_weights_dump_path}' ) torch.save(__lowerCamelCase , __lowerCamelCase ) print("Conversion is done!" ) print("\nLast step is to upload the files to s3" ) print(F'cd {data_root}' ) print(F'transformers-cli upload {model_dir}' ) if __name__ == "__main__": _snake_case : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fsmt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) _snake_case : Any = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
81
import math from datetime import datetime, timedelta def UpperCamelCase_ ( __a ) -> datetime: a__ : Union[str, Any] = year % 19 a__ : List[str] = year % 4 a__ : str = year % 7 a__ : Any = math.floor(year / 100 ) a__ : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) a__ : Optional[int] = leap_day_inhibits / 4 a__ : Union[str, Any] = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 a__ : Dict = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 a__ : Any = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon a__ : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(__a , 4 , 18 ) else: return datetime(__a , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): UpperCamelCase : Tuple = """will be""" if year > datetime.now().year else """was""" print(f"""Easter in {year} {tense} {gauss_easter(year)}""")
37
0
"""simple docstring""" import gc import threading import time import psutil import torch class lowercase__ : '''simple docstring''' def __init__( self : Tuple ) -> Dict: '''simple docstring''' UpperCAmelCase_ = psutil.Process() UpperCAmelCase_ = False def lowercase__ ( self : Optional[Any] ) -> List[str]: '''simple docstring''' UpperCAmelCase_ = -1 while True: UpperCAmelCase_ = max(self.process.memory_info().rss , self.cpu_memory_peak ) # can't sleep or will not catch the peak right (this comment is here on purpose) if not self.peak_monitoring: break def lowercase__ ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase_ = True UpperCAmelCase_ = threading.Thread(target=self.peak_monitor ) UpperCAmelCase_ = True self.thread.start() def lowercase__ ( self : str ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase_ = False self.thread.join() return self.cpu_memory_peak lowerCamelCase = PeakCPUMemory() def a__ ( ): # Time UpperCAmelCase_ = {"time": time.time()} gc.collect() torch.cuda.empty_cache() # CPU mem UpperCAmelCase_ = psutil.Process().memory_info().rss cpu_peak_tracker.start() # GPU mem for i in range(torch.cuda.device_count() ): UpperCAmelCase_ = torch.cuda.memory_allocated(lowerCAmelCase__ ) torch.cuda.reset_peak_memory_stats() return measures def a__ ( lowerCAmelCase__ ): # Time UpperCAmelCase_ = {"time": time.time() - start_measures["time"]} gc.collect() torch.cuda.empty_cache() # CPU mem UpperCAmelCase_ = (psutil.Process().memory_info().rss - start_measures["cpu"]) / 2**20 UpperCAmelCase_ = (cpu_peak_tracker.stop() - start_measures["cpu"]) / 2**20 # GPU mem for i in range(torch.cuda.device_count() ): UpperCAmelCase_ = (torch.cuda.memory_allocated(lowerCAmelCase__ ) - start_measures[str(lowerCAmelCase__ )]) / 2**20 UpperCAmelCase_ = (torch.cuda.max_memory_allocated(lowerCAmelCase__ ) - start_measures[str(lowerCAmelCase__ )]) / 2**20 return measures def a__ ( lowerCAmelCase__ , lowerCAmelCase__ ): print(f"""{description}:""" ) print(f"""- Time: {measures['time']:.2f}s""" ) for i in range(torch.cuda.device_count() ): print(f"""- GPU {i} allocated: {measures[str(lowerCAmelCase__ )]:.2f}MiB""" ) UpperCAmelCase_ = measures[f"""{i}-peak"""] print(f"""- GPU {i} peak: {peak:.2f}MiB""" ) print(f"""- CPU RAM allocated: {measures['cpu']:.2f}MiB""" ) print(f"""- CPU RAM peak: {measures['cpu-peak']:.2f}MiB""" )
82
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def UpperCamelCase_ ( __a ) -> Union[str, Any]: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class A__ ( nn.Module ): """simple docstring""" def __init__( self : List[str] , lowerCamelCase__ : nn.Module , lowerCamelCase__ : int ): super().__init__() a__ : int = module a__ : Any = nn.Sequential( nn.Linear(module.in_features , lowerCamelCase__ , bias=lowerCamelCase__ ) , nn.Linear(lowerCamelCase__ , module.out_features , bias=lowerCamelCase__ ) , ) a__ : Tuple = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=lowerCamelCase__ ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Optional[int] , *lowerCamelCase__ : int , **lowerCamelCase__ : Dict ): return self.module(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) + self.adapter(lowerCamelCase__ ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" _lowercase = 'bigscience/bloom-1b7' # Constant values _lowercase = 2.1_09_65_95_52_69_25_74 _lowercase = 'Hello my name is' _lowercase = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) _lowercase = 1_0 def _UpperCamelCase( self : Dict ): # Models and tokenizer a__ : List[str] = AutoTokenizer.from_pretrained(self.model_name ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Union[str, Any] ): super().setUp() # Models and tokenizer a__ : List[Any] = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map="auto" ) a__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : List[Any] ): a__ : str = self.model_abit.config self.assertTrue(hasattr(lowerCamelCase__ , "quantization_config" ) ) a__ : Optional[Any] = config.to_dict() a__ : int = config.to_diff_dict() a__ : List[str] = config.to_json_string() def _UpperCamelCase( self : int ): from bitsandbytes.nn import Paramsabit a__ : List[Any] = self.model_fpaa.get_memory_footprint() a__ : str = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) a__ : Optional[Any] = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def _UpperCamelCase( self : Tuple ): from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(lowerCamelCase__ , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def _UpperCamelCase( self : str ): a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Tuple = self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[Any] = BitsAndBytesConfig() a__ : Optional[int] = True a__ : int = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , device_map="auto" ) a__ : str = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : int = model_abit_from_config.generate( input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase( self : Dict ): with self.assertRaises(lowerCamelCase__ ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] ): a__ : int = BitsAndBytesConfig() with self.assertRaises(lowerCamelCase__ ): a__ : Dict = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCamelCase__ , load_in_abit=lowerCamelCase__ , device_map="auto" , bnb_abit_quant_type="nf4" , ) def _UpperCamelCase( self : int ): with self.assertRaises(lowerCamelCase__ ): # Tries with `str` self.model_abit.to("cpu" ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.to(torch.device("cuda:0" ) ) with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.float() with self.assertRaises(lowerCamelCase__ ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything a__ : int = self.tokenizer(self.input_text , return_tensors="pt" ) a__ : Any = self.model_fpaa.to(torch.floataa ) a__ : List[Any] = self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.to("cpu" ) # Check this does not throw an error a__ : Tuple = self.model_fpaa.half() # Check this does not throw an error a__ : Dict = self.model_fpaa.float() def _UpperCamelCase( self : Dict ): a__ : List[str] = AutoModelForSeqaSeqLM.from_pretrained("t5-small" , load_in_abit=lowerCamelCase__ , device_map="auto" ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class A__ ( unittest.TestCase ): """simple docstring""" @classmethod def _UpperCamelCase( cls : str ): a__ : Dict = "t5-small" a__ : List[Any] = "google/flan-t5-small" # flan-t5 uses dense-act instead of dense-relu-dense a__ : int = AutoTokenizer.from_pretrained(cls.model_name ) a__ : str = "Translate in German: Hello, my dog is cute" def _UpperCamelCase( self : Optional[int] ): gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Optional[int] ): from transformers import TaForConditionalGeneration a__ : List[Any] = TaForConditionalGeneration._keep_in_fpaa_modules a__ : Optional[Any] = None # test with `t5-small` a__ : Any = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : Dict = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Dict = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Any = model.generate(**lowerCamelCase__ ) a__ : Union[str, Any] = modules def _UpperCamelCase( self : List[Any] ): import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` a__ : List[str] = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) a__ : Union[str, Any] = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : int = model.generate(**lowerCamelCase__ ) # test with `flan-t5-small` a__ : int = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) a__ : Any = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) a__ : Optional[int] = model.generate(**lowerCamelCase__ ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : List[str] ): super().setUp() # model_name a__ : Union[str, Any] = "bigscience/bloom-560m" a__ : Union[str, Any] = "t5-small" # Different types of model a__ : int = AutoModel.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Sequence classification model a__ : Dict = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # CausalLM model a__ : str = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) # Seq2seq model a__ : Dict = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=lowerCamelCase__ , device_map="auto" ) def _UpperCamelCase( self : List[Any] ): del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Union[str, Any] ): from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): super().setUp() def _UpperCamelCase( self : int ): del self.pipe gc.collect() torch.cuda.empty_cache() def _UpperCamelCase( self : Tuple ): a__ : int = pipeline( "text-generation" , model=self.model_name , model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass a__ : Tuple = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]["generated_text"] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Tuple ): super().setUp() def _UpperCamelCase( self : List[Any] ): a__ : str = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=lowerCamelCase__ , device_map="balanced" ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model a__ : List[Any] = self.tokenizer(self.input_text , return_tensors="pt" ) # Second real batch a__ : List[Any] = model_parallel.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=lowerCamelCase__ ) , self.EXPECTED_OUTPUTS ) class A__ ( A__ ): """simple docstring""" def _UpperCamelCase( self : Dict ): a__ : Any = "facebook/opt-350m" super().setUp() def _UpperCamelCase( self : int ): if version.parse(importlib.metadata.version("bitsandbytes" ) ) < version.parse("0.37.0" ): return # Step 1: freeze all parameters a__ : Tuple = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCamelCase__ ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): a__ : Any = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability a__ : Tuple = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(lowerCamelCase__ ) ): a__ : Dict = LoRALayer(module.q_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.k_proj , rank=16 ) a__ : List[Any] = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch a__ : Dict = self.tokenizer("Test batch " , return_tensors="pt" ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): a__ : Optional[Any] = model.forward(**lowerCamelCase__ ) out.logits.norm().backward() for module in model.modules(): if isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(lowerCamelCase__ , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class A__ ( A__ ): """simple docstring""" _lowercase = 'gpt2-xl' _lowercase = 3.31_91_85_48_54_15_21_87
37
0
"""simple docstring""" import numpy as np lowerCAmelCase__ = [ ['''a''', '''b''', '''c''', '''d''', '''e'''], ['''f''', '''g''', '''h''', '''i''', '''k'''], ['''l''', '''m''', '''n''', '''o''', '''p'''], ['''q''', '''r''', '''s''', '''t''', '''u'''], ['''v''', '''w''', '''x''', '''y''', '''z'''], ] class __snake_case : def __init__( self : Optional[Any] ): """simple docstring""" _lowerCamelCase : List[str] = np.array(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[str] , __lowerCAmelCase : str ): """simple docstring""" _lowerCamelCase , _lowerCamelCase : str = np.where(letter == self.SQUARE ) _lowerCamelCase : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : int ): """simple docstring""" _lowerCamelCase : Any = self.SQUARE[indexa - 1, indexa - 1] return letter def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : str ): """simple docstring""" _lowerCamelCase : Tuple = message.lower() _lowerCamelCase : List[Any] = message.replace(''' ''' , '''''' ) _lowerCamelCase : str = message.replace('''j''' , '''i''' ) _lowerCamelCase : Optional[Any] = np.empty((2, len(__lowerCAmelCase )) ) for letter_index in range(len(__lowerCAmelCase ) ): _lowerCamelCase : int = self.letter_to_numbers(message[letter_index] ) _lowerCamelCase : Dict = numbers[0] _lowerCamelCase : Optional[int] = numbers[1] _lowerCamelCase : Tuple = first_step.reshape(2 * len(__lowerCAmelCase ) ) _lowerCamelCase : str = '''''' for numbers_index in range(len(__lowerCAmelCase ) ): _lowerCamelCase : Any = int(second_step[numbers_index * 2] ) _lowerCamelCase : Tuple = int(second_step[(numbers_index * 2) + 1] ) _lowerCamelCase : int = self.numbers_to_letter(__lowerCAmelCase , __lowerCAmelCase ) _lowerCamelCase : List[Any] = encoded_message + letter return encoded_message def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : str ): """simple docstring""" _lowerCamelCase : Optional[int] = message.lower() message.replace(''' ''' , '''''' ) _lowerCamelCase : Tuple = np.empty(2 * len(__lowerCAmelCase ) ) for letter_index in range(len(__lowerCAmelCase ) ): _lowerCamelCase : Optional[int] = self.letter_to_numbers(message[letter_index] ) _lowerCamelCase : Optional[int] = numbers[0] _lowerCamelCase : Union[str, Any] = numbers[1] _lowerCamelCase : Tuple = first_step.reshape((2, len(__lowerCAmelCase )) ) _lowerCamelCase : Union[str, Any] = '''''' for numbers_index in range(len(__lowerCAmelCase ) ): _lowerCamelCase : Dict = int(second_step[0, numbers_index] ) _lowerCamelCase : Union[str, Any] = int(second_step[1, numbers_index] ) _lowerCamelCase : Dict = self.numbers_to_letter(__lowerCAmelCase , __lowerCAmelCase ) _lowerCamelCase : Any = decoded_message + letter return decoded_message
83
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Optional[int]=100 , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[int]=30 , lowerCamelCase__ : Union[str, Any]=2 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Tuple=True , lowerCamelCase__ : int=32 , lowerCamelCase__ : Union[str, Any]=4 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Union[str, Any]=37 , lowerCamelCase__ : List[Any]="gelu" , lowerCamelCase__ : List[Any]=0.1 , lowerCamelCase__ : int=0.1 , lowerCamelCase__ : Union[str, Any]=10 , lowerCamelCase__ : str=0.02 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]=[0, 1, 2, 3] , ): a__ : Dict = parent a__ : Dict = 100 a__ : Optional[int] = batch_size a__ : Union[str, Any] = image_size a__ : Any = patch_size a__ : Optional[Any] = num_channels a__ : int = is_training a__ : List[str] = use_labels a__ : Optional[Any] = hidden_size a__ : List[Any] = num_hidden_layers a__ : str = num_attention_heads a__ : str = intermediate_size a__ : int = hidden_act a__ : List[Any] = hidden_dropout_prob a__ : Dict = attention_probs_dropout_prob a__ : Union[str, Any] = type_sequence_label_size a__ : Optional[Any] = initializer_range a__ : List[str] = scope a__ : int = out_indices a__ : List[str] = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a__ : Optional[int] = (image_size // patch_size) ** 2 a__ : Union[str, Any] = num_patches + 1 def _UpperCamelCase( self : int ): a__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a__ : Optional[Any] = None a__ : Tuple = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) a__ : Optional[int] = self.get_config() return config, pixel_values, labels, pixel_labels def _UpperCamelCase( self : Tuple ): return BeitConfig( vocab_size=self.vocab_size , 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=lowerCamelCase__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any ): a__ : str = BeitModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : List[str] , lowerCamelCase__ : Tuple ): a__ : int = BeitForMaskedImageModeling(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _UpperCamelCase( self : str , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Any ): a__ : List[str] = self.type_sequence_label_size a__ : Optional[Any] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a__ : Optional[Any] = 1 a__ : List[str] = BeitForImageClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a__ : Union[str, Any] = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _UpperCamelCase( self : Any , lowerCamelCase__ : str , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): a__ : int = self.num_labels a__ : List[str] = BeitForSemanticSegmentation(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Tuple = model(lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) a__ : str = model(lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def _UpperCamelCase( self : Optional[int] ): a__ : Any = self.prepare_config_and_inputs() a__, a__, a__, a__ : Union[str, Any] = config_and_inputs a__ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) _lowercase = ( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Any ): a__ : int = BeitModelTester(self ) a__ : Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def _UpperCamelCase( self : str ): pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _UpperCamelCase( self : Dict ): pass def _UpperCamelCase( self : Optional[Any] ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : List[str] = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : str ): a__, a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : int = model_class(lowerCamelCase__ ) a__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def _UpperCamelCase( self : int ): a__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): a__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] ): a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): if not self.model_tester.is_training: return a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : str = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling]: continue a__ : List[str] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.train() a__ : Any = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : Tuple = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : Tuple ): a__, a__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return a__ : List[Any] = False a__ : List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(lowerCamelCase__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue a__ : Optional[Any] = model_class(lowerCamelCase__ ) model.gradient_checkpointing_enable() model.to(lowerCamelCase__ ) model.train() a__ : Union[str, Any] = self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ , return_labels=lowerCamelCase__ ) a__ : int = model(**lowerCamelCase__ ).loss loss.backward() def _UpperCamelCase( self : List[str] ): a__, a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : Dict = _config_zero_init(lowerCamelCase__ ) for model_class in self.all_model_classes: a__ : str = model_class(config=lowerCamelCase__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _UpperCamelCase( self : Optional[int] ): for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Tuple = BeitModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : List[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 _UpperCamelCase( self : Optional[int] ): return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def _UpperCamelCase( self : str ): a__ : int = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(lowerCamelCase__ ) a__ : Optional[Any] = self.default_image_processor a__ : Dict = prepare_img() a__ : Optional[int] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).pixel_values.to(lowerCamelCase__ ) # prepare bool_masked_pos a__ : Optional[Any] = torch.ones((1, 196) , dtype=torch.bool ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(pixel_values=lowerCamelCase__ , bool_masked_pos=lowerCamelCase__ ) a__ : Tuple = outputs.logits # verify the logits a__ : List[str] = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[int] = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , lowerCamelCase__ , atol=1E-2 ) ) @slow def _UpperCamelCase( self : Dict ): a__ : str = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(lowerCamelCase__ ) a__ : int = self.default_image_processor a__ : List[Any] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Union[str, Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Union[str, Any] = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Tuple = 281 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : Any ): a__ : Dict = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( lowerCamelCase__ ) a__ : str = self.default_image_processor a__ : List[str] = prepare_img() a__ : Tuple = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Dict = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Optional[int] = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : Optional[Any] = torch.tensor([1.6881, -0.2787, 0.5901] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) ) a__ : Optional[Any] = 2_396 self.assertEqual(logits.argmax(-1 ).item() , lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): a__ : Optional[Any] = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : Tuple = model.to(lowerCamelCase__ ) a__ : List[Any] = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Tuple = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : Union[str, Any] = Image.open(ds[0]["file"] ) a__ : List[Any] = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Optional[Any] = model(**lowerCamelCase__ ) a__ : List[str] = outputs.logits # verify the logits a__ : Tuple = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , lowerCamelCase__ ) a__ : int = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: a__ : Dict = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=lowerCamelCase__ , ) else: a__ : Dict = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=lowerCamelCase__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , lowerCamelCase__ , atol=1E-4 ) ) @slow def _UpperCamelCase( self : Tuple ): a__ : str = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) a__ : List[Any] = model.to(lowerCamelCase__ ) a__ : int = BeitImageProcessor(do_resize=lowerCamelCase__ , size=640 , do_center_crop=lowerCamelCase__ ) a__ : Optional[int] = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) a__ : str = Image.open(ds[0]["file"] ) a__ : str = image_processor(images=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : List[Any] = model(**lowerCamelCase__ ) a__ : Any = outputs.logits.detach().cpu() a__ : List[Any] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ , target_sizes=[(500, 300)] ) a__ : Optional[int] = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ ) a__ : List[str] = image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase__ ) a__ : Any = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , lowerCamelCase__ )
37
0
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class A_ ( __lowerCamelCase ): '''simple docstring''' def __init__( self , snake_case , snake_case = None , snake_case = None , snake_case = True , snake_case = None , snake_case = False , snake_case = None , snake_case = True , snake_case = "arrow" , **snake_case , ): super().__init__( split=snake_case , features=snake_case , cache_dir=snake_case , keep_in_memory=snake_case , streaming=snake_case , **snake_case , ) lowercase = load_from_cache_file lowercase = file_format lowercase = Spark( df=snake_case , features=snake_case , cache_dir=snake_case , working_dir=snake_case , **snake_case , ) def SCREAMING_SNAKE_CASE__ ( self ): if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) lowercase = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=snake_case , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
84
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) def UpperCamelCase_ ( __a ) -> Union[str, Any]: a__ : Tuple = R"\w+[.]\d+" a__ : List[Any] = re.findall(__a , __a ) for pat in pats: a__ : Union[str, Any] = key.replace(__a , "_".join(pat.split("." ) ) ) return key def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : List[str] = pt_tuple_key[:-1] + ("scale",) if ( any("norm" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): a__ : Any = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: a__ : Optional[Any] = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: a__ : Union[str, Any] = pt_tuple_key[:-1] + ("embedding",) return renamed_pt_tuple_key, pt_tensor # conv layer a__ : List[str] = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: a__ : str = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer a__ : Tuple = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight": a__ : Tuple = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight a__ : Optional[Any] = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias a__ : Union[str, Any] = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCamelCase_ ( __a , __a , __a=42 ) -> str: # Step 1: Convert pytorch tensor to numpy a__ : Optional[int] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params a__ : Tuple = flax_model.init_weights(PRNGKey(__a ) ) a__ : Optional[Any] = flatten_dict(__a ) a__ : Union[str, Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): a__ : Optional[int] = rename_key(__a ) a__ : Optional[int] = tuple(renamed_pt_key.split("." ) ) # Correctly rename weight parameters a__, a__ : Union[str, Any] = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ''' f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) # also add unexpected weight so that warning is thrown a__ : str = jnp.asarray(__a ) return unflatten_dict(__a )
37
0
from statistics import mean import numpy as np def _a ( lowercase__ : list , lowercase__ : list , lowercase__ : list , lowercase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = 0 # Number of processes finished SCREAMING_SNAKE_CASE__ : Tuple = 0 # Displays the finished process. # If it is 0, the performance is completed if it is 1, before the performance. SCREAMING_SNAKE_CASE__ : Optional[int] = [0] * no_of_process # List to include calculation results SCREAMING_SNAKE_CASE__ : Optional[Any] = [0] * no_of_process # Sort by arrival time. SCREAMING_SNAKE_CASE__ : Optional[int] = [burst_time[i] for i in np.argsort(lowercase__ )] SCREAMING_SNAKE_CASE__ : List[str] = [process_name[i] for i in np.argsort(lowercase__ )] arrival_time.sort() while no_of_process > finished_process_count: SCREAMING_SNAKE_CASE__ : Tuple = 0 while finished_process[i] == 1: i += 1 if current_time < arrival_time[i]: SCREAMING_SNAKE_CASE__ : str = arrival_time[i] SCREAMING_SNAKE_CASE__ : Any = 0 # Index showing the location of the process being performed SCREAMING_SNAKE_CASE__ : Any = 0 # Saves the current response ratio. SCREAMING_SNAKE_CASE__ : Any = 0 for i in range(0 , lowercase__ ): if finished_process[i] == 0 and arrival_time[i] <= current_time: SCREAMING_SNAKE_CASE__ : Any = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ i ] if response_ratio < temp: SCREAMING_SNAKE_CASE__ : Union[str, Any] = temp SCREAMING_SNAKE_CASE__ : Optional[Any] = i # Calculate the turn around time SCREAMING_SNAKE_CASE__ : Optional[int] = current_time + burst_time[loc] - arrival_time[loc] current_time += burst_time[loc] # Indicates that the process has been performed. SCREAMING_SNAKE_CASE__ : Tuple = 1 # Increase finished_process_count by 1 finished_process_count += 1 return turn_around_time def _a ( lowercase__ : list , lowercase__ : list , lowercase__ : list , lowercase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = [0] * no_of_process for i in range(0 , lowercase__ ): SCREAMING_SNAKE_CASE__ : Any = turn_around_time[i] - burst_time[i] return waiting_time if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : List[str] = 5 SCREAMING_SNAKE_CASE__ : Optional[int] = ["A", "B", "C", "D", "E"] SCREAMING_SNAKE_CASE__ : Dict = [1, 2, 3, 4, 5] SCREAMING_SNAKE_CASE__ : Dict = [1, 2, 3, 4, 5] SCREAMING_SNAKE_CASE__ : Optional[Any] = calculate_turn_around_time( process_name, arrival_time, burst_time, no_of_process ) SCREAMING_SNAKE_CASE__ : Optional[Any] = calculate_waiting_time( process_name, turn_around_time, burst_time, no_of_process ) print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time") for i in range(0, no_of_process): print( F"""{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t""" F"""{turn_around_time[i]}\t\t\t{waiting_time[i]}""" ) print(F"""average waiting time : {mean(waiting_time):.5f}""") print(F"""average turn around time : {mean(turn_around_time):.5f}""")
85
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def UpperCamelCase_ ( ) -> int: a__ : Any = HfArgumentParser(__a ) a__ : Any = parser.parse_args_into_dataclasses()[0] a__ : Optional[int] = TensorFlowBenchmark(args=__a ) try: a__ : Optional[int] = parser.parse_args_into_dataclasses()[0] except ValueError as e: a__ : Tuple = "Arg --no_{0} is no longer used, please use --no-{0} instead." a__ : List[Any] = " ".join(str(__a ).split(" " )[:-1] ) a__ : str = "" a__ : List[Any] = eval(str(__a ).split(" " )[-1] ) a__ : List[str] = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(__a ) if len(__a ) > 0: a__ : Tuple = full_error_msg + begin_error_msg + str(__a ) raise ValueError(__a ) benchmark.run() if __name__ == "__main__": main()
37
0
import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : List[str] ): A_ = torch.nn.Linear(10 , 10 ) A_ = torch.optim.SGD(model.parameters() , 0.1 ) A_ = Accelerator() A_ = accelerator.prepare(UpperCAmelCase ) try: pickle.loads(pickle.dumps(UpperCAmelCase ) ) except Exception as e: self.fail(f'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
86
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip UpperCamelCase : Optional[int] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def UpperCamelCase_ ( __a ) -> Any: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def UpperCamelCase_ ( __a , __a , __a ) -> Any: return max(metric_fn(__a , __a ) for gt in ground_truths ) def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [] if args.gold_data_mode == "qa": a__ : Any = pd.read_csv(__a , sep="\t" , header=__a ) for answer_list in data[1]: a__ : Union[str, Any] = ast.literal_eval(__a ) answers.append(__a ) else: a__ : List[str] = [line.strip() for line in open(__a , "r" ).readlines()] a__ : List[str] = [[reference] for reference in references] a__ : List[str] = 0 for prediction, ground_truths in zip(__a , __a ): total += 1 em += metric_max_over_ground_truths(__a , __a , __a ) fa += metric_max_over_ground_truths(__a , __a , __a ) a__ : Dict = 100.0 * em / total a__ : Optional[Any] = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Optional[Any] = args.k a__ : str = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = [line.strip() for line in open(__a , "r" ).readlines()] a__ : Tuple = 0 for hypo, reference in zip(__a , __a ): a__ : Any = set(hypo.split("\t" )[:k] ) a__ : Union[str, Any] = set(reference.split("\t" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k a__ : Union[str, Any] = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: def strip_title(__a ): if title.startswith("\"" ): a__ : Optional[Any] = title[1:] if title.endswith("\"" ): a__ : Union[str, Any] = title[:-1] return title a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a , )["input_ids"].to(args.device ) a__ : Optional[int] = rag_model.rag.question_encoder(__a ) a__ : Union[str, Any] = question_enc_outputs[0] a__ : Optional[int] = rag_model.retriever( __a , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="pt" , ) a__ : List[Any] = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) a__ : int = [] for docs in all_docs: a__ : Optional[int] = [strip_title(__a ) for title in docs["title"]] provenance_strings.append("\t".join(__a ) ) return provenance_strings def UpperCamelCase_ ( __a , __a , __a ) -> Dict: with torch.no_grad(): a__ : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __a , return_tensors="pt" , padding=__a , truncation=__a ) a__ : Any = inputs_dict.input_ids.to(args.device ) a__ : Dict = inputs_dict.attention_mask.to(args.device ) a__ : Optional[int] = rag_model.generate( # rag_model overwrites generate __a , attention_mask=__a , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__a , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) a__ : int = rag_model.retriever.generator_tokenizer.batch_decode(__a , skip_special_tokens=__a ) if args.print_predictions: for q, a in zip(__a , __a ): logger.info("Q: {} - A: {}".format(__a , __a ) ) return answers def UpperCamelCase_ ( ) -> List[str]: a__ : int = argparse.ArgumentParser() parser.add_argument( "--model_type" , choices=["rag_sequence", "rag_token", "bart"] , type=__a , help=( "RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the" " model_name_or_path" ) , ) parser.add_argument( "--index_name" , default=__a , choices=["exact", "compressed", "legacy"] , type=__a , help="RAG model retriever type" , ) parser.add_argument( "--index_path" , default=__a , type=__a , help="Path to the retrieval index" , ) parser.add_argument("--n_docs" , default=5 , type=__a , help="Number of retrieved docs" ) parser.add_argument( "--model_name_or_path" , default=__a , type=__a , required=__a , help="Path to pretrained checkpoints or model identifier from huggingface.co/models" , ) parser.add_argument( "--eval_mode" , choices=["e2e", "retrieval"] , default="e2e" , type=__a , help=( "Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates" " precision@k." ) , ) parser.add_argument("--k" , default=1 , type=__a , help="k for the precision@k calculation" ) parser.add_argument( "--evaluation_set" , default=__a , type=__a , required=__a , help="Path to a file containing evaluation samples" , ) parser.add_argument( "--gold_data_path" , default=__a , type=__a , required=__a , help="Path to a tab-separated file with gold samples" , ) parser.add_argument( "--gold_data_mode" , default="qa" , type=__a , choices=["qa", "ans"] , help=( "Format of the gold data file" "qa - a single line in the following format: question [tab] answer_list" "ans - a single line of the gold file contains the expected answer string" ) , ) parser.add_argument( "--predictions_path" , type=__a , default="predictions.txt" , help="Name of the predictions file, to be stored in the checkpoints directory" , ) parser.add_argument( "--eval_all_checkpoints" , action="store_true" , help="Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number" , ) parser.add_argument( "--eval_batch_size" , default=8 , type=__a , help="Batch size per GPU/CPU for evaluation." , ) parser.add_argument( "--recalculate" , help="Recalculate predictions even if the prediction file exists" , action="store_true" , ) parser.add_argument( "--num_beams" , default=4 , type=__a , help="Number of beams to be used when generating answers" , ) parser.add_argument("--min_length" , default=1 , type=__a , help="Min length of the generated answers" ) parser.add_argument("--max_length" , default=50 , type=__a , help="Max length of the generated answers" ) parser.add_argument( "--print_predictions" , action="store_true" , help="If True, prints predictions while evaluating." , ) parser.add_argument( "--print_docs" , action="store_true" , help="If True, prints docs retried while generating." , ) a__ : int = parser.parse_args() a__ : Dict = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) return args def UpperCamelCase_ ( __a ) -> Optional[int]: a__ : Tuple = {} if args.model_type is None: a__ : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("rag" ): a__ : int = RagTokenForGeneration if args.model_type == "rag_token" else RagSequenceForGeneration a__ : Tuple = args.n_docs if args.index_name is not None: a__ : Any = args.index_name if args.index_path is not None: a__ : int = args.index_path else: a__ : Optional[Any] = BartForConditionalGeneration a__ : Tuple = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("Evaluate the following checkpoints: %s" , __a ) a__ : Any = get_scores if args.eval_mode == "e2e" else get_precision_at_k a__ : Union[str, Any] = evaluate_batch_eae if args.eval_mode == "e2e" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("Calculating metrics based on an existing predictions file: {}".format(args.predictions_path ) ) score_fn(__a , args.predictions_path , args.gold_data_path ) continue logger.info("***** Running evaluation for {} *****".format(__a ) ) logger.info(" Batch size = %d" , args.eval_batch_size ) logger.info(" Predictions will be stored under {}".format(args.predictions_path ) ) if args.model_type.startswith("rag" ): a__ : str = RagRetriever.from_pretrained(__a , **__a ) a__ : Optional[int] = model_class.from_pretrained(__a , retriever=__a , **__a ) model.retriever.init_retrieval() else: a__ : Dict = model_class.from_pretrained(__a , **__a ) model.to(args.device ) with open(args.evaluation_set , "r" ) as eval_file, open(args.predictions_path , "w" ) as preds_file: a__ : List[Any] = [] for line in tqdm(__a ): questions.append(line.strip() ) if len(__a ) == args.eval_batch_size: a__ : Union[str, Any] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) + "\n" ) preds_file.flush() a__ : Any = [] if len(__a ) > 0: a__ : List[str] = evaluate_batch_fn(__a , __a , __a ) preds_file.write("\n".join(__a ) ) preds_file.flush() score_fn(__a , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": UpperCamelCase : List[Any] = get_args() main(args)
37
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : Tuple = logging.get_logger(__name__) _lowerCamelCase : Optional[int] = { """microsoft/cvt-13""": """https://huggingface.co/microsoft/cvt-13/resolve/main/config.json""", # See all Cvt models at https://huggingface.co/models?filter=cvt } class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = '''cvt''' def __init__( self : List[str] , UpperCAmelCase__ : str=3 , UpperCAmelCase__ : int=[7, 3, 3] , UpperCAmelCase__ : List[str]=[4, 2, 2] , UpperCAmelCase__ : Dict=[2, 1, 1] , UpperCAmelCase__ : Optional[Any]=[64, 192, 384] , UpperCAmelCase__ : Tuple=[1, 3, 6] , UpperCAmelCase__ : Optional[int]=[1, 2, 10] , UpperCAmelCase__ : Any=[4.0, 4.0, 4.0] , UpperCAmelCase__ : Optional[Any]=[0.0, 0.0, 0.0] , UpperCAmelCase__ : Dict=[0.0, 0.0, 0.0] , UpperCAmelCase__ : List[Any]=[0.0, 0.0, 0.1] , UpperCAmelCase__ : List[Any]=[True, True, True] , UpperCAmelCase__ : List[Any]=[False, False, True] , UpperCAmelCase__ : Any=["dw_bn", "dw_bn", "dw_bn"] , UpperCAmelCase__ : str=[3, 3, 3] , UpperCAmelCase__ : Optional[int]=[1, 1, 1] , UpperCAmelCase__ : Tuple=[2, 2, 2] , UpperCAmelCase__ : List[Any]=[1, 1, 1] , UpperCAmelCase__ : int=[1, 1, 1] , UpperCAmelCase__ : List[str]=0.02 , UpperCAmelCase__ : Union[str, Any]=1e-12 , **UpperCAmelCase__ : Any , ) ->Optional[Any]: '''simple docstring''' super().__init__(**UpperCAmelCase__) A__ = num_channels A__ = patch_sizes A__ = patch_stride A__ = patch_padding A__ = embed_dim A__ = num_heads A__ = depth A__ = mlp_ratio A__ = attention_drop_rate A__ = drop_rate A__ = drop_path_rate A__ = qkv_bias A__ = cls_token A__ = qkv_projection_method A__ = kernel_qkv A__ = padding_kv A__ = stride_kv A__ = padding_q A__ = stride_q A__ = initializer_range A__ = layer_norm_eps
87
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a = None , ) -> str: a__ : int = {} if train_file is not None: a__ : int = [train_file] if eval_file is not None: a__ : Union[str, Any] = [eval_file] if test_file is not None: a__ : str = [test_file] a__ : Optional[Any] = datasets.load_dataset("csv" , data_files=__a ) a__ : List[Any] = list(ds[list(files.keys() )[0]].features.keys() ) a__ : str = features_name.pop(__a ) a__ : Dict = list(set(ds[list(files.keys() )[0]][label_name] ) ) a__ : str = {label: i for i, label in enumerate(__a )} a__ : Tuple = tokenizer.model_input_names a__ : List[str] = {} if len(__a ) == 1: for k in files.keys(): a__ : Optional[Any] = ds[k].map( lambda __a : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=__a , max_length=__a , padding="max_length" ) , batched=__a , ) elif len(__a ) == 2: for k in files.keys(): a__ : Dict = ds[k].map( lambda __a : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=__a , max_length=__a , padding="max_length" , ) , batched=__a , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: a__ : str = {k: v for k, v in ex.items() if k in input_names} a__ : str = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: a__ : Tuple = {k: v for k, v in ex.items() if k in input_names} a__ : List[Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: a__ : List[Any] = {k: v for k, v in ex.items() if k in input_names} a__ : Optional[int] = labelaid[ex[label_name]] yield (d, label) a__ : Optional[Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: a__ : Optional[int] = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: a__ : Optional[Any] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) a__ : Union[str, Any] = ( tf.data.Dataset.from_generator( __a , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: a__ : Tuple = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid UpperCamelCase : Optional[Any] = logging.getLogger(__name__) @dataclass class A__ : """simple docstring""" _lowercase = field(metadata={'help': 'Which column contains the label'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the training file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the development file'} ) _lowercase = field(default=A__ , metadata={'help': 'The path of the test file'} ) _lowercase = field( default=1_2_8 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowercase = field( default=A__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) @dataclass class A__ : """simple docstring""" _lowercase = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowercase = field( default=A__ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowercase = field(default=A__ , metadata={'help': 'Set this flag to use fast tokenization.'} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. _lowercase = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) def UpperCamelCase_ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. a__ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) a__, a__, a__ : str = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.info( f'''n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, ''' f'''16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a__ : Union[str, Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) a__, a__, a__, a__ : Optional[Any] = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=__a , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) a__ : Optional[int] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(__a ) , labelaid=__a , idalabel={id: label for label, id in labelaid.items()} , finetuning_task="text-classification" , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): a__ : Any = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool(".bin" in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , ) def compute_metrics(__a ) -> Dict: a__ : Union[str, Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer a__ : Dict = TFTrainer( model=__a , args=__a , train_dataset=__a , eval_dataset=__a , compute_metrics=__a , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation a__ : Optional[Any] = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) a__ : Dict = trainer.evaluate() a__ : int = os.path.join(training_args.output_dir , "eval_results.txt" ) with open(__a , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(f''' {key} = {value}''' ) writer.write(f'''{key} = {value}\n''' ) results.update(__a ) return results if __name__ == "__main__": main()
37
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel from diffusers.utils.testing_utils import ( enable_full_determinism, load_numpy, nightly, require_torch_gpu, slow, torch_device, ) from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowercase__ ( A_ ,unittest.TestCase ): __UpperCAmelCase = LDMTextToImagePipeline __UpperCAmelCase = TEXT_TO_IMAGE_PARAMS - { '''negative_prompt''', '''negative_prompt_embeds''', '''cross_attention_kwargs''', '''prompt_embeds''', } __UpperCAmelCase = PipelineTesterMixin.required_optional_params - { '''num_images_per_prompt''', '''callback''', '''callback_steps''', } __UpperCAmelCase = TEXT_TO_IMAGE_BATCH_PARAMS __UpperCAmelCase = False def UpperCamelCase_ ( self) -> List[str]: torch.manual_seed(0) _lowerCamelCase : Any = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) _lowerCamelCase : List[Any] = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule="""scaled_linear""" , clip_sample=SCREAMING_SNAKE_CASE , set_alpha_to_one=SCREAMING_SNAKE_CASE , ) torch.manual_seed(0) _lowerCamelCase : 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) _lowerCamelCase : Tuple = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) _lowerCamelCase : str = CLIPTextModel(SCREAMING_SNAKE_CASE) _lowerCamelCase : Tuple = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""") _lowerCamelCase : List[str] = { """unet""": unet, """scheduler""": scheduler, """vqvae""": vae, """bert""": text_encoder, """tokenizer""": tokenizer, } return components def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=0) -> str: if str(SCREAMING_SNAKE_CASE).startswith("""mps"""): _lowerCamelCase : Dict = torch.manual_seed(SCREAMING_SNAKE_CASE) else: _lowerCamelCase : Optional[int] = torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(SCREAMING_SNAKE_CASE) _lowerCamelCase : Any = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCamelCase_ ( self) -> List[Any]: _lowerCamelCase : Optional[int] = """cpu""" # ensure determinism for the device-dependent torch.Generator _lowerCamelCase : List[Any] = self.get_dummy_components() _lowerCamelCase : int = LDMTextToImagePipeline(**SCREAMING_SNAKE_CASE) pipe.to(SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE) _lowerCamelCase : Dict = self.get_dummy_inputs(SCREAMING_SNAKE_CASE) _lowerCamelCase : List[Any] = pipe(**SCREAMING_SNAKE_CASE).images _lowerCamelCase : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 16, 16, 3) _lowerCamelCase : Optional[Any] = np.array([0.61_01, 0.61_56, 0.56_22, 0.48_95, 0.66_61, 0.38_04, 0.57_48, 0.61_36, 0.50_14]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3 @slow @require_torch_gpu class lowercase__ ( unittest.TestCase ): def UpperCamelCase_ ( self) -> str: super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=torch.floataa , SCREAMING_SNAKE_CASE=0) -> List[Any]: _lowerCamelCase : Optional[int] = torch.manual_seed(SCREAMING_SNAKE_CASE) _lowerCamelCase : List[str] = np.random.RandomState(SCREAMING_SNAKE_CASE).standard_normal((1, 4, 32, 32)) _lowerCamelCase : List[Any] = torch.from_numpy(SCREAMING_SNAKE_CASE).to(device=SCREAMING_SNAKE_CASE , dtype=SCREAMING_SNAKE_CASE) _lowerCamelCase : int = { """prompt""": """A painting of a squirrel eating a burger""", """latents""": latents, """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCamelCase_ ( self) -> Union[str, Any]: _lowerCamelCase : Any = LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256""").to(SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE) _lowerCamelCase : Dict = self.get_inputs(SCREAMING_SNAKE_CASE) _lowerCamelCase : List[Any] = pipe(**SCREAMING_SNAKE_CASE).images _lowerCamelCase : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 256, 256, 3) _lowerCamelCase : str = np.array([0.5_18_25, 0.5_28_50, 0.5_25_43, 0.5_42_58, 0.5_23_04, 0.5_25_69, 0.5_43_63, 0.5_52_76, 0.5_68_78]) _lowerCamelCase : Optional[int] = np.abs(expected_slice - image_slice).max() assert max_diff < 1e-3 @nightly @require_torch_gpu class lowercase__ ( unittest.TestCase ): def UpperCamelCase_ ( self) -> Optional[int]: super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=torch.floataa , SCREAMING_SNAKE_CASE=0) -> int: _lowerCamelCase : Union[str, Any] = torch.manual_seed(SCREAMING_SNAKE_CASE) _lowerCamelCase : Any = np.random.RandomState(SCREAMING_SNAKE_CASE).standard_normal((1, 4, 32, 32)) _lowerCamelCase : str = torch.from_numpy(SCREAMING_SNAKE_CASE).to(device=SCREAMING_SNAKE_CASE , dtype=SCREAMING_SNAKE_CASE) _lowerCamelCase : Tuple = { """prompt""": """A painting of a squirrel eating a burger""", """latents""": latents, """generator""": generator, """num_inference_steps""": 50, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCamelCase_ ( self) -> Optional[Any]: _lowerCamelCase : Union[str, Any] = LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256""").to(SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE) _lowerCamelCase : str = self.get_inputs(SCREAMING_SNAKE_CASE) _lowerCamelCase : Any = pipe(**SCREAMING_SNAKE_CASE).images[0] _lowerCamelCase : Union[str, Any] = load_numpy( """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy""") _lowerCamelCase : str = np.abs(expected_image - image).max() assert max_diff < 1e-3
88
import argparse import collections import json import os import re import string import sys import numpy as np UpperCamelCase : List[str] = re.compile(r"""\b(a|an|the)\b""", re.UNICODE) UpperCamelCase : Union[str, Any] = None def UpperCamelCase_ ( ) -> List[str]: a__ : List[Any] = 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 UpperCamelCase_ ( __a ) -> str: a__ : Optional[Any] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : Dict = bool(qa["answers"]["text"] ) return qid_to_has_ans def UpperCamelCase_ ( __a ) -> List[Any]: def remove_articles(__a ): return ARTICLES_REGEX.sub(" " , __a ) def white_space_fix(__a ): return " ".join(text.split() ) def remove_punc(__a ): a__ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def UpperCamelCase_ ( __a ) -> Dict: if not s: return [] return normalize_answer(__a ).split() def UpperCamelCase_ ( __a , __a ) -> str: return int(normalize_answer(__a ) == normalize_answer(__a ) ) def UpperCamelCase_ ( __a , __a ) -> Dict: a__ : int = get_tokens(__a ) a__ : Optional[Any] = get_tokens(__a ) a__ : Any = collections.Counter(__a ) & collections.Counter(__a ) a__ : Dict = 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 a__ : Tuple = 1.0 * num_same / len(__a ) a__ : str = 1.0 * num_same / len(__a ) a__ : str = (2 * precision * recall) / (precision + recall) return fa def UpperCamelCase_ ( __a , __a ) -> int: a__ : List[str] = {} a__ : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: a__ : List[Any] = qa["id"] a__ : Dict = [t for t in qa["answers"]["text"] if normalize_answer(__a )] if not gold_answers: # For unanswerable questions, only correct answer is empty string a__ : Tuple = [""] if qid not in preds: print(f'''Missing prediction for {qid}''' ) continue a__ : Tuple = preds[qid] # Take max over all gold answers a__ : Optional[int] = max(compute_exact(__a , __a ) for a in gold_answers ) a__ : str = max(compute_fa(__a , __a ) for a in gold_answers ) return exact_scores, fa_scores def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: a__ : Optional[Any] = {} for qid, s in scores.items(): a__ : Dict = na_probs[qid] > na_prob_thresh if pred_na: a__ : Dict = float(not qid_to_has_ans[qid] ) else: a__ : Optional[Any] = s return new_scores def UpperCamelCase_ ( __a , __a , __a=None ) -> Tuple: if not qid_list: a__ : Union[str, Any] = len(__a ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: a__ : int = 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 UpperCamelCase_ ( __a , __a , __a ) -> List[str]: for k in new_eval: a__ : Optional[Any] = new_eval[k] def UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[int]: 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 UpperCamelCase_ ( __a , __a , __a , __a , __a=None , __a=None ) -> Dict: a__ : Optional[Any] = sorted(__a , key=lambda __a : na_probs[k] ) a__ : Any = 0.0 a__ : Optional[int] = 1.0 a__ : Optional[int] = 0.0 a__ : Any = [1.0] a__ : Tuple = [0.0] a__ : List[str] = 0.0 for i, qid in enumerate(__a ): if qid_to_has_ans[qid]: true_pos += scores[qid] a__ : Any = true_pos / float(i + 1 ) a__ : int = 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 UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> str: if out_image_dir and not os.path.exists(__a ): os.makedirs(__a ) a__ : Optional[int] = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return a__ : Optional[int] = 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" , ) a__ : Optional[Any] = make_precision_recall_eval( __a , __a , __a , __a , out_image=os.path.join(__a , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) a__ : str = {k: float(__a ) for k, v in qid_to_has_ans.items()} a__ : Optional[Any] = 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> str: if not qid_list: return a__ : Optional[Any] = [na_probs[k] for k in qid_list] a__ : 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 UpperCamelCase_ ( __a , __a , __a , __a ) -> Optional[Any]: a__ : str = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) a__ : Optional[Any] = num_no_ans a__ : Dict = cur_score a__ : Any = 0.0 a__ : Optional[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]: a__ : Optional[int] = scores[qid] else: if preds[qid]: a__ : str = -1 else: a__ : Union[str, Any] = 0 cur_score += diff if cur_score > best_score: a__ : Any = cur_score a__ : Dict = na_probs[qid] return 100.0 * best_score / len(__a ), best_thresh def UpperCamelCase_ ( __a , __a , __a , __a , __a , __a ) -> Any: a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__, a__ : Tuple = find_best_thresh(__a , __a , __a , __a ) a__ : Any = best_exact a__ : Any = exact_thresh a__ : List[Any] = best_fa a__ : Optional[int] = fa_thresh def UpperCamelCase_ ( ) -> Tuple: with open(OPTS.data_file ) as f: a__ : List[Any] = json.load(__a ) a__ : Any = dataset_json["data"] with open(OPTS.pred_file ) as f: a__ : int = json.load(__a ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: a__ : List[str] = json.load(__a ) else: a__ : Optional[int] = {k: 0.0 for k in preds} a__ : Optional[Any] = make_qid_to_has_ans(__a ) # maps qid to True/False a__ : List[Any] = [k for k, v in qid_to_has_ans.items() if v] a__ : Union[str, Any] = [k for k, v in qid_to_has_ans.items() if not v] a__, a__ : str = get_raw_scores(__a , __a ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : str = apply_no_ans_threshold(__a , __a , __a , OPTS.na_prob_thresh ) a__ : Tuple = make_eval_dict(__a , __a ) if has_ans_qids: a__ : str = make_eval_dict(__a , __a , qid_list=__a ) merge_eval(__a , __a , "HasAns" ) if no_ans_qids: a__ : List[Any] = 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__": UpperCamelCase : Any = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("""Agg""") import matplotlib.pyplot as plt main()
37
0
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class _lowerCamelCase( _a ): lowercase_ : Dict = """""" lowercase_ : str = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) lowercase_ : str = None # compression type in fsspec. ex: "gzip" lowercase_ : str = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self, lowerCamelCase = "", lowerCamelCase = None, lowerCamelCase = None, **lowerCamelCase) -> List[str]: """simple docstring""" super().__init__(self, **lowerCamelCase) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode _lowercase : str = fsspec.open( lowerCamelCase, mode='rb', protocol=lowerCamelCase, compression=self.compression, client_kwargs={ 'requote_redirect_url': False, # see https://github.com/huggingface/datasets/pull/5459 'trust_env': True, # Enable reading proxy env variables. **(target_options or {}).pop('client_kwargs', {}), # To avoid issues if it was already passed. }, **(target_options or {}), ) _lowercase : Dict = os.path.basename(self.file.path.split('::')[0]) _lowercase : int = ( self.compressed_name[: self.compressed_name.rindex('.')] if '.' in self.compressed_name else self.compressed_name ) _lowercase : int = None @classmethod def UpperCamelCase ( cls, lowerCamelCase) -> Any: """simple docstring""" return super()._strip_protocol(lowerCamelCase).lstrip('/') def UpperCamelCase ( self) -> Optional[Any]: """simple docstring""" if self.dir_cache is None: _lowercase : List[str] = {**self.file.fs.info(self.file.path), 'name': self.uncompressed_name} _lowercase : Optional[int] = {f['name']: f} def UpperCamelCase ( self, lowerCamelCase) -> Union[str, Any]: """simple docstring""" return self.file.open().read() def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase = "rb", lowerCamelCase=None, lowerCamelCase=True, lowerCamelCase=None, **lowerCamelCase, ) -> str: """simple docstring""" _lowercase : Optional[int] = self._strip_protocol(lowerCamelCase) if mode != "rb": raise ValueError(F'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''') return self.file.open() class _lowerCamelCase( _a ): lowercase_ : Tuple = """bz2""" lowercase_ : Dict = """bz2""" lowercase_ : List[Any] = """.bz2""" class _lowerCamelCase( _a ): lowercase_ : Optional[Any] = """gzip""" lowercase_ : int = """gzip""" lowercase_ : Optional[Any] = """.gz""" class _lowerCamelCase( _a ): lowercase_ : Any = """lz4""" lowercase_ : List[str] = """lz4""" lowercase_ : str = """.lz4""" class _lowerCamelCase( _a ): lowercase_ : List[Any] = """xz""" lowercase_ : str = """xz""" lowercase_ : int = """.xz""" class _lowerCamelCase( _a ): lowercase_ : List[str] = """zstd""" lowercase_ : List[Any] = """zstd""" lowercase_ : Tuple = """.zst""" def __init__( self, lowerCamelCase, lowerCamelCase = "rb", lowerCamelCase = None, lowerCamelCase = None, lowerCamelCase = DEFAULT_BLOCK_SIZE, **lowerCamelCase, ) -> Dict: """simple docstring""" super().__init__( fo=lowerCamelCase, mode=lowerCamelCase, target_protocol=lowerCamelCase, target_options=lowerCamelCase, block_size=lowerCamelCase, **lowerCamelCase, ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 _lowercase : List[Any] = self.file.__enter__ class _lowerCamelCase: def __init__( self, lowerCamelCase) -> Tuple: """simple docstring""" _lowercase : Tuple = file_ def __enter__( self) -> Any: """simple docstring""" self._file.__enter__() return self def __exit__( self, *lowerCamelCase, **lowerCamelCase) -> Any: """simple docstring""" self._file.__exit__(*lowerCamelCase, **lowerCamelCase) def __iter__( self) -> Optional[int]: """simple docstring""" return iter(self._file) def UpperCamelCase ( self) -> Any: """simple docstring""" return next(self._file) def __getattr__( self, lowerCamelCase) -> Tuple: """simple docstring""" return getattr(self._file, lowerCamelCase) def fixed_enter(*lowerCamelCase, **lowerCamelCase): return WrappedFile(_enter(*lowerCamelCase, **lowerCamelCase)) _lowercase : Optional[int] = fixed_enter
89
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = CLIPTokenizer _lowercase = CLIPTokenizerFast _lowercase = True _lowercase = {} _lowercase = False def _UpperCamelCase( self : List[Any] ): super().setUp() # fmt: off a__ : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on a__ : Optional[Any] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) a__ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] a__ : Optional[Any] = {"unk_token": "<unk>"} a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase__ ) ) def _UpperCamelCase( self : Dict , **lowerCamelCase__ : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Union[str, Any] , **lowerCamelCase__ : Optional[int] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : int = "lower newer" a__ : Optional[int] = "lower newer" return input_text, output_text def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a__ : int = "lower newer" a__ : List[str] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] a__ : Union[str, Any] = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = tokens + [tokenizer.unk_token] a__ : Union[str, Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ ) @require_ftfy def _UpperCamelCase( self : Optional[Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : List[str] = self.tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : Any = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ , **lowerCamelCase__ ) a__ : int = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." a__ : Optional[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : Dict = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways a__ : Optional[Any] = "xa\u0303y" + " " + "x\xe3y" a__ : Optional[int] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of space type a__ : str = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: a__ : Any = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) # Test that the tokenization is identical on unicode of line break type a__ : Union[str, Any] = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: a__ : List[Any] = tokenizer_s.tokenize(lowerCamelCase__ ) a__ : int = tokenizer_r.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): a__ : str = "hello" # `hello` is a token in the vocabulary of `pretrained_name` a__ : Tuple = f'''{text_of_1_token} {text_of_1_token}''' a__ : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Union[str, Any] = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCamelCase__ ) + 1, len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) a__ : Optional[Any] = f''' {text}''' a__ : str = self.rust_tokenizer_class.from_pretrained( lowerCamelCase__ , use_fast=lowerCamelCase__ , ) a__ : Dict = tokenizer_r(lowerCamelCase__ , return_offsets_mapping=lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCamelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCamelCase__ ) + 1, 1 + len(lowerCamelCase__ ) + 1 + len(lowerCamelCase__ )) , ) def _UpperCamelCase( self : int ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCamelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _UpperCamelCase( self : int ): super().test_tokenization_python_rust_equals() def _UpperCamelCase( self : str ): # CLIP always lower cases letters pass
37
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class a__ ( a__ , a__ , a__ , unittest.TestCase ): '''simple docstring''' lowercase__ : Optional[Any] = AltDiffusionPipeline lowercase__ : Dict = TEXT_TO_IMAGE_PARAMS lowercase__ : str = TEXT_TO_IMAGE_BATCH_PARAMS lowercase__ : Any = TEXT_TO_IMAGE_IMAGE_PARAMS lowercase__ : List[str] = TEXT_TO_IMAGE_IMAGE_PARAMS def __SCREAMING_SNAKE_CASE ( self ) -> str: torch.manual_seed(0 ) lowerCAmelCase__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) lowerCAmelCase__ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=lowerCamelCase_ , set_alpha_to_one=lowerCamelCase_ , ) torch.manual_seed(0 ) lowerCAmelCase__ = 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 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) lowerCAmelCase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_02 , ) lowerCAmelCase__ = CLIPTextModel(lowerCamelCase_ ) lowerCAmelCase__ = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) lowerCAmelCase__ = 77 lowerCAmelCase__ = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __SCREAMING_SNAKE_CASE ( self , lowerCamelCase_ , lowerCamelCase_=0 ) -> List[str]: if str(lowerCamelCase_ ).startswith('''mps''' ): lowerCAmelCase__ = torch.manual_seed(lowerCamelCase_ ) else: lowerCAmelCase__ = torch.Generator(device=lowerCamelCase_ ).manual_seed(lowerCamelCase_ ) lowerCAmelCase__ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def __SCREAMING_SNAKE_CASE ( self ) -> List[str]: lowerCAmelCase__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ = self.get_dummy_components() torch.manual_seed(0 ) lowerCAmelCase__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=50_02 , ) # TODO: remove after fixing the non-deterministic text encoder lowerCAmelCase__ = RobertaSeriesModelWithTransformation(lowerCamelCase_ ) lowerCAmelCase__ = text_encoder lowerCAmelCase__ = AltDiffusionPipeline(**lowerCamelCase_ ) lowerCAmelCase__ = alt_pipe.to(lowerCamelCase_ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCAmelCase__ = self.get_dummy_inputs(lowerCamelCase_ ) lowerCAmelCase__ = '''A photo of an astronaut''' lowerCAmelCase__ = alt_pipe(**lowerCamelCase_ ) lowerCAmelCase__ = output.images lowerCAmelCase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ = np.array( [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: lowerCAmelCase__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowerCAmelCase__ = self.get_dummy_components() lowerCAmelCase__ = PNDMScheduler(skip_prk_steps=lowerCamelCase_ ) torch.manual_seed(0 ) lowerCAmelCase__ = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=50_02 , ) # TODO: remove after fixing the non-deterministic text encoder lowerCAmelCase__ = RobertaSeriesModelWithTransformation(lowerCamelCase_ ) lowerCAmelCase__ = text_encoder lowerCAmelCase__ = AltDiffusionPipeline(**lowerCamelCase_ ) lowerCAmelCase__ = alt_pipe.to(lowerCamelCase_ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCAmelCase__ = self.get_dummy_inputs(lowerCamelCase_ ) lowerCAmelCase__ = alt_pipe(**lowerCamelCase_ ) lowerCAmelCase__ = output.images lowerCAmelCase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ = np.array( [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a__ ( unittest.TestCase ): '''simple docstring''' def __SCREAMING_SNAKE_CASE ( self ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: # make sure here that pndm scheduler skips prk lowerCAmelCase__ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , safety_checker=lowerCamelCase_ ) lowerCAmelCase__ = alt_pipe.to(lowerCamelCase_ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCAmelCase__ = '''A painting of a squirrel eating a burger''' lowerCAmelCase__ = torch.manual_seed(0 ) lowerCAmelCase__ = alt_pipe([prompt] , generator=lowerCamelCase_ , guidance_scale=6.0 , num_inference_steps=20 , output_type='''np''' ) lowerCAmelCase__ = output.images lowerCAmelCase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) lowerCAmelCase__ = np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __SCREAMING_SNAKE_CASE ( self ) -> Any: lowerCAmelCase__ = DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''' , subfolder='''scheduler''' ) lowerCAmelCase__ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ ) lowerCAmelCase__ = alt_pipe.to(lowerCamelCase_ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCAmelCase__ = '''A painting of a squirrel eating a burger''' lowerCAmelCase__ = torch.manual_seed(0 ) lowerCAmelCase__ = alt_pipe([prompt] , generator=lowerCamelCase_ , num_inference_steps=2 , output_type='''numpy''' ) lowerCAmelCase__ = output.images lowerCAmelCase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) lowerCAmelCase__ = np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
90
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase : Dict = """<<<<<<< This should probably be modified because it mentions: """ UpperCamelCase : List[Any] = """======= >>>>>>> """ UpperCamelCase : Optional[Any] = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] UpperCamelCase : 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 UpperCamelCase_ ( __a ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class A__ ( A__ ): """simple docstring""" @staticmethod def _UpperCamelCase( lowerCamelCase__ : ArgumentParser ): a__ : List[str] = parser.add_parser( "convert" , help="Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset." , ) train_parser.add_argument( "--tfds_path" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to a TensorFlow Datasets folder to convert or a single tfds file to convert." , ) train_parser.add_argument( "--datasets_directory" , type=lowerCamelCase__ , required=lowerCamelCase__ , help="Path to the HuggingFace Datasets folder." ) train_parser.set_defaults(func=lowerCamelCase__ ) def __init__( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str , *lowerCamelCase__ : Tuple ): a__ : str = get_logger("datasets-cli/converting" ) a__ : Optional[Any] = tfds_path a__ : Optional[int] = datasets_directory def _UpperCamelCase( self : int ): if os.path.isdir(self._tfds_path ): a__ : List[str] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a__ : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("--tfds_path is neither a directory nor a file. Please check path." ) a__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a__ : Tuple = [] a__ : str = [] a__ : List[Any] = {} if os.path.isdir(self._tfds_path ): a__ : List[str] = os.listdir(lowerCamelCase__ ) else: a__ : Union[str, Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'''Looking at file {f_name}''' ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Dict = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) if not os.path.isfile(lowerCamelCase__ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("Skipping file" ) continue with open(lowerCamelCase__ , encoding="utf-8" ) as f: a__ : List[Any] = f.readlines() a__ : Union[str, Any] = [] a__ : Union[str, Any] = False a__ : Union[str, Any] = False a__ : Dict = [] for line in lines: a__ : Optional[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: a__ : List[Any] = "import datasets\n" elif "import tensorflow" in out_line: # order is important here a__ : List[str] = "" continue elif "from absl import logging" in out_line: a__ : Dict = "from datasets import logging\n" elif "getLogger" in out_line: a__ : List[Any] = out_line.replace("getLogger" , "get_logger" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a__ : List[str] = True a__ : Dict = list(filter(lambda lowerCamelCase__ : e in out_line , lowerCamelCase__ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(lowerCamelCase__ ) + "\n" ) out_lines.append(lowerCamelCase__ ) out_lines.append(lowerCamelCase__ ) continue else: for pattern, replacement in TO_CONVERT: a__ : Tuple = re.sub(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a__ : Optional[int] = re.match(r"from\stensorflow_datasets.*import\s([^\.\r\n]+)" , lowerCamelCase__ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split("," ) ) a__ : Optional[Any] = "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: a__ : Optional[int] = True out_lines.append(lowerCamelCase__ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a__ : Dict = f_name.replace(".py" , "" ) a__ : Optional[int] = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) a__ : Any = os.path.join(lowerCamelCase__ , lowerCamelCase__ ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) 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(lowerCamelCase__ ) if needs_manual_update: with_manual_update.append(lowerCamelCase__ ) with open(lowerCamelCase__ , "w" , encoding="utf-8" ) as f: f.writelines(lowerCamelCase__ ) self._logger.info(f'''Converted in {output_file}''' ) for utils_file in utils_files: try: a__ : Any = os.path.basename(lowerCamelCase__ ) a__ : Optional[int] = imports_to_builder_map[f_name.replace(".py" , "" )] self._logger.info(f'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(lowerCamelCase__ , lowerCamelCase__ ) 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\'.''' )
37
0
"""simple docstring""" def _snake_case ( snake_case__ : list[list[int | float]] ): A = len(snake_case__ ) A = len(matrix[0] ) A = min(snake_case__ , snake_case__ ) for row in range(snake_case__ ): # Check if diagonal element is not zero if matrix[row][row] != 0: # Eliminate all the elements below the diagonal for col in range(row + 1 , snake_case__ ): A = matrix[col][row] / matrix[row][row] for i in range(snake_case__ , snake_case__ ): matrix[col][i] -= multiplier * matrix[row][i] else: # Find a non-zero diagonal element to swap rows A = True for i in range(row + 1 , snake_case__ ): if matrix[i][row] != 0: A , A = matrix[i], matrix[row] A = False break if reduce: rank -= 1 for i in range(snake_case__ ): A = matrix[i][rank] # Reduce the row pointer by one to stay on the same row row -= 1 return rank if __name__ == "__main__": import doctest doctest.testmod()
91
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class A__ ( A__ ): """simple docstring""" _lowercase = '' _lowercase = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _lowercase = None # compression type in fsspec. ex: "gzip" _lowercase = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , lowerCamelCase__ : str = "" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , **lowerCamelCase__ : List[str] ): super().__init__(self , **lowerCamelCase__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode a__ : str = fsspec.open( lowerCamelCase__ , mode="rb" , protocol=lowerCamelCase__ , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) a__ : Optional[int] = os.path.basename(self.file.path.split("::" )[0] ) a__ : int = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) a__ : List[Any] = None @classmethod def _UpperCamelCase( cls : int , lowerCamelCase__ : int ): # compressed file paths are always relative to the archive root return super()._strip_protocol(lowerCamelCase__ ).lstrip("/" ) def _UpperCamelCase( self : Dict ): if self.dir_cache is None: a__ : Dict = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} a__ : int = {f["name"]: f} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : str ): return self.file.open().read() def _UpperCamelCase( self : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : int=None , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[str]=None , **lowerCamelCase__ : Optional[Any] , ): a__ : Optional[int] = self._strip_protocol(lowerCamelCase__ ) if mode != "rb": raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class A__ ( A__ ): """simple docstring""" _lowercase = 'bz2' _lowercase = 'bz2' _lowercase = '.bz2' class A__ ( A__ ): """simple docstring""" _lowercase = 'gzip' _lowercase = 'gzip' _lowercase = '.gz' class A__ ( A__ ): """simple docstring""" _lowercase = 'lz4' _lowercase = 'lz4' _lowercase = '.lz4' class A__ ( A__ ): """simple docstring""" _lowercase = 'xz' _lowercase = 'xz' _lowercase = '.xz' class A__ ( A__ ): """simple docstring""" _lowercase = 'zstd' _lowercase = 'zstd' _lowercase = '.zst' def __init__( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : str = "rb" , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[dict] = None , lowerCamelCase__ : int = DEFAULT_BLOCK_SIZE , **lowerCamelCase__ : Tuple , ): super().__init__( fo=lowerCamelCase__ , mode=lowerCamelCase__ , target_protocol=lowerCamelCase__ , target_options=lowerCamelCase__ , block_size=lowerCamelCase__ , **lowerCamelCase__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 a__ : Any = self.file.__enter__ class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : str ): a__ : List[Any] = file_ def __enter__( self : str ): self._file.__enter__() return self def __exit__( self : int , *lowerCamelCase__ : List[str] , **lowerCamelCase__ : int ): self._file.__exit__(*lowerCamelCase__ , **lowerCamelCase__ ) def __iter__( self : List[str] ): return iter(self._file ) def _UpperCamelCase( self : Any ): return next(self._file ) def __getattr__( self : Optional[Any] , lowerCamelCase__ : Tuple ): return getattr(self._file , lowerCamelCase__ ) def fixed_enter(*lowerCamelCase__ : List[str] , **lowerCamelCase__ : str ): return WrappedFile(_enter(*lowerCamelCase__ , **lowerCamelCase__ ) ) a__ : Any = fixed_enter
37
0
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.linear_k""": """encoder.layers.*.self_attn.linear_k""", """self_attn.linear_v""": """encoder.layers.*.self_attn.linear_v""", """self_attn.linear_q""": """encoder.layers.*.self_attn.linear_q""", """self_attn.pos_bias_u""": """encoder.layers.*.self_attn.pos_bias_u""", """self_attn.pos_bias_v""": """encoder.layers.*.self_attn.pos_bias_v""", """self_attn.linear_out""": """encoder.layers.*.self_attn.linear_out""", """self_attn.linear_pos""": """encoder.layers.*.self_attn.linear_pos""", """self_attn.rotary_emb""": """encoder.embed_positions""", """self_attn_layer_norm""": """encoder.layers.*.self_attn_layer_norm""", """conv_module.pointwise_conv1""": """encoder.layers.*.conv_module.pointwise_conv1""", """conv_module.pointwise_conv2""": """encoder.layers.*.conv_module.pointwise_conv2""", """conv_module.depthwise_conv""": """encoder.layers.*.conv_module.depthwise_conv""", """conv_module.batch_norm""": """encoder.layers.*.conv_module.batch_norm""", """conv_module.layer_norm""": """encoder.layers.*.conv_module.layer_norm""", """ffn1.w_1""": """encoder.layers.*.ffn1.intermediate_dense""", """ffn1.w_2""": """encoder.layers.*.ffn1.output_dense""", """ffn1.layer_norm""": """encoder.layers.*.ffn1_layer_norm""", """ffn2.w_1""": """encoder.layers.*.ffn2.intermediate_dense""", """ffn2.w_2""": """encoder.layers.*.ffn2.output_dense""", """ffn2.layer_norm""": """encoder.layers.*.ffn2_layer_norm""", """final_layer_norm""": """encoder.layers.*.final_layer_norm""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } UpperCamelCase_ = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def _lowerCAmelCase ( __magic_name__ : Any , __magic_name__ : Union[str, Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple ) -> str: for attribute in key.split('''.''' ): lowercase : Tuple =getattr(__magic_name__ , __magic_name__ ) if weight_type is not None: lowercase : Optional[int] =getattr(__magic_name__ , __magic_name__ ).shape else: lowercase : List[Any] =hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": lowercase : Any =value elif weight_type == "weight_g": lowercase : List[Any] =value elif weight_type == "weight_v": lowercase : Union[str, Any] =value elif weight_type == "bias": lowercase : Tuple =value elif weight_type == "running_mean": lowercase : Union[str, Any] =value elif weight_type == "running_var": lowercase : str =value elif weight_type == "num_batches_tracked": lowercase : Tuple =value elif weight_type == "inv_freq": lowercase : Optional[Any] =value else: lowercase : Tuple =value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def _lowerCAmelCase ( __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : str ) -> Union[str, Any]: lowercase : Optional[int] =[] lowercase : Tuple =fairseq_model.state_dict() lowercase : List[Any] =hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): lowercase : Tuple =False if "conv_layers" in name: load_conv_layer( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , hf_model.config.feat_extract_norm == '''group''' , ) lowercase : List[Any] =True else: for key, mapped_key in MAPPING.items(): lowercase : Optional[int] ='''wav2vec2_conformer.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: lowercase : Union[str, Any] =True if "*" in mapped_key: lowercase : Optional[int] =name.split(__magic_name__ )[0].split('''.''' )[-2] lowercase : List[str] =mapped_key.replace('''*''' , __magic_name__ ) if "pos_bias_u" in name: lowercase : Optional[Any] =None elif "pos_bias_v" in name: lowercase : Union[str, Any] =None elif "weight_g" in name: lowercase : Any ='''weight_g''' elif "weight_v" in name: lowercase : Tuple ='''weight_v''' elif "bias" in name: lowercase : Optional[int] ='''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj lowercase : Optional[int] ='''weight''' elif "running_mean" in name: lowercase : Union[str, Any] ='''running_mean''' elif "inv_freq" in name: lowercase : Any ='''inv_freq''' elif "running_var" in name: lowercase : Tuple ='''running_var''' elif "num_batches_tracked" in name: lowercase : Dict ='''num_batches_tracked''' else: lowercase : str =None set_recursively(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) continue if not is_used: unused_weights.append(__magic_name__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) def _lowerCAmelCase ( __magic_name__ : int , __magic_name__ : Optional[Any] , __magic_name__ : List[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Tuple ) -> int: lowercase : Optional[Any] =full_name.split('''conv_layers.''' )[-1] lowercase : Any =name.split('''.''' ) lowercase : List[str] =int(items[0] ) lowercase : Union[str, Any] =int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) lowercase : Union[str, Any] =value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) lowercase : Optional[Any] =value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''' ) lowercase : Optional[int] =value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''' ) lowercase : str =value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__magic_name__ ) @torch.no_grad() def _lowerCAmelCase ( __magic_name__ : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Tuple=None , __magic_name__ : Optional[Any]=None , __magic_name__ : Union[str, Any]=True ) -> Union[str, Any]: if config_path is not None: lowercase : Optional[Any] =WavaVecaConformerConfig.from_pretrained(__magic_name__ , hidden_act='''swish''' ) else: lowercase : Optional[int] =WavaVecaConformerConfig() if "rope" in checkpoint_path: lowercase : Dict ='''rotary''' if is_finetuned: if dict_path: lowercase : Optional[Any] =Dictionary.load(__magic_name__ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq lowercase : str =target_dict.pad_index lowercase : Union[str, Any] =target_dict.bos_index lowercase : Any =target_dict.eos_index lowercase : Tuple =len(target_dict.symbols ) lowercase : str =os.path.join(__magic_name__ , '''vocab.json''' ) if not os.path.isdir(__magic_name__ ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(__magic_name__ ) ) return os.makedirs(__magic_name__ , exist_ok=__magic_name__ ) lowercase : Dict =target_dict.indices # fairseq has the <pad> and <s> switched lowercase : str =0 lowercase : List[Any] =1 with open(__magic_name__ , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(__magic_name__ , __magic_name__ ) lowercase : List[str] =WavaVecaCTCTokenizer( __magic_name__ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=__magic_name__ , ) lowercase : Optional[int] =True if config.feat_extract_norm == '''layer''' else False lowercase : str =WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=__magic_name__ , return_attention_mask=__magic_name__ , ) lowercase : Tuple =WavaVecaProcessor(feature_extractor=__magic_name__ , tokenizer=__magic_name__ ) processor.save_pretrained(__magic_name__ ) lowercase : str =WavaVecaConformerForCTC(__magic_name__ ) else: lowercase : Tuple =WavaVecaConformerForPreTraining(__magic_name__ ) if is_finetuned: lowercase , lowercase , lowercase : Optional[int] =fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) else: lowercase : Dict =argparse.Namespace(task='''audio_pretraining''' ) lowercase : Optional[int] =fairseq.tasks.setup_task(__magic_name__ ) lowercase , lowercase , lowercase : str =fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__magic_name__ ) lowercase : List[Any] =model[0].eval() recursively_load_weights(__magic_name__ , __magic_name__ , not is_finetuned ) hf_wavavec.save_pretrained(__magic_name__ ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument( """--not_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not""" ) UpperCamelCase_ = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
92
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def UpperCamelCase_ ( __a , __a , __a ) -> Optional[Any]: a__ : Union[str, Any] = ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value") a__ : Union[str, Any] = ( ("layer.", "layer_"), ("word_embeddings.weight", "word_embeddings"), ("position_embeddings.weight", "position_embeddings"), ("token_type_embeddings.weight", "token_type_embeddings"), (".", "/"), ("LayerNorm/weight", "LayerNorm/gamma"), ("LayerNorm/bias", "LayerNorm/beta"), ("weight", "kernel"), ) if not os.path.isdir(__a ): os.makedirs(__a ) a__ : Any = model.state_dict() def to_tf_var_name(__a ): for patt, repl in iter(__a ): a__ : Tuple = name.replace(__a , __a ) return f'''bert/{name}''' def create_tf_var(__a , __a , __a ): a__ : Tuple = tf.dtypes.as_dtype(tensor.dtype ) a__ : Dict = tf.get_variable(dtype=__a , shape=tensor.shape , name=__a , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(__a ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: a__ : int = to_tf_var_name(__a ) a__ : Union[str, Any] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): a__ : int = torch_tensor.T a__ : Optional[Any] = create_tf_var(tensor=__a , name=__a , session=__a ) tf.keras.backend.set_value(__a , __a ) a__ : int = session.run(__a ) print(f'''Successfully created {tf_name}: {np.allclose(__a , __a )}''' ) a__ : Any = tf.train.Saver(tf.trainable_variables() ) saver.save(__a , os.path.join(__a , model_name.replace("-" , "_" ) + ".ckpt" ) ) def UpperCamelCase_ ( __a=None ) -> int: a__ : Dict = argparse.ArgumentParser() parser.add_argument("--model_name" , type=__a , required=__a , help="model name e.g. bert-base-uncased" ) parser.add_argument( "--cache_dir" , type=__a , default=__a , required=__a , help="Directory containing pytorch model" ) parser.add_argument("--pytorch_model_path" , type=__a , required=__a , help="/path/to/<pytorch-model-name>.bin" ) parser.add_argument("--tf_cache_dir" , type=__a , required=__a , help="Directory in which to save tensorflow model" ) a__ : Optional[Any] = parser.parse_args(__a ) a__ : Tuple = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=__a , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
37
0
"""simple docstring""" from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { """snap-research/efficientformer-l1-300""": ( """https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json""" ), } class _lowerCAmelCase ( a ): """simple docstring""" __magic_name__ :Union[str, Any] = """efficientformer""" def __init__( self , __UpperCAmelCase = [3, 2, 6, 4] , __UpperCAmelCase = [4_8, 9_6, 2_2_4, 4_4_8] , __UpperCAmelCase = [True, True, True, True] , __UpperCAmelCase = 4_4_8 , __UpperCAmelCase = 3_2 , __UpperCAmelCase = 4 , __UpperCAmelCase = 7 , __UpperCAmelCase = 5 , __UpperCAmelCase = 8 , __UpperCAmelCase = 4 , __UpperCAmelCase = 0.0 , __UpperCAmelCase = 1_6 , __UpperCAmelCase = 3 , __UpperCAmelCase = 3 , __UpperCAmelCase = 3 , __UpperCAmelCase = 2 , __UpperCAmelCase = 1 , __UpperCAmelCase = 0.0 , __UpperCAmelCase = 1 , __UpperCAmelCase = True , __UpperCAmelCase = True , __UpperCAmelCase = 1E-5 , __UpperCAmelCase = "gelu" , __UpperCAmelCase = 0.02 , __UpperCAmelCase = 1E-12 , __UpperCAmelCase = 2_2_4 , __UpperCAmelCase = 1E-05 , **__UpperCAmelCase , ): '''simple docstring''' super().__init__(**__UpperCAmelCase ) lowerCAmelCase__ :Any = hidden_act lowerCAmelCase__ :List[Any] = hidden_dropout_prob lowerCAmelCase__ :Any = hidden_sizes lowerCAmelCase__ :str = num_hidden_layers lowerCAmelCase__ :Tuple = num_attention_heads lowerCAmelCase__ :List[str] = initializer_range lowerCAmelCase__ :Optional[Any] = layer_norm_eps lowerCAmelCase__ :Optional[int] = patch_size lowerCAmelCase__ :int = num_channels lowerCAmelCase__ :Optional[int] = depths lowerCAmelCase__ :Dict = mlp_expansion_ratio lowerCAmelCase__ :Any = downsamples lowerCAmelCase__ :Dict = dim lowerCAmelCase__ :Optional[Any] = key_dim lowerCAmelCase__ :int = attention_ratio lowerCAmelCase__ :int = resolution lowerCAmelCase__ :List[str] = pool_size lowerCAmelCase__ :Union[str, Any] = downsample_patch_size lowerCAmelCase__ :Optional[Any] = downsample_stride lowerCAmelCase__ :int = downsample_pad lowerCAmelCase__ :int = drop_path_rate lowerCAmelCase__ :List[str] = num_metaad_blocks lowerCAmelCase__ :Any = distillation lowerCAmelCase__ :Union[str, Any] = use_layer_scale lowerCAmelCase__ :Optional[int] = layer_scale_init_value lowerCAmelCase__ :Any = image_size lowerCAmelCase__ :Optional[Any] = batch_norm_eps
93
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ASTConfig from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_torchaudio_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ASTForAudioClassification, ASTModel from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_torchaudio_available(): import torchaudio from transformers import ASTFeatureExtractor class A__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=2 , lowerCamelCase__ : Any=24 , lowerCamelCase__ : Optional[Any]=16 , lowerCamelCase__ : int=True , lowerCamelCase__ : List[str]=True , lowerCamelCase__ : List[Any]=32 , lowerCamelCase__ : List[str]=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Optional[Any]=37 , lowerCamelCase__ : Any="gelu" , lowerCamelCase__ : Union[str, Any]=0.1 , lowerCamelCase__ : Optional[int]=0.1 , lowerCamelCase__ : str=10 , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : str=None , lowerCamelCase__ : List[str]=2 , lowerCamelCase__ : Optional[Any]=2 , ): a__ : str = parent a__ : Any = batch_size a__ : Dict = patch_size a__ : List[Any] = max_length a__ : str = num_mel_bins a__ : Optional[Any] = is_training a__ : Optional[int] = use_labels a__ : List[Any] = hidden_size a__ : str = num_hidden_layers a__ : Any = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : List[str] = hidden_act a__ : str = hidden_dropout_prob a__ : Tuple = attention_probs_dropout_prob a__ : List[Any] = type_sequence_label_size a__ : Any = initializer_range a__ : str = scope a__ : List[str] = frequency_stride a__ : Union[str, Any] = time_stride # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) a__ : List[Any] = (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1 a__ : List[str] = (self.max_length - self.patch_size) // self.time_stride + 1 a__ : Tuple = frequency_out_dimension * time_out_dimension a__ : List[str] = num_patches + 2 def _UpperCamelCase( self : List[str] ): a__ : Any = floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] ) a__ : List[Any] = None if self.use_labels: a__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : List[str] = self.get_config() return config, input_values, labels def _UpperCamelCase( self : Optional[int] ): return ASTConfig( patch_size=self.patch_size , max_length=self.max_length , num_mel_bins=self.num_mel_bins , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , frequency_stride=self.frequency_stride , time_stride=self.time_stride , ) def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] ): a__ : List[Any] = ASTModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() a__ : Dict = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase( self : str ): a__ : Dict = self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ) : Optional[int] = config_and_inputs a__ : List[Any] = {"input_values": input_values} return config, inputs_dict @require_torch class A__ ( A__ , A__ , unittest.TestCase ): """simple docstring""" _lowercase = ( ( ASTModel, ASTForAudioClassification, ) if is_torch_available() else () ) _lowercase = ( {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel} if is_torch_available() else {} ) _lowercase = False _lowercase = False _lowercase = False _lowercase = False def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Any , lowerCamelCase__ : Dict ): if pipeline_test_casse_name == "AudioClassificationPipelineTests": return True return False def _UpperCamelCase( self : str ): a__ : str = ASTModelTester(self ) a__ : Any = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def _UpperCamelCase( self : List[str] ): self.config_tester.run_common_tests() @unittest.skip(reason="AST does not use inputs_embeds" ) def _UpperCamelCase( self : List[str] ): pass def _UpperCamelCase( self : Optional[int] ): a__, a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Any = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) a__ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def _UpperCamelCase( self : Tuple ): a__, a__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Dict = model_class(lowerCamelCase__ ) a__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[int] = [*signature.parameters.keys()] a__ : Optional[Any] = ["input_values"] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) @slow def _UpperCamelCase( self : int ): for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Union[str, Any] = ASTModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def UpperCamelCase_ ( ) -> Any: a__ : Optional[int] = hf_hub_download( repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset" ) a__, a__ : List[str] = torchaudio.load(__a ) return audio, sampling_rate @require_torch @require_torchaudio class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _UpperCamelCase( self : List[str] ): return ( ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ) if is_torchaudio_available() else None ) @slow def _UpperCamelCase( self : Optional[int] ): a__ : int = self.default_feature_extractor a__ : Optional[Any] = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593" ).to(lowerCamelCase__ ) a__ : Any = self.default_feature_extractor a__, a__ : Dict = prepare_audio() a__ : str = audio.squeeze().numpy() a__ : Any = feature_extractor(lowerCamelCase__ , sampling_rate=lowerCamelCase__ , return_tensors="pt" ).to(lowerCamelCase__ ) # forward pass with torch.no_grad(): a__ : Any = model(**lowerCamelCase__ ) # verify the logits a__ : Union[str, Any] = torch.Size((1, 527) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) a__ : List[str] = torch.tensor([-0.8760, -7.0042, -8.6602] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase__ , atol=1E-4 ) )
37
0
'''simple docstring''' import importlib import os import fsspec import pytest from fsspec import register_implementation from fsspec.registry import _registry as _fsspec_registry from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem from .utils import require_lza, require_zstandard def lowercase_ ( __A : Optional[Any] ) -> int: """simple docstring""" assert "mock" in _fsspec_registry assert "bz2" in _fsspec_registry def lowercase_ ( ) -> List[Any]: """simple docstring""" assert "mock" not in _fsspec_registry assert "bz2" in _fsspec_registry def lowercase_ ( ) -> List[Any]: """simple docstring""" lowercase : int ='''mock-s3-bucket''' lowercase : Optional[int] =F's3://{mock_bucket}' lowercase : List[Any] =extract_path_from_uri(__A ) assert dataset_path.startswith('''s3://''' ) is False lowercase : int ='''./local/path''' lowercase : str =extract_path_from_uri(__A ) assert dataset_path == new_dataset_path def lowercase_ ( __A : Union[str, Any] ) -> Optional[int]: """simple docstring""" lowercase : Tuple =is_remote_filesystem(__A ) assert is_remote is True lowercase : Optional[Any] =fsspec.filesystem('''file''' ) lowercase : int =is_remote_filesystem(__A ) assert is_remote is False @pytest.mark.parametrize('''compression_fs_class''' , __A ) def lowercase_ ( __A : List[str] , __A : int , __A : Union[str, Any] , __A : List[str] , __A : Optional[int] , __A : Optional[int] , __A : Optional[int] ) -> List[str]: """simple docstring""" lowercase : str ={'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_file, '''bz2''': bza_file, '''lz4''': lza_file} lowercase : Any =input_paths[compression_fs_class.protocol] if input_path is None: lowercase : Tuple =F'for \'{compression_fs_class.protocol}\' compression protocol, ' if compression_fs_class.protocol == "lz4": reason += require_lza.kwargs["reason"] elif compression_fs_class.protocol == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(__A ) lowercase : Any =fsspec.filesystem(compression_fs_class.protocol , fo=__A ) assert isinstance(__A , __A ) lowercase : int =os.path.basename(__A ) lowercase : str =expected_filename[: expected_filename.rindex('''.''' )] assert fs.glob('''*''' ) == [expected_filename] with fs.open(__A , '''r''' , encoding='''utf-8''' ) as f, open(__A , encoding='''utf-8''' ) as expected_file: assert f.read() == expected_file.read() @pytest.mark.parametrize('''protocol''' , ['''zip''', '''gzip'''] ) def lowercase_ ( __A : Union[str, Any] , __A : Optional[int] , __A : int ) -> Any: """simple docstring""" lowercase : List[str] ={'''zip''': zip_jsonl_path, '''gzip''': jsonl_gz_path} lowercase : int =compressed_file_paths[protocol] lowercase : Optional[Any] ='''dataset.jsonl''' lowercase : Any =F'{protocol}://{member_file_path}::{compressed_file_path}' lowercase , *lowercase : Dict =fsspec.get_fs_token_paths(__A ) assert fs.isfile(__A ) assert not fs.isfile('''non_existing_''' + member_file_path ) @pytest.mark.integration def lowercase_ ( __A : List[str] , __A : Tuple , __A : Dict , __A : List[str] ) -> List[Any]: """simple docstring""" lowercase : Tuple =hf_api.dataset_info(__A , token=__A ) lowercase : List[Any] =HfFileSystem(repo_info=__A , token=__A ) assert sorted(hffs.glob('''*''' ) ) == [".gitattributes", "data"] assert hffs.isdir('''data''' ) assert hffs.isfile('''.gitattributes''' ) and hffs.isfile('''data/text_data.txt''' ) with open(__A ) as f: assert hffs.open('''data/text_data.txt''' , '''r''' ).read() == f.read() def lowercase_ ( ) -> Dict: """simple docstring""" lowercase : str ='''bz2''' # Import module import datasets.filesystems # Overwrite protocol and reload register_implementation(__A , __A , clobber=__A ) with pytest.warns(__A ) as warning_info: importlib.reload(datasets.filesystems ) assert len(__A ) == 1 assert ( str(warning_info[0].message ) == F'A filesystem protocol was already set for {protocol} and will be overwritten.' )
94
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast 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 UpperCamelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( A__ , unittest.TestCase ): """simple docstring""" _lowercase = XGLMTokenizer _lowercase = XGLMTokenizerFast _lowercase = True _lowercase = True def _UpperCamelCase( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase( self : List[Any] ): a__ : int = "<pad>" a__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(lowerCamelCase__ ) , 1_008 ) def _UpperCamelCase( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def _UpperCamelCase( self : Optional[int] ): a__ : str = XGLMTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) a__ : List[str] = tokenizer.tokenize("This is a test" ) self.assertListEqual(lowerCamelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) a__ : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a__ : List[str] = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) a__ : Dict = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _UpperCamelCase( self : Dict ): return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def _UpperCamelCase( self : Union[str, Any] ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCamelCase__ , f.name ) a__ : Any = XGLMTokenizer(f.name , keep_accents=lowerCamelCase__ ) a__ : List[str] = pickle.dumps(lowerCamelCase__ ) pickle.loads(lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): if not self.test_rust_tokenizer: return a__ : Any = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : Tuple = "I was born in 92000, and this is falsé." a__ : List[str] = tokenizer.tokenize(lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : Optional[int] = tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) a__ : Union[str, Any] = rust_tokenizer.encode(lowerCamelCase__ , add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = self.get_rust_tokenizer() a__ : Tuple = tokenizer.encode(lowerCamelCase__ ) a__ : Optional[Any] = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) @slow def _UpperCamelCase( self : List[str] ): a__ : Union[str, Any] = "Hello World!" a__ : List[str] = [2, 31_227, 4_447, 35] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : Union[str, Any] ): a__ : Optional[int] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off a__ : Union[str, Any] = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def _UpperCamelCase( self : List[Any] ): # fmt: off a__ : Optional[int] = { "input_ids": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], "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, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name="facebook/xglm-564M" , padding=lowerCamelCase__ , )
37
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''', '''distilbert-base-uncased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''', '''distilbert-base-cased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''', '''distilbert-base-multilingual-cased''': ( '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json''' ), '''distilbert-base-uncased-finetuned-sst-2-english''': ( '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json''' ), } class UpperCamelCase_ (__A ): __magic_name__ = '''distilbert''' __magic_name__ = { '''hidden_size''': '''dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', } def __init__( self : str , lowerCAmelCase_ : Dict=30_522 , lowerCAmelCase_ : Optional[int]=512 , lowerCAmelCase_ : int=False , lowerCAmelCase_ : Optional[int]=6 , lowerCAmelCase_ : Tuple=12 , lowerCAmelCase_ : List[str]=768 , lowerCAmelCase_ : Optional[Any]=4 * 768 , lowerCAmelCase_ : str=0.1 , lowerCAmelCase_ : Optional[Any]=0.1 , lowerCAmelCase_ : List[Any]="gelu" , lowerCAmelCase_ : Optional[Any]=0.0_2 , lowerCAmelCase_ : str=0.1 , lowerCAmelCase_ : Dict=0.2 , lowerCAmelCase_ : int=0 , **lowerCAmelCase_ : Union[str, Any] , ) -> List[str]: UpperCAmelCase_ : Optional[int] = vocab_size UpperCAmelCase_ : Any = max_position_embeddings UpperCAmelCase_ : Tuple = sinusoidal_pos_embds UpperCAmelCase_ : Optional[int] = n_layers UpperCAmelCase_ : Optional[Any] = n_heads UpperCAmelCase_ : Optional[int] = dim UpperCAmelCase_ : List[Any] = hidden_dim UpperCAmelCase_ : Optional[Any] = dropout UpperCAmelCase_ : List[Any] = attention_dropout UpperCAmelCase_ : List[Any] = activation UpperCAmelCase_ : Dict = initializer_range UpperCAmelCase_ : List[str] = qa_dropout UpperCAmelCase_ : List[str] = seq_classif_dropout super().__init__(**lowerCAmelCase_ , pad_token_id=lowerCAmelCase_ ) class UpperCamelCase_ (__A ): @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCAmelCase_ : Tuple = {0: "batch", 1: "choice", 2: "sequence"} else: UpperCAmelCase_ : Tuple = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
95
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCamelCase_ ( ) -> int: a__ : int = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg" a__ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ).convert("RGB" ) return image def UpperCamelCase_ ( __a ) -> Optional[Any]: a__ : Any = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.embeddings.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.embeddings.layernorm.bias") ) # fmt: on return rename_keys def UpperCamelCase_ ( __a , __a , __a ) -> List[str]: a__ : Union[str, Any] = dct.pop(__a ) a__ : List[str] = val def UpperCamelCase_ ( __a , __a ) -> Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__ : Any = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) a__ : Tuple = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict a__ : str = torch.cat((q_bias, torch.zeros_like(__a , requires_grad=__a ), v_bias) ) a__ : int = qkv_bias def UpperCamelCase_ ( __a ) -> Dict: a__ : Tuple = 364 if "coco" in model_name else 224 a__ : int = InstructBlipVisionConfig(image_size=__a ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: a__ : Tuple = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__ : Dict = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: a__ : List[Any] = LlamaConfig.from_pretrained("decapoda-research/llama-7b-hf" , vocab_size=32_001 ).to_dict() elif "vicuna-13b" in model_name: a__ : Optional[int] = LlamaConfig.from_pretrained("decapoda-research/llama-13b-hf" , vocab_size=32_001 ).to_dict() else: raise ValueError("Model name not supported" ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 a__ : Optional[Any] = InstructBlipQFormerConfig(vocab_size=30_523 ).to_dict() a__ : Any = InstructBlipConfig(vision_config=__a , text_config=__a , qformer_config=__a ) return config, image_size @torch.no_grad() def UpperCamelCase_ ( __a , __a=None , __a=False ) -> int: a__ : Tuple = AutoTokenizer.from_pretrained("bert-base-uncased" , truncation_side="left" ) qformer_tokenizer.add_special_tokens({"bos_token": "[DEC]"} ) if "t5" in model_name: a__ : List[Any] = TaTokenizerFast.from_pretrained("google/flan-t5-xl" , truncation_side="left" ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) a__ : Union[str, Any] = LlamaTokenizerFast.from_pretrained( "huggyllama/llama-7b" , truncation_side="left" , bos_token="</s>" , unk_token="</s>" ) tokenizer.add_special_tokens({"pad_token": "[PAD]"} ) a__, a__ : List[str] = get_blipa_config(__a ) a__ : Any = InstructBlipForConditionalGeneration(__a ).eval() a__ : Dict = { "instructblip-vicuna-7b": ("blip2_vicuna_instruct", "vicuna7b"), "instructblip-vicuna-13b": ("blip2_vicuna_instruct", "vicuna13b"), "instructblip-flan-t5-xl": ("blip2_t5_instruct", "flant5xl"), "instructblip-flan-t5-xxl": ("blip2_t5_instruct", "flant5xxl"), } a__, a__ : Dict = model_name_to_original[model_name] # load original model print("Loading original model..." ) a__ : Optional[Any] = "cuda:1" if torch.cuda.is_available() else "cpu" a__ : List[Any] = "cuda:2" if torch.cuda.is_available() else "cpu" a__, a__, a__ : Tuple = load_model_and_preprocess( name=__a , model_type=__a , is_eval=__a , device=__a ) original_model.eval() print("Done!" ) # update state dict keys a__ : Dict = original_model.state_dict() a__ : Optional[int] = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a , __a , __a ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__ : Optional[int] = state_dict.pop(__a ) if key.startswith("Qformer.bert" ): a__ : List[Any] = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: a__ : Any = key.replace("self" , "attention" ) if "llm_proj" in key: a__ : Dict = key.replace("llm_proj" , "language_projection" ) if "t5_proj" in key: a__ : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("llm_model" ): a__ : List[str] = key.replace("llm_model" , "language_model" ) if key.startswith("t5" ): a__ : str = key.replace("t5" , "language" ) a__ : Dict = val # read in qv biases read_in_q_v_bias(__a , __a ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(__a , strict=__a ) a__ : Union[str, Any] = load_demo_image() a__ : int = "What is unusual about this image?" # create processor a__ : Any = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__a , image_std=__a ) a__ : Tuple = InstructBlipProcessor( image_processor=__a , tokenizer=__a , qformer_tokenizer=__a , ) a__ : Tuple = processor(images=__a , text=__a , return_tensors="pt" ).to(__a ) # make sure processor creates exact same pixel values a__ : Optional[int] = vis_processors["eval"](__a ).unsqueeze(0 ).to(__a ) a__ : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , __a ) original_model.to(__a ) hf_model.to(__a ) with torch.no_grad(): if "vicuna" in model_name: a__ : str = original_model({"image": original_pixel_values, "text_input": [prompt]} ).logits a__ : List[str] = hf_model(**__a ).logits else: a__ : List[Any] = original_model( {"image": original_pixel_values, "text_input": [prompt], "text_output": ["\n"]} ).logits a__ : str = tokenizer("\n" , return_tensors="pt" ).input_ids.to(__a ) a__ : Dict = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -100 ) a__ : Any = hf_model(**__a , labels=__a ).logits print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape a__ : Tuple = 1e-4 if "vicuna" in model_name else 1e-5 assert torch.allclose(original_logits.to(logits.device ) , __a , atol=__a ) print("Looks ok!" ) print("Generating with original model..." ) a__ : Tuple = original_model.generate({"image": original_pixel_values, "prompt": prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print("Generating with HF model..." ) a__ : int = hf_model.generate( **__a , do_sample=__a , num_beams=5 , max_length=256 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? a__ : int = 2 print("Original generation:" , __a ) a__ : str = processor.batch_decode(__a , skip_special_tokens=__a ) a__ : str = [text.strip() for text in output_text] print("HF generation:" , __a ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if push_to_hub: processor.push_to_hub(f'''Salesforce/{model_name}''' ) hf_model.push_to_hub(f'''Salesforce/{model_name}''' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() UpperCamelCase : Optional[int] = [ """instructblip-vicuna-7b""", """instructblip-vicuna-13b""", """instructblip-flan-t5-xl""", """instructblip-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""instructblip-flan-t5-xl""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase : Dict = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
37
0
"""simple docstring""" import unittest from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline from transformers.convert_graph_to_onnx import ( convert, ensure_valid_input, generate_identified_filename, infer_shapes, quantize, ) from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow class __A : def lowerCamelCase__ ( self : List[str] , __snake_case : List[str] , __snake_case : Any , __snake_case : Any ) -> Tuple: return None class __A : def lowerCamelCase__ ( self : Optional[Any] , __snake_case : Union[str, Any] , __snake_case : Union[str, Any] , __snake_case : int , __snake_case : str ) -> str: return None class __A ( unittest.TestCase ): UpperCAmelCase__ = [ # (model_name, model_kwargs) ("bert-base-cased", {}), ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore ] @require_tf @slow def lowerCamelCase__ ( self : Union[str, Any] ) -> Dict: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__snake_case , """tf""" , 1_2 , **__snake_case ) @require_torch @slow def lowerCamelCase__ ( self : List[str] ) -> int: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__snake_case , """pt""" , 1_2 , **__snake_case ) @require_torch @slow def lowerCamelCase__ ( self : Optional[int] ) -> List[str]: from transformers import BertModel __magic_name__: Optional[Any] = ["""[UNK]""", """[SEP]""", """[CLS]""", """[PAD]""", """[MASK]""", """some""", """other""", """words"""] with NamedTemporaryFile(mode="""w+t""" ) as vocab_file: vocab_file.write("""\n""".join(__snake_case ) ) vocab_file.flush() __magic_name__: str = BertTokenizerFast(vocab_file.name ) with TemporaryDirectory() as bert_save_dir: __magic_name__: Optional[int] = BertModel(BertConfig(vocab_size=len(__snake_case ) ) ) model.save_pretrained(__snake_case ) self._test_export(__snake_case , """pt""" , 1_2 , __snake_case ) @require_tf @slow def lowerCamelCase__ ( self : Tuple ) -> List[str]: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: __magic_name__: int = self._test_export(__snake_case , """tf""" , 1_2 , **__snake_case ) __magic_name__: List[str] = quantize(Path(__snake_case ) ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__snake_case ).stat().st_size: self.fail("""Quantized model is bigger than initial ONNX model""" ) @require_torch @slow def lowerCamelCase__ ( self : List[Any] ) -> List[Any]: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: __magic_name__: Dict = self._test_export(__snake_case , """pt""" , 1_2 , **__snake_case ) __magic_name__: Tuple = quantize(__snake_case ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__snake_case ).stat().st_size: self.fail("""Quantized model is bigger than initial ONNX model""" ) def lowerCamelCase__ ( self : Tuple , __snake_case : Optional[Any] , __snake_case : Optional[Any] , __snake_case : Any , __snake_case : Tuple=None , **__snake_case : int ) -> Dict: try: # Compute path with TemporaryDirectory() as tempdir: __magic_name__: Tuple = Path(__snake_case ).joinpath("""model.onnx""" ) # Remove folder if exists if path.parent.exists(): path.parent.rmdir() # Export convert(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case , **__snake_case ) return path except Exception as e: self.fail(__snake_case ) @require_torch @require_tokenizers @slow def lowerCamelCase__ ( self : List[str] ) -> Dict: from transformers import BertModel __magic_name__: str = BertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random""" ) ) __magic_name__: int = BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random""" ) self._test_infer_dynamic_axis(__snake_case , __snake_case , """pt""" ) @require_tf @require_tokenizers @slow def lowerCamelCase__ ( self : Union[str, Any] ) -> List[str]: from transformers import TFBertModel __magic_name__: str = TFBertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random""" ) ) __magic_name__: List[Any] = BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random""" ) self._test_infer_dynamic_axis(__snake_case , __snake_case , """tf""" ) def lowerCamelCase__ ( self : str , __snake_case : Union[str, Any] , __snake_case : int , __snake_case : int ) -> int: __magic_name__: List[Any] = FeatureExtractionPipeline(__snake_case , __snake_case ) __magic_name__: Optional[int] = ["""input_ids""", """token_type_ids""", """attention_mask""", """output_0""", """output_1"""] __magic_name__, __magic_name__, __magic_name__, __magic_name__: List[str] = infer_shapes(__snake_case , __snake_case ) # Assert all variables are present self.assertEqual(len(__snake_case ) , len(__snake_case ) ) self.assertTrue(all(var_name in shapes for var_name in variable_names ) ) self.assertSequenceEqual(variable_names[:3] , __snake_case ) self.assertSequenceEqual(variable_names[3:] , __snake_case ) # Assert inputs are {0: batch, 1: sequence} for var_name in ["input_ids", "token_type_ids", "attention_mask"]: self.assertDictEqual(shapes[var_name] , {0: """batch""", 1: """sequence"""} ) # Assert outputs are {0: batch, 1: sequence} and {0: batch} self.assertDictEqual(shapes["""output_0"""] , {0: """batch""", 1: """sequence"""} ) self.assertDictEqual(shapes["""output_1"""] , {0: """batch"""} ) def lowerCamelCase__ ( self : int ) -> List[str]: __magic_name__: int = ["""input_ids""", """attention_mask""", """token_type_ids"""] __magic_name__: str = {"""input_ids""": [1, 2, 3, 4], """attention_mask""": [0, 0, 0, 0], """token_type_ids""": [1, 1, 1, 1]} __magic_name__, __magic_name__: int = ensure_valid_input(FuncContiguousArgs() , __snake_case , __snake_case ) # Should have exactly the same number of args (all are valid) self.assertEqual(len(__snake_case ) , 3 ) # Should have exactly the same input names self.assertEqual(set(__snake_case ) , set(__snake_case ) ) # Parameter should be reordered according to their respective place in the function: # (input_ids, token_type_ids, attention_mask) self.assertEqual(__snake_case , (tokens["""input_ids"""], tokens["""token_type_ids"""], tokens["""attention_mask"""]) ) # Generated args are interleaved with another args (for instance parameter "past" in GPT2) __magic_name__, __magic_name__: List[Any] = ensure_valid_input(FuncNonContiguousArgs() , __snake_case , __snake_case ) # Should have exactly the one arg (all before the one not provided "some_other_args") self.assertEqual(len(__snake_case ) , 1 ) self.assertEqual(len(__snake_case ) , 1 ) # Should have only "input_ids" self.assertEqual(inputs_args[0] , tokens["""input_ids"""] ) self.assertEqual(ordered_input_names[0] , """input_ids""" ) def lowerCamelCase__ ( self : Union[str, Any] ) -> List[str]: __magic_name__: Optional[int] = generate_identified_filename(Path("""/home/something/my_fake_model.onnx""" ) , """-test""" ) self.assertEqual("""/home/something/my_fake_model-test.onnx""" , generated.as_posix() )
96
def UpperCamelCase_ ( __a , __a ) -> Tuple: a__ : Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 a__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. a__ : Any = min(__a , __a ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
37
0
import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def a ( snake_case__: List[Any] ): '''simple docstring''' if "cls_token" in name: lowercase_ = name.replace('''cls_token''' , '''vit.embeddings.cls_token''' ) if "mask_token" in name: lowercase_ = name.replace('''mask_token''' , '''decoder.mask_token''' ) if "decoder_pos_embed" in name: lowercase_ = name.replace('''decoder_pos_embed''' , '''decoder.decoder_pos_embed''' ) if "pos_embed" in name and "decoder" not in name: lowercase_ = name.replace('''pos_embed''' , '''vit.embeddings.position_embeddings''' ) if "patch_embed.proj" in name: lowercase_ = name.replace('''patch_embed.proj''' , '''vit.embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: lowercase_ = name.replace('''patch_embed.norm''' , '''vit.embeddings.norm''' ) if "decoder_blocks" in name: lowercase_ = name.replace('''decoder_blocks''' , '''decoder.decoder_layers''' ) if "blocks" in name: lowercase_ = name.replace('''blocks''' , '''vit.encoder.layer''' ) if "attn.proj" in name: lowercase_ = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: lowercase_ = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: lowercase_ = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: lowercase_ = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: lowercase_ = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: lowercase_ = name.replace('''mlp.fc2''' , '''output.dense''' ) if "decoder_embed" in name: lowercase_ = name.replace('''decoder_embed''' , '''decoder.decoder_embed''' ) if "decoder_norm" in name: lowercase_ = name.replace('''decoder_norm''' , '''decoder.decoder_norm''' ) if "decoder_pred" in name: lowercase_ = name.replace('''decoder_pred''' , '''decoder.decoder_pred''' ) if "norm.weight" in name and "decoder" not in name: lowercase_ = name.replace('''norm.weight''' , '''vit.layernorm.weight''' ) if "norm.bias" in name and "decoder" not in name: lowercase_ = name.replace('''norm.bias''' , '''vit.layernorm.bias''' ) return name def a ( snake_case__: Optional[int] , snake_case__: Any ): '''simple docstring''' for key in orig_state_dict.copy().keys(): lowercase_ = orig_state_dict.pop(snake_case__ ) if "qkv" in key: lowercase_ = key.split('''.''' ) lowercase_ = int(key_split[1] ) if "decoder_blocks" in key: lowercase_ = config.decoder_hidden_size lowercase_ = '''decoder.decoder_layers.''' if "weight" in key: lowercase_ = val[:dim, :] lowercase_ = val[dim : dim * 2, :] lowercase_ = val[-dim:, :] elif "bias" in key: lowercase_ = val[:dim] lowercase_ = val[dim : dim * 2] lowercase_ = val[-dim:] else: lowercase_ = config.hidden_size lowercase_ = '''vit.encoder.layer.''' if "weight" in key: lowercase_ = val[:dim, :] lowercase_ = val[dim : dim * 2, :] lowercase_ = val[-dim:, :] elif "bias" in key: lowercase_ = val[:dim] lowercase_ = val[dim : dim * 2] lowercase_ = val[-dim:] else: lowercase_ = val return orig_state_dict def a ( snake_case__: str , snake_case__: int ): '''simple docstring''' lowercase_ = ViTMAEConfig() if "large" in checkpoint_url: lowercase_ = 1_024 lowercase_ = 4_096 lowercase_ = 24 lowercase_ = 16 elif "huge" in checkpoint_url: lowercase_ = 14 lowercase_ = 1_280 lowercase_ = 5_120 lowercase_ = 32 lowercase_ = 16 lowercase_ = ViTMAEForPreTraining(snake_case__ ) lowercase_ = torch.hub.load_state_dict_from_url(snake_case__ , map_location='''cpu''' )['''model'''] lowercase_ = ViTMAEImageProcessor(size=config.image_size ) lowercase_ = convert_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ ) model.eval() lowercase_ = '''https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg''' lowercase_ = Image.open(requests.get(snake_case__ , stream=snake_case__ ).raw ) lowercase_ = ViTMAEImageProcessor(size=config.image_size ) lowercase_ = image_processor(images=snake_case__ , return_tensors='''pt''' ) # forward pass torch.manual_seed(2 ) lowercase_ = model(**snake_case__ ) lowercase_ = outputs.logits if "large" in checkpoint_url: lowercase_ = torch.tensor( [[-0.7_3_0_9, -0.7_1_2_8, -1.0_1_6_9], [-1.0_1_6_1, -0.9_0_5_8, -1.1_8_7_8], [-1.0_4_7_8, -0.9_4_1_1, -1.1_9_1_1]] ) elif "huge" in checkpoint_url: lowercase_ = torch.tensor( [[-1.1_5_9_9, -0.9_1_9_9, -1.2_2_2_1], [-1.1_9_5_2, -0.9_2_6_9, -1.2_3_0_7], [-1.2_1_4_3, -0.9_3_3_7, -1.2_2_6_2]] ) else: lowercase_ = torch.tensor( [[-0.9_1_9_2, -0.8_4_8_1, -1.1_2_5_9], [-1.1_3_4_9, -1.0_0_3_4, -1.2_5_9_9], [-1.1_7_5_7, -1.0_4_2_9, -1.2_7_2_6]] ) # verify logits assert torch.allclose(logits[0, :3, :3] , snake_case__ , atol=1e-4 ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(snake_case__ ) if __name__ == "__main__": __a = 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.' ) __a = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
97
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Optional[Any] = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } UpperCamelCase : Dict = { """allenai/led-base-16384""": 1_6384, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = LEDTokenizer _lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[str]=None , lowerCamelCase__ : Any=None , lowerCamelCase__ : int="replace" , lowerCamelCase__ : Union[str, Any]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Tuple="</s>" , lowerCamelCase__ : Optional[int]="<s>" , lowerCamelCase__ : str="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Any="<mask>" , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : int=True , **lowerCamelCase__ : Union[str, Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : List[str] = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : Optional[Any] = add_prefix_space a__ : List[str] = pre_tok_class(**lowerCamelCase__ ) a__ : Optional[int] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ : Any = "post_processor" a__ : str = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Optional[Any] = tuple(state["sep"] ) if "cls" in state: a__ : Optional[Any] = tuple(state["cls"] ) a__ : Optional[int] = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Dict = add_prefix_space a__ : int = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : List[Any] = trim_offsets a__ : List[str] = True if changes_to_apply: a__ : int = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : int = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Union[str, Any] ): a__ : Any = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : Union[str, Any] = value def _UpperCamelCase( self : Any , *lowerCamelCase__ : Optional[int] , **lowerCamelCase__ : Optional[Any] ): a__ : List[str] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Any , *lowerCamelCase__ : Dict , **lowerCamelCase__ : Optional[Any] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : List[str] = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[Any]=None ): a__ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : List[str] = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0] def _UpperCamelCase( self : Dict , lowerCamelCase__ : Union[Dict[str, EncodedInput], BatchEncoding] , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , lowerCamelCase__ : Optional[int] = None , lowerCamelCase__ : Optional[bool] = None , ): a__ : str = super()._pad( encoded_inputs=lowerCamelCase__ , max_length=lowerCamelCase__ , padding_strategy=lowerCamelCase__ , pad_to_multiple_of=lowerCamelCase__ , return_attention_mask=lowerCamelCase__ , ) # Load from model defaults if return_attention_mask is None: a__ : Optional[int] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ : Tuple = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ : Dict = len(encoded_inputs["global_attention_mask"] ) != len(lowerCamelCase__ ) if needs_to_be_padded: a__ : Union[str, Any] = len(lowerCamelCase__ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ : List[Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a__ : Any = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
37
0
'''simple docstring''' lowercase__ : List[str] = 'Alexander Joslin' import operator as op from .stack import Stack def a__ ( lowercase : str ) -> int: """simple docstring""" _UpperCamelCase = {'''*''': op.mul, '''/''': op.truediv, '''+''': op.add, '''-''': op.sub} _UpperCamelCase = Stack() _UpperCamelCase = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(lowercase ) ) elif i in operators: # RULE 2 operator_stack.push(lowercase ) elif i == ")": # RULE 4 _UpperCamelCase = operator_stack.peek() operator_stack.pop() _UpperCamelCase = operand_stack.peek() operand_stack.pop() _UpperCamelCase = operand_stack.peek() operand_stack.pop() _UpperCamelCase = operators[opr](lowercase, lowercase ) operand_stack.push(lowercase ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": lowercase__ : Tuple = '(5 + ((4 * 2) * (2 + 3)))' # answer = 45 print(F"""{equation} = {dijkstras_two_stack_algorithm(equation)}""")
98
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Any = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : Union[str, Any] = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : List[str] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class A__ ( A__ ): """simple docstring""" _lowercase = VOCAB_FILES_NAMES _lowercase = PRETRAINED_VOCAB_FILES_MAP _lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowercase = ['input_ids', 'attention_mask'] _lowercase = RobertaTokenizer def __init__( self : List[str] , lowerCamelCase__ : Any=None , lowerCamelCase__ : List[Any]=None , lowerCamelCase__ : Dict=None , lowerCamelCase__ : List[str]="replace" , lowerCamelCase__ : List[str]="<s>" , lowerCamelCase__ : Union[str, Any]="</s>" , lowerCamelCase__ : Any="</s>" , lowerCamelCase__ : Any="<s>" , lowerCamelCase__ : int="<unk>" , lowerCamelCase__ : Any="<pad>" , lowerCamelCase__ : Tuple="<mask>" , lowerCamelCase__ : Any=False , lowerCamelCase__ : Dict=True , **lowerCamelCase__ : Optional[Any] , ): super().__init__( lowerCamelCase__ , lowerCamelCase__ , tokenizer_file=lowerCamelCase__ , errors=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , add_prefix_space=lowerCamelCase__ , trim_offsets=lowerCamelCase__ , **lowerCamelCase__ , ) a__ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : Any = getattr(lowerCamelCase__ , pre_tok_state.pop("type" ) ) a__ : int = add_prefix_space a__ : Tuple = pre_tok_class(**lowerCamelCase__ ) a__ : str = add_prefix_space a__ : Tuple = "post_processor" a__ : Dict = getattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) if tokenizer_component_instance: a__ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ : Tuple = tuple(state["sep"] ) if "cls" in state: a__ : str = tuple(state["cls"] ) a__ : str = False if state.get("add_prefix_space" , lowerCamelCase__ ) != add_prefix_space: a__ : str = add_prefix_space a__ : Any = True if state.get("trim_offsets" , lowerCamelCase__ ) != trim_offsets: a__ : int = trim_offsets a__ : Dict = True if changes_to_apply: a__ : Union[str, Any] = getattr(lowerCamelCase__ , state.pop("type" ) ) a__ : str = component_class(**lowerCamelCase__ ) setattr(self.backend_tokenizer , lowerCamelCase__ , lowerCamelCase__ ) @property def _UpperCamelCase( self : Union[str, Any] ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : Tuple ): a__ : List[Any] = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else value a__ : List[str] = value def _UpperCamelCase( self : Union[str, Any] , *lowerCamelCase__ : int , **lowerCamelCase__ : int ): a__ : Optional[int] = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : Tuple , *lowerCamelCase__ : Dict , **lowerCamelCase__ : List[str] ): a__ : Dict = kwargs.get("is_split_into_words" , lowerCamelCase__ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase__ , **lowerCamelCase__ ) def _UpperCamelCase( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Optional[str] = None ): a__ : int = self._tokenizer.model.save(lowerCamelCase__ , name=lowerCamelCase__ ) return tuple(lowerCamelCase__ ) def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int]=None ): a__ : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _UpperCamelCase( self : Dict , lowerCamelCase__ : List[int] , lowerCamelCase__ : Optional[List[int]] = None ): a__ : Tuple = [self.sep_token_id] a__ : 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 + sep + token_ids_a + sep ) * [0]
37
0
import argparse import os from . import ( ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BART_PRETRAINED_MODEL_ARCHIVE_LIST, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, BartConfig, BertConfig, CamembertConfig, CTRLConfig, DistilBertConfig, DPRConfig, ElectraConfig, FlaubertConfig, GPTaConfig, LayoutLMConfig, LxmertConfig, OpenAIGPTConfig, RobertaConfig, TaConfig, TFAlbertForPreTraining, TFBartForConditionalGeneration, TFBartForSequenceClassification, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFCamembertForMaskedLM, TFCTRLLMHeadModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, TFElectraForPreTraining, TFFlaubertWithLMHeadModel, TFGPTaLMHeadModel, TFLayoutLMForMaskedLM, TFLxmertForPreTraining, TFLxmertVisualFeatureEncoder, TFOpenAIGPTLMHeadModel, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForSequenceClassification, TFTaForConditionalGeneration, TFTransfoXLLMHeadModel, TFWavaVecaModel, TFXLMRobertaForMaskedLM, TFXLMWithLMHeadModel, TFXLNetLMHeadModel, TransfoXLConfig, WavaVecaConfig, WavaVecaModel, XLMConfig, XLMRobertaConfig, XLNetConfig, is_torch_available, load_pytorch_checkpoint_in_tfa_model, ) from .utils import CONFIG_NAME, WEIGHTS_NAME, cached_file, logging if is_torch_available(): import numpy as np import torch from . import ( AlbertForPreTraining, BartForConditionalGeneration, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, CamembertForMaskedLM, CTRLLMHeadModel, DistilBertForMaskedLM, DistilBertForQuestionAnswering, DPRContextEncoder, DPRQuestionEncoder, DPRReader, ElectraForPreTraining, FlaubertWithLMHeadModel, GPTaLMHeadModel, LayoutLMForMaskedLM, LxmertForPreTraining, LxmertVisualFeatureEncoder, OpenAIGPTLMHeadModel, RobertaForMaskedLM, RobertaForSequenceClassification, TaForConditionalGeneration, TransfoXLLMHeadModel, XLMRobertaForMaskedLM, XLMWithLMHeadModel, XLNetLMHeadModel, ) logging.set_verbosity_info() SCREAMING_SNAKE_CASE = { 'bart': ( BartConfig, TFBartForConditionalGeneration, TFBartForSequenceClassification, BartForConditionalGeneration, BART_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'bert': ( BertConfig, TFBertForPreTraining, BertForPreTraining, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-base-cased-finetuned-mrpc': ( BertConfig, TFBertForSequenceClassification, BertForSequenceClassification, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'dpr': ( DPRConfig, TFDPRQuestionEncoder, TFDPRContextEncoder, TFDPRReader, DPRQuestionEncoder, DPRContextEncoder, DPRReader, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'gpt2': ( GPTaConfig, TFGPTaLMHeadModel, GPTaLMHeadModel, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlnet': ( XLNetConfig, TFXLNetLMHeadModel, XLNetLMHeadModel, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm': ( XLMConfig, TFXLMWithLMHeadModel, XLMWithLMHeadModel, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm-roberta': ( XLMRobertaConfig, TFXLMRobertaForMaskedLM, XLMRobertaForMaskedLM, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'transfo-xl': ( TransfoXLConfig, TFTransfoXLLMHeadModel, TransfoXLLMHeadModel, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'openai-gpt': ( OpenAIGPTConfig, TFOpenAIGPTLMHeadModel, OpenAIGPTLMHeadModel, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'roberta': ( RobertaConfig, TFRobertaForCausalLM, TFRobertaForMaskedLM, RobertaForMaskedLM, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'layoutlm': ( LayoutLMConfig, TFLayoutLMForMaskedLM, LayoutLMForMaskedLM, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'roberta-large-mnli': ( RobertaConfig, TFRobertaForSequenceClassification, RobertaForSequenceClassification, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'camembert': ( CamembertConfig, TFCamembertForMaskedLM, CamembertForMaskedLM, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'flaubert': ( FlaubertConfig, TFFlaubertWithLMHeadModel, FlaubertWithLMHeadModel, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert': ( DistilBertConfig, TFDistilBertForMaskedLM, DistilBertForMaskedLM, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert-base-distilled-squad': ( DistilBertConfig, TFDistilBertForQuestionAnswering, DistilBertForQuestionAnswering, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert': ( LxmertConfig, TFLxmertForPreTraining, LxmertForPreTraining, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert-visual-feature-encoder': ( LxmertConfig, TFLxmertVisualFeatureEncoder, LxmertVisualFeatureEncoder, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'ctrl': ( CTRLConfig, TFCTRLLMHeadModel, CTRLLMHeadModel, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'albert': ( AlbertConfig, TFAlbertForPreTraining, AlbertForPreTraining, ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 't5': ( TaConfig, TFTaForConditionalGeneration, TaForConditionalGeneration, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'electra': ( ElectraConfig, TFElectraForPreTraining, ElectraForPreTraining, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'wav2vec2': ( WavaVecaConfig, TFWavaVecaModel, WavaVecaModel, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), } def a (lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=False , lowerCAmelCase__=True ): if model_type not in MODEL_CLASSES: raise ValueError(f'''Unrecognized model type, should be one of {list(MODEL_CLASSES.keys() )}.''' ) __a , __a , __a , __a = MODEL_CLASSES[model_type] # Initialise TF model if config_file in aws_config_map: __a = cached_file(lowerCAmelCase__ , lowerCAmelCase__ , force_download=not use_cached_models ) __a = config_class.from_json_file(lowerCAmelCase__ ) __a = True __a = True print(f'''Building TensorFlow model from configuration: {config}''' ) __a = model_class(lowerCAmelCase__ ) # Load weights from tf checkpoint if pytorch_checkpoint_path in aws_config_map.keys(): __a = cached_file( lowerCAmelCase__ , lowerCAmelCase__ , force_download=not use_cached_models ) # Load PyTorch checkpoint in tf2 model: __a = load_pytorch_checkpoint_in_tfa_model(lowerCAmelCase__ , lowerCAmelCase__ ) if compare_with_pt_model: __a = tf_model(tf_model.dummy_inputs , training=lowerCAmelCase__ ) # build the network __a = torch.load(lowerCAmelCase__ , map_location="""cpu""" ) __a = pt_model_class.from_pretrained( pretrained_model_name_or_path=lowerCAmelCase__ , config=lowerCAmelCase__ , state_dict=lowerCAmelCase__ ) with torch.no_grad(): __a = pt_model(**pt_model.dummy_inputs ) __a = pto[0].numpy() __a = tfo[0].numpy() __a = np.amax(np.abs(np_pt - np_tf ) ) print(f'''Max absolute difference between models outputs {diff}''' ) assert diff <= 2E-2, f'''Error, model absolute difference is >2e-2: {diff}''' # Save pytorch-model print(f'''Save TensorFlow model to {tf_dump_path}''' ) tf_model.save_weights(lowerCAmelCase__ , save_format="""h5""" ) def a (lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__=False , lowerCAmelCase__=False , lowerCAmelCase__=False , lowerCAmelCase__=False , ): if args_model_type is None: __a = list(MODEL_CLASSES.keys() ) else: __a = [args_model_type] for j, model_type in enumerate(lowerCAmelCase__ , start=1 ): print("""=""" * 100 ) print(f''' Converting model type {j}/{len(lowerCAmelCase__ )}: {model_type}''' ) print("""=""" * 100 ) if model_type not in MODEL_CLASSES: raise ValueError(f'''Unrecognized model type {model_type}, should be one of {list(MODEL_CLASSES.keys() )}.''' ) __a , __a , __a , __a , __a = MODEL_CLASSES[model_type] if model_shortcut_names_or_path is None: __a = list(aws_model_maps.keys() ) if config_shortcut_names_or_path is None: __a = model_shortcut_names_or_path for i, (model_shortcut_name, config_shortcut_name) in enumerate( zip(lowerCAmelCase__ , lowerCAmelCase__ ) , start=1 ): print("""-""" * 100 ) if "-squad" in model_shortcut_name or "-mrpc" in model_shortcut_name or "-mnli" in model_shortcut_name: if not only_convert_finetuned_models: print(f''' Skipping finetuned checkpoint {model_shortcut_name}''' ) continue __a = model_shortcut_name elif only_convert_finetuned_models: print(f''' Skipping not finetuned checkpoint {model_shortcut_name}''' ) continue print( f''' Converting checkpoint {i}/{len(lowerCAmelCase__ )}: {model_shortcut_name} - model_type {model_type}''' ) print("""-""" * 100 ) if config_shortcut_name in aws_config_map: __a = cached_file(lowerCAmelCase__ , lowerCAmelCase__ , force_download=not use_cached_models ) else: __a = config_shortcut_name if model_shortcut_name in aws_model_maps: __a = cached_file(lowerCAmelCase__ , lowerCAmelCase__ , force_download=not use_cached_models ) else: __a = model_shortcut_name if os.path.isfile(lowerCAmelCase__ ): __a = """converted_model""" convert_pt_checkpoint_to_tf( model_type=lowerCAmelCase__ , pytorch_checkpoint_path=lowerCAmelCase__ , config_file=lowerCAmelCase__ , tf_dump_path=os.path.join(lowerCAmelCase__ , model_shortcut_name + """-tf_model.h5""" ) , compare_with_pt_model=lowerCAmelCase__ , ) if remove_cached_files: os.remove(lowerCAmelCase__ ) os.remove(lowerCAmelCase__ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_dump_path', default=None, type=str, required=True, help='Path to the output Tensorflow dump file.' ) parser.add_argument( '--model_type', default=None, type=str, help=( f'''Model type selected in the list of {list(MODEL_CLASSES.keys())}. If not given, will download and ''' 'convert all the models from AWS.' ), ) parser.add_argument( '--pytorch_checkpoint_path', default=None, type=str, help=( 'Path to the PyTorch checkpoint path or shortcut name to download from AWS. ' 'If not given, will download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--config_file', default=None, type=str, help=( 'The config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture. If not given and ' '--pytorch_checkpoint_path is not given or is a shortcut name ' 'use the configuration associated to the shortcut name on the AWS' ), ) parser.add_argument( '--compare_with_pt_model', action='store_true', help='Compare Tensorflow and PyTorch model predictions.' ) parser.add_argument( '--use_cached_models', action='store_true', help='Use cached models if possible instead of updating to latest checkpoint versions.', ) parser.add_argument( '--remove_cached_files', action='store_true', help='Remove pytorch models after conversion (save memory when converting in batches).', ) parser.add_argument('--only_convert_finetuned_models', action='store_true', help='Only convert finetuned models.') SCREAMING_SNAKE_CASE = parser.parse_args() # if args.pytorch_checkpoint_path is not None: # convert_pt_checkpoint_to_tf(args.model_type.lower(), # args.pytorch_checkpoint_path, # args.config_file if args.config_file is not None else args.pytorch_checkpoint_path, # args.tf_dump_path, # compare_with_pt_model=args.compare_with_pt_model, # use_cached_models=args.use_cached_models) # else: convert_all_pt_checkpoints_to_tf( args.model_type.lower() if args.model_type is not None else None, args.tf_dump_path, model_shortcut_names_or_path=[args.pytorch_checkpoint_path] if args.pytorch_checkpoint_path is not None else None, config_shortcut_names_or_path=[args.config_file] if args.config_file is not None else None, compare_with_pt_model=args.compare_with_pt_model, use_cached_models=args.use_cached_models, remove_cached_files=args.remove_cached_files, only_convert_finetuned_models=args.only_convert_finetuned_models, )
99
from statistics import mean, stdev def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : List[str] = min(__a ) a__ : str = max(__a ) # normalize data return [round((x - x_min) / (x_max - x_min) , __a ) for x in data] def UpperCamelCase_ ( __a , __a = 3 ) -> list: a__ : str = mean(__a ) a__ : List[str] = stdev(__a ) # standardize data return [round((x - mu) / (sigma) , __a ) for x in data]
37
0
import unittest from transformers import DebertaVaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DebertaVaForMaskedLM, DebertaVaForMultipleChoice, DebertaVaForQuestionAnswering, DebertaVaForSequenceClassification, DebertaVaForTokenClassification, DebertaVaModel, ) from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , A_ , A_=13 , A_=7 , A_=True , A_=True , A_=True , A_=True , A_=99 , A_=32 , A_=5 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=5_12 , A_=16 , A_=2 , A_=0.02 , A_=False , A_=True , A_="None" , A_=3 , A_=4 , A_=None , ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = seq_length SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_input_mask SCREAMING_SNAKE_CASE__ = use_token_type_ids SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = type_vocab_size SCREAMING_SNAKE_CASE__ = type_sequence_label_size SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = num_labels SCREAMING_SNAKE_CASE__ = num_choices SCREAMING_SNAKE_CASE__ = relative_attention SCREAMING_SNAKE_CASE__ = position_biased_input SCREAMING_SNAKE_CASE__ = pos_att_type SCREAMING_SNAKE_CASE__ = scope def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = None if self.use_input_mask: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE__ = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None if self.use_labels: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self ): '''simple docstring''' return 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 , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def lowercase_ ( self , A_ ): '''simple docstring''' self.parent.assertListEqual(list(result.loss.size() ) , [] ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaModel(config=A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = model(A_ , attention_mask=A_ , token_type_ids=A_ )[0] SCREAMING_SNAKE_CASE__ = model(A_ , token_type_ids=A_ )[0] SCREAMING_SNAKE_CASE__ = model(A_ )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaForMaskedLM(config=A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = model(A_ , attention_mask=A_ , token_type_ids=A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = DebertaVaForSequenceClassification(A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = model(A_ , attention_mask=A_ , token_type_ids=A_ , labels=A_ ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(A_ ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = DebertaVaForTokenClassification(config=A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = model(A_ , attention_mask=A_ , token_type_ids=A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaForQuestionAnswering(config=A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = model( A_ , attention_mask=A_ , token_type_ids=A_ , start_positions=A_ , end_positions=A_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowercase_ ( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaForMultipleChoice(config=A_ ) model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE__ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE__ = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE__ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE__ = model( A_ , attention_mask=A_ , token_type_ids=A_ , labels=A_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = config_and_inputs SCREAMING_SNAKE_CASE__ = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : List[str] = ( ( DebertaVaModel, DebertaVaForMaskedLM, DebertaVaForSequenceClassification, DebertaVaForTokenClassification, DebertaVaForQuestionAnswering, DebertaVaForMultipleChoice, ) if is_torch_available() else () ) lowerCamelCase__ : Tuple = ( { """feature-extraction""": DebertaVaModel, """fill-mask""": DebertaVaForMaskedLM, """question-answering""": DebertaVaForQuestionAnswering, """text-classification""": DebertaVaForSequenceClassification, """token-classification""": DebertaVaForTokenClassification, """zero-shot""": DebertaVaForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase__ : str = True lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : List[Any] = False lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : Any = False def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=A_ , hidden_size=37 ) def lowercase_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*A_ ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*A_ ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*A_ ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*A_ ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*A_ ) def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_multiple_choice(*A_ ) @slow def lowercase_ ( self ): '''simple docstring''' for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE__ = DebertaVaModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason='''Model not available yet''' ) def lowercase_ ( self ): '''simple docstring''' pass @slow def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge''' ) SCREAMING_SNAKE_CASE__ = torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] ) SCREAMING_SNAKE_CASE__ = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE__ = model(A_ , attention_mask=A_ )[0] # compare the actual values for a slice. SCREAMING_SNAKE_CASE__ = torch.tensor( [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , A_ , atol=1E-4 ) , f'''{output[:, 1:4, 1:4]}''' )
100
def UpperCamelCase_ ( __a = 50 ) -> int: a__ : Tuple = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
37
0